Add graceful_timeout argument to kolla_docker

Currently, when stoping/restarting container, Kolla uses default timeout value
between SIGTERM and SIGKILL provided by docker which is 10 sec. But some
services require more than it to finish graceful shutdown progress.

This patchset adds graceful_timeout to kolla_docker to override the default
one.

Partial Implements: bp signaling-to-container

Change-Id: Ica0b48a53c650cc23dfa1955027d2cf936a5932f
This commit is contained in:
Duong Ha-Quang 2017-04-04 15:04:14 +07:00
parent 1295f583a1
commit d929359550
3 changed files with 20 additions and 4 deletions

View File

@ -595,6 +595,7 @@ class DockerWorker(object):
# If config_strategy is COPY_ONCE or container's parameters are
# changed, try to start a new one.
if config_strategy == 'COPY_ONCE' or self.check_container_differs():
self.stop_container()
self.remove_container()
self.start_container()
elif config_strategy == 'COPY_ALWAYS':
@ -606,6 +607,7 @@ class DockerWorker(object):
container = self.check_container()
if container and self.check_container_differs():
self.stop_container()
self.remove_container()
container = self.check_container()
@ -627,6 +629,7 @@ class DockerWorker(object):
msg="Container exited with non-zero return code"
)
if self.params.get('remove_on_exit'):
self.stop_container()
self.remove_container()
def get_container_env(self):
@ -655,23 +658,29 @@ class DockerWorker(object):
def stop_container(self):
name = self.params.get('name')
graceful_timeout = self.params.get('graceful_timeout')
if not graceful_timeout:
graceful_timeout = 10
container = self.check_container()
if not container:
self.module.fail_json(
msg="No such container: {} to stop".format(name))
elif not container['Status'].startswith('Exited '):
self.changed = True
self.dc.stop(name)
self.dc.stop(name, timeout=graceful_timeout)
def restart_container(self):
name = self.params.get('name')
graceful_timeout = self.params.get('graceful_timeout')
if not graceful_timeout:
graceful_timeout = 10
info = self.get_container_info()
if not info:
self.module.fail_json(
msg="No such container: {}".format(name))
else:
self.changed = True
self.dc.restart(name)
self.dc.restart(name, timeout=graceful_timeout)
def create_volume(self):
if not self.check_volume():
@ -722,6 +731,7 @@ def generate_module():
security_opt=dict(required=False, type='list', default=list()),
pid_mode=dict(required=False, type='str', choices=['host', '']),
privileged=dict(required=False, type='bool', default=False),
graceful_timeout=dict(required=False, type='int', default=10),
remove_on_exit=dict(required=False, type='bool', default=True),
restart_policy=dict(required=False, type='str', choices=[
'no',

View File

@ -0,0 +1,4 @@
---
features:
- Add graceful timeout argument to kolla_docker library for stoping,
restaring container.

View File

@ -65,6 +65,7 @@ class ModuleArgsTest(base.BaseTestCase):
security_opt=dict(required=False, type='list', default=list()),
pid_mode=dict(required=False, type='str', choices=['host', '']),
privileged=dict(required=False, type='bool', default=False),
graceful_timeout=dict(required=False, type='int', default=10),
remove_on_exit=dict(required=False, type='bool', default=True),
restart_policy=dict(
required=False, type='str', choices=['no',
@ -228,6 +229,7 @@ class TestContainer(base.BaseTestCase):
updated_cont_list = copy.deepcopy(self.fake_data['containers'])
updated_cont_list.pop(0)
self.dw.dc.containers.side_effect = [self.fake_data['containers'],
self.fake_data['containers'],
self.fake_data['containers'],
updated_cont_list,
self.fake_data['containers']
@ -270,7 +272,7 @@ class TestContainer(base.BaseTestCase):
self.assertTrue(self.dw.changed)
self.dw.dc.containers.assert_called_once_with(all=True)
self.dw.dc.stop.assert_called_once_with('my_container')
self.dw.dc.stop.assert_called_once_with('my_container', timeout=10)
def test_stop_container_not_exists(self):
self.dw = get_DockerWorker({'name': 'fake_container',
@ -296,7 +298,7 @@ class TestContainer(base.BaseTestCase):
self.assertTrue(self.dw.changed)
self.dw.dc.containers.assert_called_once_with(all=True)
self.dw.dc.inspect_container.assert_called_once_with('my_container')
self.dw.dc.restart.assert_called_once_with('my_container')
self.dw.dc.restart.assert_called_once_with('my_container', timeout=10)
def test_restart_container_not_exists(self):
self.dw = get_DockerWorker({'name': 'fake-container',