From 64b12d2c0da07d6945f1ed93da8e3446b8f0c35b Mon Sep 17 00:00:00 2001 From: Hannes Ljungberg Date: Mon, 18 Feb 2019 14:12:05 +0100 Subject: [PATCH] docker_swarm_service: Add groups option (#52428) * Add groups option * Add changelog fragment * Remove whitespace * Remove whitespace --- ...docker_swarm_service-add_groups_option.yml | 2 + .../cloud/docker/docker_swarm_service.py | 21 +++++++ .../tasks/tests/options.yml | 63 +++++++++++++++++++ .../docker_swarm_service/vars/main.yml | 1 + 4 files changed, 87 insertions(+) create mode 100644 changelogs/fragments/52428-docker_swarm_service-add_groups_option.yml diff --git a/changelogs/fragments/52428-docker_swarm_service-add_groups_option.yml b/changelogs/fragments/52428-docker_swarm_service-add_groups_option.yml new file mode 100644 index 00000000000..66073aab91d --- /dev/null +++ b/changelogs/fragments/52428-docker_swarm_service-add_groups_option.yml @@ -0,0 +1,2 @@ +minor_changes: + - "docker_swarm_service - Added support for ``groups`` parameter." diff --git a/lib/ansible/modules/cloud/docker/docker_swarm_service.py b/lib/ansible/modules/cloud/docker/docker_swarm_service.py index 5ebb273d658..d73d91a1812 100644 --- a/lib/ansible/modules/cloud/docker/docker_swarm_service.py +++ b/lib/ansible/modules/cloud/docker/docker_swarm_service.py @@ -135,6 +135,13 @@ options: - Force update even if no changes require it. - Corresponds to the C(--force) option of C(docker service update). - Requires API version >= 1.25. + groups: + type: list + description: + - List of additional group names and/or IDs that the container process will run as. + - Corresponds to the C(--group) option of C(docker service update). + - Requires API version >= 1.25. + version_added: "2.8" labels: type: dict description: @@ -704,6 +711,7 @@ class DockerService(DockerBaseClass): self.dns_options = None self.env = None self.force_update = None + self.groups = None self.log_driver = None self.log_driver_options = None self.labels = None @@ -753,6 +761,7 @@ class DockerService(DockerBaseClass): 'hostname': self.hostname, 'env': self.env, 'force_update': self.force_update, + 'groups': self.groups, 'log_driver': self.log_driver, 'log_driver_options': self.log_driver_options, 'publish': self.publish, @@ -849,6 +858,11 @@ class DockerService(DockerBaseClass): if ap['force_update']: s.force_update = int(str(time.time()).replace('.', '')) + if ap['groups'] is not None: + # In case integers are passed as groups, we need to convert them to + # strings as docker internally treats them as strings. + s.groups = [str(g) for g in ap['groups']] + if ap['replicas'] == -1: if old_service: s.replicas = old_service.replicas @@ -943,6 +957,8 @@ class DockerService(DockerBaseClass): differences.add('constraints', parameter=self.constraints, active=os.constraints) if self.placement_preferences is not None and self.placement_preferences != (os.placement_preferences or []): differences.add('placement_preferences', parameter=self.placement_preferences, active=os.placement_preferences) + if self.groups is not None and self.groups != (os.groups or []): + differences.add('groups', parameter=self.groups, active=os.groups) if self.labels is not None and self.labels != (os.labels or {}): differences.add('labels', parameter=self.labels, active=os.labels) if self.limit_cpu is not None and self.limit_cpu != os.limit_cpu: @@ -1120,6 +1136,8 @@ class DockerService(DockerBaseClass): container_spec_args['stop_signal'] = self.stop_signal if self.tty is not None: container_spec_args['tty'] = self.tty + if self.groups is not None: + container_spec_args['groups'] = self.groups if secrets is not None: container_spec_args['secrets'] = secrets if self.mounts is not None: @@ -1308,6 +1326,7 @@ class DockerServiceManager(object): ds.env = task_template_data['ContainerSpec'].get('Env') ds.command = task_template_data['ContainerSpec'].get('Command') ds.args = task_template_data['ContainerSpec'].get('Args') + ds.groups = task_template_data['ContainerSpec'].get('Groups') ds.stop_signal = task_template_data['ContainerSpec'].get('StopSignal') healthcheck_data = task_template_data['ContainerSpec'].get('Healthcheck') @@ -1640,6 +1659,7 @@ def main(): env=dict(type='raw'), env_files=dict(type='list', elements='path'), force_update=dict(default=False, type='bool'), + groups=dict(type='list', elements='str'), log_driver=dict(type='str'), log_driver_options=dict(type='dict'), publish=dict(type='list', elements='dict', options=dict( @@ -1694,6 +1714,7 @@ def main(): force_update=dict(docker_py_version='2.1.0', docker_api_version='1.25'), healthcheck=dict(docker_py_version='2.0.0', docker_api_version='1.25'), hostname=dict(docker_py_version='2.2.0', docker_api_version='1.25'), + groups=dict(docker_py_version='2.6.0', docker_api_version='1.25'), tty=dict(docker_py_version='2.4.0', docker_api_version='1.25'), secrets=dict(docker_py_version='2.1.0', docker_api_version='1.25'), configs=dict(docker_py_version='2.6.0', docker_api_version='1.30'), diff --git a/test/integration/targets/docker_swarm_service/tasks/tests/options.yml b/test/integration/targets/docker_swarm_service/tasks/tests/options.yml index a331b92b544..1c44e0302c8 100644 --- a/test/integration/targets/docker_swarm_service/tasks/tests/options.yml +++ b/test/integration/targets/docker_swarm_service/tasks/tests/options.yml @@ -889,6 +889,69 @@ - "('version is ' ~ docker_api_version ~'. Minimum version required is 1.25') in force_update_1.msg" when: docker_api_version is version('1.25', '<') +#################################################################### +## groups ########################################################## +#################################################################### + +- name: groups + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + groups: + - 1234 + - 5678 + register: groups_1 + +- name: groups (idempotency) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + groups: + - 1234 + - 5678 + register: groups_2 + +- name: groups (change) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + groups: + - 1234 + register: groups_3 + +- name: groups (empty) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + groups: [] + register: groups_4 + +- name: groups (empty idempotency) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + groups: [] + register: groups_5 + +- name: cleanup + docker_swarm_service: + name: "{{ service_name }}" + state: absent + diff: no + +- assert: + that: + - groups_1 is changed + - groups_2 is not changed + - groups_3 is changed + - groups_4 is changed + - groups_5 is not changed + #################################################################### ## healthcheck ##################################################### #################################################################### diff --git a/test/integration/targets/docker_swarm_service/vars/main.yml b/test/integration/targets/docker_swarm_service/vars/main.yml index bcc81fbc5bb..cdde68fce88 100644 --- a/test/integration/targets/docker_swarm_service/vars/main.yml +++ b/test/integration/targets/docker_swarm_service/vars/main.yml @@ -12,6 +12,7 @@ service_expected_output: endpoint_mode: vip env: null force_update: null + groups: null healthcheck: null healthcheck_disabled: null hostname: null