diff --git a/paunch/__init__.py b/paunch/__init__.py index 11dbc02..a0869f1 100644 --- a/paunch/__init__.py +++ b/paunch/__init__.py @@ -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() diff --git a/paunch/builder/base.py b/paunch/builder/base.py index 57bef8d..eb3a2a0 100644 --- a/paunch/builder/base.py +++ b/paunch/builder/base.py @@ -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( diff --git a/paunch/builder/compose1.py b/paunch/builder/compose1.py index ba58a8d..200c11d 100644 --- a/paunch/builder/compose1.py +++ b/paunch/builder/compose1.py @@ -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. diff --git a/paunch/builder/podman.py b/paunch/builder/podman.py index 5bd9044..9643b47 100644 --- a/paunch/builder/podman.py +++ b/paunch/builder/podman.py @@ -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. diff --git a/paunch/cmd.py b/paunch/cmd.py index df749bc..ec2f0c0 100644 --- a/paunch/cmd.py +++ b/paunch/cmd.py @@ -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 diff --git a/paunch/tests/test_paunch.py b/paunch/tests/test_paunch.py index 3ae2062..200e3a5 100644 --- a/paunch/tests/test_paunch.py +++ b/paunch/tests/test_paunch.py @@ -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()