From 174ecdfb6dcb062b939a9e3392c0cbe7d3200e34 Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Thu, 23 Apr 2020 22:00:00 +0200 Subject: [PATCH] Add reusing of existing steps. --- Fockerfile | 4 +++- image.py | 5 +++++ zfs.py | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Fockerfile b/Fockerfile index 15371fa..e6cdf13 100644 --- a/Fockerfile +++ b/Fockerfile @@ -1,6 +1,8 @@ -base: freebsd-12.1 +base: freebsd-latest steps: - run: | pkg -y install -y python3 && \ pkg -y install -y py37-pip + - run: | + pkg install -y py37-yaml diff --git a/image.py b/image.py index 87c3d5b..f21e61d 100644 --- a/image.py +++ b/image.py @@ -38,6 +38,11 @@ def build(spec): for st in steps: st = create_step(st) st_sha256 = st.hash(base_sha256) + if zfs_exists_snapshot_sha256(st_sha256): + base = zfs_snapshot_by_sha256(st_sha256) + base_sha256 = st_sha256 + print('Reusing:', base) + continue for pre in range(7, 64): name = root + '/' + st_sha256[:pre] if not zfs_exists(name): diff --git a/zfs.py b/zfs.py index adc6d56..3d250e6 100644 --- a/zfs.py +++ b/zfs.py @@ -54,6 +54,24 @@ def zfs_mountpoint(name): return lst[0][0] +def zfs_exists_snapshot_sha256(sha256): + lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256', '-t', 'snap']) + lst = list(filter(lambda a: a[0] == sha256, lst)) + if len(lst) == 0: + return False + return True + + +def zfs_snapshot_by_sha256(sha256): + lst = zfs_parse_output(['zfs', 'list', '-o', 'focker:sha256,name', '-t', 'snap', '-H']) + lst = list(filter(lambda a: a[0] == sha256, lst)) + if len(lst) == 0: + raise ValueError('Snapshot with given sha256 does not exist: ' + sha256) + if len(lst) > 1: + raise ValueError('Ambiguous snapshot sha256: ' + sha256) + return lst[0][1] + + def zfs_init(): poolname = zfs_parse_output(['zfs', 'list', '-H', '/']) if len(poolname) == 0: