Container: support not having a build job and new methods

This commit is contained in:
jpic 2020-08-02 06:51:07 +02:00
parent 9fab1b2411
commit 75747a8ee9

View File

@ -1,12 +1,17 @@
import copy
import os import os
from .image import Image from .image import Image
class Container: class Container:
def __init__(self, build=None, image=None): def __init__(self, build=None, image=None, env=None, volumes=None):
self.build = build 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] prefix = os.getcwd().split('/')[-1]
repo = self.image.repository.replace('/', '-') repo = self.image.repository.replace('/', '-')
if prefix == repo: if prefix == repo:
@ -14,19 +19,44 @@ class Container:
else: else:
self.name = '-'.join([prefix, repo]) self.name = '-'.join([prefix, repo])
async def start(self, target): async def up(self, target, *args):
"""Start the container""" """Start the container foreground"""
await target.rexec( cmd = [
'podman', 'podman',
'run', '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', '--name',
self.name, self.name,
str(self.image), 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): async def stop(self, target):
"""Start the container""" """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): def __str__(self):
return f'Container(name={self.name}, image={self.image})' return f'Container(name={self.name}, image={self.image}, volumes={self.volumes})'