From b9977ce0ee197278a36b98049bf2e640cf5d5c2e Mon Sep 17 00:00:00 2001 From: Rajat Gupta <35985127+rjt-gupta@users.noreply.github.com> Date: Thu, 25 Jul 2019 00:44:30 +0530 Subject: [PATCH] Aiodocker Comments (#342) --- tanner/utils/aiodocker_helper.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tanner/utils/aiodocker_helper.py b/tanner/utils/aiodocker_helper.py index fc4aae5d..ddb03c93 100644 --- a/tanner/utils/aiodocker_helper.py +++ b/tanner/utils/aiodocker_helper.py @@ -13,6 +13,11 @@ def __init__(self): self.host_image = TannerConfig.get('DOCKER', 'host_image') async def setup_host_image(self, remote_path=None, tag=None): + """ + Helper to pull host image or build an image with remote Dockerfile + :param remote_path (str): remote path of Dockerfile + :param tag (str): tag to be given to new image build ex: 'myimage:latest' + """ try: if remote_path and tag is not None: @@ -27,6 +32,11 @@ async def setup_host_image(self, remote_path=None, tag=None): self.logger.exception('Error while pulling %s image %s', self.host_image, docker_error) async def get_container(self, container_name): + """ + Gets the container object having specified name + :param container_name (str): name of the target container + :return: container (object) + """ container = None try: container = await self.docker_client.containers.get(container=container_name) @@ -36,6 +46,13 @@ async def get_container(self, container_name): return container async def create_container(self, container_name, cmd=None, image=None): + """ + Helper to create or replace a container (initially pulls the given image) + :param container_name (str): name to be given to new container or replace existing if any + :param cmd (list): contains commands to run in the container. ex: ["sh", "-c", "echo 'Hello'"] + :param image (str): name of image to be used + :return: container (object): newly created container object + """ await self.setup_host_image() container = None if image is None: @@ -53,6 +70,12 @@ async def create_container(self, container_name, cmd=None, image=None): return container async def execute_cmd(self, cmd, image=None): + """ + Creates a new container, runs the cmd in it and deletes the used container + :param cmd (list): contains commands to run in the container. ex: ["sh", "-c", "echo 'Hello'"] + :param image (str): name of image to be used + :return: execute_result (str): execution output/errors of cmd from the container + """ execute_result = None try: if image is None: @@ -74,6 +97,10 @@ async def execute_cmd(self, cmd, image=None): return execute_result async def delete_container(self, container_name): + """ + Delete an existing container + :param container_name (str): name of container to be deleted + """ container = await self.get_container(container_name) try: if container: