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 (...) --cleanup=False, 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.

Change-Id: I479eaa3b58c3df091e1b78a01c4fb0595d81b37c
This commit is contained in:
Emilien Macchi 2020-02-11 19:29:54 -05:00
parent 3012fe75aa
commit 8489a0cfbe
6 changed files with 40 additions and 16 deletions

View File

@ -26,7 +26,7 @@ __version__ = pbr.version.VersionInfo('paunch').version_string()
def apply(config_id, config, managed_by, labels=None, cont_cmd='podman',
default_runtime=None, log_level=None, log_file=None,
cont_log_path=None, healthcheck_disabled=False):
cont_log_path=None, healthcheck_disabled=False, cleanup=True):
"""Execute supplied container configuration.
:param str config_id: Unique config ID, should not be re-used until any
@ -46,6 +46,8 @@ def apply(config_id, config, managed_by, labels=None, cont_cmd='podman',
podman engine. Must be an absolute path.
:param bool healthcheck_disabled: optional boolean to disable container
healthcheck.
: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
@ -66,7 +68,8 @@ def apply(config_id, config, managed_by, labels=None, cont_cmd='podman',
labels=labels,
log=log,
cont_log_path=cont_log_path,
healthcheck_disabled=healthcheck_disabled
healthcheck_disabled=healthcheck_disabled,
cleanup=cleanup
)
else:
r = runner.DockerRunner(managed_by, cont_cmd=cont_cmd, log=log)
@ -75,7 +78,8 @@ def apply(config_id, config, managed_by, labels=None, cont_cmd='podman',
config=config,
runner=r,
labels=labels,
log=log
log=log,
cleanup=cleanup
)
return builder.apply()

View File

@ -26,7 +26,7 @@ from paunch.utils import systemd
class BaseBuilder(object):
def __init__(self, config_id, config, runner, labels, log=None,
cont_log_path=None, healthcheck_disabled=False):
cont_log_path=None, healthcheck_disabled=False, cleanup=True):
self.config_id = config_id
self.config = config
self.labels = labels
@ -35,6 +35,7 @@ class BaseBuilder(object):
self.log = log or common.configure_logging(__name__)
self.cont_log_path = cont_log_path
self.healthcheck_disabled = healthcheck_disabled
self.cleanup = cleanup
if os.path.isfile('/var/lib/tripleo-config/.ansible-managed'):
msg = ('Containers were previously deployed with '
@ -178,8 +179,13 @@ class BaseBuilder(object):
# 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)
if self.cleanup:
self.log.debug("Deleting container (removed): "
"%s" % container)
self.runner.remove_container(container)
else:
self.log.debug("Skipping container (cleanup disabled): "
"%s" % container)
continue
ex_data_str = self.runner.inspect(

View File

@ -17,9 +17,10 @@ from paunch.utils import common
class ComposeV1Builder(base.BaseBuilder):
def __init__(self, config_id, config, runner, labels=None, log=None):
def __init__(self, config_id, config, runner, labels=None, log=None,
cleanup=True):
super(ComposeV1Builder, self).__init__(config_id, config, runner,
labels, log)
labels, log, cleanup)
def container_run_args(self, cmd, container, delegate=None):
"""Prepare the run command args, from the container configuration.

View File

@ -19,10 +19,10 @@ from paunch.utils import common
class PodmanBuilder(base.BaseBuilder):
def __init__(self, config_id, config, runner, labels=None, log=None,
cont_log_path=None, healthcheck_disabled=False):
cont_log_path=None, healthcheck_disabled=False, cleanup=True):
super(PodmanBuilder, self).__init__(config_id, config, runner,
labels, log, cont_log_path,
healthcheck_disabled)
healthcheck_disabled, cleanup)
def container_run_args(self, cmd, container, delegate=None):
"""Prepare the run command args, from the container configuration.

View File

@ -79,6 +79,13 @@ class Apply(command.Command):
default=False,
help=('Whether or not we disable the containers healthchecks')
)
parser.add_argument(
'--cleanup',
dest='cleanup',
action='store_true',
default=True,
help=('Whether or not we delete containers missing in the config')
)
return parser
def take_action(self, parsed_args):
@ -101,7 +108,8 @@ class Apply(command.Command):
log_level=log_level,
log_file=log_file,
cont_log_path=parsed_args.cont_log_path,
healthcheck_disabled=parsed_args.healthcheck_disabled
healthcheck_disabled=parsed_args.healthcheck_disabled,
cleanup=parsed_args.cleanup
)
return rc

View File

@ -31,7 +31,8 @@ class TestPaunchDockerRuntime(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()
@ -52,7 +53,8 @@ class TestPaunchDockerRuntime(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()
@ -119,7 +121,8 @@ class TestPaunchPodmanRuntime(base.TestCase):
labels=None,
log=mock.ANY,
cont_log_path=None,
healthcheck_disabled=False
healthcheck_disabled=False,
cleanup=True
)
builder.return_value.apply.assert_called_once_with()
@ -143,7 +146,8 @@ class TestPaunchPodmanRuntime(base.TestCase):
labels=None,
log=mock.ANY,
cont_log_path='/var/log',
healthcheck_disabled=False
healthcheck_disabled=False,
cleanup=True
)
builder.return_value.apply.assert_called_once_with()
@ -168,7 +172,8 @@ class TestPaunchPodmanRuntime(base.TestCase):
labels={'bink': 'boop'},
log=mock.ANY,
cont_log_path=None,
healthcheck_disabled=False
healthcheck_disabled=False,
cleanup=True
)
builder.return_value.apply.assert_called_once_with()