docker_swarm_service: Add stop_grace_period option (#52519)

* Add stop_grace_period option

* Add changelog fragment

* Move doc type to last

* Add version_added
pull/52754/head
Hannes Ljungberg 6 years ago committed by John R Barker
parent 1234df131e
commit dd4b4aa941

@ -0,0 +1,2 @@
minor_changes:
- "docker_swarm_service - Added support for ``stop_grace_period`` parameter."

@ -338,6 +338,14 @@ options:
If changes are made the service will then be removed and recreated.
- Corresponds to the C(--network) option of C(docker service create).
type: list
stop_grace_period:
description:
- Time to wait before force killing a container.
- "Accepts a duration as a string in a format that look like:
C(5h34m56s), C(1m30s) etc. The supported units are C(us), C(ms), C(s), C(m) and C(h)."
- Corresponds to the C(--stop-grace-period) option of C(docker service create).
type: str
version_added: "2.8"
stop_signal:
description:
- Override default signal used to stop the container.
@ -771,6 +779,7 @@ class DockerService(DockerBaseClass):
self.secrets = None
self.constraints = None
self.networks = None
self.stop_grace_period = None
self.stop_signal = None
self.publish = None
self.placement_preferences = None
@ -819,6 +828,7 @@ class DockerService(DockerBaseClass):
'replicas': self.replicas,
'endpoint_mode': self.endpoint_mode,
'restart_policy': self.restart_policy,
'stop_grace_period': self.stop_grace_period,
'stop_signal': self.stop_signal,
'limit_cpu': self.limit_cpu,
'limit_memory': self.limit_memory,
@ -908,6 +918,8 @@ class DockerService(DockerBaseClass):
'restart_policy_window',
ap['restart_policy_window']
)
if ap['stop_grace_period'] is not None:
s.stop_grace_period = convert_duration_to_nanosecond(ap['stop_grace_period'])
s.update_delay = get_nanoseconds_from_raw_option(
'update_delay',
ap['update_delay']
@ -1036,6 +1048,8 @@ class DockerService(DockerBaseClass):
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.stop_grace_period is not None and self.stop_grace_period != os.stop_grace_period:
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.restart_policy is not None and self.restart_policy != os.restart_policy:
@ -1197,6 +1211,8 @@ class DockerService(DockerBaseClass):
container_spec_args['healthcheck'] = types.Healthcheck(**self.healthcheck)
if self.hostname is not None:
container_spec_args['hostname'] = self.hostname
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:
container_spec_args['stop_signal'] = self.stop_signal
if self.tty is not None:
@ -1384,6 +1400,7 @@ class DockerServiceManager(object):
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_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')
@ -1757,6 +1774,7 @@ def main():
mode=dict(type='str', default='replicated'),
replicas=dict(type='int', default=-1),
endpoint_mode=dict(type='str', choices=['vip', 'dnsrr']),
stop_grace_period=dict(type='str'),
stop_signal=dict(type='str'),
limit_cpu=dict(type='float'),
limit_memory=dict(type='str'),

@ -1634,6 +1634,46 @@
- networks_5.rebuilt == true
when: docker_api_version is version('1.29', '<')
####################################################################
## stop_grace_period ###############################################
####################################################################
- name: stop_signal
docker_swarm_service:
name: "{{ service_name }}"
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
stop_grace_period: 60s
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_grace_period: 60s
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_grace_period: 1m30s
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
####################################################################
## stop_signal #####################################################
####################################################################

@ -25,6 +25,7 @@ service_expected_output:
mode: global
mounts: null
networks: null
stop_grace_period: null
stop_signal: null
placement_preferences: null
publish:

Loading…
Cancel
Save