From 47db32ad0c20b93f6f0e847ad391e84237c93faa Mon Sep 17 00:00:00 2001 From: jpic Date: Sun, 26 Jan 2020 20:02:29 +0100 Subject: [PATCH] GitLab cache --- .gitlab-ci.yml | 6 +++++- podctl/visitors/packages.py | 11 ++++++----- podctl/visitors/pip.py | 9 ++++++++- tests/test_build.py | 2 ++ tests/test_build_packages.sh | 4 ++-- tests/test_build_user.sh | 4 ++-- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2496b35..957eb69 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -11,7 +11,11 @@ test: build: stage: test image: yourlabs/podctl - script: pip install . && podctl build + script: pip install . && CACHE_DIR=$(pwd)/.cache podctl build + cache: + paths: + - .cache + key: cache pypi: stage: deploy diff --git a/podctl/visitors/packages.py b/podctl/visitors/packages.py index 01e5269..ec909c3 100644 --- a/podctl/visitors/packages.py +++ b/podctl/visitors/packages.py @@ -1,3 +1,4 @@ +import os import subprocess @@ -42,11 +43,11 @@ class Packages: raise Exception('Packages does not yet support this distro') def build(self, script): - cache = f'.cache/{self.mgr}' - script.mount( - '$(pwd)/' + cache, - f'/var/cache/{self.mgr}' - ) + if 'CACHE_DIR' in os.environ: + cache = os.path.join(os.getenv('CACHE_DIR'), self.mgr) + else: + cache = os.path.join(os.getenv('HOME'), '.cache', self.mgr) + script.mount(cache, f'/var/cache/{self.mgr}') if self.mgr == 'apk': # special step to enable apk cache diff --git a/podctl/visitors/pip.py b/podctl/visitors/pip.py index 9e2457b..3dbba31 100644 --- a/podctl/visitors/pip.py +++ b/podctl/visitors/pip.py @@ -1,3 +1,6 @@ +import os + + class Pip: def __init__(self, *pip_packages, pip=None): self.pip_packages = pip_packages @@ -16,7 +19,11 @@ class Pip: _pip=pip2 fi ''') - script.mount('.cache/pip', '/root/.cache/pip') + if 'CACHE_DIR' in os.environ: + cache = os.path.join(os.getenv('CACHE_DIR'), 'pip') + else: + cache = os.path.join(os.getenv('HOME'), '.cache', 'pip') + script.mount(cache, '/root/.cache/pip') script.run('sudo $_pip install --upgrade pip') source = [p for p in self.pip_packages if p.startswith('/')] if source: diff --git a/tests/test_build.py b/tests/test_build.py index 442d644..d04cf20 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -16,6 +16,8 @@ from unittest import mock from podctl.visitors import packages packages.subprocess.check_call = mock.Mock() +os.environ['CACHE_DIR'] = '/test' + def script_test(name, *visitors): result = str(Container(*visitors).script('build')) diff --git a/tests/test_build_packages.sh b/tests/test_build_packages.sh index 3e1bf17..5d3fa0b 100644 --- a/tests/test_build_packages.sh +++ b/tests/test_build_packages.sh @@ -16,8 +16,8 @@ mnt=$(buildah mount $ctr) echo "Packages.pre_build" echo "Packages.build" buildah run --user root $ctr -- mkdir -p /var/cache/apk -mkdir -p $(pwd)/.cache/apk -mount -o bind $(pwd)/.cache/apk $mnt/var/cache/apk +mkdir -p /test/apk +mount -o bind /test/apk $mnt/var/cache/apk mounts=("$mnt/var/cache/apk" "${mounts[@]}") buildah run $ctr -- ln -s /var/cache/apk /etc/apk/cache old="$(find .cache/apk/ -name APKINDEX.* -mtime +3)" diff --git a/tests/test_build_user.sh b/tests/test_build_user.sh index 9bb2278..ce008f4 100644 --- a/tests/test_build_user.sh +++ b/tests/test_build_user.sh @@ -19,8 +19,8 @@ echo "Packages.pre_build" echo "User.pre_build" echo "Packages.build" buildah run --user root $ctr -- mkdir -p /var/cache/apk -mkdir -p $(pwd)/.cache/apk -mount -o bind $(pwd)/.cache/apk $mnt/var/cache/apk +mkdir -p /test/apk +mount -o bind /test/apk $mnt/var/cache/apk mounts=("$mnt/var/cache/apk" "${mounts[@]}") buildah run $ctr -- ln -s /var/cache/apk /etc/apk/cache old="$(find .cache/apk/ -name APKINDEX.* -mtime +3)"