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.
This commit is contained in:
Felix Fontein 2020-12-27 16:13:40 +01:00 committed by GitHub
parent 5eef093e99
commit a11f24c3a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View File

@ -190,6 +190,7 @@ stages:
- 1 - 1
- 2 - 2
- 3 - 3
- 4
- stage: Remote_2_9 - stage: Remote_2_9
displayName: Remote 2.9 displayName: Remote 2.9
dependsOn: [] dependsOn: []
@ -204,6 +205,7 @@ stages:
- 1 - 1
- 2 - 2
- 3 - 3
- 4
## Finally ## Finally

View File

@ -138,7 +138,7 @@ class Connection(ConnectionBase):
def __init__(self, play_context, new_stdin, *args, **kwargs): def __init__(self, play_context, new_stdin, *args, **kwargs):
super(Connection, self).__init__(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() self.ids = dict()
# Windows uses Powershell modules # Windows uses Powershell modules
@ -146,13 +146,6 @@ class Connection(ConnectionBase):
self.module_implementation_preferences = ('.ps1', '.exe', '') self.module_implementation_preferences = ('.ps1', '.exe', '')
self.actual_user = play_context.remote_user 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): def _connect(self, port=None):
""" Connect to the container. Nothing to do """ """ Connect to the container. Nothing to do """
@ -161,8 +154,21 @@ class Connection(ConnectionBase):
display.vvv(u"ESTABLISH DOCKER CONNECTION FOR USER: {0}".format( display.vvv(u"ESTABLISH DOCKER CONNECTION FOR USER: {0}".format(
self.actual_user or u'?'), host=self._play_context.remote_addr 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 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): def exec_command(self, cmd, in_data=None, sudoable=False):
""" Run a command on the docker host """ """ Run a command on the docker host """