From 48e4094636742e7d8e51815de35dd656d11be622 Mon Sep 17 00:00:00 2001 From: jpic Date: Sat, 25 Jan 2020 19:23:42 +0100 Subject: [PATCH] Change Tag by Commit, give it some powers --- pod.py | 4 ++-- podctl/visitors/__init__.py | 3 ++- podctl/visitors/commit.py | 48 +++++++++++++++++++++++++++++++++++++ podctl/visitors/tag.py | 6 ----- 4 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 podctl/visitors/commit.py delete mode 100644 podctl/visitors/tag.py diff --git a/pod.py b/pod.py index 92360a9..4716740 100644 --- a/pod.py +++ b/pod.py @@ -12,6 +12,6 @@ podctl = Container( User('app', 1000, '/app'), Copy(['setup.py', 'podctl'], '/app'), Pip('/app'), - Config(cmd='podctl'), - Tag('yourlabs/podctl'), + Config(cmd='podctl', author='jpic'), + Commit('docker.io/yourlabs/podctl'), ) diff --git a/podctl/visitors/__init__.py b/podctl/visitors/__init__.py index 45a7dc8..3642b86 100644 --- a/podctl/visitors/__init__.py +++ b/podctl/visitors/__init__.py @@ -1,8 +1,9 @@ from .base import Base # noqa +from .commit import Commit # noqa from .config import Config # noqa from .copy import Copy # noqa from .packages import Packages # noqa from .pip import Pip # noqa +from .push import Push # noqa from .run import Run # noqa -from .tag import Tag # noqa from .user import User # noqa diff --git a/podctl/visitors/commit.py b/podctl/visitors/commit.py new file mode 100644 index 0000000..54f86e9 --- /dev/null +++ b/podctl/visitors/commit.py @@ -0,0 +1,48 @@ +import os + +CI_VARS = ( + # gitlab + 'CI_COMMIT_SHORT_SHA', + 'CI_COMMIT_REF_NAME', + 'CI_COMMIT_TAG', + # CircleCI + 'CIRCLE_SHA1', + 'CIRCLE_TAG', + 'CIRCLE_BRANCH', +) + + +class Commit: + def __init__(self, repo, tags=None, format=None, push=None): + self.repo = repo + self.format = format or 'oci' + self.push = push + self.tags = tags or [] + if self.repo.startswith('docker.io/'): + self.format = 'docker' + + if not self.tags: + for name in CI_VARS: + value = os.getenv(name) + if value: + self.tags.append(value) + + self.tags = [t for t in self.tags if t is not None] + + def post_build(self, script): + creds = None + ''' + if 'DOCKER_USER' in os.environ: + creds = '--creds ' + os.getenv('DOCKER_USER') + if 'DOCKER_PASS' in os.environ: + creds += ':' + os.getenv('DOCKER_PASS') + ''' + + script.append(f''' + umounts + trap - 0 + buildah commit --format={self.format} $ctr {self.repo} + ''') + + if self.tags: + script.append(f'buildah tag {self.repo} ' + ' '.join(self.tags)) diff --git a/podctl/visitors/tag.py b/podctl/visitors/tag.py deleted file mode 100644 index a0a346b..0000000 --- a/podctl/visitors/tag.py +++ /dev/null @@ -1,6 +0,0 @@ -class Tag: - def __init__(self, tag): - self.tag = tag - - def post_build(self, script): - script.append(f'umounts && trap - 0 && buildah commit $ctr {self.tag}')