diff --git a/paunch/__init__.py b/paunch/__init__.py index 1cfef93..6dfc2c4 100644 --- a/paunch/__init__.py +++ b/paunch/__init__.py @@ -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() diff --git a/paunch/builder/compose1.py b/paunch/builder/compose1.py index 8bb6739..0d86107 100644 --- a/paunch/builder/compose1.py +++ b/paunch/builder/compose1.py @@ -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): diff --git a/paunch/cmd.py b/paunch/cmd.py index 41f78bb..4177794 100644 --- a/paunch/cmd.py +++ b/paunch/cmd.py @@ -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 diff --git a/paunch/tests/test_paunch.py b/paunch/tests/test_paunch.py index 3478f16..7ec1a9e 100644 --- a/paunch/tests/test_paunch.py +++ b/paunch/tests/test_paunch.py @@ -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()