From 6e8b2ec2a79a16539ca39012fadf8edd7db8825d Mon Sep 17 00:00:00 2001 From: jpic Date: Thu, 13 Feb 2020 04:53:09 +0100 Subject: [PATCH] IMprove --- examples/simple/pod.py | 5 ++--- podctl/console_script.py | 24 ++++++++++++++++-------- podctl/container.py | 20 +++++++++++--------- podctl/script.py | 18 ++++++++++++++---- podctl/visitable.py | 11 ++++------- podctl/visitors/commit.py | 16 +++++++++------- 6 files changed, 56 insertions(+), 38 deletions(-) diff --git a/examples/simple/pod.py b/examples/simple/pod.py index 9f87f0a..a3ad737 100644 --- a/examples/simple/pod.py +++ b/examples/simple/pod.py @@ -21,9 +21,8 @@ async def test_pod_story2(pod): async def test_pod_story(pod): await pod.script('down')() await pod.script('build')('ex') - return - assert await pod.script('up')() == 0 - assert await pod.script('down')() == 0 + await pod.script('up')() + await pod.script('down')() async def aoeutest_podctl(host): diff --git a/podctl/console_script.py b/podctl/console_script.py index 4a549d0..15ae830 100644 --- a/podctl/console_script.py +++ b/podctl/console_script.py @@ -31,6 +31,12 @@ async def test(*args, **kwargs): continue podfile = Podfile.factory(candidate) + output.print( + '\n\x1b[1;38;5;160;48;5;118m BUILD START \x1b[0m' + + ' ' + podfile.path + '\n' + ) + podfile.pod.script('build')() + for name, test in podfile.tests.items(): name = '::'.join([podfile.path, name]) output.print( @@ -65,14 +71,16 @@ async def test(*args, **kwargs): '\n\x1b[1;38;5;200;48;5;44m TEST TOTAL: \x1b[0m' + str(len(report)) ) - output.print( - '\n\x1b[1;38;5;15;48;5;196m TEST FAIL: \x1b[0m' - + str(len(failures)) - ) - output.print( - '\n\x1b[1;38;5;200;48;5;44m TEST SUCCESS: \x1b[0m' - + str(len(success)) - ) + if success: + output.print( + '\n\x1b[1;38;5;200;48;5;44m TEST SUCCESS: \x1b[0m' + + str(len(success)) + ) + if failures: + output.print( + '\n\x1b[1;38;5;15;48;5;196m TEST FAIL: \x1b[0m' + + str(len(failures)) + ) if failures: console_script.exit_code = 1 diff --git a/podctl/container.py b/podctl/container.py index dd4a938..9513f0e 100644 --- a/podctl/container.py +++ b/podctl/container.py @@ -1,5 +1,6 @@ from .build import Build from .exceptions import WrongResult +from .proc import output from .visitable import Visitable @@ -12,6 +13,10 @@ class Container(Visitable): def container_name(self): return '-'.join([self.pod.name, self.name]) + @property + def image_name(self): + return self.pod.visitor(self.name).variable('repotags')[0] + async def down(self, script): try: await script.exec('podman', 'inspect', self.container_name) @@ -37,17 +42,14 @@ class Container(Visitable): try: await script.exec('podman', 'inspect', self.container_name) except WrongResult as ee: - tag = ':'.join(( - self.variable('repo'), - self.variable('tags')[0], - )) - print(f'{self.name} | Container creating') + output('Container creating', self.name) + breakpoint() await script.exec( 'podman', 'run', '-d', '--name', self.container_name, - tag, + self.image_name, ) - print(f'{self.name} | Container created') + output('Container created', self.name) else: - print(f'{self.name} | Container starting') + output('Container starting', self.name) await script.exec('podman', 'start', self.container_name) - print(f'{self.name} | Container started') + output('Container started', self.name) diff --git a/podctl/script.py b/podctl/script.py index 9a046c3..b1297a3 100644 --- a/podctl/script.py +++ b/podctl/script.py @@ -47,6 +47,14 @@ class Script: visitors = visitable.visitors + def val(k, v): + if isinstance(v, list) and len(v) > 1: + return '[' + str(v[-1]) + '...]' + + if k == 'scripts': + return dict() + return v + results = [] async def clean(): for visitor in visitable.visitors: @@ -58,10 +66,11 @@ class Script: ''.join([ '.'.join([type(visitor).__name__, method]), '(', - ', '.join(f'{k}={v}' for k, v in visitor.__dict__.items()), + ', '.join(f'{k}={val(k, v)}' for k, v in visitor.__dict__.items()), ')' ]), - getattr(visitable, 'name', None) + getattr(visitor, 'name', + getattr(visitable, 'name', None)) ) if result: await result @@ -78,10 +87,11 @@ class Script: ''.join([ '.'.join([type(visitor).__name__, method]), '(', - ', '.join(f'{k}={v}' for k, v in visitor.__dict__.items()), + ', '.join(f'{k}={val(k, v)}' for k, v in visitor.__dict__.items()), ')' ]), - getattr(visitable, 'name', None) + getattr(visitor, 'name', + getattr(visitable, 'name', None)) ) result = getattr(visitor, method)(self) diff --git a/podctl/visitable.py b/podctl/visitable.py index e66f886..a2c6964 100644 --- a/podctl/visitable.py +++ b/podctl/visitable.py @@ -10,15 +10,12 @@ class Visitable: self.scripts = deepcopy(self.default_scripts) self.scripts.update(scripts) - ''' - def script(self, name): - script = copy(self.scripts[name]) - return script -''' - def visitor(self, name): for visitor in self.visitors: - if name.lower() == type(visitor).__name__.lower(): + if name.lower() in ( + type(visitor).__name__.lower(), + getattr(visitor, 'name', '') + ): return visitor def variable(self, name): diff --git a/podctl/visitors/commit.py b/podctl/visitors/commit.py index e337caa..208b69f 100644 --- a/podctl/visitors/commit.py +++ b/podctl/visitors/commit.py @@ -48,6 +48,12 @@ class Commit: if not self.tags: self.tags = ['latest'] + # default tag for master too + if 'master' in self.tags: + self.tags.append('latest') + + self.repotags = [f'{self.registry}/{self.repo}:{tag}' for tag in self.tags] + async def post_build(self, script): self.sha = (await script.exec( 'buildah', @@ -56,13 +62,9 @@ class Commit: script.ctr, )).out - if 'master' in self.tags: - self.tags.append('latest') - if self.tags: - for tag in self.tags: - await script.exec('buildah', 'tag', self.sha, self.repo, - f'{self.repo}:{tag}') + for tag in self.repotags: + await script.exec('buildah', 'tag', self.sha, self.repo, tag) if self.push: user = os.getenv('DOCKER_USER') @@ -79,7 +81,7 @@ class Commit: ) for tag in self.tags: - await script.exec('podman', 'push', f'{self.repo}:{tag}') + await script.exec('podman', 'push', tag) await script.umount() def __repr__(self):