Skip to content

Commit

Permalink
Add runtime option to docker_container module (ansible#47247)
Browse files Browse the repository at this point in the history
* Add runtime option to docker_container module

Signed-off-by: Antoine Bardoux <[email protected]>

* Add changelog fragment

Signed-off-by: Antoine Bardoux <[email protected]>

* Add idempotency test for docker_container.runtime

Signed-off-by: Antoine Bardoux <[email protected]>
  • Loading branch information
Pluggi authored and gundalow committed Oct 19, 2018
1 parent 131efcf commit f13091d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "docker_container - Add runtime option."
16 changes: 16 additions & 0 deletions lib/ansible/modules/cloud/docker/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@
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>[<unit>])). Number is positive integer.
Expand Down Expand Up @@ -855,6 +859,7 @@ def __init__(self, client):
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
Expand Down Expand Up @@ -1118,6 +1123,9 @@ def _host_config(self):
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:
Expand Down Expand Up @@ -1561,6 +1569,7 @@ def has_different_configuration(self, image):
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"),
Expand Down Expand Up @@ -2421,13 +2430,19 @@ def __init__(self, **kwargs):
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
self.HAS_CPUSET_MEMS_OPT = cpuset_mems_supported
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.")

Expand Down Expand Up @@ -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'),
Expand Down
33 changes: 33 additions & 0 deletions test/integration/targets/docker_container/tasks/tests/options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###################################################
####################################################################
Expand Down

0 comments on commit f13091d

Please sign in to comment.