diff --git a/devices/clients/docker_client.py b/devices/clients/docker_client.py new file mode 100644 index 0000000000000000000000000000000000000000..c5b0e8b81f69e7f83ae381468b6bcd738f9ec296 --- /dev/null +++ b/devices/clients/docker_client.py @@ -0,0 +1,71 @@ +import logging +import docker + +from .comms_client import CommClient + +logger = logging.getLogger() + +class DockerClient(CommClient): + """ + Controls & queries running docker containers. + """ + + def start(self): + super().start() + + def __init__(self, base_url, fault_func, streams): + super().__init__(fault_func, streams) + + self.base_url = base_url + + def connect(self): + """ + Function used to connect to the client. + """ + if not self.connected: + self.client = docker.DockerClient(self.base_url) + + return super().connect() + + def ping(self): + return True + + def disconnect(self): + self.client = None + + return super().disconnect() + + def setup_value_conversion(self, attribute): + """ + gives the client access to the attribute_wrapper object in order to access all data it could potentially need. + the OPC ua read/write functions require the dimensionality and the type to be known + """ + return + + def setup_attribute(self, annotation, attribute): + """ + MANDATORY function: is used by the attribute wrapper to get read/write functions. must return the read and write functions + """ + + container_name = annotation["container"] + + # get all the necessary data to set up the read/write functions from the attribute_wrapper + self.setup_value_conversion(attribute) + + def read_function(): + try: + container = self.client.containers.get(container_name) + except docker.errors.NotFound: + return False + + return container.status == 'running' + + def write_function(value): + container = self.client.containers.get(container_name) + + if value: + container.start() + else: + container.stop() + + return read_function, write_function