From 9d0949eb99b7c25638833eda3a84fc828cd453a8 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 27 Apr 2018 02:59:52 +0100 Subject: [PATCH] docker: fixes & add username parameter. --- docs/ansible.rst | 6 +++--- mitogen/core.py | 1 + mitogen/docker.py | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/docs/ansible.rst b/docs/ansible.rst index 6ac03ca0..662004ae 100644 --- a/docs/ansible.rst +++ b/docs/ansible.rst @@ -467,10 +467,10 @@ Sudo Docker ~~~~~~ -Docker support has received relatively little testing, expect increased -probability of surprises for the time being. +Docker support is fairly new, expect increased surprises for now. -* ``ansible_host`` +* ``ansible_host``: Name of Docker container. +* ``ansible_user``: Name of user within the container to execute as. Debugging diff --git a/mitogen/core.py b/mitogen/core.py index 4a48dac0..62733a7a 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -485,6 +485,7 @@ class Importer(object): self._present = {'mitogen': [ 'compat', 'debug', + 'docker', 'fakessh', 'fork', 'master', diff --git a/mitogen/docker.py b/mitogen/docker.py index 159579fc..c265dc4f 100644 --- a/mitogen/docker.py +++ b/mitogen/docker.py @@ -38,9 +38,12 @@ LOG = logging.getLogger(__name__) class Stream(mitogen.parent.Stream): container = None image = None + username = None docker_path = 'docker' - def construct(self, container=None, image=None, docker_path=None, **kwargs): + def construct(self, container=None, image=None, + docker_path=None, username=None, + **kwargs): assert container or image super(Stream, self).construct(**kwargs) if container: @@ -49,16 +52,22 @@ class Stream(mitogen.parent.Stream): self.image = image if docker_path: self.docker_path = docker_path + if username: + self.username = username def connect(self): super(Stream, self).connect() self.name = 'docker.' + (self.container or self.image) def get_boot_command(self): + args = ['--interactive'] + if self.username: + args += ['--user=' + self.username] + bits = [self.docker_path] if self.container: - bits += ['exec', '-i', self.container] + bits += ['exec'] + args + [self.container] elif self.image: - bits += ['run', '-i', '--rm', self.image] - bits += super(Stream, self).get_boot_command() - return bits + bits += ['run'] + args + ['--rm', self.image] + + return bits + super(Stream, self).get_boot_command()