Merge "Add graceful_timeout argument to kolla_docker"

This commit is contained in:
Jenkins 2017-05-10 08:40:36 +00:00 committed by Gerrit Code Review
commit a8433495dd
3 changed files with 20 additions and 4 deletions

View File

@ -587,6 +587,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':
@ -598,6 +599,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()
@ -619,6 +621,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):
@ -647,23 +650,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():
@ -714,6 +723,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',