Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b48e997a4b | ||
|
|
3a45cdef4a | ||
|
|
bd3bf37643 | ||
|
|
e196d321c5 | ||
|
|
422da7043f | ||
|
|
1854d7116c | ||
|
|
c8cdbf38bf | ||
|
|
5f1afd89ee | ||
|
|
8ff4239a65 | ||
|
|
7ea6e10282 | ||
|
|
e97e900fc6 | ||
|
|
7e9ec37103 | ||
|
|
d9e74c2538 | ||
|
|
9124e93dca | ||
|
|
cd9ccec19c | ||
|
|
73af20ded7 | ||
|
|
f127f27ecd | ||
|
|
222eda3746 | ||
|
|
c38ad7e089 | ||
|
|
bc7cb9c355 | ||
|
|
96c533fc1a | ||
|
|
8c75cac48a | ||
|
|
cc20c7b205 | ||
|
|
50b266013b | ||
|
|
75747a8ee9 | ||
|
|
9fab1b2411 | ||
|
|
77f0d1f67b | ||
|
|
189c4c3856 | ||
|
|
59d652710a | ||
|
|
ea2f212787 | ||
|
|
f78be1cb56 | ||
|
|
0255655615 | ||
|
|
f1df4dcc69 | ||
|
|
4317cf9cd4 | ||
|
|
150736f4f4 | ||
|
|
d2784cc87b | ||
|
|
62f1f5d162 | ||
|
|
cfe75d2873 | ||
|
|
5f451a786f | ||
|
|
3b6a4f3fc4 |
+2
-2
@@ -5,14 +5,14 @@ build:
|
|||||||
image: yourlabs/buildah
|
image: yourlabs/buildah
|
||||||
script:
|
script:
|
||||||
- pip3 install -U --user -e .[cli]
|
- pip3 install -U --user -e .[cli]
|
||||||
- CACHE_DIR=$(pwd)/.cache python3 ./shlaxfile.py build
|
- CACHE_DIR=$(pwd)/.cache python3 ./shlaxfile.py build push
|
||||||
stage: build
|
stage: build
|
||||||
|
|
||||||
build-itself:
|
build-itself:
|
||||||
cache:
|
cache:
|
||||||
key: cache
|
key: cache
|
||||||
paths: [.cache]
|
paths: [.cache]
|
||||||
image: shlax:$CI_COMMIT_SHORT_SHA
|
image: quay.io/yourlabs/shlax:$CI_COMMIT_SHORT_SHA
|
||||||
script: python3 ./shlaxfile.py build
|
script: python3 ./shlaxfile.py build
|
||||||
stage: test
|
stage: test
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,30 @@ Shlax is a Python framework for system automation, initially with the purpose
|
|||||||
of replacing docker, docker-compose and ansible with a single tool with the
|
of replacing docker, docker-compose and ansible with a single tool with the
|
||||||
purpose of code-reuse made possible by target abstraction.
|
purpose of code-reuse made possible by target abstraction.
|
||||||
|
|
||||||
|
## Development status: Design state
|
||||||
|
|
||||||
|
I got the thing to work with an ugly PoC that I basically brute-forced, I'm
|
||||||
|
currently rewriting the codebase with a proper design.
|
||||||
|
|
||||||
|
The stories are in development in this order:
|
||||||
|
|
||||||
|
- replacing docker build, that's in the state of polishing
|
||||||
|
- replacing docker-compose, not in use but the PoC works so far
|
||||||
|
- replacing ansible, also working in working PoC state, the shlax command line
|
||||||
|
demonstrates
|
||||||
|
|
||||||
|
This project is supposed to unblock me from adding the CI feature to the
|
||||||
|
Sentry/GitLab/Portainer implementation I'm doing in pure python on top of
|
||||||
|
Django, CRUDLFA+ and Ryzom (isomorphic components in Python to replace
|
||||||
|
templates). So, as you can see, I'm really deep in it with a strong
|
||||||
|
determination.
|
||||||
|
|
||||||
|
Shlax builds its container itself, so check the shlaxfile.py of this repository
|
||||||
|
to see what it currently looks like, and check the build job of the CI pipeline
|
||||||
|
to see the output.
|
||||||
|
|
||||||
|
# Design
|
||||||
|
|
||||||
The pattern resolves around two moving parts: Actions and Targets.
|
The pattern resolves around two moving parts: Actions and Targets.
|
||||||
|
|
||||||
## Action
|
## Action
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ setup(
|
|||||||
setup_requires='setupmeta',
|
setup_requires='setupmeta',
|
||||||
extras_require=dict(
|
extras_require=dict(
|
||||||
cli=[
|
cli=[
|
||||||
'cli2>=2.2.2',
|
'cli2>=2.3.0',
|
||||||
],
|
],
|
||||||
test=[
|
test=[
|
||||||
'pytest',
|
'pytest',
|
||||||
|
|||||||
+42
-31
@@ -37,11 +37,13 @@ class Packages:
|
|||||||
update='pacman -Sy',
|
update='pacman -Sy',
|
||||||
upgrade='pacman -Su --noconfirm',
|
upgrade='pacman -Su --noconfirm',
|
||||||
install='pacman -S --noconfirm',
|
install='pacman -S --noconfirm',
|
||||||
|
lastupdate='stat -c %Y /var/lib/pacman/sync/core.db',
|
||||||
),
|
),
|
||||||
dnf=dict(
|
dnf=dict(
|
||||||
update='dnf makecache --assumeyes',
|
update='dnf makecache --assumeyes',
|
||||||
upgrade='dnf upgrade --best --assumeyes --skip-broken', # noqa
|
upgrade='dnf upgrade --best --assumeyes --skip-broken', # noqa
|
||||||
install='dnf install --setopt=install_weak_deps=False --best --assumeyes', # noqa
|
install='dnf install --setopt=install_weak_deps=False --best --assumeyes', # noqa
|
||||||
|
lastupdate='stat -c %Y /var/cache/dnf/* | head -n1',
|
||||||
),
|
),
|
||||||
yum=dict(
|
yum=dict(
|
||||||
update='yum update',
|
update='yum update',
|
||||||
@@ -52,54 +54,52 @@ class Packages:
|
|||||||
|
|
||||||
installed = []
|
installed = []
|
||||||
|
|
||||||
def __init__(self, *packages, upgrade=True):
|
def __init__(self, *packages, upgrade=False):
|
||||||
self.packages = []
|
self.packages = []
|
||||||
self.upgrade = upgrade
|
self.upgrade = upgrade
|
||||||
for package in packages:
|
for package in packages:
|
||||||
line = dedent(package).strip().replace('\n', ' ')
|
line = dedent(package).strip().replace('\n', ' ')
|
||||||
self.packages += line.split(' ')
|
self.packages += line.split(' ')
|
||||||
|
|
||||||
@property
|
async def cache_setup(self, target):
|
||||||
def cache_root(self):
|
|
||||||
if 'CACHE_DIR' in os.environ:
|
if 'CACHE_DIR' in os.environ:
|
||||||
return os.path.join(os.getenv('CACHE_DIR'))
|
self.cache_root = os.path.join(os.getenv('CACHE_DIR'))
|
||||||
else:
|
else:
|
||||||
return os.path.join(os.getenv('HOME'), '.cache')
|
self.cache_root = os.path.join(await target.parent.getenv('HOME'), '.cache')
|
||||||
|
|
||||||
|
# run pkgmgr_setup functions ie. apk_setup
|
||||||
|
await getattr(self, self.mgr + '_setup')(target)
|
||||||
|
|
||||||
async def update(self, target):
|
async def update(self, target):
|
||||||
# run pkgmgr_setup functions ie. apk_setup
|
# lastupdate = await target.exec(self.cmds['lastupdate'], raises=False)
|
||||||
cachedir = await getattr(self, self.mgr + '_setup')(target)
|
# lastupdate = int(lastupdate.out) if lastupdate.rc == 0 else None
|
||||||
|
|
||||||
lastupdate = None
|
lastupdate = None
|
||||||
if os.path.exists(cachedir + '/lastupdate'):
|
|
||||||
with open(cachedir + '/lastupdate', 'r') as f:
|
|
||||||
try:
|
|
||||||
lastupdate = int(f.read().strip())
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if not os.path.exists(cachedir):
|
|
||||||
os.makedirs(cachedir)
|
|
||||||
|
|
||||||
now = int(datetime.now().strftime('%s'))
|
now = int(datetime.now().strftime('%s'))
|
||||||
# cache for a week
|
if not lastupdate or now - lastupdate > 604800:
|
||||||
|
await target.rexec(self.cmds['update'])
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
# disabling with the above return call until needed again
|
||||||
|
# might have to rewrite this to not have our own lockfile
|
||||||
|
# or find a better place on the filesystem
|
||||||
|
# also make sure the lockfile is actually needed when running on
|
||||||
|
# targets that don't have isguest=True
|
||||||
if not lastupdate or now - lastupdate > 604800:
|
if not lastupdate or now - lastupdate > 604800:
|
||||||
# crude lockfile implementation, should work against *most*
|
# crude lockfile implementation, should work against *most*
|
||||||
# race-conditions ...
|
# race-conditions ...
|
||||||
lockfile = cachedir + '/update.lock'
|
lockfile = cachedir + '/update.lock'
|
||||||
if not os.path.exists(lockfile):
|
if not await target.parent.exists(lockfile):
|
||||||
with open(lockfile, 'w+') as f:
|
await target.parent.write(lockfile, str(os.getpid()))
|
||||||
f.write(str(os.getpid()))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await target.rexec(self.cmds['update'])
|
await target.rexec(self.cmds['update'])
|
||||||
finally:
|
finally:
|
||||||
os.unlink(lockfile)
|
await target.parent.rm(lockfile)
|
||||||
|
|
||||||
with open(cachedir + '/lastupdate', 'w+') as f:
|
await target.parent.write(cachedir + '/lastupdate', str(now))
|
||||||
f.write(str(now))
|
|
||||||
else:
|
else:
|
||||||
while os.path.exists(lockfile):
|
while await target.parent.exists(lockfile):
|
||||||
print(f'{self.target} | Waiting for {lockfile} ...')
|
print(f'{self.target} | Waiting for {lockfile} ...')
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
@@ -116,7 +116,13 @@ class Packages:
|
|||||||
raise Exception('Packages does not yet support this distro')
|
raise Exception('Packages does not yet support this distro')
|
||||||
|
|
||||||
self.cmds = self.mgrs[self.mgr]
|
self.cmds = self.mgrs[self.mgr]
|
||||||
|
|
||||||
|
if target.isguest:
|
||||||
|
# we're going to mount
|
||||||
|
await self.cache_setup(target)
|
||||||
|
|
||||||
await self.update(target)
|
await self.update(target)
|
||||||
|
|
||||||
if self.upgrade:
|
if self.upgrade:
|
||||||
await target.rexec(self.cmds['upgrade'])
|
await target.rexec(self.cmds['upgrade'])
|
||||||
|
|
||||||
@@ -147,19 +153,24 @@ class Packages:
|
|||||||
return cachedir
|
return cachedir
|
||||||
|
|
||||||
async def apt_setup(self, target):
|
async def apt_setup(self, target):
|
||||||
codename = (await self.rexec(
|
codename = (await target.rexec(
|
||||||
f'source {self.mnt}/etc/os-release; echo $VERSION_CODENAME'
|
f'source /etc/os-release; echo $VERSION_CODENAME'
|
||||||
)).out
|
)).out
|
||||||
cachedir = os.path.join(self.cache_root, self.mgr, codename)
|
cachedir = os.path.join(self.cache_root, self.mgr, codename)
|
||||||
await self.rexec('rm /etc/apt/apt.conf.d/docker-clean')
|
await self.rexec('rm /etc/apt/apt.conf.d/docker-clean')
|
||||||
cache_archives = os.path.join(cachedir, 'archives')
|
cache_archives = os.path.join(cachedir, 'archives')
|
||||||
await self.mount(cache_archives, f'/var/cache/apt/archives')
|
await target.mount(cache_archives, f'/var/cache/apt/archives')
|
||||||
cache_lists = os.path.join(cachedir, 'lists')
|
cache_lists = os.path.join(cachedir, 'lists')
|
||||||
await self.mount(cache_lists, f'/var/lib/apt/lists')
|
await target.mount(cache_lists, f'/var/lib/apt/lists')
|
||||||
return cachedir
|
return cachedir
|
||||||
|
|
||||||
async def pacman_setup(self, target):
|
async def pacman_setup(self, target):
|
||||||
return self.cache_root + '/pacman'
|
cachedir = os.path.join(self.cache_root, self.mgr)
|
||||||
|
await target.mkdir(cachedir + '/cache', cachedir + '/sync')
|
||||||
|
await target.mount(cachedir + '/sync', '/var/lib/pacman/sync')
|
||||||
|
await target.mount(cachedir + '/cache', '/var/cache/pacman')
|
||||||
|
if await target.host.exists('/etc/pacman.d/mirrorlist'):
|
||||||
|
await target.copy('/etc/pacman.d/mirrorlist', '/etc/pacman.d/mirrorlist')
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'Packages({self.packages}, upgrade={self.upgrade})'
|
return f'Packages({self.packages}, upgrade={self.upgrade})'
|
||||||
|
|||||||
@@ -9,3 +9,6 @@ class Parallel:
|
|||||||
return await asyncio.gather(*[
|
return await asyncio.gather(*[
|
||||||
target(action) for action in self.actions
|
target(action) for action in self.actions
|
||||||
])
|
])
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return 'Parallel executor'
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
|
|
||||||
|
|
||||||
class Run:
|
class Run:
|
||||||
def __init__(self, cmd):
|
def __init__(self, cmd, root=False):
|
||||||
self.cmd = cmd
|
self.cmd = cmd
|
||||||
|
self.root = root
|
||||||
|
|
||||||
async def __call__(self, target):
|
async def __call__(self, target):
|
||||||
|
if self.root:
|
||||||
|
self.proc = await target.rexec(self.cmd)
|
||||||
|
else:
|
||||||
self.proc = await target.exec(self.cmd)
|
self.proc = await target.exec(self.cmd)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class User:
|
|||||||
return f'User({self.username}, {self.home}, {self.uid})'
|
return f'User({self.username}, {self.home}, {self.uid})'
|
||||||
|
|
||||||
async def __call__(self, target):
|
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:
|
if result.rc == 0:
|
||||||
old = re.match('.*\(([^)]*)\).*', result.out).group(1)
|
old = re.match('.*\(([^)]*)\).*', result.out).group(1)
|
||||||
await target.rexec(
|
await target.rexec(
|
||||||
@@ -40,3 +40,5 @@ class User:
|
|||||||
'-u', self.uid,
|
'-u', self.uid,
|
||||||
self.username
|
self.username
|
||||||
)
|
)
|
||||||
|
await target.mkdir(self.home)
|
||||||
|
await target.rexec('chown', self.uid, self.home)
|
||||||
|
|||||||
+35
-10
@@ -13,6 +13,8 @@ import importlib
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from .proc import ProcFailure
|
||||||
|
|
||||||
|
|
||||||
class Group(cli2.Group):
|
class Group(cli2.Group):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@@ -21,36 +23,59 @@ class Group(cli2.Group):
|
|||||||
|
|
||||||
|
|
||||||
class TargetArgument(cli2.Argument):
|
class TargetArgument(cli2.Argument):
|
||||||
"""DSN of the target to execute on, localhost by default, TBI"""
|
"""
|
||||||
|
Target to execute on: localhost by default, target=@ssh_host for ssh.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, cmd, param, doc=None, color=None, default=None):
|
def __init__(self, cmd, param, doc=None, color=None, default=None):
|
||||||
from shlax.targets.base import Target
|
from shlax.targets.base import Target
|
||||||
super().__init__(cmd, param, doc=self.__doc__, default=Target())
|
super().__init__(cmd, param, doc=self.__doc__, default=Target())
|
||||||
self.alias = ['target', 't']
|
self.alias = ['target', 't']
|
||||||
|
|
||||||
|
def cast(self, value):
|
||||||
|
from shlax.targets.ssh import Ssh
|
||||||
|
user, host = value.split('@')
|
||||||
|
return Ssh(host=host, user=user)
|
||||||
|
|
||||||
|
def match(self, arg):
|
||||||
|
return arg if isinstance(arg, str) and '@' in arg else None
|
||||||
|
|
||||||
|
|
||||||
class Command(cli2.Command):
|
class Command(cli2.Command):
|
||||||
def setargs(self):
|
def setargs(self):
|
||||||
cli2.arg('target', cls=TargetArgument)(self.target)
|
|
||||||
super().setargs()
|
super().setargs()
|
||||||
# that works though, so I went for that
|
if 'target' in self.sig.parameters:
|
||||||
# self['target'] = TargetArgument(
|
self['target'] = TargetArgument(
|
||||||
# self,
|
self,
|
||||||
# self.sig.parameters['target'],
|
self.sig.parameters['target'],
|
||||||
# )
|
)
|
||||||
if 'actions' in self:
|
if 'actions' in self:
|
||||||
del self['actions']
|
del self['actions']
|
||||||
|
|
||||||
def __call__(self, *argv):
|
def __call__(self, *argv):
|
||||||
super().__call__(*argv)
|
result = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = super().__call__(*argv)
|
||||||
|
except ProcFailure:
|
||||||
|
# just output the failure without TB, as command was already
|
||||||
|
# printed anyway
|
||||||
|
pass
|
||||||
self['target'].value.output.results(self['target'].value)
|
self['target'].value.output.results(self['target'].value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class ActionCommand(cli2.Command):
|
class ActionCommand(cli2.Command):
|
||||||
|
def setargs(self):
|
||||||
|
super().setargs()
|
||||||
|
self['target'] = TargetArgument(
|
||||||
|
self,
|
||||||
|
inspect.Parameter('target', inspect.Parameter.KEYWORD_ONLY),
|
||||||
|
)
|
||||||
|
|
||||||
def call(self, *args, **kwargs):
|
def call(self, *args, **kwargs):
|
||||||
self.target = self.target(*args, **kwargs)
|
self.target = self.target(*args, **kwargs)
|
||||||
from shlax.targets.base import Target
|
return super().call(self['target'].value)
|
||||||
return super().call(Target())
|
|
||||||
|
|
||||||
|
|
||||||
class ConsoleScript(Group):
|
class ConsoleScript(Group):
|
||||||
|
|||||||
+106
-9
@@ -1,12 +1,19 @@
|
|||||||
|
import copy
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from .podman import Podman
|
||||||
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 +21,109 @@ class Container:
|
|||||||
else:
|
else:
|
||||||
self.name = '-'.join([prefix, repo])
|
self.name = '-'.join([prefix, repo])
|
||||||
|
|
||||||
async def start(self, target):
|
self.pod = None
|
||||||
"""Start the container"""
|
|
||||||
await target.rexec(
|
@property
|
||||||
|
def full_name(self):
|
||||||
|
if self.pod:
|
||||||
|
return '-'.join([self.pod.name, self.name])
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
async def up(self, target, *args):
|
||||||
|
"""Start the container foreground"""
|
||||||
|
podman = Podman(target)
|
||||||
|
if self.pod:
|
||||||
|
pod = None
|
||||||
|
for _ in await podman.pod.ps():
|
||||||
|
if _['Name'] == self.pod.name:
|
||||||
|
pod = _
|
||||||
|
break
|
||||||
|
if not pod:
|
||||||
|
await podman.pod.create('--name', self.pod.name)
|
||||||
|
args = list(args) + ['--pod', self.pod.name]
|
||||||
|
|
||||||
|
# skip if already up
|
||||||
|
for result in await podman.ps('-a'):
|
||||||
|
for name in result['Names']:
|
||||||
|
if name == self.full_name:
|
||||||
|
if result['State'] == 'running':
|
||||||
|
target.output.info(f'{self.full_name} already running')
|
||||||
|
return
|
||||||
|
elif result['State'] in ('exited', 'configured'):
|
||||||
|
target.output.info(f'{self.full_name} starting')
|
||||||
|
startargs = ['podman', 'start']
|
||||||
|
if '-d' not in args:
|
||||||
|
startargs.append('--attach')
|
||||||
|
startargs.append(self.full_name)
|
||||||
|
await target.exec(*startargs)
|
||||||
|
return
|
||||||
|
|
||||||
|
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.full_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.full_name)
|
||||||
|
|
||||||
|
async def inspect(self, target):
|
||||||
|
"""Inspect container"""
|
||||||
|
await target.exec('podman', 'inspect', self.full_name)
|
||||||
|
|
||||||
|
async def logs(self, target):
|
||||||
|
"""Show container logs"""
|
||||||
|
await target.exec('podman', 'logs', self.full_name)
|
||||||
|
|
||||||
|
async def exec(self, target, cmd=None):
|
||||||
|
"""Execute a command in the container"""
|
||||||
|
cmd = cmd or 'bash'
|
||||||
|
if cmd.endswith('sh'):
|
||||||
|
import os
|
||||||
|
os.execvp(
|
||||||
|
'/usr/bin/podman',
|
||||||
|
[
|
||||||
|
'podman',
|
||||||
|
'exec',
|
||||||
|
'-it',
|
||||||
|
self.full_name,
|
||||||
|
cmd,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
result = await target.exec(
|
||||||
|
'podman',
|
||||||
|
'exec',
|
||||||
|
self.full_name,
|
||||||
|
cmd,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def down(self, target):
|
||||||
|
"""Start the container"""
|
||||||
|
await target.exec('podman', 'rm', '-f', self.full_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})'
|
||||||
|
|||||||
+46
-14
@@ -1,21 +1,52 @@
|
|||||||
import copy
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class Layers(set):
|
||||||
|
def __init__(self, image):
|
||||||
|
self.image = image
|
||||||
|
|
||||||
|
async def ls(self, target):
|
||||||
|
"""Fetch layers from localhost"""
|
||||||
|
ret = set()
|
||||||
|
results = await target.parent.exec(
|
||||||
|
'buildah images --json',
|
||||||
|
quiet=True,
|
||||||
|
)
|
||||||
|
results = json.loads(results.out)
|
||||||
|
|
||||||
|
prefix = 'localhost/' + self.image.repository + ':layer-'
|
||||||
|
for result in results:
|
||||||
|
if not result.get('names', None):
|
||||||
|
continue
|
||||||
|
for name in result['names']:
|
||||||
|
if name.startswith(prefix):
|
||||||
|
self.add(name)
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def rm(self, target, tags=None):
|
||||||
|
"""Drop layers for this image"""
|
||||||
|
if tags is None:
|
||||||
|
tags = [layer for layer in await self.ls(target)]
|
||||||
|
await target.exec('podman', 'rmi', *tags, raises=False)
|
||||||
|
|
||||||
|
|
||||||
class Image:
|
class Image:
|
||||||
PATTERN = re.compile(
|
PATTERN = re.compile(
|
||||||
'^((?P<backend>[a-z]*)://)?((?P<registry>[^/]*[.][^/]*)/)?((?P<repository>[^:]+))?(:(?P<tags>.*))?$' # noqa
|
'^((?P<backend>[a-z]*)://)?((?P<registry>[^/]*[.][^/]*)/)?((?P<repository>[^:]+))?(:(?P<tags>.*))?$' # noqa
|
||||||
, re.I
|
, re.I
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, arg=None, format=None, backend=None, registry=None, repository=None, tags=None):
|
def __init__(self, arg=None, format=None, backend=None, registry=None,
|
||||||
|
repository=None, tags=None):
|
||||||
self.arg = arg
|
self.arg = arg
|
||||||
self.format = format
|
self.format = format
|
||||||
self.backend = backend
|
self.backend = backend
|
||||||
self.registry = registry
|
self.registry = registry
|
||||||
self.repository = repository
|
self.repository = repository
|
||||||
self.tags = tags or []
|
self.tags = tags or []
|
||||||
|
self.layers = Layers(self)
|
||||||
|
|
||||||
match = re.match(self.PATTERN, arg)
|
match = re.match(self.PATTERN, arg)
|
||||||
if match:
|
if match:
|
||||||
@@ -43,18 +74,19 @@ class Image:
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.repository}:{self.tags[-1]}'
|
return f'{self.repository}:{self.tags[-1]}'
|
||||||
|
|
||||||
async def push(self, *args, **kwargs):
|
async def push(self, target):
|
||||||
user = os.getenv('DOCKER_USER')
|
user = os.getenv('IMAGES_USER')
|
||||||
passwd = os.getenv('DOCKER_PASS')
|
passwd = os.getenv('IMAGES_PASS')
|
||||||
action = kwargs.get('action', self)
|
|
||||||
if user and passwd:
|
if user and passwd:
|
||||||
action.output.cmd('buildah login -u ... -p ...' + self.registry)
|
target.output.cmd('buildah login -u ... -p ...' + self.registry)
|
||||||
await action.exec('buildah', 'login', '-u', user, '-p', passwd, self.registry or 'docker.io', debug=False)
|
await target.parent.exec(
|
||||||
|
'buildah', 'login', '-u', user, '-p', passwd,
|
||||||
|
self.registry or 'docker.io', debug=False)
|
||||||
|
|
||||||
for tag in self.tags:
|
for tag in self.tags:
|
||||||
await action.exec('buildah', 'push', f'{self.repository}:{tag}')
|
await target.parent.exec(
|
||||||
|
'buildah',
|
||||||
def layer(self, key):
|
'push',
|
||||||
layer = copy.deepcopy(self)
|
self.repository + ':final',
|
||||||
layer.tags = ['layer-' + key]
|
f'{self.registry}/{self.repository}:{tag}'
|
||||||
return layer
|
)
|
||||||
|
|||||||
@@ -185,6 +185,8 @@ class Output:
|
|||||||
]))
|
]))
|
||||||
|
|
||||||
def results(self, action):
|
def results(self, action):
|
||||||
|
if len(action.results) < 2:
|
||||||
|
return
|
||||||
success = 0
|
success = 0
|
||||||
fail = 0
|
fail = 0
|
||||||
for result in action.results:
|
for result in action.results:
|
||||||
|
|||||||
@@ -1,13 +1,23 @@
|
|||||||
import cli2
|
import cli2
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
from shlax.targets.base import Target
|
from shlax.targets.base import Target
|
||||||
from shlax.actions.parallel import Parallel
|
from shlax.actions.parallel import Parallel
|
||||||
|
from shlax.proc import Proc
|
||||||
|
|
||||||
|
from .podman import Podman
|
||||||
|
|
||||||
|
|
||||||
class Pod:
|
class Pod:
|
||||||
"""Help text"""
|
"""Help text"""
|
||||||
def __init__(self, **containers):
|
def __init__(self, **containers):
|
||||||
self.containers = containers
|
self.containers = containers
|
||||||
|
for name, container in self.containers.items():
|
||||||
|
container.pod = self
|
||||||
|
container.name = name
|
||||||
|
self.name = os.getcwd().split('/')[-1]
|
||||||
|
|
||||||
async def _call(self, target, method, *names):
|
async def _call(self, target, method, *names):
|
||||||
methods = [
|
methods = [
|
||||||
@@ -19,8 +29,50 @@ class Pod:
|
|||||||
|
|
||||||
async def build(self, target, *names):
|
async def build(self, target, *names):
|
||||||
"""Build container images"""
|
"""Build container images"""
|
||||||
|
if not (Proc.test or os.getuid() == 0):
|
||||||
|
os.execvp('buildah', ['buildah', 'unshare'] + sys.argv)
|
||||||
|
else:
|
||||||
await self._call(target, 'build', *names)
|
await self._call(target, 'build', *names)
|
||||||
|
|
||||||
|
async def down(self, target, *names):
|
||||||
|
"""Delete container images"""
|
||||||
|
await self._call(target, 'down', *names)
|
||||||
|
|
||||||
async def start(self, target, *names):
|
async def start(self, target, *names):
|
||||||
"""Start container images"""
|
"""Start container images"""
|
||||||
await self._call(target, 'start', *names)
|
await self._call(target, 'start', *names)
|
||||||
|
|
||||||
|
async def logs(self, target, *names):
|
||||||
|
"""Start container images"""
|
||||||
|
await self._call(target, 'logs', *names)
|
||||||
|
|
||||||
|
async def ps(self, target):
|
||||||
|
"""Show containers and volumes"""
|
||||||
|
containers = []
|
||||||
|
names = []
|
||||||
|
for container in await Podman(target).ps('-a'):
|
||||||
|
for name in container['Names']:
|
||||||
|
if name.startswith(self.name + '-'):
|
||||||
|
container['Name'] = name
|
||||||
|
containers.append(container)
|
||||||
|
names.append(name)
|
||||||
|
|
||||||
|
for name, container in self.containers.items():
|
||||||
|
full_name = '-'.join([self.name, container.name])
|
||||||
|
if full_name in names:
|
||||||
|
continue
|
||||||
|
containers.append(dict(
|
||||||
|
Name=full_name,
|
||||||
|
State='not created',
|
||||||
|
))
|
||||||
|
|
||||||
|
cli2.Table(
|
||||||
|
['Name', 'State'],
|
||||||
|
*[
|
||||||
|
(container['Name'], container['State'])
|
||||||
|
for container in containers
|
||||||
|
]
|
||||||
|
).print()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'Pod({self.name})'
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
class Podman(list):
|
||||||
|
def __init__(self, target, *args):
|
||||||
|
self.target = target
|
||||||
|
super().__init__(args or ['podman'])
|
||||||
|
|
||||||
|
def __getattr__(self, command):
|
||||||
|
if command.startswith('_'):
|
||||||
|
return super().__getattr__(command)
|
||||||
|
return Podman(self.target, *self + [command])
|
||||||
|
|
||||||
|
async def __call__(self, *args, **kwargs):
|
||||||
|
cmd = self + list(args) + [
|
||||||
|
f'--{k}={v}' for k, v in kwargs.items()
|
||||||
|
]
|
||||||
|
if 'ps' in cmd:
|
||||||
|
cmd += ['--format=json']
|
||||||
|
return (await self.target.exec(*cmd, quiet=True)).json
|
||||||
+54
-8
@@ -11,12 +11,14 @@ from ..result import Result, Results
|
|||||||
|
|
||||||
|
|
||||||
class Target:
|
class Target:
|
||||||
|
isguest = False
|
||||||
|
|
||||||
def __init__(self, *actions, root=None):
|
def __init__(self, *actions, root=None):
|
||||||
self.actions = actions
|
self.actions = actions
|
||||||
self.results = []
|
self.results = []
|
||||||
self.output = Output()
|
self.output = Output()
|
||||||
self.parent = None
|
self.parent = None
|
||||||
self.root = root or os.getcwd()
|
self.root = root or ''
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'localhost'
|
return 'localhost'
|
||||||
@@ -45,10 +47,18 @@ class Target:
|
|||||||
# the calling target
|
# the calling target
|
||||||
self.parent = target
|
self.parent = target
|
||||||
|
|
||||||
|
result = Result(self, self)
|
||||||
|
result.status = 'success'
|
||||||
|
|
||||||
for action in actions or self.actions:
|
for action in actions or self.actions:
|
||||||
if await self.action(action, reraise=bool(actions)):
|
if await self.action(action, reraise=bool(actions)):
|
||||||
|
result.status = 'failure'
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if getattr(self, 'clean', None):
|
||||||
|
self.output.clean(self)
|
||||||
|
await self.clean(self, result)
|
||||||
|
|
||||||
async def action(self, action, reraise=False):
|
async def action(self, action, reraise=False):
|
||||||
result = Result(self, action)
|
result = Result(self, action)
|
||||||
self.output.start(action)
|
self.output.start(action)
|
||||||
@@ -65,6 +75,9 @@ class Target:
|
|||||||
import traceback
|
import traceback
|
||||||
traceback.print_exception(type(e), e, sys.exc_info()[2])
|
traceback.print_exception(type(e), e, sys.exc_info()[2])
|
||||||
return True
|
return True
|
||||||
|
else:
|
||||||
|
if getattr(action, 'skipped', False):
|
||||||
|
self.output.skip(action)
|
||||||
else:
|
else:
|
||||||
self.output.success(action)
|
self.output.success(action)
|
||||||
result.status = 'success'
|
result.status = 'success'
|
||||||
@@ -130,20 +143,53 @@ class Target:
|
|||||||
|
|
||||||
@root.setter
|
@root.setter
|
||||||
def root(self, value):
|
def root(self, value):
|
||||||
self._root = Path(value or os.getcwd())
|
self._root = Path(value) if value else ''
|
||||||
|
|
||||||
|
@property
|
||||||
|
def host(self):
|
||||||
|
current = self
|
||||||
|
while current.isguest:
|
||||||
|
current = self.parent
|
||||||
|
return current
|
||||||
|
|
||||||
def path(self, path):
|
def path(self, path):
|
||||||
|
if not self.root:
|
||||||
|
return path
|
||||||
if str(path).startswith('/'):
|
if str(path).startswith('/'):
|
||||||
path = str(path)[1:]
|
path = str(path)[1:]
|
||||||
return self.root / path
|
return str(self.root / path)
|
||||||
|
|
||||||
async def mkdir(self, path):
|
async def mkdir(self, *paths):
|
||||||
if '_mkdir' not in self.__dict__:
|
if '_mkdir' not in self.__dict__:
|
||||||
self._mkdir = []
|
self._mkdir = []
|
||||||
path = str(path)
|
|
||||||
if path not in self._mkdir:
|
make = [str(path) for path in paths if str(path) not in self._mkdir]
|
||||||
await self.exec('mkdir', '-p', path)
|
if make:
|
||||||
self._mkdir.append(path)
|
await self.exec('mkdir', '-p', *make)
|
||||||
|
self._mkdir += make
|
||||||
|
|
||||||
async def copy(self, *args):
|
async def copy(self, *args):
|
||||||
return await self.exec('cp', '-a', *args)
|
return await self.exec('cp', '-a', *args)
|
||||||
|
|
||||||
|
async def exists(self, path):
|
||||||
|
return (await self.exec('ls ' + self.path(path), raises=False)).rc == 0
|
||||||
|
|
||||||
|
async def read(self, path):
|
||||||
|
return (await self.exec('cat', self.path(path))).out
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
async def getenv(self, key):
|
||||||
|
return (await self.exec('echo $' + key)).out
|
||||||
|
|
||||||
|
async def getcwd(self):
|
||||||
|
return (await self.exec('pwd')).out
|
||||||
|
|||||||
+69
-51
@@ -14,11 +14,9 @@ from ..proc import Proc
|
|||||||
|
|
||||||
class Buildah(Target):
|
class Buildah(Target):
|
||||||
"""Build container image with buildah"""
|
"""Build container image with buildah"""
|
||||||
|
isguest = True
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self, *actions, base=None, commit=None):
|
||||||
*actions,
|
|
||||||
base=None, commit=None,
|
|
||||||
cmd=None):
|
|
||||||
self.base = base or 'alpine'
|
self.base = base or 'alpine'
|
||||||
self.image = Image(commit) if commit else None
|
self.image = Image(commit) if commit else None
|
||||||
|
|
||||||
@@ -26,10 +24,6 @@ class Buildah(Target):
|
|||||||
self.root = None
|
self.root = None
|
||||||
self.mounts = dict()
|
self.mounts = dict()
|
||||||
|
|
||||||
self.config = dict(
|
|
||||||
cmd=cmd or 'sh',
|
|
||||||
)
|
|
||||||
|
|
||||||
# Always consider localhost as parent for now
|
# Always consider localhost as parent for now
|
||||||
self.parent = Target()
|
self.parent = Target()
|
||||||
|
|
||||||
@@ -43,22 +37,23 @@ class Buildah(Target):
|
|||||||
return 'Replacing with: buildah unshare ' + ' '.join(sys.argv)
|
return 'Replacing with: buildah unshare ' + ' '.join(sys.argv)
|
||||||
return f'Buildah({self.image})'
|
return f'Buildah({self.image})'
|
||||||
|
|
||||||
async def __call__(self, *actions, target=None):
|
async def __call__(self, *actions, target=None, push: bool=False):
|
||||||
if target:
|
if target:
|
||||||
self.parent = target
|
self.parent = target
|
||||||
|
|
||||||
|
self.push = push
|
||||||
|
|
||||||
if not self.is_runnable():
|
if not self.is_runnable():
|
||||||
os.execvp('buildah', ['buildah', 'unshare'] + sys.argv)
|
os.execvp('buildah', ['buildah', 'unshare'] + sys.argv)
|
||||||
# program has been replaced
|
return # process has been replaced
|
||||||
|
|
||||||
layers = await self.layers()
|
layers = await self.image.layers.ls(self)
|
||||||
keep = await self.cache_setup(layers, *actions)
|
keep = await self.cache_setup(self.image.layers, *actions)
|
||||||
keepnames = [*map(lambda x: 'localhost/' + str(x), keep)]
|
keepnames = [*map(lambda x: 'localhost/' + str(x), keep)]
|
||||||
self.invalidate = [name for name in layers if name not in keepnames]
|
self.invalidate = [name for name in self.image.layers if name not in keepnames]
|
||||||
if self.invalidate:
|
if self.invalidate:
|
||||||
self.output.info('Invalidating old layers')
|
self.output.info('Invalidating old layers')
|
||||||
await self.parent.exec(
|
await self.image.layers.rm(self.parent, self.invalidate)
|
||||||
'buildah', 'rmi', *self.invalidate, raises=False)
|
|
||||||
|
|
||||||
if actions:
|
if actions:
|
||||||
actions = actions[len(keep):]
|
actions = actions[len(keep):]
|
||||||
@@ -74,23 +69,6 @@ class Buildah(Target):
|
|||||||
|
|
||||||
return await super().__call__(*actions)
|
return await super().__call__(*actions)
|
||||||
|
|
||||||
async def layers(self):
|
|
||||||
ret = set()
|
|
||||||
results = await self.parent.exec(
|
|
||||||
'buildah images --json',
|
|
||||||
quiet=True,
|
|
||||||
)
|
|
||||||
results = json.loads(results.out)
|
|
||||||
|
|
||||||
prefix = 'localhost/' + self.image.repository + ':layer-'
|
|
||||||
for result in results:
|
|
||||||
if not result.get('names', None):
|
|
||||||
continue
|
|
||||||
for name in result['names']:
|
|
||||||
if name.startswith(prefix):
|
|
||||||
ret.add(name)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
async def cache_setup(self, layers, *actions):
|
async def cache_setup(self, layers, *actions):
|
||||||
keep = []
|
keep = []
|
||||||
self.image_previous = Image(self.base)
|
self.image_previous = Image(self.base)
|
||||||
@@ -121,7 +99,9 @@ class Buildah(Target):
|
|||||||
action_key = str(action)
|
action_key = str(action)
|
||||||
key = prefix + action_key
|
key = prefix + action_key
|
||||||
sha1 = hashlib.sha1(key.encode('ascii'))
|
sha1 = hashlib.sha1(key.encode('ascii'))
|
||||||
return self.image.layer(sha1.hexdigest())
|
action_image = copy.deepcopy(self.image)
|
||||||
|
action_image.tags = ['layer-' + sha1.hexdigest()]
|
||||||
|
return action_image
|
||||||
|
|
||||||
async def action(self, action, reraise=False):
|
async def action(self, action, reraise=False):
|
||||||
stop = await super().action(action, reraise)
|
stop = await super().action(action, reraise)
|
||||||
@@ -143,13 +123,15 @@ class Buildah(Target):
|
|||||||
for src, dst in self.mounts.items():
|
for src, dst in self.mounts.items():
|
||||||
await self.parent.exec('umount', self.root / str(dst)[1:])
|
await self.parent.exec('umount', self.root / str(dst)[1:])
|
||||||
await self.parent.exec('buildah', 'umount', self.ctr)
|
await self.parent.exec('buildah', 'umount', self.ctr)
|
||||||
await self.parent.exec('buildah', 'rm', self.ctr)
|
|
||||||
|
|
||||||
if result.status == 'success':
|
if result.status == 'success' and self.ctr:
|
||||||
await self.commit()
|
await self.commit()
|
||||||
if os.getenv('BUILDAH_PUSH'):
|
if self.push:
|
||||||
await self.image.push(target)
|
await self.image.push(target)
|
||||||
|
|
||||||
|
if self.ctr is not None:
|
||||||
|
await self.parent.exec('buildah', 'rm', self.ctr)
|
||||||
|
|
||||||
async def mount(self, src, dst):
|
async def mount(self, src, dst):
|
||||||
"""Mount a host directory into the container."""
|
"""Mount a host directory into the container."""
|
||||||
target = self.root / str(dst)[1:]
|
target = self.root / str(dst)[1:]
|
||||||
@@ -165,15 +147,10 @@ class Buildah(Target):
|
|||||||
_args += [' '.join([str(a) for a in args])]
|
_args += [' '.join([str(a) for a in args])]
|
||||||
return await self.parent.exec(*_args, **kwargs)
|
return await self.parent.exec(*_args, **kwargs)
|
||||||
|
|
||||||
async def commit(self, image=None):
|
async def commit(self):
|
||||||
image = image or self.image
|
await self.parent.exec(
|
||||||
if not image:
|
f'buildah commit {self.ctr} {self.image.repository}:final'
|
||||||
return
|
)
|
||||||
|
|
||||||
if not image:
|
|
||||||
# don't go through that if layer commit
|
|
||||||
for key, value in self.config.items():
|
|
||||||
await self.parent.exec(f'buildah config --{key} "{value}" {self.ctr}')
|
|
||||||
|
|
||||||
ENV_TAGS = (
|
ENV_TAGS = (
|
||||||
# gitlab
|
# gitlab
|
||||||
@@ -193,15 +170,56 @@ class Buildah(Target):
|
|||||||
if value:
|
if value:
|
||||||
self.image.tags.append(value)
|
self.image.tags.append(value)
|
||||||
|
|
||||||
if image.tags:
|
if self.image.tags:
|
||||||
tags = [f'{image.repository}:{tag}' for tag in image.tags]
|
tags = [f'{self.image.repository}:{tag}' for tag in self.image.tags]
|
||||||
else:
|
else:
|
||||||
tags = [image.repository]
|
tags = [self.image.repository]
|
||||||
|
|
||||||
await self.parent.exec('buildah', 'tag', self.image_previous, *tags)
|
await self.parent.exec('buildah', 'tag', self.image.repository + ':final', *tags)
|
||||||
|
|
||||||
async def mkdir(self, path):
|
async def mkdir(self, *paths):
|
||||||
return await self.parent.mkdir(self.path(path))
|
return await self.parent.mkdir(*[self.path(path) for path in paths])
|
||||||
|
|
||||||
async def copy(self, *args):
|
async def copy(self, *args):
|
||||||
return await self.parent.copy(*args[:-1], self.path(args[-1]))
|
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})'
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
from .base import Target
|
||||||
|
|
||||||
|
|
||||||
|
class Ssh(Target):
|
||||||
|
def __init__(self, *actions, host, user=None):
|
||||||
|
self.host = host
|
||||||
|
self.user = user
|
||||||
|
super().__init__(*actions)
|
||||||
|
|
||||||
|
async def exec(self, *args, user=None, **kwargs):
|
||||||
|
_args = ['ssh', self.host]
|
||||||
|
if user == 'root':
|
||||||
|
_args += ['sudo']
|
||||||
|
_args += [' '.join([str(a) for a in args])]
|
||||||
|
return await self.parent.exec(*_args, **kwargs)
|
||||||
+1
-1
@@ -11,7 +11,7 @@ shlax = Container(
|
|||||||
Copy('setup.py', 'shlax', '/app'),
|
Copy('setup.py', 'shlax', '/app'),
|
||||||
Pip('/app'),
|
Pip('/app'),
|
||||||
base='quay.io/podman/stable',
|
base='quay.io/podman/stable',
|
||||||
commit='shlax',
|
commit='quay.io/yourlabs/shlax',
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -72,6 +72,37 @@ async def test_function():
|
|||||||
await Stub()(hello)
|
await Stub()(hello)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_action_clean():
|
||||||
|
class Example:
|
||||||
|
def __init__(self):
|
||||||
|
self.was_called = False
|
||||||
|
async def clean(self, target, result):
|
||||||
|
self.was_called = True
|
||||||
|
async def __call__(self, target):
|
||||||
|
raise Exception('lol')
|
||||||
|
|
||||||
|
action = Example()
|
||||||
|
target = Stub()
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
await target(action)
|
||||||
|
assert action.was_called
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_target_clean():
|
||||||
|
class Example(Stub):
|
||||||
|
def __init__(self, action):
|
||||||
|
self.was_called = False
|
||||||
|
super().__init__(action)
|
||||||
|
async def clean(self, target, result):
|
||||||
|
self.was_called = True
|
||||||
|
|
||||||
|
target = Example(Error())
|
||||||
|
await target()
|
||||||
|
assert target.was_called
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_method():
|
async def test_method():
|
||||||
class Example:
|
class Example:
|
||||||
|
|||||||
Reference in New Issue
Block a user