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_container: simplify minimal required version per option handling #47711

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "docker_container - refactored minimal docker-py/API version handling, and fixing such handling of some options."
11 changes: 7 additions & 4 deletions lib/ansible/module_utils/docker_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ def __init__(self, argument_spec=None, supports_check_mode=False, mutually_exclu

NEEDS_DOCKER_PY2 = (LooseVersion(min_docker_version) >= LooseVersion('2.0.0'))

self.docker_py_version = LooseVersion(docker_version)

if HAS_DOCKER_MODELS and HAS_DOCKER_SSLADAPTER:
self.fail("Cannot have both the docker-py and docker python modules installed together as they use the same namespace and "
"cause a corrupt installation. Please uninstall both packages, and re-install only the docker-py or docker python "
Expand All @@ -201,7 +203,7 @@ def __init__(self, argument_spec=None, supports_check_mode=False, mutually_exclu
msg = "Failed to import docker or docker-py - %s. Try `pip install docker` or `pip install docker-py` (Python 2.6)"
self.fail(msg % HAS_DOCKER_ERROR)

if LooseVersion(docker_version) < LooseVersion(min_docker_version):
if self.docker_py_version < LooseVersion(min_docker_version):
if NEEDS_DOCKER_PY2:
if docker_version < LooseVersion('2.0'):
msg = "Error: docker-py version is %s, while this module requires docker %s. Try `pip uninstall docker-py` and then `pip install docker`"
Expand All @@ -226,9 +228,10 @@ def __init__(self, argument_spec=None, supports_check_mode=False, mutually_exclu
self.fail("Error connecting: %s" % exc)

if min_docker_api_version is not None:
docker_api_version = self.version()['ApiVersion']
if LooseVersion(docker_api_version) < LooseVersion(min_docker_api_version):
self.fail('docker API version is %s. Minimum version required is %s.' % (docker_api_version, min_docker_api_version))
self.docker_api_version_str = self.version()['ApiVersion']
self.docker_api_version = LooseVersion(self.docker_api_version_str)
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))

def log(self, msg, pretty_print=False):
pass
Expand Down
322 changes: 168 additions & 154 deletions lib/ansible/modules/cloud/docker/docker_container.py

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions test/integration/targets/docker_config/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
- name: Check Docker API version
command: "{{ ansible_python.executable }} -c 'import docker; print(docker.from_env().version()[\"ApiVersion\"])'"
register: docker_api_version
ignore_errors: yes

---
- include_tasks: test_docker_config.yml
when: docker_api_version.rc == 0 and docker_api_version.stdout is version('1.30', '>=')
when: docker_api_version is version('1.30', '>=')
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
- name: Make sure we're not already using Docker swarm
docker_swarm:
state: absent
Expand Down
6 changes: 4 additions & 2 deletions test/integration/targets/docker_container/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
# Create random name prefix (for containers, networks, ...)
- name: Create random container name prefix
set_fact:
cname_prefix: "{{ 'ansible-test-%0x' % ((2**32) | random) }}"
Expand All @@ -8,6 +9,7 @@
- debug:
msg: "Using container name prefix {{ cname_prefix }}"

# Run the tests
- block:
- include_tasks: run-test.yml
with_fileglob:
Expand All @@ -26,6 +28,6 @@
state: absent
force: yes
with_items: "{{ dnetworks }}"
when: docker_py_version is version('1.10.0', '>=')

# Skip for CentOS 6
when: ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6
when: docker_py_version is version('1.8.0', '>=') and docker_api_version is version('1.20', '>=')