Allow to not cleanup containers that aren't in config

Similar to Ic06ac0f41767ca513c612b1ebb38d2ff92500ea5 :

Relax Paunch and allows an operator to apply a config on a specific
container without removing the other containers in the same config_id.
Thanks to the paunch apply (...) --no-cleanup, if a container is
installed on the host for a given config_id, but not in the given config
anymore; it won't be removed.

The cleanup still happens by default for backward compatibility.

Signed-off-by: Bogdan Dobrelya <bdobreli@redhat.com>
Change-Id: I479eaa3b58c3df091e1b78a01c4fb0595d81b37c
(cherry picked from commit 8489a0cfbe
and 5dcc4df592)
This commit is contained in:
Bogdan Dobrelya 2020-11-17 15:13:59 +01:00
parent cb7f023d22
commit 643d83c855
4 changed files with 30 additions and 10 deletions

View File

@ -24,7 +24,7 @@ __version__ = pbr.version.VersionInfo('paunch').version_string()
def apply(config_id, config, managed_by, labels=None, docker_cmd=None,
log_level=None, log_file=None):
log_level=None, log_file=None, cleanup=True):
"""Execute supplied container configuration.
:param str config_id: Unique config ID, should not be re-used until any
@ -39,6 +39,8 @@ def apply(config_id, config, managed_by, labels=None, docker_cmd=None,
:param str docker_cmd: Optional override to the docker command to run.
:param int log_level: optional log level for loggers
:param int log_file: optional log file for messages
:param bool cleanup: optional boolean to delete containers missing in the
config.
:returns (list, list, int) lists of stdout and stderr for each execution,
and a single return code representing the
@ -53,7 +55,8 @@ def apply(config_id, config, managed_by, labels=None, docker_cmd=None,
config=config,
runner=r,
labels=labels,
log=log
log=log,
cleanup=cleanup
)
return builder.apply()

View File

@ -21,13 +21,15 @@ from paunch.utils import common
class ComposeV1Builder(object):
def __init__(self, config_id, config, runner, labels=None, log=None):
def __init__(self, config_id, config, runner, labels=None, log=None,
cleanup=True):
self.config_id = config_id
self.config = config
self.labels = labels
self.runner = runner
# Leverage pre-configured logger
self.log = log or common.configure_logging(__name__)
self.cleanup = cleanup
def apply(self):
@ -108,10 +110,15 @@ class ComposeV1Builder(object):
container = cn[0]
# if the desired name is not in the config, delete it
if cn[-1] not in self.config:
self.log.debug("Deleting container (removed): "
"%s" % container)
self.runner.remove_container(container)
deleted = True
if self.cleanup:
self.log.debug("Deleting container (removed): "
"%s" % container)
self.runner.remove_container(container)
deleted = True
else:
self.log.debug("Skipping container (cleanup disabled): "
"%s" % container)
continue
return deleted
def delete_updated(self, container, container_names):

View File

@ -59,6 +59,13 @@ class Apply(command.Command):
required=True,
help=('ID to assign to containers'),
)
parser.add_argument(
'--no-cleanup',
dest='cleanup',
action='store_false',
default=True,
help=('Whether or not we delete containers missing in the config')
)
return parser
def take_action(self, parsed_args):
@ -83,7 +90,8 @@ class Apply(command.Command):
managed_by='paunch',
labels=labels,
log_level=log_level,
log_file=log_file
log_file=log_file,
cleanup=parsed_args.cleanup
)
return rc

View File

@ -31,7 +31,8 @@ class TestPaunch(base.TestCase):
config={'bar': 'baz'},
runner=runner.return_value,
labels=None,
log=mock.ANY
log=mock.ANY,
cleanup=True
)
builder.return_value.apply.assert_called_once_with()
@ -51,7 +52,8 @@ class TestPaunch(base.TestCase):
config={'bar': 'baz'},
runner=runner.return_value,
labels={'bink': 'boop'},
log=mock.ANY
log=mock.ANY,
cleanup=True
)
builder.return_value.apply.assert_called_once_with()