@@ -3,6 +3,7 @@ import yaml | |||||
import os | import os | ||||
# from weir import zfs, process | # from weir import zfs, process | ||||
from .image import command_image_build, \ | from .image import command_image_build, \ | ||||
command_image_tag, \ | |||||
command_image_untag, \ | command_image_untag, \ | ||||
command_image_list, \ | command_image_list, \ | ||||
command_image_prune, \ | command_image_prune, \ | ||||
@@ -22,6 +23,11 @@ def create_parser(): | |||||
parser.add_argument('focker_dir', type=str) | parser.add_argument('focker_dir', type=str) | ||||
parser.add_argument('--tag', '-t', type=str, nargs='+', default=[]) | 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 = subparsers.add_parser('untag') | ||||
parser.set_defaults(func=command_image_untag) | parser.set_defaults(func=command_image_untag) | ||||
parser.add_argument('tags', type=str, nargs='+', default=[]) | parser.add_argument('tags', type=str, nargs='+', default=[]) | ||||
@@ -78,6 +78,12 @@ def command_image_build(args): | |||||
zfs_tag(image.split('@')[0], args.tag) | 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): | def command_image_untag(args): | ||||
zfs_untag(args.tags) | zfs_untag(args.tags) | ||||
@@ -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] | 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)) | print('Running:', ' '.join(command)) | ||||
try: | try: | ||||
shutil.copyfile('/etc/resolv.conf', os.path.join(path, 'etc/resolv.conf')) | |||||
res = subprocess.run(command) | res = subprocess.run(command) | ||||
finally: | finally: | ||||
try: | try: | ||||
@@ -42,7 +43,6 @@ def command_jail_run(args): | |||||
break | break | ||||
zfs_run(['zfs', 'clone', base, name]) | zfs_run(['zfs', 'clone', base, name]) | ||||
try: | try: | ||||
shutil.copyfile('/etc/resolv.conf', os.path.join(zfs_mountpoint(name), 'etc/resolv.conf')) | |||||
jail_run(zfs_mountpoint(name), args.command) | 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]) | # subprocess.check_output(['jail', '-c', 'interface=lo1', 'ip4.addr=127.0.1.0', 'path=' + zfs_mountpoint(name), 'command', command]) | ||||
finally: | finally: | ||||
@@ -32,6 +32,23 @@ def zfs_snapshot_by_tag_or_sha256(s): | |||||
return (lst[0][3], lst[0][0]) | 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): | def zfs_clone(name, target_name): | ||||
zfs_run(['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) | zfs_tag(row[0], cur_tags, replace=True) | ||||
def zfs_init(): | |||||
def zfs_poolname(): | |||||
poolname = zfs_parse_output(['zfs', 'list', '-H', '/']) | poolname = zfs_parse_output(['zfs', 'list', '-H', '/']) | ||||
if len(poolname) == 0: | if len(poolname) == 0: | ||||
raise ValueError('Not a ZFS root') | raise ValueError('Not a ZFS root') | ||||
poolname = poolname[0][0].split('/')[0] | poolname = poolname[0][0].split('/')[0] | ||||
return poolname | |||||
def zfs_init(): | |||||
poolname = zfs_poolname() | |||||
print('poolname:', poolname) | print('poolname:', poolname) | ||||
for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']: | for path in ['/focker', '/focker/images', '/focker/volumes', '/focker/jails']: | ||||
if not os.path.exists(path): | if not os.path.exists(path): | ||||