From f13091d142d3eea2ce94bf0c6b164039472c32a5 Mon Sep 17 00:00:00 2001 From: Pluggi Date: Fri, 19 Oct 2018 01:04:17 -0700 Subject: [PATCH] Add runtime option to docker_container module (#47247) * Add runtime option to docker_container module Signed-off-by: Antoine Bardoux * Add changelog fragment Signed-off-by: Antoine Bardoux * Add idempotency test for docker_container.runtime Signed-off-by: Antoine Bardoux --- ...7-docker_container-add-runtime-option.yaml | 2 ++ .../modules/cloud/docker/docker_container.py | 16 +++++++++ .../docker_container/tasks/tests/options.yml | 33 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 changelogs/fragments/47247-docker_container-add-runtime-option.yaml diff --git a/changelogs/fragments/47247-docker_container-add-runtime-option.yaml b/changelogs/fragments/47247-docker_container-add-runtime-option.yaml new file mode 100644 index 00000000000..6a0bbeca7ae --- /dev/null +++ b/changelogs/fragments/47247-docker_container-add-runtime-option.yaml @@ -0,0 +1,2 @@ +minor_changes: +- "docker_container - Add runtime option." diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index 4898f50bd42..62ec4247521 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -359,6 +359,10 @@ options: description: - Use with restart policy to control maximum number of restart attempts. default: 0 + runtime: + description: + - Runtime to use for the container. + version_added: "2.8" shm_size: description: - "Size of C(/dev/shm) (format: C([])). Number is positive integer. @@ -855,6 +859,7 @@ class TaskParameters(DockerBaseClass): self.restart = None self.restart_retries = None self.restart_policy = None + self.runtime = None self.shm_size = None self.security_opts = None self.state = None @@ -1118,6 +1123,9 @@ class TaskParameters(DockerBaseClass): if self.client.HAS_UTS_MODE_OPT: host_config_params['uts_mode'] = 'uts' + if self.client.HAS_RUNTIME_OPT: + host_config_params['runtime'] = 'runtime' + params = dict() for key, value in host_config_params.items(): if getattr(self, value, None) is not None: @@ -1561,6 +1569,7 @@ class Container(DockerBaseClass): expected_ports=host_config.get('PortBindings'), read_only=host_config.get('ReadonlyRootfs'), restart_policy=restart_policy.get('Name'), + runtime=host_config.get('Runtime'), # Cannot test shm_size, as shm_size is not included in container inspection results. # shm_size=host_config.get('ShmSize'), security_opts=host_config.get("SecurityOpt"), @@ -2421,6 +2430,10 @@ class AnsibleDockerClientContainer(AnsibleDockerClient): self.module.warn("docker API version is %s. Minimum version required is 1.25 to set or " "update the container's stop_timeout configuration." % (docker_api_version,)) + runtime_supported = LooseVersion(docker_api_version) >= LooseVersion('1.12') + if self.module.params.get("runtime") and not runtime_supported: + self.fail('docker API version is %s. Minimum version required is 1.12 to set runtime option.' % (docker_api_version,)) + self.HAS_INIT_OPT = init_supported self.HAS_UTS_MODE_OPT = uts_mode_supported self.HAS_BLKIO_WEIGHT_OPT = blkio_weight_supported @@ -2428,6 +2441,8 @@ class AnsibleDockerClientContainer(AnsibleDockerClient): self.HAS_STOP_TIMEOUT_OPT = stop_timeout_supported self.HAS_AUTO_REMOVE_OPT = HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3 + self.HAS_RUNTIME_OPT = runtime_supported + if self.module.params.get('auto_remove') and not self.HAS_AUTO_REMOVE_OPT: self.fail("'auto_remove' is not compatible with the 'docker-py' Python package. It requires the newer 'docker' Python package.") @@ -2496,6 +2511,7 @@ def main(): restart=dict(type='bool', default=False), restart_policy=dict(type='str', choices=['no', 'on-failure', 'always', 'unless-stopped']), restart_retries=dict(type='int', default=None), + runtime=dict(type='str', default=None), security_opts=dict(type='list'), shm_size=dict(type='str'), state=dict(type='str', choices=['absent', 'present', 'started', 'stopped'], default='started'), diff --git a/test/integration/targets/docker_container/tasks/tests/options.yml b/test/integration/targets/docker_container/tasks/tests/options.yml index 2db3ef40c41..9d3dc90809b 100644 --- a/test/integration/targets/docker_container/tasks/tests/options.yml +++ b/test/integration/targets/docker_container/tasks/tests/options.yml @@ -2457,6 +2457,39 @@ - restart_retries_2 is not changed - restart_retries_3 is changed +#################################################################### +## runtime ######################################################### +#################################################################### + +- name: runtime + docker_container: + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + name: "{{ cname }}" + runtime: runc + state: started + register: runtime_1 + +- name: runtime (idempotency) + docker_container: + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + name: "{{ cname }}" + runtime: runc + state: started + register: runtime_2 + +- name: cleanup + docker_container: + name: "{{ cname }}" + state: absent + stop_timeout: 1 + +- assert: + that: + - runtime_1 is changed + - runtime_2 is not changed + #################################################################### ## security_opts ################################################### ####################################################################