From 77d116f66e4fa8b9e946f97de1d14c94dfc6f43b Mon Sep 17 00:00:00 2001 From: Hannes Ljungberg Date: Mon, 18 Feb 2019 10:43:35 +0100 Subject: [PATCH] docker_swarm_service: Add stop_signal option (#52424) * Add stop_signal option * Add changelog fragment --- ...r_swarm_service-add_stop_signal_option.yml | 2 + .../cloud/docker/docker_swarm_service.py | 16 ++++++++ .../tasks/tests/options.yml | 40 +++++++++++++++++++ .../docker_swarm_service/vars/main.yml | 1 + 4 files changed, 59 insertions(+) create mode 100644 changelogs/fragments/52424-docker_swarm_service-add_stop_signal_option.yml diff --git a/changelogs/fragments/52424-docker_swarm_service-add_stop_signal_option.yml b/changelogs/fragments/52424-docker_swarm_service-add_stop_signal_option.yml new file mode 100644 index 0000000000..780c216845 --- /dev/null +++ b/changelogs/fragments/52424-docker_swarm_service-add_stop_signal_option.yml @@ -0,0 +1,2 @@ +minor_changes: + - "docker_swarm_service - Added support for ``stop_signal`` parameter." diff --git a/lib/ansible/modules/cloud/docker/docker_swarm_service.py b/lib/ansible/modules/cloud/docker/docker_swarm_service.py index 2b0114b700..ea7b62f8f9 100644 --- a/lib/ansible/modules/cloud/docker/docker_swarm_service.py +++ b/lib/ansible/modules/cloud/docker/docker_swarm_service.py @@ -292,6 +292,12 @@ options: description: - List of the service networks names. - Corresponds to the C(--network) option of C(docker service create). + stop_signal: + type: str + description: + - Override default signal used to stop the container. + - Corresponds to the C(--stop-signal) option of C(docker service create). + version_added: "2.8" publish: type: list description: @@ -664,6 +670,7 @@ class DockerService(DockerBaseClass): self.secrets = None self.constraints = None self.networks = None + self.stop_signal = None self.publish = None self.placement_preferences = None self.replicas = -1 @@ -706,6 +713,7 @@ class DockerService(DockerBaseClass): 'replicas': self.replicas, 'endpoint_mode': self.endpoint_mode, 'restart_policy': self.restart_policy, + 'stop_signal': self.stop_signal, 'limit_cpu': self.limit_cpu, 'limit_memory': self.limit_memory, 'reserve_cpu': self.reserve_cpu, @@ -742,6 +750,7 @@ class DockerService(DockerBaseClass): s.reserve_cpu = ap['reserve_cpu'] s.mode = ap['mode'] s.networks = ap['networks'] + s.stop_signal = ap['stop_signal'] s.restart_policy = ap['restart_policy'] s.restart_policy_attempts = ap['restart_policy_attempts'] s.restart_policy_delay = ap['restart_policy_delay'] @@ -894,6 +903,8 @@ class DockerService(DockerBaseClass): differences.add('reserve_memory', parameter=self.reserve_memory, active=os.reserve_memory) if self.container_labels is not None and self.container_labels != (os.container_labels or {}): differences.add('container_labels', parameter=self.container_labels, active=os.container_labels) + if self.stop_signal is not None and self.stop_signal != os.stop_signal: + differences.add('stop_signal', parameter=self.stop_signal, active=os.stop_signal) if self.has_publish_changed(os.publish): differences.add('publish', parameter=self.publish, active=os.publish) if self.restart_policy is not None and self.restart_policy != os.restart_policy: @@ -1042,6 +1053,8 @@ class DockerService(DockerBaseClass): container_spec_args['labels'] = self.container_labels if self.hostname is not None: container_spec_args['hostname'] = self.hostname + if self.stop_signal is not None: + container_spec_args['stop_signal'] = self.stop_signal if self.tty is not None: container_spec_args['tty'] = self.tty if secrets is not None: @@ -1232,6 +1245,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.stop_signal = task_template_data['ContainerSpec'].get('StopSignal') update_config_data = raw_data['Spec'].get('UpdateConfig') if update_config_data: @@ -1570,6 +1584,7 @@ def main(): mode=dict(default='replicated', type='str'), replicas=dict(default=-1, type='int'), endpoint_mode=dict(choices=['vip', 'dnsrr']), + stop_signal=dict(type='str'), limit_cpu=dict(type='float'), limit_memory=dict(type='str'), reserve_cpu=dict(type='float'), @@ -1601,6 +1616,7 @@ def main(): update_max_failure_ratio=dict(docker_py_version='2.1.0', docker_api_version='1.25'), update_monitor=dict(docker_py_version='2.1.0', docker_api_version='1.25'), 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'), placement_preferences=dict(docker_py_version='2.4.0', docker_api_version='1.27'), publish=dict(docker_py_version='3.0.0', docker_api_version='1.25'), # specials 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 bef816f27d..b1a282a5f5 100644 --- a/test/integration/targets/docker_swarm_service/tasks/tests/options.yml +++ b/test/integration/targets/docker_swarm_service/tasks/tests/options.yml @@ -1390,6 +1390,46 @@ - networks_3 is changed - networks_4 is not changed +#################################################################### +## stop_signal ##################################################### +#################################################################### + +- name: stop_signal + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + stop_signal: 30 + register: stop_signal_1 + +- name: stop_signal (idempotency) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + stop_signal: 30 + register: stop_signal_2 + +- name: stop_signal (change) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + command: '/bin/sh -v -c "sleep 10m"' + stop_signal: 9 + register: stop_signal_3 + +- name: cleanup + docker_swarm_service: + name: "{{ service_name }}" + state: absent + diff: no + +- assert: + that: + - stop_signal_1 is changed + - stop_signal_2 is not changed + - stop_signal_3 is changed + #################################################################### ## placement_preferences ########################################### #################################################################### diff --git a/test/integration/targets/docker_swarm_service/vars/main.yml b/test/integration/targets/docker_swarm_service/vars/main.yml index 57fd89073b..241172010f 100644 --- a/test/integration/targets/docker_swarm_service/vars/main.yml +++ b/test/integration/targets/docker_swarm_service/vars/main.yml @@ -22,6 +22,7 @@ service_expected_output: mode: global mounts: null networks: null + stop_signal: null placement_preferences: null publish: - {mode: null, protocol: tcp, published_port: 60001, target_port: 60001}