From 516e10efeb2a9cbe75175d0b8e4e9979072a5fc8 Mon Sep 17 00:00:00 2001 From: Jan David Mol <mol@astron.nl> Date: Fri, 24 Sep 2021 11:44:10 +0200 Subject: [PATCH] L2SS-379: Added docker client. --- devices/clients/docker_client.py | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 devices/clients/docker_client.py diff --git a/devices/clients/docker_client.py b/devices/clients/docker_client.py new file mode 100644 index 000000000..c5b0e8b81 --- /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 -- GitLab