From 0435624b8d1c5443936995ff7d1ad1c1fff9e98e Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Fri, 24 Apr 2020 17:16:45 +0200 Subject: [PATCH] Added focker image tag --- focker.py | 6 ++++++ image.py | 6 ++++++ jail.py | 2 +- zfs.py | 24 +++++++++++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/focker.py b/focker.py index 835f556..8759a22 100644 --- a/focker.py +++ b/focker.py @@ -3,6 +3,7 @@ import yaml import os # from weir import zfs, process from .image import command_image_build, \ + command_image_tag, \ command_image_untag, \ command_image_list, \ command_image_prune, \ @@ -22,6 +23,11 @@ def create_parser(): parser.add_argument('focker_dir', type=str) parser.add_argument('--tag', '-t', type=str, nargs='+', default=[]) + parser = subparsers.add_parser('tag') + parser.set_defaults(func=command_image_tag) + parser.add_argument('reference', type=str) + parser.add_argument('tags', type=str, nargs='+') + parser = subparsers.add_parser('untag') parser.set_defaults(func=command_image_untag) parser.add_argument('tags', type=str, nargs='+', default=[]) diff --git a/image.py b/image.py index dc66699..aa10fb3 100644 --- a/image.py +++ b/image.py @@ -78,6 +78,12 @@ def command_image_build(args): zfs_tag(image.split('@')[0], args.tag) +def command_image_tag(args): + zfs_untag(args.tags) + name, _ = zfs_find(args.reference, focker_type='image', zfs_type='filesystem') + zfs_tag(name, args.tags) + + def command_image_untag(args): zfs_untag(args.tags) diff --git a/jail.py b/jail.py index 2dc1691..a24bb2e 100644 --- a/jail.py +++ b/jail.py @@ -20,6 +20,7 @@ def jail_run(path, command): command = ['jail', '-c', 'host.hostname=' + os.path.split(path)[1], 'persist=1', 'mount.devfs=1', 'interface=lo1', 'ip4.addr=127.0.1.0', 'path=' + path, 'command', '/bin/sh', '-c', command] print('Running:', ' '.join(command)) try: + shutil.copyfile('/etc/resolv.conf', os.path.join(path, 'etc/resolv.conf')) res = subprocess.run(command) finally: try: @@ -42,7 +43,6 @@ def command_jail_run(args): break zfs_run(['zfs', 'clone', base, name]) try: - shutil.copyfile('/etc/resolv.conf', os.path.join(zfs_mountpoint(name), 'etc/resolv.conf')) jail_run(zfs_mountpoint(name), args.command) # subprocess.check_output(['jail', '-c', 'interface=lo1', 'ip4.addr=127.0.1.0', 'path=' + zfs_mountpoint(name), 'command', command]) finally: diff --git a/zfs.py b/zfs.py index 2d6699f..b578305 100644 --- a/zfs.py +++ b/zfs.py @@ -32,6 +32,23 @@ def zfs_snapshot_by_tag_or_sha256(s): return (lst[0][3], lst[0][0]) +def zfs_find(reference, focker_type='image', zfs_type='filesystem'): + poolname = zfs_poolname() + lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,focker:tags,type,name', '-H', '-t', zfs_type, '-r', poolname + '/focker/' + focker_type + 's']) + def match(sha256, tags, type, name): + if sha256.startswith(reference) or \ + any(map(lambda a: a.startswith(reference), tags.split(' '))) or \ + name.split('/')[-1].startswith(reference): + return True + return False + lst = list(filter(lambda a: match(*a), lst)) + if len(lst) == 0: + raise ValueError('Reference not found: ' + reference) + if len(lst) > 1: + raise ValueError('Ambiguous reference: ' + reference) + return (lst[0][3], lst[0][0]) + + def zfs_clone(name, target_name): zfs_run(['zfs', 'clone', name, target_name]) @@ -97,11 +114,16 @@ def zfs_untag(tags): zfs_tag(row[0], cur_tags, replace=True) -def zfs_init(): +def zfs_poolname(): poolname = zfs_parse_output(['zfs', 'list', '-H', '/']) if len(poolname) == 0: raise ValueError('Not a ZFS root') poolname = poolname[0][0].split('/')[0] + return poolname + + +def zfs_init(): + poolname = zfs_poolname() print('poolname:', poolname) for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']: if not os.path.exists(path):