From 4afb7b0e56371fb6750cf26d0c7f62f348e87616 Mon Sep 17 00:00:00 2001 From: Bogdan Dobrelya Date: Tue, 13 Aug 2019 11:42:26 +0200 Subject: [PATCH] Fix mismatching fixed vs unique container names Sometimes, like when redeploying in-place, containers for the "fixed" service name might already existed, thus get the prefixed names. That might create mismatches. For example the pidfile names may diverge by the "fixzed" container service name vs its predictable prefixed unique name. Fix that by using the predictable unique names instead of the service container names for the builder and paunch actions run, debug/print-cmd that rely on it. This is achieved via a new parameter for the real container name (a delegate) used for the "fixed" service container name. For podman builder, we use that delegate for conmon pidfile and logging setup. So that now it always matches the PIDFile specified in the systemd unit generated for that container. For docker builder, we have no special uses for delegates, but we support that parameter to simplify the code around (so that there will be no need to wrap things with 'if cli == podman else...'). Conflicts: paunch/builder/podman.py Closes-bug: #1839929 Change-Id: I5617e11f5d315f408d818e1ce47aa68f4a0d777a Signed-off-by: Bogdan Dobrelya (cherry picked from commit 5d174c1bea474a34da6f4f68173323a8243c4fc5) --- paunch/__init__.py | 10 ++++++---- paunch/builder/base.py | 4 +++- paunch/builder/compose1.py | 7 ++++++- paunch/builder/podman.py | 13 ++++++++++--- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/paunch/__init__.py b/paunch/__init__.py index 93dae65..c6f4868 100644 --- a/paunch/__init__.py +++ b/paunch/__init__.py @@ -187,13 +187,14 @@ def debug(config_id, container_name, action, config, managed_by, labels=None, log=log ) if action == 'print-cmd': + uname = r.unique_container_name(container_name) cmd = [ r.cont_cmd, 'run', '--name', - r.unique_container_name(container_name) + uname ] - builder.container_run_args(cmd, container_name) + builder.container_run_args(cmd, container_name, uname) if '--health-cmd' in cmd: health_check_arg_index = cmd.index('--health-cmd') + 1 @@ -208,13 +209,14 @@ def debug(config_id, container_name, action, config, managed_by, labels=None, print(' '.join(cmd)) elif action == 'run': + uname = r.unique_container_name(container_name) cmd = [ r.cont_cmd, 'run', '--name', - r.unique_container_name(container_name) + uname ] - if builder.container_run_args(cmd, container_name): + if builder.container_run_args(cmd, container_name, uname): return r.execute_interactive(cmd, log) elif action == 'dump-yaml': print(yaml.safe_dump(config, default_flow_style=False)) diff --git a/paunch/builder/base.py b/paunch/builder/base.py index 71277d3..45bec7b 100644 --- a/paunch/builder/base.py +++ b/paunch/builder/base.py @@ -105,7 +105,9 @@ class BaseBuilder(object): ] self.label_arguments(cmd, container) self.log.debug("Start container {}.".format(container)) - validations_passed = self.container_run_args(cmd, container) + validations_passed = self.container_run_args(cmd, + container, + container_name) elif action == 'exec': cmd = [self.runner.cont_cmd, 'exec'] validations_passed = self.cont_exec_args(cmd, container) diff --git a/paunch/builder/compose1.py b/paunch/builder/compose1.py index 6c03a01..717b65d 100644 --- a/paunch/builder/compose1.py +++ b/paunch/builder/compose1.py @@ -22,13 +22,18 @@ class ComposeV1Builder(base.BaseBuilder): super(ComposeV1Builder, self).__init__(config_id, config, runner, labels, log) - def container_run_args(self, cmd, container): + def container_run_args(self, cmd, container, delegate=None): """Prepare the run command args, from the container configuration. :param cmd: The list of command options to be modified :param container: A dict with container configurations + :delegate: A compatibility parameter for podman, does nothing here :returns: True if configuration is valid, otherwise False """ + if delegate and container != delegate: + self.log.debug("Delegate {} of container {} has no special " + "meanings for this context and will be " + "ignored".format(delegate, container)) cconfig = self.config[container] if cconfig.get('detach', True): cmd.append('--detach=true') diff --git a/paunch/builder/podman.py b/paunch/builder/podman.py index 374a16c..21620d5 100644 --- a/paunch/builder/podman.py +++ b/paunch/builder/podman.py @@ -23,17 +23,24 @@ class PodmanBuilder(base.BaseBuilder): labels, log, cont_log_path, healthcheck_disabled) - def container_run_args(self, cmd, container): + def container_run_args(self, cmd, container, delegate=None): """Prepare the run command args, from the container configuration. :param cmd: The list of command options to be modified :param container: A dict with container configurations + :param delegate: A predictable/unique name of the actual container :returns: True if configuration is valid, otherwise False """ + if delegate and container != delegate: + self.log.debug("Container {} has a delegate " + "{}".format(container, delegate)) + if not delegate: + delegate = container cconfig = self.config[container] - # write out a pid file so we can restart the container via systemd - cmd.append('--conmon-pidfile=/var/run/{}.pid'.format(container)) + # write out a pid file so we can restart the container delegate + # via systemd + cmd.append('--conmon-pidfile=/var/run/{}.pid'.format(delegate)) if cconfig.get('detach', True): cmd.append('--detach=true')