...
 
Commits (6)
class Run:
def __init__(self, cmd):
def __init__(self, cmd, root=False):
self.cmd = cmd
self.root = root
async def __call__(self, target):
self.proc = await target.exec(self.cmd)
if self.root:
self.proc = await target.rexec(self.cmd)
else:
self.proc = await target.exec(self.cmd)
def __str__(self):
return f'Run({self.cmd})'
......@@ -24,7 +24,7 @@ class User:
return f'User({self.username}, {self.home}, {self.uid})'
async def __call__(self, target):
result = await target.rexec('id', self.uid)
result = await target.rexec('id', self.uid, raises=False)
if result.rc == 0:
old = re.match('.*\(([^)]*)\).*', result.out).group(1)
await target.rexec(
......@@ -40,3 +40,5 @@ class User:
'-u', self.uid,
self.username
)
await target.mkdir(self.home)
await target.rexec('chown', self.uid, self.home)
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})'
......@@ -68,7 +68,10 @@ class Target:
traceback.print_exception(type(e), e, sys.exc_info()[2])
return True
else:
self.output.success(action)
if getattr(action, 'skipped', False):
self.output.skip(action)
else:
self.output.success(action)
result.status = 'success'
finally:
self.caller.results.append(result)
......@@ -166,8 +169,13 @@ class Target:
async def read(self, path):
return (await self.exec('cat', self.path(path))).out
async def write(self, path, content):
return await self.exec('echo ' + content + ' > ' + self.path(path))
async def write(self, path, content, **kwargs):
return await self.exec(
f'cat > {self.path(path)} <<EOF\n'
+ content
+ '\nEOF',
**kwargs
)
async def rm(self, path):
return await self.exec('rm', self.path(path))
......
......@@ -16,10 +16,7 @@ class Buildah(Target):
"""Build container image with buildah"""
isguest = True
def __init__(self,
*actions,
base=None, commit=None,
cmd=None):
def __init__(self, *actions, base=None, commit=None):
self.base = base or 'alpine'
self.image = Image(commit) if commit else None
......@@ -27,10 +24,6 @@ class Buildah(Target):
self.root = None
self.mounts = dict()
self.config = dict(
cmd=cmd or 'sh',
)
# Always consider localhost as parent for now
self.parent = Target()
......@@ -169,9 +162,6 @@ class Buildah(Target):
return await self.parent.exec(*_args, **kwargs)
async def commit(self):
for key, value in self.config.items():
await self.parent.exec(f'buildah config --{key} "{value}" {self.ctr}')
await self.parent.exec(
f'buildah commit {self.ctr} {self.image.repository}:final'
)
......@@ -206,3 +196,44 @@ class Buildah(Target):
async def copy(self, *args):
return await self.parent.copy(*args[:-1], self.path(args[-1]))
async def write(self, path, content):
return await self.write(path, content)
async def write(self, path, content, **kwargs):
return await self.exec(
f'cat > {path} <<EOF\n'
+ content
+ '\nEOF',
**kwargs
)
class Config:
def __init__(self, **config):
self.config = config
async def __call__(self, target):
for key, value in self.config.items():
await target.parent.exec(
f'buildah config --{key} "{value}" {target.ctr}'
)
def __str__(self):
return f'Buildah.Config({self.config})'
class Env:
def __init__(self, **env):
self.env = env
async def __call__(self, target):
for key, value in self.env.items():
await target.parent.exec(
'buildah',
'config',
'--env',
f'{key}={value}',
target.ctr,
)
def __str__(self):
return f'Buildah.Env({self.env})'