diff --git a/focker.py b/focker.py index 767f5af..e061106 100644 --- a/focker.py +++ b/focker.py @@ -69,9 +69,11 @@ def create_parser(): parser = subparsers.add_parser('list') parser.set_defaults(func=command_volume_list) + parser.add_argument('--full-sha256', '-f', action='store_true') parser = subparsers.add_parser('tag') parser.set_defaults(func=command_volume_tag) + parser.add_argument('reference', type=str) parser.add_argument('tags', type=str, nargs='+') parser = subparsers.add_parser('untag') diff --git a/image.py b/image.py index 2de9353..4799c8b 100644 --- a/image.py +++ b/image.py @@ -98,10 +98,11 @@ def command_image_list(args): def command_image_prune(args): + poolname = zfs_poolname() again = True while again: again = False - lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,origin,name', '-H']) + lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,origin,name', '-H', '-r', poolname + '/focker/images']) used = set() for r in lst: if r[2] == '-': diff --git a/volume.py b/volume.py index 1a447e2..a11e5ed 100644 --- a/volume.py +++ b/volume.py @@ -1,5 +1,6 @@ from .misc import random_sha256_hexdigest from .zfs import * +from tabulate import tabulate def command_volume_create(args): @@ -14,16 +15,23 @@ def command_volume_create(args): def command_volume_prune(args): - raise NotImplementedError + zfs_prune(focker_type='volume') def command_volume_list(args): - raise NotImplementedError + poolname = zfs_poolname() + lst = zfs_parse_output(['zfs', 'list', '-o', 'name,refer,focker:sha256,focker:tags', '-H', '-r', poolname + '/focker/volumes']) + lst = list(filter(lambda a: a[2] != '-', lst)) + lst = list(map(lambda a: [ a[3], a[1], + a[2] if args.full_sha256 else a[2][:7] ], lst)) + print(tabulate(lst, headers=['Tags', 'Size', 'SHA256'])) def command_volume_tag(args): - raise NotImplementedError + name, _ = zfs_find(args.reference, focker_type='volume') + zfs_untag(args.tags) + zfs_tag(name, args.tags) def command_volume_untag(args): - raise NotImplementedError + zfs_untag(args.tags) diff --git a/zfs.py b/zfs.py index b578305..f60d3fa 100644 --- a/zfs.py +++ b/zfs.py @@ -49,6 +49,26 @@ def zfs_find(reference, focker_type='image', zfs_type='filesystem'): return (lst[0][3], lst[0][0]) +def zfs_prune(focker_type='image'): + poolname = zfs_poolname() + again = True + while again: + again = False + lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,origin,name', '-H', '-r', poolname + '/focker/' + focker_type + 's']) + used = set() + for r in lst: + if r[2] == '-': + continue + used.add(r[2].split('@')[0]) + for r in lst: + if r[0] == '-' or r[1] != '-': + continue + if r[3] not in used: + print('Removing:', r[3]) + zfs_run(['zfs', 'destroy', '-r', '-f', r[3]]) + again = True + + def zfs_clone(name, target_name): zfs_run(['zfs', 'clone', name, target_name])