diff --git a/ansible/library/kolla_docker.py b/ansible/library/kolla_docker.py index d521e2e8fd..f06531d102 100644 --- a/ansible/library/kolla_docker.py +++ b/ansible/library/kolla_docker.py @@ -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', diff --git a/releasenotes/notes/add-graceful-timeout-argument-a8b71a389351599b.yaml b/releasenotes/notes/add-graceful-timeout-argument-a8b71a389351599b.yaml new file mode 100644 index 0000000000..0775df5825 --- /dev/null +++ b/releasenotes/notes/add-graceful-timeout-argument-a8b71a389351599b.yaml @@ -0,0 +1,4 @@ +--- +features: + - Add graceful timeout argument to kolla_docker library for stoping, + restaring container. diff --git a/tests/test_kolla_docker.py b/tests/test_kolla_docker.py index 65874f179d..8b7e837ff1 100644 --- a/tests/test_kolla_docker.py +++ b/tests/test_kolla_docker.py @@ -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',