Skip to content
Snippets Groups Projects
Verified Commit 7e7980ab authored by Karel Koci's avatar Karel Koci :metal:
Browse files

nsfarm: use logger instead of print

parent ffcf0b74
Branches
1 merge request!2Initial development
......@@ -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)
......
......@@ -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)
......
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment