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_swarm_service: Add read_only option #53482

Merged
merged 4 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "docker_swarm_service - Added support for ``read_only`` parameter."
17 changes: 17 additions & 0 deletions lib/ansible/modules/cloud/docker/docker_swarm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@
choices:
- ingress
- host
read_only:
description:
- Mount the containers root filesystem as read only.
- Corresponds to the C(--read-only) option of C(docker service create).
type: bool
version_added: "2.8"
replicas:
description:
- Number of containers instantiated in the service. Valid only if I(mode) is C(replicated).
Expand Down Expand Up @@ -747,6 +753,7 @@
}
],
"publish": null,
"read_only": null,
"replicas": 1,
"reserve_cpu": 0.25,
"reserve_memory": 20971520,
Expand Down Expand Up @@ -1055,6 +1062,7 @@ def __init__(self):
self.replicas = -1
self.service_id = False
self.service_version = False
self.read_only = None
self.restart_policy = None
self.restart_policy_attempts = None
self.restart_policy_delay = None
Expand Down Expand Up @@ -1103,6 +1111,7 @@ def get_facts(self):
'stop_signal': self.stop_signal,
'limit_cpu': self.limit_cpu,
'limit_memory': self.limit_memory,
'read_only': self.read_only,
'reserve_cpu': self.reserve_cpu,
'reserve_memory': self.reserve_memory,
'restart_policy_delay': self.restart_policy_delay,
Expand Down Expand Up @@ -1307,6 +1316,7 @@ def from_ansible_params(cls, ap, old_service, image_digest, can_update_networks)
s.stop_signal = ap['stop_signal']
s.user = ap['user']
s.working_dir = ap['working_dir']
s.read_only = ap['read_only']

s.command = ap['command']
if isinstance(s.command, string_types):
Expand Down Expand Up @@ -1482,6 +1492,8 @@ def compare(self, os):
differences.add('stop_grace_period', parameter=self.stop_grace_period, active=os.stop_grace_period)
if self.has_publish_changed(os.publish):
differences.add('publish', parameter=self.publish, active=os.publish)
if self.read_only is not None and self.read_only != os.read_only:
differences.add('read_only', parameter=self.read_only, active=os.read_only)
if self.restart_policy is not None and self.restart_policy != os.restart_policy:
differences.add('restart_policy', parameter=self.restart_policy, active=os.restart_policy)
if self.restart_policy_attempts is not None and self.restart_policy_attempts != os.restart_policy_attempts:
Expand Down Expand Up @@ -1645,6 +1657,8 @@ def build_container_spec(self):
container_spec_args['hostname'] = self.hostname
if self.hosts is not None:
container_spec_args['hosts'] = self.hosts
if self.read_only is not None:
container_spec_args['read_only'] = self.read_only
if self.stop_grace_period is not None:
container_spec_args['stop_grace_period'] = self.stop_grace_period
if self.stop_signal is not None:
Expand Down Expand Up @@ -1837,6 +1851,7 @@ def get_service(self, name):
ds.stop_grace_period = task_template_data['ContainerSpec'].get('StopGracePeriod')
ds.stop_signal = task_template_data['ContainerSpec'].get('StopSignal')
ds.working_dir = task_template_data['ContainerSpec'].get('Dir')
ds.read_only = task_template_data['ContainerSpec'].get('ReadOnly')

healthcheck_data = task_template_data['ContainerSpec'].get('Healthcheck')
if healthcheck_data:
Expand Down Expand Up @@ -2243,6 +2258,7 @@ def main():
)),
limit_cpu=dict(type='float', removed_in_version='2.12'),
limit_memory=dict(type='str', removed_in_version='2.12'),
read_only=dict(type='bool'),
reservations=dict(type='dict', options=dict(
cpus=dict(type='float'),
memory=dict(type='str'),
Expand Down Expand Up @@ -2309,6 +2325,7 @@ def main():
update_order=dict(docker_py_version='2.7.0', docker_api_version='1.29'),
stop_signal=dict(docker_py_version='2.6.0', docker_api_version='1.28'),
publish=dict(docker_py_version='3.0.0', docker_api_version='1.25'),
read_only=dict(docker_py_version='2.6.0', docker_api_version='1.28'),
# specials
publish_mode=dict(
docker_py_version='3.0.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,58 @@
- "'Minimum version required' in publish_1.msg"
when: docker_api_version is version('1.25', '<') or docker_py_version is version('3.0.0', '<')

###################################################################
## read_only ######################################################
###################################################################

- name: read_only
docker_swarm_service:
name: "{{ service_name }}"
image: alpine:3.8
resolve_image: no
command: '/bin/sh -v -c "sleep 10m"'
read_only: true
register: read_only_1
ignore_errors: yes

- name: read_only (idempotency)
docker_swarm_service:
name: "{{ service_name }}"
image: alpine:3.8
resolve_image: no
command: '/bin/sh -v -c "sleep 10m"'
read_only: true
register: read_only_2
ignore_errors: yes

- name: read_only (change)
docker_swarm_service:
name: "{{ service_name }}"
image: alpine:3.8
resolve_image: no
command: '/bin/sh -v -c "sleep 10m"'
read_only: false
register: read_only_3
ignore_errors: yes

- name: cleanup
docker_swarm_service:
name: "{{ service_name }}"
state: absent
diff: no

- assert:
that:
- read_only_1 is changed
- read_only_2 is not changed
- read_only_3 is changed
when: docker_api_version is version('1.28', '>=') and docker_py_version is version('2.6.0', '>=')
- assert:
that:
- read_only_1 is failed
- "'Minimum version required' in read_only_1.msg"
when: docker_api_version is version('1.28', '<') or docker_py_version is version('2.6.0', '<')

###################################################################
## replicas #######################################################
###################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ service_expected_output:
publish:
- {mode: null, protocol: tcp, published_port: 60001, target_port: 60001}
- {mode: null, protocol: udp, published_port: 60001, target_port: 60001}
read_only: null
replicas: null
reserve_cpu: null
reserve_memory: null
Expand Down