Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker connection: handle version and docker_args #327

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions plugins/connection/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import fcntl
import os
import os.path
import shlex
import subprocess
import re

Expand Down Expand Up @@ -116,6 +117,7 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):

self._docker_args = []
self._container_user_cache = {}
self._version = None

# Windows uses Powershell modules
if getattr(self._shell, "_IS_WINDOWS", False):
Expand All @@ -129,12 +131,6 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
except ValueError:
raise AnsibleError("docker command not found in PATH")

self.docker_version = self._get_docker_version()
if self.docker_version == u'dev':
display.warning(u'Docker version number is "dev". Will assume latest version.')
if self.docker_version != u'dev' and LooseVersion(self.docker_version) < LooseVersion(u'1.3'):
raise AnsibleError('docker connection type requires docker 1.3 or higher')

@staticmethod
def _sanitize_version(version):
version = re.sub(u'[^0-9a-zA-Z.]', u'', version)
Expand Down Expand Up @@ -220,16 +216,19 @@ def _build_exec_cmd(self, cmd):

return local_cmd

def _set_conn_data(self):

''' initialize for the connection, cannot do only in init since all data is not ready at that point '''

def _set_docker_args(self):
# TODO: this is mostly for backwards compatibility, play_context is used as fallback for older versions
# docker arguments
del self._docker_args[:]
extra_args = self.get_option('docker_extra_args') or self._play_context.docker_extra_args
extra_args = self.get_option('docker_extra_args') or getattr(self._play_context, 'docker_extra_args', '')
if extra_args:
self._docker_args += extra_args.split(' ')
self._docker_args = shlex.split(extra_args)
felixfontein marked this conversation as resolved.
Show resolved Hide resolved

def _set_conn_data(self):

''' initialize for the connection, cannot do only in init since all data is not ready at that point '''

self._set_docker_args()

self.remote_user = self.get_option('remote_user')
if self.remote_user is None and self._play_context.remote_user is not None:
Expand All @@ -240,6 +239,19 @@ def _set_conn_data(self):
if self.timeout == 10 and self.timeout != self._play_context.timeout:
self.timeout = self._play_context.timeout

@property
def docker_version(self):

if not self._version:
self._set_docker_args()

self._version = self._get_docker_version()
if self._version == u'dev':
display.warning(u'Docker version number is "dev". Will assume latest version.')
if self._version != u'dev' and LooseVersion(self._version) < LooseVersion(u'1.3'):
raise AnsibleError('docker connection type requires docker 1.3 or higher')
return self._version

def _get_actual_user(self):
if self.remote_user is not None:
# An explicit user is provided
Expand Down Expand Up @@ -377,8 +389,7 @@ def put_file(self, in_path, out_path):
args = self._build_exec_cmd([self._play_context.executable, "-c", "dd of=%s bs=%s%s" % (out_path, BUFSIZE, count)])
args = [to_bytes(i, errors='surrogate_or_strict') for i in args]
try:
p = subprocess.Popen(args, stdin=in_file,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = subprocess.Popen(args, stdin=in_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
raise AnsibleError("docker connection requires dd command in the container to put files")
stdout, stderr = p.communicate()
Expand Down