From 715893d6b2a82310603752a9985fd64fafc5604c Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Mon, 27 Apr 2020 19:09:02 +0200 Subject: [PATCH] Compose seems to work pretty much. --- focker/compose.py | 37 +++++++++++++++++++++++++++++++++++-- focker/jail.py | 4 +++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/focker/compose.py b/focker/compose.py index 9b16f73..5439907 100644 --- a/focker/compose.py +++ b/focker/compose.py @@ -2,10 +2,17 @@ import os import yaml from .zfs import AmbiguousValueError, \ zfs_find, \ - zfs_tag + zfs_tag, \ + zfs_untag, \ + zfs_mountpoint +from .jail import jail_fs_create, \ + jail_create, \ + jail_remove from .misc import random_sha256_hexdigest, \ find_prefix import subprocess +import jailconf +import os def build_volumes(spec): @@ -34,7 +41,33 @@ def build_images(spec, path): def build_jails(spec): - print('build_jails(): NotImplementedError') + #if os.path.exists('/etc/jail.conf'): + # conf = jailconf.load('/etc/jail.conf') + #else: + # conf = jailconf.JailConf() + for (jailname, jailspec) in spec.items(): + try: + name, _ = zfs_find(jailname, focker_type='jail') + jail_remove(zfs_mountpoint(name)) + except AmbiguousValueError: + raise + except ValueError: + pass + name = jail_fs_create(jailspec['image']) + zfs_untag([ jailname ], focker_type='jail') + zfs_tag(name, [ jailname ]) + path = zfs_mountpoint(name) + jail_create(path, + jailspec.get('exec.start', '/bin/sh /etc/rc'), + jailspec.get('env', {}), + [ [from_, on] \ + for (from_, on) in jailspec.get('mounts', {}).items() ], + hostname=jailname, + overrides={ + 'exec.stop': jailspec.get('exec.stop', '/bin/sh /etc/rc.shutdown'), + 'ip4.addr': jailspec.get('ip4.addr', '127.0.1.0'), + 'interface': jailspec.get('interface', 'lo1') + }) def command_compose_build(args): diff --git a/focker/jail.py b/focker/jail.py index 5eee2bc..eedc2f5 100644 --- a/focker/jail.py +++ b/focker/jail.py @@ -47,7 +47,7 @@ def quote(s): return s -def jail_create(path, command, env, mounts, hostname=None): +def jail_create(path, command, env, mounts, hostname=None, overrides={}): name = os.path.split(path)[-1] if os.path.exists('/etc/jail.conf'): conf = jailconf.load('/etc/jail.conf') @@ -83,6 +83,8 @@ def jail_create(path, command, env, mounts, hostname=None): blk['mount.devfs'] = True blk['exec.clean'] = True blk['host.hostname'] = hostname or name + for (k, v) in overrides.items(): + blk[k] = quote(v) conf.write('/etc/jail.conf') return name