mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 19:31:26 -07:00
Docker common consolidation (#49707)
* [docker] Consolidating Python Boolean conversion for Docker API (#49563) * [docker] Consolidating docker option min version checks (#49564) * [docker] Moving option min version checks out of docker_swarm (#49564) Also renaming Boolean cleanup function and fixing docker_container minimum version check for network interfaces. * Cleanup from PR feedback
This commit is contained in:
parent
8fcf9e1a28
commit
b67719ba1d
5 changed files with 132 additions and 227 deletions
|
@ -163,7 +163,8 @@ class AnsibleDockerClient(Client):
|
|||
|
||||
def __init__(self, argument_spec=None, supports_check_mode=False, mutually_exclusive=None,
|
||||
required_together=None, required_if=None, min_docker_version=MIN_DOCKER_VERSION,
|
||||
min_docker_api_version=None):
|
||||
min_docker_api_version=None, option_minimal_versions=None,
|
||||
option_minimal_versions_ignore_params=None):
|
||||
|
||||
merged_arg_spec = dict()
|
||||
merged_arg_spec.update(DOCKER_COMMON_ARGS)
|
||||
|
@ -235,6 +236,9 @@ class AnsibleDockerClient(Client):
|
|||
if self.docker_api_version < LooseVersion(min_docker_api_version):
|
||||
self.fail('docker API version is %s. Minimum version required is %s.' % (self.docker_api_version_str, min_docker_api_version))
|
||||
|
||||
if option_minimal_versions is not None:
|
||||
self._get_minimal_versions(option_minimal_versions, option_minimal_versions_ignore_params)
|
||||
|
||||
def log(self, msg, pretty_print=False):
|
||||
pass
|
||||
# if self.debug:
|
||||
|
@ -416,6 +420,58 @@ class AnsibleDockerClient(Client):
|
|||
% (self.auth_params['tls_hostname'], match.group(1), match.group(1)))
|
||||
self.fail("SSL Exception: %s" % (error))
|
||||
|
||||
def _get_minimal_versions(self, option_minimal_versions, ignore_params=None):
|
||||
self.option_minimal_versions = dict()
|
||||
for option in self.module.argument_spec:
|
||||
if ignore_params is not None:
|
||||
if option in ignore_params:
|
||||
continue
|
||||
self.option_minimal_versions[option] = dict()
|
||||
self.option_minimal_versions.update(option_minimal_versions)
|
||||
|
||||
for option, data in self.option_minimal_versions.items():
|
||||
# Test whether option is supported, and store result
|
||||
support_docker_py = True
|
||||
support_docker_api = True
|
||||
if 'docker_py_version' in data:
|
||||
support_docker_py = self.docker_py_version >= LooseVersion(data['docker_py_version'])
|
||||
if 'docker_api_version' in data:
|
||||
support_docker_api = self.docker_api_version >= LooseVersion(data['docker_api_version'])
|
||||
data['supported'] = support_docker_py and support_docker_api
|
||||
# Fail if option is not supported but used
|
||||
if not data['supported']:
|
||||
# Test whether option is specified
|
||||
if 'detect_usage' in data:
|
||||
used = data['detect_usage']()
|
||||
else:
|
||||
used = self.module.params.get(option) is not None
|
||||
if used and 'default' in self.module.argument_spec[option]:
|
||||
used = self.module.params[option] != self.module.argument_spec[option]['default']
|
||||
if used:
|
||||
# If the option is used, compose error message.
|
||||
if 'usage_msg' in data:
|
||||
usg = data['usage_msg']
|
||||
else:
|
||||
usg = 'set %s option' % (option, )
|
||||
if not support_docker_api:
|
||||
msg = 'docker API version is %s. Minimum version required is %s to %s.'
|
||||
msg = msg % (self.docker_api_version_str, data['docker_api_version'], usg)
|
||||
elif not support_docker_py:
|
||||
if LooseVersion(data['docker_py_version']) < LooseVersion('2.0.0'):
|
||||
msg = ("docker-py version is %s. Minimum version required is %s to %s. "
|
||||
"Consider switching to the 'docker' package if you do not require Python 2.6 support.")
|
||||
elif self.docker_py_version < LooseVersion('2.0.0'):
|
||||
msg = ("docker-py version is %s. Minimum version required is %s to %s. "
|
||||
"You have to switch to the Python 'docker' package. First uninstall 'docker-py' before "
|
||||
"installing 'docker' to avoid a broken installation.")
|
||||
else:
|
||||
msg = "docker version is %s. Minimum version required is %s to %s."
|
||||
msg = msg % (docker_version, data['docker_py_version'], usg)
|
||||
else:
|
||||
# should not happen
|
||||
msg = 'Cannot %s with your configuration.' % (usg, )
|
||||
self.fail(msg)
|
||||
|
||||
def get_container(self, name=None):
|
||||
'''
|
||||
Lookup a container and return the inspection results.
|
||||
|
@ -741,3 +797,23 @@ class DifferenceTracker(object):
|
|||
'''
|
||||
result = [entry['name'] for entry in self._diff]
|
||||
return result
|
||||
|
||||
|
||||
def clean_dict_booleans_for_docker_api(data):
|
||||
'''
|
||||
Go doesn't like Python booleans 'True' or 'False', while Ansible is just
|
||||
fine with them in YAML. As such, they need to be converted in cases where
|
||||
we pass dictionaries to the Docker API (e.g. docker_network's
|
||||
driver_options and docker_prune's filters).
|
||||
'''
|
||||
result = dict()
|
||||
if data is not None:
|
||||
for k, v in data.items():
|
||||
if v is True:
|
||||
v = 'true'
|
||||
elif v is False:
|
||||
v = 'false'
|
||||
else:
|
||||
v = str(v)
|
||||
result[str(k)] = v
|
||||
return result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue