From 12ccf36b0e7f9b233b8b71a21fe24b241ecb639b Mon Sep 17 00:00:00 2001 From: Bogdan Dobrelya Date: Thu, 28 Nov 2019 15:31:55 +0100 Subject: [PATCH] Fix action Apply ignoring managed-by arg From the very beginning (06036fd6dba3ba4b9a13052ea95a08ebcc97501e), the action apply was ignoring the passed --managed-by values and was always taking defaults ('paunch'). Fix this and provide no upgrade/update impact, which is for whatever --managed-by value given to paunch, perform all checks and searches also for that historically "wrong" value 'paunch': * if a container needs to be (re)started by a new 'managed-by', make sure it can be found by the default 'paunch' value as well, then reset its managed-by to the desired value. Closes-bug: #1853812 Change-Id: If129bbc1ff32941d06ff480f26870b10840591e0 Signed-off-by: Bogdan Dobrelya --- paunch/cmd.py | 2 +- paunch/runner.py | 57 ++++++++++++++++++++++++++----- paunch/tests/test_builder_base.py | 35 +++++++++++++++++++ 3 files changed, 84 insertions(+), 10 deletions(-) diff --git a/paunch/cmd.py b/paunch/cmd.py index 208fd50..df749bc 100644 --- a/paunch/cmd.py +++ b/paunch/cmd.py @@ -95,7 +95,7 @@ class Apply(command.Command): stdout, stderr, rc = paunch.apply( parsed_args.config_id, config, - managed_by='paunch', + managed_by=parsed_args.managed_by, labels=labels, cont_cmd=parsed_args.default_runtime, log_level=log_level, diff --git a/paunch/runner.py b/paunch/runner.py index cd0b3fa..09f38d1 100644 --- a/paunch/runner.py +++ b/paunch/runner.py @@ -76,9 +76,20 @@ class BaseRunner(object): '--format', fmt ] cmd_stdout, cmd_stderr, returncode = self.execute(cmd, self.log) - if returncode != 0: - return set() - return set(cmd_stdout.split()) + results = cmd_stdout.split() + if returncode != 0 or not results or results == ['']: + # NOTE(bogdando): also look by the historically used to + # be always specified defaults, we must also identify such configs + cmd = [ + self.cont_cmd, 'ps', '-a', + '--filter', 'label=managed_by=paunch', + '--format', fmt + ] + cmd_stdout, cmd_stderr, returncode = self.execute(cmd, self.log) + if returncode != 0: + return set() + results += cmd_stdout.split() + return set(results) def containers_in_config(self, conf_id): cmd = [ @@ -87,10 +98,21 @@ class BaseRunner(object): '--filter', 'label=config_id=%s' % conf_id ] cmd_stdout, cmd_stderr, returncode = self.execute(cmd, self.log) - if returncode != 0: - return [] + results = cmd_stdout.split() + if returncode != 0 or not results or results == ['']: + # NOTE(bogdando): also look by the historically used to + # be always specified defaults, we must also identify such configs + cmd = [ + self.cont_cmd, 'ps', '-q', '-a', + '--filter', 'label=managed_by=paunch', + '--filter', 'label=config_id=%s' % conf_id + ] + cmd_stdout, cmd_stderr, returncode = self.execute(cmd, self.log) + if returncode != 0: + return [] + results += cmd_stdout.split() - return [c for c in cmd_stdout.split()] + return [c for c in results] def inspect(self, name, output_format=None, o_type='container', quiet=False): @@ -231,9 +253,26 @@ class BaseRunner(object): '--format', '{{.Names}} %s' % fmt )) cmd_stdout, cmd_stderr, returncode = self.execute(cmd, self.log) - if returncode != 0: - return - for line in cmd_stdout.split("\n"): + results = cmd_stdout.split("\n") + if returncode != 0 or not results or results == ['']: + # NOTE(bogdando): also look by the historically used to + # be always specified defaults, we must also identify such configs + cmd = [ + self.cont_cmd, 'ps', '-a', + '--filter', 'label=managed_by=paunch' + ] + if conf_id: + cmd.extend(( + '--filter', 'label=config_id=%s' % conf_id + )) + cmd.extend(( + '--format', '{{.Names}} %s' % fmt + )) + cmd_stdout, cmd_stderr, returncode = self.execute(cmd, self.log) + if returncode != 0: + return + results += cmd_stdout.split("\n") + for line in results: if line: yield line.split() diff --git a/paunch/tests/test_builder_base.py b/paunch/tests/test_builder_base.py index e14b936..4f6b670 100644 --- a/paunch/tests/test_builder_base.py +++ b/paunch/tests/test_builder_base.py @@ -62,8 +62,11 @@ class TestBaseBuilder(base.TestCase): ('Pulled centos:7', 'ouch', 1), # pull centos:6 fails ('Pulled centos:7', '', 0), # pull centos:6 succeeds ('', '', 0), # ps for delete_missing_and_updated container_names + ('', '', 0), # ps2 for delete_missing_and_updated container_names ('', '', 0), # ps for after delete_missing_and_updated renames + ('', '', 0), # ps2 for after delete_missing_and_updated renames ('', '', 0), # ps to only create containers which don't exist + ('', '', 0), # ps2 to only create containers which don't exist ('Created one-12345678', '', 0), ('Created two-12345678', '', 0), ('Created three-12345678', '', 0), @@ -119,6 +122,14 @@ class TestBaseBuilder(base.TestCase): '--format', '{{.Names}} {{.Label "container_name"}}'], mock.ANY ), + # ps2 for delete_missing_and_updated container_names + mock.call( + ['docker', 'ps', '-a', + '--filter', 'label=managed_by=paunch', + '--filter', 'label=config_id=foo', + '--format', '{{.Names}} {{.Label "container_name"}}'], + mock.ANY + ), # ps for after delete_missing_and_updated renames mock.call( ['docker', 'ps', '-a', @@ -126,6 +137,13 @@ class TestBaseBuilder(base.TestCase): '--format', '{{.Names}} {{.Label "container_name"}}'], mock.ANY ), + # ps2 for after delete_missing_and_updated renames + mock.call( + ['docker', 'ps', '-a', + '--filter', 'label=managed_by=paunch', + '--format', '{{.Names}} {{.Label "container_name"}}'], + mock.ANY + ), # ps to only create containers which don't exist mock.call( ['docker', 'ps', '-a', @@ -134,6 +152,14 @@ class TestBaseBuilder(base.TestCase): '--format', '{{.Names}} {{.Label "container_name"}}'], mock.ANY ), + # ps2 to only create containers which don't exist + mock.call( + ['docker', 'ps', '-a', + '--filter', 'label=managed_by=paunch', + '--filter', 'label=config_id=foo', + '--format', '{{.Names}} {{.Label "container_name"}}'], + mock.ANY + ), # run one mock.call( ['docker', 'run', '--name', 'one-12345678', @@ -233,6 +259,8 @@ three-12345678 three''', '', 0), ('{"start_order": 2, "image": "centos:7"}', '', 0), # ps for after delete_missing_and_updated renames ('', '', 0), + # ps2 for after delete_missing_and_updated renames + ('', '', 0), # ps to only create containers which don't exist ('three-12345678 three', '', 0), ('Created one-12345678', '', 0), @@ -288,6 +316,13 @@ three-12345678 three''', '', 0), '--format', '{{.Names}} {{.Label "container_name"}}'], mock.ANY ), + # ps2 for after delete_missing_and_updated renames + mock.call( + ['docker', 'ps', '-a', + '--filter', 'label=managed_by=paunch', + '--format', '{{.Names}} {{.Label "container_name"}}'], + mock.ANY + ), # ps to only create containers which don't exist mock.call( ['docker', 'ps', '-a',