From a11f24c3a9ccf73d77d41e24e96c650487a9e28a Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sun, 27 Dec 2020 16:13:40 +0100 Subject: [PATCH] Fix docker_api connection: do not initialize client too early (#63) * Fix docker_api connection: do not initialize client too early * Make sure connection plugin tests are also run with Ansible 2.9 and 2.10. --- .azure-pipelines/azure-pipelines.yml | 2 ++ plugins/connection/docker_api.py | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/.azure-pipelines/azure-pipelines.yml b/.azure-pipelines/azure-pipelines.yml index f7dbc8be..20469baa 100644 --- a/.azure-pipelines/azure-pipelines.yml +++ b/.azure-pipelines/azure-pipelines.yml @@ -190,6 +190,7 @@ stages: - 1 - 2 - 3 + - 4 - stage: Remote_2_9 displayName: Remote 2.9 dependsOn: [] @@ -204,6 +205,7 @@ stages: - 1 - 2 - 3 + - 4 ## Finally diff --git a/plugins/connection/docker_api.py b/plugins/connection/docker_api.py index 6b68b732..d1cccf81 100644 --- a/plugins/connection/docker_api.py +++ b/plugins/connection/docker_api.py @@ -138,7 +138,7 @@ class Connection(ConnectionBase): def __init__(self, play_context, new_stdin, *args, **kwargs): super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs) - self.client = AnsibleDockerClient(self, min_docker_version=MIN_DOCKER_PY, min_docker_api_version=MIN_DOCKER_API) + self.client = None self.ids = dict() # Windows uses Powershell modules @@ -146,13 +146,6 @@ class Connection(ConnectionBase): self.module_implementation_preferences = ('.ps1', '.exe', '') self.actual_user = play_context.remote_user - if self.actual_user is None and display.verbosity > 2: - # Since we're not setting the actual_user, look it up so we have it for logging later - # Only do this if display verbosity is high enough that we'll need the value - # This saves overhead from calling into docker when we don't need to - result = self._call_client(play_context, lambda: self.client.inspect_container(play_context.remote_addr)) - if result.get('Config'): - self.actual_user = result['Config'].get('User') def _connect(self, port=None): """ Connect to the container. Nothing to do """ @@ -161,8 +154,21 @@ class Connection(ConnectionBase): display.vvv(u"ESTABLISH DOCKER CONNECTION FOR USER: {0}".format( self.actual_user or u'?'), host=self._play_context.remote_addr ) + if self.client is None: + self.client = AnsibleDockerClient(self, min_docker_version=MIN_DOCKER_PY, min_docker_api_version=MIN_DOCKER_API) self._connected = True + if self.actual_user is None and display.verbosity > 2: + # Since we're not setting the actual_user, look it up so we have it for logging later + # Only do this if display verbosity is high enough that we'll need the value + # This saves overhead from calling into docker when we don't need to + display.vvv(u"Trying to determine actual user") + result = self._call_client(self._play_context, lambda: self.client.inspect_container(self._play_context.remote_addr)) + if result.get('Config'): + self.actual_user = result['Config'].get('User') + if self.actual_user is not None: + display.vvv(u"Actual user is '{0}'".format(self.actual_user)) + def exec_command(self, cmd, in_data=None, sudoable=False): """ Run a command on the docker host """