From b4a8c24c1f0dc1eb27087527a4222f1f5fa36912 Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Mon, 27 Apr 2020 17:57:48 +0200 Subject: [PATCH] Started implementing focker compose --- compose.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++ focker-compose.yml | 3 +++ 2 files changed, 59 insertions(+) create mode 100644 compose.py diff --git a/compose.py b/compose.py new file mode 100644 index 0000000..9b16f73 --- /dev/null +++ b/compose.py @@ -0,0 +1,56 @@ +import os +import yaml +from .zfs import AmbiguousValueError, \ + zfs_find, \ + zfs_tag +from .misc import random_sha256_hexdigest, \ + find_prefix +import subprocess + + +def build_volumes(spec): + for tag in spec.keys(): + try: + name, _ = zfs_find(tag, focker_type='volume') + continue + except AmbiguousValueError: + raise + except ValueError: + pass + sha256 = random_sha256_hexdigest() + name = find_prefix(poolname + '/focker/volumes/', sha256) + subprocess.check_output(['zfs', 'create', name]) + zfs_untag([ tag ], focker_type='volume') + zfs_tag(name, [ tag ]) + + +def build_images(spec, path): + # print('build_images(): NotImplementedError') + for (tag, focker_dir) in spec.items(): + res = subprocess.run(['focker', 'image', 'build', + os.path.join(path, focker_dir), '-t', tag]) + if res.returncode != 0: + raise RuntimeError('Image build failed: ' + str(res.returncode)) + + +def build_jails(spec): + print('build_jails(): NotImplementedError') + + +def command_compose_build(args): + if not os.path.exists(args.filename): + raise ValueError('File not found: ' + args.filename) + path, _ = os.path.split(args.filename) + with open(args.filename, 'r') as f: + spec = yaml.safe_load(f) + if 'volumes' in spec: + build_volumes(spec['volumes']) + if 'images' in spec: + build_images(spec['images'], path) + if 'jails' in spec: + build_jails(spec['jails']) + + + +def command_compose_run(args): + raise NotImplementedError diff --git a/focker-compose.yml b/focker-compose.yml index d76429c..2209327 100644 --- a/focker-compose.yml +++ b/focker-compose.yml @@ -4,6 +4,9 @@ # focker compose wordpress.yml restore # +images: + wordpress-5: . + jails: wordpress: image: wordpress-5