From 7a46e18d9639f66c53fe0280e54f721ddddfd8e4 Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Sat, 25 Apr 2020 11:36:42 +0200 Subject: [PATCH] Started implementing focker jail prune. --- focker.py | 6 +++++- jail.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/focker.py b/focker.py index 3d31467..e4f41e4 100644 --- a/focker.py +++ b/focker.py @@ -18,7 +18,8 @@ from .zfs import zfs_init from .jail import command_jail_run, \ command_jail_list, \ command_jail_tag, \ - command_jail_untag + command_jail_untag, \ + command_jail_prune def create_parser(): @@ -75,6 +76,9 @@ def create_parser(): parser.set_defaults(func=command_jail_untag) parser.add_argument('tags', type=str, nargs='+') + parser = subparsers.add_parser('prune') + parser.set_defaults(func=command_jail_prune) + # volume subparsers = subparsers_top.add_parser('volume').add_subparsers() parser = subparsers.add_parser('create') diff --git a/jail.py b/jail.py index 153bddd..81c575e 100644 --- a/jail.py +++ b/jail.py @@ -56,6 +56,10 @@ def jail_run(path, command, mounts=[]): raise RuntimeError('Command failed') +def jail_remove(path): + print('Removing jail:', path) + + def command_jail_run(args): base, _ = zfs_snapshot_by_tag_or_sha256(args.image) # root = '/'.join(base.split('/')[:-1]) @@ -95,3 +99,15 @@ def command_jail_tag(args): def command_jail_untag(args): zfs_untag(args.tags, focker_type='jail') + + +def command_jail_prune(args): + jails = subprocess.check_output(['jls', '--libxo=json']) + jails = json.loads(jails)['jail-information']['jail'] + used = set() + for j in jails: + used.add(j['path']) + lst = zfs_list(fields=['focker:sha256,focker:tags,mountpoint,name'], focker_type='jail') + for j in lst: + if j[1] == '-' and j[2] not in used: + jail_remove(j[3])