From 69a9be66f1855eda6a75a4cf17a973eebfa9f05b Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Tue, 21 Apr 2020 22:38:57 +0200 Subject: [PATCH] Sketched step execution implementation --- Fockerfile | 10 +--------- jail.py | 4 ++++ steps.py | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 jail.py diff --git a/Fockerfile b/Fockerfile index 31fee49..7ab82e7 100644 --- a/Fockerfile +++ b/Fockerfile @@ -1,14 +1,6 @@ -from: freebsd-12.1 +base: freebsd-12.1 steps: - run: | pkg install python3 && \ pkg install py37-pip - -jail: - name: xxx - mount.devfs: true - ip4.address: 127.0.0.2 - interface: lo1 - exec.start: /bin/sh /etc/rc - exec.stop: /bin/sh /etc/rc.shutdown diff --git a/jail.py b/jail.py new file mode 100644 index 0000000..e079650 --- /dev/null +++ b/jail.py @@ -0,0 +1,4 @@ +import subprocess + +def jail_run(path, command): + subprocess.check_output(['jail', '-c', 'path=' + path, 'command', '/bin/sh', '-c', command) diff --git a/steps.py b/steps.py index b046e3a..196e762 100644 --- a/steps.py +++ b/steps.py @@ -1,5 +1,7 @@ import hashlib import json +from jail import jail_run +import shutil def filehash(fname): @@ -15,25 +17,32 @@ def filehash(fname): class RunStep(object): - def __init__(self, base, spec): - self.base = base + def __init__(self, spec): + if not isinstance(spec, list) and \ + not isinstance(spec, str): + raise ValueError('Run spec must be a list or a string') self.spec = spec - def hash(self): + def hash(self, base): res = hashlib.sha256( - json.dumps(( self.base, self.spec )) + json.dumps(( base, self.spec )) .encode('utf-8')).hexdigest() return res + def execute(self, path): + spec = self.spec + if isinstance(spec, list) + spec = ' && ' .join(self.spec) + jail_run(path, spec) + class CopyStep(object): - def __init__(self, base, spec): + def __init__(self, spec): if not isinstance(spec, list): raise ValueError('CopyStep spec should be a list') - self.base = base self.spec = spec - def hash(self): + def hash(self, base): if len(self.spec) == 0: fh = [] elif isinstance(self.spec[0], list): @@ -41,6 +50,23 @@ class CopyStep(object): else: fh = [ filehash(self.spec[0]) ] res = hashlib.sha256( - json.dumps(( self.base, fh, self.spec )) + json.dumps(( base, fh, self.spec )) .encode('utf-8')).hexdigest() return res + + def execute(self, path): + lst = [ self.spec ] \ + if not isinstance(self.spec[0], list) \ + else self.spec + for a in lst: + shutil.copyfile(a[0], os.path.join(path, a[1])) + + +def create_step(spec): + if not isinstance(spec, dict): + raise ValueError('Step specification must be a dictionary') + if 'copy' in spec: + return CopyStep(spec['copy']) + elif 'run' in spec: + return RunStep(spec['run']) + raise ValueError('Unrecognized step spec: ' + json.dumps(spec))