diff --git a/nsfarm/lxd/__main__.py b/nsfarm/lxd/__main__.py index 4a8c3c004836be59e737f2c5ca7015da9fcec96b..07e8d4ac03034c33c3ebc55d9137a94004df2c97 100644 --- a/nsfarm/lxd/__main__.py +++ b/nsfarm/lxd/__main__.py @@ -9,7 +9,7 @@ from . import Container def parser(parser): subparsers = parser.add_subparsers() - clean = subparsers.add_parser('clean', help='Remove old and unused packages') + clean = subparsers.add_parser('clean', help='Remove old and unused containers') clean.set_defaults(lxd_op='clean') clean.add_argument( 'DELTA', @@ -82,7 +82,9 @@ def parse_deltatime(spec): def op_clean(args, _): """Handler for command line operation clean """ - utils.clean(parse_deltatime(args.DELTA), dry_run=args.dry_run) + removed = utils.clean(parse_deltatime(args.DELTA), dry_run=args.dry_run) + if removed: + print('\n'.join(removed)) sys.exit(0) diff --git a/nsfarm/lxd/container.py b/nsfarm/lxd/container.py index 06c08ff4f079ef5a2afd7b04103441c84f16d394..fec44566cbf917f5e6c03ea2e394b3107d034adc 100644 --- a/nsfarm/lxd/container.py +++ b/nsfarm/lxd/container.py @@ -12,7 +12,8 @@ from . import _lxd IMAGE_INIT_PATH = "/nsfarm-init.sh" # Where we deploy initialization script for image IMGS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "imgs") -LOGGER = logging.getLogger(__package__) + +logger = logging.getLogger(__package__) class Container: @@ -92,7 +93,7 @@ class Container: self._lxd_image = _lxd.LOCAL.images.get_by_alias(self._image_alias) return # We do not have appropriate image so prepare it - LOGGER.warning("Bootstrapping image: %s", self._image_alias) + logger.warning("Bootstrapping image: %s", self._image_alias) image_source = { 'type': 'image', } @@ -116,7 +117,7 @@ class Container: # TODO found other way to match reason if not str(elxd).endswith("This container already exists"): raise - LOGGER.warning("Other instance is already bootsrapping image probably. " + logger.warning("Other instance is already bootsrapping image probably. " "Waiting for following container to go away: %s", container_name) while _lxd.LOCAL.containers.exists(container_name): time.sleep(1) diff --git a/nsfarm/lxd/utils.py b/nsfarm/lxd/utils.py index 06963731ce57e8140c66878f5a18633955b590df..d693a70de90a3b777c743ac42e2fb63bbee4c126 100644 --- a/nsfarm/lxd/utils.py +++ b/nsfarm/lxd/utils.py @@ -7,16 +7,21 @@ import dateutil.parser from . import container from . import _lxd +logger = logging.getLogger(__package__) + def clean(delta, dry_run=False): """Remove all images that were not used for longer then given delta. delta: this should be instance of datetime.relativedelta dry_run: do not remove anything only report alias of those to be removed on stdout + + Returns list of (to be) removed images. """ _lxd.connect() since = datetime.today() - delta + removed = list() for img in _lxd.LOCAL.images.all(): if next((alias for alias in img.aliases if alias["name"].startswith("nsfarm/")), None) is None: continue @@ -25,11 +30,11 @@ def clean(delta, dry_run=False): img.last_used_at if not img.last_used_at.startswith("0001-01-01") else img.uploaded_at ).replace(tzinfo=None) if last_used < since: + removed.append(img.aliases[0]["name"]) if not dry_run: - logging.warning("Removing image: %s %s", img.aliases[0]["name"], img.fingerprint) + logger.warning("Removing image: %s %s", img.aliases[0]["name"], img.fingerprint) img.delete() - else: - print(img.aliases[0]["name"]) + return removed def all_images(): @@ -49,10 +54,10 @@ def bootstrap(imgs=None): """ success = True for img in all_images() if imgs is None else imgs: - logging.info("Trying to bootstrap: %s", img) + logger.info("Trying to bootstrap: %s", img) try: container.Container(img).prepare_image() - except Exception as exc: + except Exception: success = False - logging.exception("Bootstrap failed for: %s", img) + logger.exception("Bootstrap failed for: %s", img) return success