From 75747a8ee952fe50947e14048b393f8ba7616838 Mon Sep 17 00:00:00 2001 From: jpic Date: Sun, 2 Aug 2020 06:51:07 +0200 Subject: [PATCH] Container: support not having a build job and new methods --- shlax/container.py | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/shlax/container.py b/shlax/container.py index fcc074b..6091704 100644 --- a/shlax/container.py +++ b/shlax/container.py @@ -1,12 +1,17 @@ +import copy import os from .image import Image class Container: - def __init__(self, build=None, image=None): + def __init__(self, build=None, image=None, env=None, volumes=None): self.build = build - self.image = self.build.image + self.image = image or self.build.image + if isinstance(self.image, str): + self.image = Image(self.image) + self.volumes = volumes or {} + self.env = env or {} prefix = os.getcwd().split('/')[-1] repo = self.image.repository.replace('/', '-') if prefix == repo: @@ -14,19 +19,44 @@ class Container: else: self.name = '-'.join([prefix, repo]) - async def start(self, target): - """Start the container""" - await target.rexec( + async def up(self, target, *args): + """Start the container foreground""" + cmd = [ 'podman', 'run', + ] + list(args) + + for src, dest in self.volumes.items(): + cmd += ['--volume', ':'.join([src, dest])] + + for src, dest in self.env.items(): + cmd += ['--env', '='.join([src, str(dest)])] + + cmd += [ '--name', self.name, str(self.image), - ) + ] + await target.exec(*cmd) + + async def start(self, target): + """Start the container background""" + await self.up(target, '-d') async def stop(self, target): """Start the container""" - await target.rexec('podman', 'stop', self.name) + await target.exec('podman', 'stop', self.name) + + async def down(self, target): + """Start the container""" + await target.exec('podman', 'rm', '-f', self.name, raises=False) + + async def apply(self, target): + """Start the container""" + if self.build: + await target(self.build) + await target(self.down) + await target(self.start) def __str__(self): - return f'Container(name={self.name}, image={self.image})' + return f'Container(name={self.name}, image={self.image}, volumes={self.volumes})'