Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b6650efdb | ||
|
|
28bfef29b0 | ||
|
|
bcbe66a37f | ||
|
|
44dd1bc6a6 | ||
|
|
9846425b29 | ||
|
|
a7cd98ffbb | ||
|
|
8544a76bf2 | ||
|
|
7467f79bd9 | ||
|
|
3cd3dae619 | ||
|
|
d23b3945f3 | ||
|
|
9e586de312 | ||
|
|
5c941d38d6 | ||
|
|
b206a1e107 | ||
|
|
92246fc7be | ||
|
|
94890226e6 | ||
|
|
1221ac5016 | ||
|
|
84b1230146 | ||
|
|
78252a439c | ||
|
|
ae2aa62d0e | ||
|
|
a80e3fb48c | ||
|
|
77439b6dc3 | ||
|
|
cada94ecc5 | ||
|
|
0edd573f4a | ||
|
|
f5765c08a9 | ||
|
|
c46b42b23a | ||
|
|
4fcabf1aed | ||
|
|
b37e4c1aed | ||
|
|
4ea9783c93 | ||
|
|
54e3266789 | ||
|
|
5c76ef3f2d | ||
|
|
c5af902382 | ||
|
|
ecabf6c9ce | ||
|
|
324a8f4962 | ||
|
|
61fb39ebea | ||
|
|
5a20ccf2f7 | ||
|
|
d5544a6e85 | ||
|
|
ce931a2cc6 | ||
|
|
12edb5168f | ||
|
|
d2c0694005 | ||
|
|
15c71a3bf7 | ||
|
|
6e7f957d49 | ||
|
|
30798fc66c | ||
|
|
7408cf95c0 | ||
|
|
7f150ae17a | ||
|
|
073c3713c1 | ||
|
|
1ce27e1cd1 | ||
|
|
9235ffcd27 | ||
|
|
07a6c5b628 | ||
|
|
47740f01f9 | ||
|
|
9330125208 | ||
|
|
afc1823e54 | ||
|
|
f07787d865 | ||
|
|
84b10ce143 | ||
|
|
ae61e1b8cf | ||
|
|
6a0b60d68a | ||
|
|
63e610ca4f | ||
|
|
4f5326c81b | ||
|
|
224608878e | ||
|
|
eafa371617 | ||
|
|
b6fa18d82a | ||
|
|
754227d2d1 | ||
|
|
62299da7c1 | ||
|
|
0929b37b36 | ||
|
|
8dd4fa52ac | ||
|
|
0ac1764464 | ||
|
|
70e354dd02 | ||
|
|
0a983340b8 | ||
|
|
c6bc849574 | ||
|
|
2e1bbf098a | ||
|
|
c6d7eedde2 | ||
|
|
93b95a1f50 | ||
|
|
a07d9c5e67 | ||
|
|
c570f0fd6d | ||
|
|
5caf9d92e3 | ||
|
|
c58d1618ed | ||
|
|
4af938fab0 | ||
|
|
7a61b405ae | ||
|
|
600043ae64 | ||
|
|
b2169ff7eb | ||
|
|
f374d77c21 | ||
|
|
b6bb06054d | ||
|
|
1b1a121a24 | ||
|
|
1d5e8ab1c8 | ||
|
|
e62d44514a | ||
|
|
b044dd015e |
+18
-7
@@ -1,16 +1,27 @@
|
|||||||
build:
|
build:
|
||||||
cache:
|
cache:
|
||||||
key: cache
|
key: cache
|
||||||
paths: [.cache]
|
paths: [.cache, /var/lib/containers/]
|
||||||
image: quay.io/buildah/stable
|
image: yourlabs/buildah
|
||||||
script:
|
script:
|
||||||
- dnf install -y curl python38
|
- pip3 install -U --user .[cli]
|
||||||
- curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
|
- CACHE_DIR=$(pwd)/.cache python3 ./shlaxfile.py build push=docker://docker.io/yourlabs/shlax:$CI_COMMIT_SHORT_SHA
|
||||||
- python3.8 get-pip.py
|
|
||||||
- pip3.8 install -U --user -e .[cli]
|
|
||||||
- CACHE_DIR=$(pwd)/.cache ~/.local/bin/shlax ./shlaxfile.py build
|
|
||||||
stage: build
|
stage: build
|
||||||
|
|
||||||
|
build-itself:
|
||||||
|
cache:
|
||||||
|
key: cache
|
||||||
|
paths: [.cache, /var/lib/containers/]
|
||||||
|
image: yourlabs/shlax:$CI_COMMIT_SHORT_SHA
|
||||||
|
script: python3 ./shlaxfile.py build push=docker://docker.io/yourlabs/shlax:$CI_COMMIT_REF
|
||||||
|
stage: test
|
||||||
|
|
||||||
|
test-exitcode:
|
||||||
|
image: yourlabs/shlax:$CI_COMMIT_SHORT_SHA
|
||||||
|
script:
|
||||||
|
- tests/shlaxfail.py build || [ $? -eq 1 ]
|
||||||
|
- tests/shlaxsuccess.py build
|
||||||
|
|
||||||
test:
|
test:
|
||||||
image: yourlabs/python
|
image: yourlabs/python
|
||||||
stage: build
|
stage: build
|
||||||
|
|||||||
@@ -1,9 +1,32 @@
|
|||||||
# Shlax: Pythonic automation tool
|
# Shlax: Pythonic automation tool
|
||||||
|
|
||||||
Shlax is a Python framework for system automation, initially with the purpose
|
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. It may be viewed as "async fabric rewrite by a
|
purpose of code-reuse made possible by target abstraction.
|
||||||
megalomanic Django fanboy".
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
@@ -187,12 +210,13 @@ class Docker(Target):
|
|||||||
return await self.parent.exec(*['docker', 'exec', self.name] + args)
|
return await self.parent.exec(*['docker', 'exec', self.name] + args)
|
||||||
```
|
```
|
||||||
|
|
||||||
Don't worry about `self.parent` being set, it is enforced to `Localhost` if
|
This also means that you always need a parent with an exec implementation,
|
||||||
unset so that we always have something that actually spawns a process in the
|
there are two:
|
||||||
chain ;)
|
|
||||||
|
|
||||||
The result of that design is that the following use cases are open for
|
- Localhost, executes on localhost
|
||||||
business:
|
- Stub, for testing
|
||||||
|
|
||||||
|
The result of that design is that the following use cases are available:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# This action installs my favorite package on any distro
|
# This action installs my favorite package on any distro
|
||||||
@@ -215,14 +239,44 @@ Ssh(host='yourhost')(build)
|
|||||||
|
|
||||||
# Or on a server behingh a bastion:
|
# Or on a server behingh a bastion:
|
||||||
# ssh yourbastion ssh yourhost build exec apt install python3
|
# ssh yourbastion ssh yourhost build exec apt install python3
|
||||||
Ssh(host='bastion')(Ssh(host='yourhost')(build))
|
Localhost()(Ssh(host='bastion')(Ssh(host='yourhost')(build))
|
||||||
|
|
||||||
# That's going to do the same
|
# That's going to do the same
|
||||||
Ssh(
|
Localhost(Ssh(
|
||||||
Ssh(
|
Ssh(
|
||||||
build,
|
build,
|
||||||
host='yourhost'
|
host='yourhost'
|
||||||
),
|
),
|
||||||
host='bastion'
|
host='bastion'
|
||||||
)()
|
))()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## CLI
|
||||||
|
|
||||||
|
You can execute Shlax actions directly on the command line with the `shlax` CLI
|
||||||
|
command.
|
||||||
|
|
||||||
|
For your own Shlaxfiles, you can build your CLI with your favorite CLI
|
||||||
|
framework. If you decide to use `cli2`, then Shlax provides a thin layer on top
|
||||||
|
of it: Group and Command objects made for Shlax objects.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
yourcontainer = Container(
|
||||||
|
build=Buildah(
|
||||||
|
User('app', '/app', 1000),
|
||||||
|
Packages('python', 'unzip', 'findutils'),
|
||||||
|
Copy('setup.py', 'yourdir', '/app'),
|
||||||
|
base='archlinux',
|
||||||
|
commit='yourimage',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print(Group(doc=__doc__).load(yourcontainer).entry_point())
|
||||||
|
```
|
||||||
|
|
||||||
|
The above will execute a cli2 command with each method of yourcontainer as a
|
||||||
|
sub-command.
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ setup(
|
|||||||
setup_requires='setupmeta',
|
setup_requires='setupmeta',
|
||||||
extras_require=dict(
|
extras_require=dict(
|
||||||
cli=[
|
cli=[
|
||||||
'cli2',
|
'cli2>=2.3.0',
|
||||||
],
|
],
|
||||||
test=[
|
test=[
|
||||||
'pytest',
|
'pytest',
|
||||||
@@ -24,7 +24,7 @@ setup(
|
|||||||
python_requires='>=3',
|
python_requires='>=3',
|
||||||
entry_points={
|
entry_points={
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
'shlax = shlax.cli:cli',
|
'shlax = shlax.cli:cli.entry_point',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import asyncio
|
||||||
|
import binascii
|
||||||
|
import glob
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class Copy:
|
||||||
|
def __init__(self, *args):
|
||||||
|
self.dst = args[-1]
|
||||||
|
self.src = []
|
||||||
|
|
||||||
|
for src in args[:-1]:
|
||||||
|
if '*' in src:
|
||||||
|
self.src += glob.glob(src)
|
||||||
|
else:
|
||||||
|
self.src.append(src)
|
||||||
|
|
||||||
|
def listfiles(self):
|
||||||
|
if getattr(self, '_listfiles', None):
|
||||||
|
return self._listfiles
|
||||||
|
|
||||||
|
result = []
|
||||||
|
for src in self.src:
|
||||||
|
if os.path.isfile(src):
|
||||||
|
result.append(src)
|
||||||
|
continue
|
||||||
|
|
||||||
|
for root, dirs, files in os.walk(src):
|
||||||
|
if '__pycache__' in root:
|
||||||
|
continue
|
||||||
|
result += [
|
||||||
|
os.path.join(root, f)
|
||||||
|
for f in files
|
||||||
|
if not f.endswith('.pyc')
|
||||||
|
]
|
||||||
|
self._listfiles = result
|
||||||
|
return result
|
||||||
|
|
||||||
|
async def __call__(self, target):
|
||||||
|
await target.mkdir(self.dst)
|
||||||
|
|
||||||
|
for path in self.listfiles():
|
||||||
|
if os.path.isdir(path):
|
||||||
|
await target.mkdir(os.path.join(self.dst, path))
|
||||||
|
elif '/' in path:
|
||||||
|
dirname = os.path.join(
|
||||||
|
self.dst,
|
||||||
|
'/'.join(path.split('/')[:-1])
|
||||||
|
)
|
||||||
|
await target.mkdir(dirname)
|
||||||
|
await target.copy(path, dirname)
|
||||||
|
else:
|
||||||
|
await target.copy(path, self.dst)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'Copy({", ".join(self.src)}, {self.dst})'
|
||||||
|
|
||||||
|
async def cachekey(self):
|
||||||
|
async def chksum(path):
|
||||||
|
with open(path, 'rb') as f:
|
||||||
|
return (path, str(binascii.crc32(f.read())))
|
||||||
|
results = await asyncio.gather(*[chksum(f) for f in self.listfiles()])
|
||||||
|
return {path: chks for path, chks in results}
|
||||||
+47
-34
@@ -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,53 +54,52 @@ class Packages:
|
|||||||
|
|
||||||
installed = []
|
installed = []
|
||||||
|
|
||||||
def __init__(self, *packages):
|
def __init__(self, *packages, upgrade=False):
|
||||||
self.packages = []
|
self.packages = []
|
||||||
|
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)
|
||||||
|
|
||||||
@@ -115,8 +116,15 @@ 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)
|
||||||
await target.rexec(self.cmds['upgrade'])
|
|
||||||
|
if self.upgrade:
|
||||||
|
await target.rexec(self.cmds['upgrade'])
|
||||||
|
|
||||||
packages = []
|
packages = []
|
||||||
for package in self.packages:
|
for package in self.packages:
|
||||||
@@ -145,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 __repr__(self):
|
def __str__(self):
|
||||||
return f'Packages({self.packages})'
|
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'
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
from glob import glob
|
||||||
|
import os
|
||||||
|
from urllib import request
|
||||||
|
|
||||||
|
from .base import Action
|
||||||
|
|
||||||
|
|
||||||
|
class Pip(Action):
|
||||||
|
"""Pip abstraction layer."""
|
||||||
|
def __init__(self, *pip_packages):
|
||||||
|
self.pip_packages = pip_packages
|
||||||
|
|
||||||
|
async def __call__(self, target):
|
||||||
|
# ensure python presence
|
||||||
|
results = await target.which('python3', 'python')
|
||||||
|
if results:
|
||||||
|
python = results[0]
|
||||||
|
else:
|
||||||
|
raise Exception('Could not find pip nor python')
|
||||||
|
|
||||||
|
# ensure pip module presence
|
||||||
|
result = await target.exec(
|
||||||
|
python, '-m', 'pip',
|
||||||
|
raises=False, quiet=True
|
||||||
|
)
|
||||||
|
if result.rc != 0:
|
||||||
|
if not os.path.exists('get-pip.py'):
|
||||||
|
req = request.urlopen(
|
||||||
|
'https://bootstrap.pypa.io/get-pip.py'
|
||||||
|
)
|
||||||
|
content = req.read()
|
||||||
|
with open('get-pip.py', 'wb+') as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
|
await target.copy('get-pip.py', '.')
|
||||||
|
await target.exec(python, 'get-pip.py')
|
||||||
|
|
||||||
|
# choose a cache directory
|
||||||
|
if 'CACHE_DIR' in os.environ:
|
||||||
|
cache = os.path.join(os.getenv('CACHE_DIR'), 'pip')
|
||||||
|
else:
|
||||||
|
cache = os.path.join(os.getenv('HOME'), '.cache', 'pip')
|
||||||
|
|
||||||
|
# and mount it
|
||||||
|
if getattr(target, 'mount', None):
|
||||||
|
# we are in a target which shares a mount command
|
||||||
|
await target.mount(cache, '/root/.cache/pip')
|
||||||
|
|
||||||
|
source = []
|
||||||
|
nonsource = []
|
||||||
|
for package in self.pip_packages:
|
||||||
|
if os.path.exists(package):
|
||||||
|
source.append(package)
|
||||||
|
else:
|
||||||
|
nonsource.append(package)
|
||||||
|
|
||||||
|
if nonsource:
|
||||||
|
await target.exec(
|
||||||
|
python, '-m', 'pip',
|
||||||
|
'install', '--upgrade',
|
||||||
|
*nonsource
|
||||||
|
)
|
||||||
|
|
||||||
|
if source:
|
||||||
|
await target.exec(
|
||||||
|
python, '-m', 'pip',
|
||||||
|
'install', '--upgrade', '--editable',
|
||||||
|
*source
|
||||||
|
)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'Pip({", ".join(self.pip_packages)})'
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
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):
|
||||||
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):
|
def __str__(self):
|
||||||
return f'Run({self.cmd})'
|
return f'Run({self.cmd})'
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
from .packages import Packages
|
||||||
|
|
||||||
|
|
||||||
|
class User:
|
||||||
|
"""
|
||||||
|
Create a user.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
User('app', '/app', getenv('_CONTAINERS_ROOTLESS_UID', 1000)),
|
||||||
|
|
||||||
|
_CONTAINERS_ROOTLESS_UID allows to get your UID during build, which happens
|
||||||
|
in buildah unshare.
|
||||||
|
"""
|
||||||
|
def __init__(self, username, home, uid):
|
||||||
|
self.username = username
|
||||||
|
self.home = home
|
||||||
|
self.uid = uid
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'User({self.username}, {self.home}, {self.uid})'
|
||||||
|
|
||||||
|
async def __call__(self, target):
|
||||||
|
result = await target.rexec('id', self.uid, raises=False)
|
||||||
|
if result.rc == 0:
|
||||||
|
old = re.match('.*\(([^)]*)\).*', result.out).group(1)
|
||||||
|
await target.rexec(
|
||||||
|
'usermod',
|
||||||
|
'-d', self.home,
|
||||||
|
'-l', self.username,
|
||||||
|
old
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await target.rexec(
|
||||||
|
'useradd',
|
||||||
|
'-d', self.home,
|
||||||
|
'-u', self.uid,
|
||||||
|
self.username
|
||||||
|
)
|
||||||
|
await target.mkdir(self.home)
|
||||||
|
await target.rexec('chown', self.uid, self.home)
|
||||||
+103
-34
@@ -5,6 +5,7 @@ Shlax executes mostly in 3 ways:
|
|||||||
- With the name of a module in shlax.repo: a community maintained shlaxfile
|
- With the name of a module in shlax.repo: a community maintained shlaxfile
|
||||||
"""
|
"""
|
||||||
import ast
|
import ast
|
||||||
|
import asyncio
|
||||||
import cli2
|
import cli2
|
||||||
import glob
|
import glob
|
||||||
import inspect
|
import inspect
|
||||||
@@ -12,44 +13,112 @@ import importlib
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from .proc import ProcFailure
|
||||||
class ConsoleScript(cli2.ConsoleScript):
|
|
||||||
def __call__(self):
|
|
||||||
repo = os.path.join(os.path.dirname(__file__), 'repo')
|
|
||||||
|
|
||||||
if len(self.argv) > 1:
|
|
||||||
repofile = os.path.join(repo, sys.argv[1] + '.py')
|
|
||||||
if os.path.isfile(self.argv[1]):
|
|
||||||
self.argv = sys.argv[1:]
|
|
||||||
self.load_shlaxfile(sys.argv[1])
|
|
||||||
elif os.path.isfile(repofile):
|
|
||||||
self.argv = sys.argv[1:]
|
|
||||||
self.load_shlaxfile(repofile)
|
|
||||||
else:
|
|
||||||
raise Exception('File not found ' + sys.argv[1])
|
|
||||||
else:
|
|
||||||
available = glob.glob(os.path.join(repo, '*.py'))
|
|
||||||
return super().__call__()
|
|
||||||
|
|
||||||
|
|
||||||
def load_shlaxfile(self, path):
|
class Group(cli2.Group):
|
||||||
with open(path) as f:
|
def __init__(self, *args, **kwargs):
|
||||||
src = f.read()
|
super().__init__(*args, **kwargs)
|
||||||
tree = ast.parse(src)
|
self.cmdclass = Command
|
||||||
|
|
||||||
members = []
|
|
||||||
for node in tree.body:
|
class TargetArgument(cli2.Argument):
|
||||||
if not isinstance(node, ast.Assign):
|
"""
|
||||||
|
Target to execute on: localhost by default, target=@ssh_host for ssh.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, cmd, param, doc=None, color=None, default=None):
|
||||||
|
from shlax.targets.base import Target
|
||||||
|
super().__init__(cmd, param, doc=self.__doc__, default=Target())
|
||||||
|
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):
|
||||||
|
def setargs(self):
|
||||||
|
super().setargs()
|
||||||
|
if 'target' in self.sig.parameters:
|
||||||
|
self['target'] = TargetArgument(
|
||||||
|
self,
|
||||||
|
self.sig.parameters['target'],
|
||||||
|
)
|
||||||
|
if 'actions' in self:
|
||||||
|
del self['actions']
|
||||||
|
|
||||||
|
def __call__(self, *argv):
|
||||||
|
result = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = super().__call__(*argv)
|
||||||
|
except ProcFailure:
|
||||||
|
# just output the failure without TB, as command was already
|
||||||
|
# printed anyway
|
||||||
|
pass
|
||||||
|
|
||||||
|
if self['target'].value.results:
|
||||||
|
if self['target'].value.results[-1].status == 'failure':
|
||||||
|
self.exit_code = 1
|
||||||
|
self['target'].value.output.results(self['target'].value)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
self.target = self.target(*args, **kwargs)
|
||||||
|
return super().call(self['target'].value)
|
||||||
|
|
||||||
|
|
||||||
|
class ConsoleScript(Group):
|
||||||
|
def __call__(self, *argv):
|
||||||
|
self.load_actions()
|
||||||
|
#self.load_shlaxfiles() # wip
|
||||||
|
return super().__call__(*argv)
|
||||||
|
|
||||||
|
def load_shlaxfiles(self):
|
||||||
|
filesdir = os.path.dirname(__file__) + '/shlaxfiles/'
|
||||||
|
for filename in os.listdir(filesdir):
|
||||||
|
filepath = filesdir + filename
|
||||||
|
if not os.path.isfile(filepath):
|
||||||
continue
|
continue
|
||||||
if not isinstance(node.value, ast.Call):
|
|
||||||
|
with open(filepath, 'r') as f:
|
||||||
|
tree = ast.parse(f.read())
|
||||||
|
group = self.group(filename[:-3])
|
||||||
|
|
||||||
|
main = Group(doc=__doc__).load(shlax)
|
||||||
|
|
||||||
|
def load_actions(self):
|
||||||
|
actionsdir = os.path.dirname(__file__) + '/actions/'
|
||||||
|
for filename in os.listdir(actionsdir):
|
||||||
|
filepath = actionsdir + filename
|
||||||
|
if not os.path.isfile(filepath):
|
||||||
continue
|
continue
|
||||||
members.append(node.targets[0].id)
|
with open(filepath, 'r') as f:
|
||||||
|
tree = ast.parse(f.read())
|
||||||
|
cls = [
|
||||||
|
node
|
||||||
|
for node in tree.body
|
||||||
|
if isinstance(node, ast.ClassDef)
|
||||||
|
]
|
||||||
|
if not cls:
|
||||||
|
continue
|
||||||
|
mod = importlib.import_module('shlax.actions.' + filename[:-3])
|
||||||
|
cls = getattr(mod, cls[0].name)
|
||||||
|
self.add(cls, name=filename[:-3], cmdclass=ActionCommand)
|
||||||
|
|
||||||
spec = importlib.util.spec_from_file_location('shlaxfile', sys.argv[1])
|
|
||||||
mod = importlib.util.module_from_spec(spec)
|
|
||||||
spec.loader.exec_module(mod)
|
|
||||||
for member in members:
|
|
||||||
from shlax.targets.localhost import Localhost
|
|
||||||
self[member] = cli2.Callable(member, Localhost(getattr(mod, member)))
|
|
||||||
|
|
||||||
cli = ConsoleScript(__doc__)
|
cli = ConsoleScript(doc=__doc__)
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
import copy
|
||||||
|
import os
|
||||||
|
|
||||||
|
from .podman import Podman
|
||||||
|
from .image import Image
|
||||||
|
|
||||||
|
|
||||||
|
class Container:
|
||||||
|
def __init__(self, build=None, image=None, env=None, volumes=None):
|
||||||
|
self.build = build
|
||||||
|
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:
|
||||||
|
self.name = repo
|
||||||
|
else:
|
||||||
|
self.name = '-'.join([prefix, repo])
|
||||||
|
|
||||||
|
self.pod = None
|
||||||
|
|
||||||
|
@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',
|
||||||
|
'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.full_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.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):
|
||||||
|
return f'Container(name={self.name}, image={self.image}, volumes={self.volumes})'
|
||||||
+49
-31
@@ -1,32 +1,52 @@
|
|||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
class Image:
|
class Layers(set):
|
||||||
ENV_TAGS = (
|
def __init__(self, image):
|
||||||
# gitlab
|
self.image = image
|
||||||
'CI_COMMIT_SHORT_SHA',
|
|
||||||
'CI_COMMIT_REF_NAME',
|
|
||||||
'CI_COMMIT_TAG',
|
|
||||||
# CircleCI
|
|
||||||
'CIRCLE_SHA1',
|
|
||||||
'CIRCLE_TAG',
|
|
||||||
'CIRCLE_BRANCH',
|
|
||||||
# contributions welcome here
|
|
||||||
)
|
|
||||||
|
|
||||||
|
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:
|
||||||
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:
|
||||||
@@ -40,15 +60,11 @@ class Image:
|
|||||||
setattr(self, k, v)
|
setattr(self, k, v)
|
||||||
|
|
||||||
# docker.io currently has issues with oci format
|
# docker.io currently has issues with oci format
|
||||||
self.format = format or 'oci'
|
|
||||||
if self.registry == 'docker.io':
|
if self.registry == 'docker.io':
|
||||||
self.format = 'docker'
|
self.backend = 'docker'
|
||||||
|
|
||||||
# figure tags from CI vars
|
if not self.format:
|
||||||
for name in self.ENV_TAGS:
|
self.format = 'docker' if self.backend == 'docker' else 'oci'
|
||||||
value = os.getenv(name)
|
|
||||||
if value:
|
|
||||||
self.tags.append(value)
|
|
||||||
|
|
||||||
# filter out tags which resolved to None
|
# filter out tags which resolved to None
|
||||||
self.tags = [t for t in self.tags if t]
|
self.tags = [t for t in self.tags if t]
|
||||||
@@ -57,20 +73,22 @@ class Image:
|
|||||||
if not self.tags:
|
if not self.tags:
|
||||||
self.tags = ['latest']
|
self.tags = ['latest']
|
||||||
|
|
||||||
async def __call__(self, action, *args, **kwargs):
|
|
||||||
args = list(args)
|
|
||||||
return await action.exec(*args, **self.kwargs)
|
|
||||||
|
|
||||||
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, name=None):
|
||||||
user = os.getenv('DOCKER_USER')
|
user = os.getenv('IMAGES_USER', os.getenv('DOCKER_USER'))
|
||||||
passwd = os.getenv('DOCKER_PASS')
|
passwd = os.getenv('IMAGES_PASS', os.getenv('DOCKER_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', quiet=True)
|
||||||
|
|
||||||
for tag in self.tags:
|
for tag in self.tags:
|
||||||
await action.exec('buildah', 'push', f'{self.repository}:{tag}')
|
await target.parent.exec(
|
||||||
|
'buildah',
|
||||||
|
'push',
|
||||||
|
self.repository + ':final',
|
||||||
|
name if isinstance(name, str) else f'{self.registry}/{self.repository}:{tag}'
|
||||||
|
)
|
||||||
|
|||||||
+49
-22
@@ -1,5 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
import types
|
||||||
|
|
||||||
from .colors import colors
|
from .colors import colors
|
||||||
|
|
||||||
@@ -25,15 +26,16 @@ class Output:
|
|||||||
def colorize(self, code, content):
|
def colorize(self, code, content):
|
||||||
return self.color(code) + content + self.color()
|
return self.color(code) + content + self.color()
|
||||||
|
|
||||||
def colorized(self):
|
def colorized(self, action):
|
||||||
if hasattr(self.subject, 'colorized'):
|
if hasattr(action, 'colorized'):
|
||||||
return self.subject.colorized(self.colors)
|
return action.colorized(self.colors)
|
||||||
|
elif isinstance(action, types.MethodType):
|
||||||
|
return f'{action.__self__}.{action.__name__}'
|
||||||
else:
|
else:
|
||||||
return str(self.subject)
|
return str(action)
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
subject=None,
|
|
||||||
prefix=None,
|
prefix=None,
|
||||||
regexps=None,
|
regexps=None,
|
||||||
debug='cmd,visit,out',
|
debug='cmd,visit,out',
|
||||||
@@ -41,7 +43,6 @@ class Output:
|
|||||||
flush=None,
|
flush=None,
|
||||||
**kwargs
|
**kwargs
|
||||||
):
|
):
|
||||||
self.subject = subject
|
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
self.prefix_length = 0
|
self.prefix_length = 0
|
||||||
@@ -100,7 +101,11 @@ class Output:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def highlight(self, line, highlight=True):
|
def highlight(self, line, highlight=True):
|
||||||
line = line.decode('utf8') if isinstance(line, bytes) else line
|
try:
|
||||||
|
line = line.decode('utf8') if isinstance(line, bytes) else line
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
highlight = False
|
||||||
|
|
||||||
if not highlight or (
|
if not highlight or (
|
||||||
'\x1b[' in line
|
'\x1b[' in line
|
||||||
or '\033[' in line
|
or '\033[' in line
|
||||||
@@ -114,59 +119,81 @@ class Output:
|
|||||||
|
|
||||||
return line
|
return line
|
||||||
|
|
||||||
def test(self):
|
def test(self, action):
|
||||||
self(''.join([
|
self(''.join([
|
||||||
self.colors['purplebold'],
|
self.colors['purplebold'],
|
||||||
'! TEST ',
|
'! TEST ',
|
||||||
self.colors['reset'],
|
self.colors['reset'],
|
||||||
self.colorized(),
|
self.colorized(action),
|
||||||
'\n',
|
'\n',
|
||||||
]))
|
]))
|
||||||
|
|
||||||
def clean(self):
|
def clean(self, action):
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self(''.join([
|
self(''.join([
|
||||||
self.colors['bluebold'],
|
self.colors['bluebold'],
|
||||||
'+ CLEAN ',
|
'+ CLEAN ',
|
||||||
self.colors['reset'],
|
self.colors['reset'],
|
||||||
self.colorized(),
|
self.colorized(action),
|
||||||
'\n',
|
'\n',
|
||||||
]))
|
]))
|
||||||
|
|
||||||
def start(self):
|
def start(self, action):
|
||||||
if self.debug is True or 'visit' in str(self.debug):
|
if self.debug is True or 'visit' in str(self.debug):
|
||||||
self(''.join([
|
self(''.join([
|
||||||
self.colors['orangebold'],
|
self.colors['orangebold'],
|
||||||
'⚠ START ',
|
'⚠ START ',
|
||||||
self.colors['reset'],
|
self.colors['reset'],
|
||||||
self.colorized(),
|
self.colorized(action),
|
||||||
'\n',
|
'\n',
|
||||||
]))
|
]))
|
||||||
|
|
||||||
def success(self):
|
def info(self, text):
|
||||||
|
if self.debug is True or 'visit' in str(self.debug):
|
||||||
|
self(''.join([
|
||||||
|
self.colors['cyanbold'],
|
||||||
|
'➤ INFO ',
|
||||||
|
self.colors['reset'],
|
||||||
|
text,
|
||||||
|
'\n',
|
||||||
|
]))
|
||||||
|
|
||||||
|
def skip(self, action):
|
||||||
|
if self.debug is True or 'visit' in str(self.debug):
|
||||||
|
self(''.join([
|
||||||
|
self.colors['yellowbold'],
|
||||||
|
'↪️ SKIP ',
|
||||||
|
self.colors['reset'],
|
||||||
|
self.colorized(action),
|
||||||
|
'\n',
|
||||||
|
]))
|
||||||
|
|
||||||
|
def success(self, action):
|
||||||
if self.debug is True or 'visit' in str(self.debug):
|
if self.debug is True or 'visit' in str(self.debug):
|
||||||
self(''.join([
|
self(''.join([
|
||||||
self.colors['greenbold'],
|
self.colors['greenbold'],
|
||||||
'✔ SUCCESS ',
|
'✔ SUCCESS ',
|
||||||
self.colors['reset'],
|
self.colors['reset'],
|
||||||
self.colorized(),
|
self.colorized(action),
|
||||||
'\n',
|
'\n',
|
||||||
]))
|
]))
|
||||||
|
|
||||||
def fail(self, exception=None):
|
def fail(self, action, exception=None):
|
||||||
if self.debug is True or 'visit' in str(self.debug):
|
if self.debug is True or 'visit' in str(self.debug):
|
||||||
self(''.join([
|
self(''.join([
|
||||||
self.colors['redbold'],
|
self.colors['redbold'],
|
||||||
'✘ FAIL ',
|
'✘ FAIL ',
|
||||||
self.colors['reset'],
|
self.colors['reset'],
|
||||||
self.colorized(),
|
self.colorized(action),
|
||||||
'\n',
|
'\n',
|
||||||
]))
|
]))
|
||||||
|
|
||||||
def results(self):
|
def results(self, action):
|
||||||
|
if len(action.results) < 2:
|
||||||
|
return
|
||||||
success = 0
|
success = 0
|
||||||
fail = 0
|
fail = 0
|
||||||
for result in self.subject.results:
|
for result in action.results:
|
||||||
if result.status == 'success':
|
if result.status == 'success':
|
||||||
success += 1
|
success += 1
|
||||||
if result.status == 'failure':
|
if result.status == 'failure':
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import cli2
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from shlax.targets.base import Target
|
||||||
|
from shlax.actions.parallel import Parallel
|
||||||
|
from shlax.proc import Proc
|
||||||
|
|
||||||
|
from .podman import Podman
|
||||||
|
|
||||||
|
|
||||||
|
class Pod:
|
||||||
|
"""Help text"""
|
||||||
|
def __init__(self, **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):
|
||||||
|
methods = [
|
||||||
|
getattr(container, method)
|
||||||
|
for name, container in self.containers.items()
|
||||||
|
if not names or name in names
|
||||||
|
]
|
||||||
|
await target(Parallel(*methods))
|
||||||
|
|
||||||
|
async def build(self, target, *names):
|
||||||
|
"""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)
|
||||||
|
|
||||||
|
async def down(self, target, *names):
|
||||||
|
"""Delete container images"""
|
||||||
|
await self._call(target, 'down', *names)
|
||||||
|
|
||||||
|
async def start(self, target, *names):
|
||||||
|
"""Start container images"""
|
||||||
|
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
|
||||||
+12
-9
@@ -32,21 +32,21 @@ class PrefixStreamProtocol(asyncio.subprocess.SubprocessStreamProtocol):
|
|||||||
make asynchronous output readable.
|
make asynchronous output readable.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, output, *args, **kwargs):
|
def __init__(self, proc, *args, **kwargs):
|
||||||
self.output = output
|
self.proc = proc
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def pipe_data_received(self, fd, data):
|
def pipe_data_received(self, fd, data):
|
||||||
if self.output.debug is True or 'out' in str(self.output.debug):
|
if self.proc.output.debug is True or 'out' in str(self.proc.output.debug):
|
||||||
if fd in (1, 2):
|
if fd in (1, 2):
|
||||||
self.output(data)
|
self.proc.output(data)
|
||||||
super().pipe_data_received(fd, data)
|
super().pipe_data_received(fd, data)
|
||||||
|
|
||||||
|
|
||||||
def protocol_factory(output):
|
def protocol_factory(proc):
|
||||||
def _p():
|
def _p():
|
||||||
return PrefixStreamProtocol(
|
return PrefixStreamProtocol(
|
||||||
output,
|
proc,
|
||||||
limit=asyncio.streams._DEFAULT_LIMIT,
|
limit=asyncio.streams._DEFAULT_LIMIT,
|
||||||
loop=asyncio.events.get_event_loop()
|
loop=asyncio.events.get_event_loop()
|
||||||
)
|
)
|
||||||
@@ -69,8 +69,11 @@ class Proc:
|
|||||||
"""
|
"""
|
||||||
test = False
|
test = False
|
||||||
|
|
||||||
def __init__(self, *args, prefix=None, raises=True, output=None):
|
def __init__(self, *args, prefix=None, raises=True, output=None, quiet=False):
|
||||||
self.output = output or Output()
|
if quiet:
|
||||||
|
self.output = Output(debug=False)
|
||||||
|
else:
|
||||||
|
self.output = output or Output()
|
||||||
self.cmd = ' '.join(args)
|
self.cmd = ' '.join(args)
|
||||||
self.args = args
|
self.args = args
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
@@ -112,7 +115,7 @@ class Proc:
|
|||||||
|
|
||||||
loop = asyncio.events.get_event_loop()
|
loop = asyncio.events.get_event_loop()
|
||||||
transport, protocol = await loop.subprocess_exec(
|
transport, protocol = await loop.subprocess_exec(
|
||||||
protocol_factory(self.output), *self.args)
|
protocol_factory(self), *self.args)
|
||||||
self.proc = asyncio.subprocess.Process(transport, protocol, loop)
|
self.proc = asyncio.subprocess.Process(transport, protocol, loop)
|
||||||
self.called = True
|
self.called = True
|
||||||
|
|
||||||
|
|||||||
@@ -3,5 +3,16 @@ from .targets.buildah import Buildah
|
|||||||
from .targets.localhost import Localhost
|
from .targets.localhost import Localhost
|
||||||
from .targets.stub import Stub
|
from .targets.stub import Stub
|
||||||
|
|
||||||
|
from .actions.copy import Copy
|
||||||
from .actions.packages import Packages
|
from .actions.packages import Packages
|
||||||
from .actions.run import Run
|
from .actions.run import Run
|
||||||
|
from .actions.pip import Pip
|
||||||
|
from .actions.parallel import Parallel
|
||||||
|
from .actions.user import User
|
||||||
|
|
||||||
|
from .cli import Command, Group
|
||||||
|
|
||||||
|
from .container import Container
|
||||||
|
from .pod import Pod
|
||||||
|
|
||||||
|
from os import getenv, environ
|
||||||
|
|||||||
+151
-30
@@ -1,27 +1,44 @@
|
|||||||
|
import asyncio
|
||||||
import copy
|
import copy
|
||||||
|
from pathlib import Path
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
from ..output import Output
|
from ..output import Output
|
||||||
from ..proc import Proc
|
from ..proc import Proc, ProcFailure
|
||||||
from ..result import Result, Results
|
from ..result import Result, Results
|
||||||
|
|
||||||
|
|
||||||
class Target:
|
class Target:
|
||||||
def __init__(self, *actions, **options):
|
isguest = False
|
||||||
|
|
||||||
|
def __init__(self, *actions, root=None):
|
||||||
self.actions = actions
|
self.actions = actions
|
||||||
self.options = options
|
|
||||||
self.results = []
|
self.results = []
|
||||||
self.output = Output(self, **self.options)
|
self.output = Output()
|
||||||
self.parent = None
|
self.parent = None
|
||||||
|
self.root = root or ''
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return 'localhost'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parent(self):
|
||||||
|
return self._parent or Target()
|
||||||
|
|
||||||
|
@parent.setter
|
||||||
|
def parent(self, value):
|
||||||
|
self._parent = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def caller(self):
|
def caller(self):
|
||||||
"""Traverse parents and return the top-levels Target."""
|
"""Traverse parents and return the top-levels Target."""
|
||||||
if not self.parent:
|
if not self._parent:
|
||||||
return self
|
return self
|
||||||
caller = self.parent
|
caller = self._parent
|
||||||
while caller.parent:
|
while caller._parent:
|
||||||
caller = caller.parent
|
caller = caller._parent
|
||||||
return caller
|
return caller
|
||||||
|
|
||||||
async def __call__(self, *actions, target=None):
|
async def __call__(self, *actions, target=None):
|
||||||
@@ -30,33 +47,53 @@ class Target:
|
|||||||
# the calling target
|
# the calling target
|
||||||
self.parent = target
|
self.parent = target
|
||||||
|
|
||||||
for action in actions or self.actions:
|
result = Result(self, self)
|
||||||
result = Result(self, action)
|
result.status = 'success'
|
||||||
|
|
||||||
self.output = Output(action, **self.options)
|
for action in actions or self.actions:
|
||||||
self.output.start()
|
if await self.action(action, reraise=bool(actions)):
|
||||||
try:
|
|
||||||
await action(target=self)
|
|
||||||
except Exception as e:
|
|
||||||
self.output.fail(e)
|
|
||||||
result.status = 'failure'
|
result.status = 'failure'
|
||||||
result.exception = e
|
break
|
||||||
if actions:
|
|
||||||
|
if getattr(self, 'clean', None):
|
||||||
|
self.output.clean(self)
|
||||||
|
await self.clean(self, result)
|
||||||
|
|
||||||
|
async def action(self, action, reraise=False):
|
||||||
|
result = Result(self, action)
|
||||||
|
self.output.start(action)
|
||||||
|
try:
|
||||||
|
await action(target=self)
|
||||||
|
except Exception as e:
|
||||||
|
self.output.fail(action, e)
|
||||||
|
result.status = 'failure'
|
||||||
|
result.exception = e
|
||||||
|
|
||||||
|
if not isinstance(e, ProcFailure):
|
||||||
|
# no need to reraise in case of command error
|
||||||
|
# because the command has been printed
|
||||||
|
|
||||||
|
if reraise:
|
||||||
# nested call, re-raise
|
# nested call, re-raise
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
break
|
import traceback
|
||||||
else:
|
traceback.print_exception(type(e), e, sys.exc_info()[2])
|
||||||
self.output.success()
|
|
||||||
result.status = 'success'
|
|
||||||
finally:
|
|
||||||
self.caller.results.append(result)
|
|
||||||
|
|
||||||
clean = getattr(action, 'clean', None)
|
return True # because it failed
|
||||||
if clean:
|
else:
|
||||||
action.result = result
|
if getattr(action, 'skipped', False):
|
||||||
self.output.clean()
|
self.output.skip(action)
|
||||||
await clean(self)
|
else:
|
||||||
|
self.output.success(action)
|
||||||
|
result.status = 'success'
|
||||||
|
finally:
|
||||||
|
self.caller.results.append(result)
|
||||||
|
|
||||||
|
clean = getattr(action, 'clean', None)
|
||||||
|
if clean:
|
||||||
|
self.output.clean(action)
|
||||||
|
await clean(self, result)
|
||||||
|
|
||||||
async def rexec(self, *args, **kwargs):
|
async def rexec(self, *args, **kwargs):
|
||||||
kwargs['user'] = 'root'
|
kwargs['user'] = 'root'
|
||||||
@@ -76,5 +113,89 @@ class Target:
|
|||||||
result.append(match.group(1))
|
result.append(match.group(1))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def shargs(self, *args, **kwargs):
|
||||||
|
user = kwargs.pop('user', None)
|
||||||
|
args = [str(arg) for arg in args if args is not None]
|
||||||
|
|
||||||
|
if args and ' ' in args[0]:
|
||||||
|
if len(args) == 1:
|
||||||
|
args = ['sh', '-euc', args[0]]
|
||||||
|
else:
|
||||||
|
args = ['sh', '-euc'] + list(args)
|
||||||
|
|
||||||
|
if user == 'root':
|
||||||
|
args = ['sudo'] + args
|
||||||
|
elif user:
|
||||||
|
args = ['sudo', '-u', user] + args
|
||||||
|
|
||||||
|
return args, kwargs
|
||||||
|
|
||||||
|
if self.parent:
|
||||||
|
return self.parent.shargs(*args, **kwargs)
|
||||||
|
else:
|
||||||
|
return args, kwargs
|
||||||
|
|
||||||
async def exec(self, *args, **kwargs):
|
async def exec(self, *args, **kwargs):
|
||||||
raise NotImplemented()
|
kwargs['output'] = self.output
|
||||||
|
args, kwargs = self.shargs(*args, **kwargs)
|
||||||
|
proc = await Proc(*args, **kwargs)()
|
||||||
|
if kwargs.get('wait', True):
|
||||||
|
await proc.wait()
|
||||||
|
return proc
|
||||||
|
|
||||||
|
@property
|
||||||
|
def root(self):
|
||||||
|
return self._root
|
||||||
|
|
||||||
|
@root.setter
|
||||||
|
def root(self, value):
|
||||||
|
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):
|
||||||
|
if not self.root:
|
||||||
|
return path
|
||||||
|
if str(path).startswith('/'):
|
||||||
|
path = str(path)[1:]
|
||||||
|
return str(self.root / path)
|
||||||
|
|
||||||
|
async def mkdir(self, *paths):
|
||||||
|
if '_mkdir' not in self.__dict__:
|
||||||
|
self._mkdir = []
|
||||||
|
|
||||||
|
make = [str(path) for path in paths if str(path) not in self._mkdir]
|
||||||
|
if make:
|
||||||
|
await self.exec('mkdir', '-p', *make)
|
||||||
|
self._mkdir += make
|
||||||
|
|
||||||
|
async def copy(self, *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
|
||||||
|
|||||||
+167
-38
@@ -1,3 +1,7 @@
|
|||||||
|
import asyncio
|
||||||
|
import copy
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -9,26 +13,21 @@ from ..proc import Proc
|
|||||||
|
|
||||||
|
|
||||||
class Buildah(Target):
|
class Buildah(Target):
|
||||||
def __init__(self,
|
"""Build container image with buildah"""
|
||||||
*actions,
|
isguest = True
|
||||||
base=None, commit=None,
|
|
||||||
cmd=None,
|
def __init__(self, *actions, base=None, commit=None):
|
||||||
**options):
|
|
||||||
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
|
||||||
|
|
||||||
self.ctr = None
|
self.ctr = None
|
||||||
self.mnt = 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()
|
||||||
|
|
||||||
super().__init__(*actions, **options)
|
super().__init__(*actions)
|
||||||
|
|
||||||
def is_runnable(self):
|
def is_runnable(self):
|
||||||
return Proc.test or os.getuid() == 0
|
return Proc.test or os.getuid() == 0
|
||||||
@@ -36,36 +35,101 @@ class Buildah(Target):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
if not self.is_runnable():
|
if not self.is_runnable():
|
||||||
return 'Replacing with: buildah unshare ' + ' '.join(sys.argv)
|
return 'Replacing with: buildah unshare ' + ' '.join(sys.argv)
|
||||||
return 'Buildah image builder'
|
return f'Buildah({self.image})'
|
||||||
|
|
||||||
|
async def __call__(self, *actions, target=None, push: str=False):
|
||||||
|
if target:
|
||||||
|
self.parent = target
|
||||||
|
|
||||||
|
self.push = push
|
||||||
|
|
||||||
async def __call__(self, *actions, target=None):
|
|
||||||
self.parent = target
|
|
||||||
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.image.layers.ls(self)
|
||||||
|
keep = await self.cache_setup(self.image.layers, *actions)
|
||||||
|
keepnames = [*map(lambda x: 'localhost/' + str(x), keep)]
|
||||||
|
self.invalidate = [name for name in self.image.layers if name not in keepnames]
|
||||||
|
if self.invalidate:
|
||||||
|
self.output.info('Invalidating old layers')
|
||||||
|
await self.image.layers.rm(self.parent, self.invalidate)
|
||||||
|
|
||||||
|
if actions:
|
||||||
|
actions = actions[len(keep):]
|
||||||
|
else:
|
||||||
|
self.actions = self.actions[len(keep):]
|
||||||
|
|
||||||
self.ctr = (await self.parent.exec('buildah', 'from', self.base)).out
|
self.ctr = (await self.parent.exec('buildah', 'from', self.base)).out
|
||||||
self.mnt = Path((await self.parent.exec('buildah', 'mount', self.ctr)).out)
|
self.root = Path((await self.parent.exec('buildah', 'mount', self.ctr)).out)
|
||||||
await super().__call__()
|
|
||||||
|
|
||||||
async def clean(self, target):
|
return await super().__call__(*actions)
|
||||||
for src, dst in self.mounts.items():
|
|
||||||
await self.parent.exec('umount', self.mnt / str(dst)[1:])
|
|
||||||
|
|
||||||
if self.result.status == 'success':
|
async def cache_setup(self, layers, *actions):
|
||||||
await self.commit()
|
keep = []
|
||||||
if os.getenv('BUILDAH_PUSH'):
|
self.image_previous = Image(self.base)
|
||||||
await self.image.push(target)
|
for action in actions or self.actions:
|
||||||
|
action_image = await self.action_image(action)
|
||||||
|
name = 'localhost/' + str(action_image)
|
||||||
|
if name in layers:
|
||||||
|
self.base = self.image_previous = action_image
|
||||||
|
keep.append(action_image)
|
||||||
|
self.output.skip(
|
||||||
|
f'Found layer for {action}: {action_image.tags[0]}'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return keep
|
||||||
|
|
||||||
if self.mnt is not None:
|
async def action_image(self, action):
|
||||||
|
prefix = str(self.image_previous)
|
||||||
|
for tag in self.image_previous.tags:
|
||||||
|
if tag.startswith('layer-'):
|
||||||
|
prefix = tag
|
||||||
|
break
|
||||||
|
if hasattr(action, 'cachekey'):
|
||||||
|
action_key = action.cachekey()
|
||||||
|
if asyncio.iscoroutine(action_key):
|
||||||
|
action_key = str(await action_key)
|
||||||
|
else:
|
||||||
|
action_key = str(action)
|
||||||
|
key = prefix + action_key
|
||||||
|
sha1 = hashlib.sha1(key.encode('ascii'))
|
||||||
|
action_image = copy.deepcopy(self.image)
|
||||||
|
action_image.tags = ['layer-' + sha1.hexdigest()]
|
||||||
|
return action_image
|
||||||
|
|
||||||
|
async def action(self, action, reraise=False):
|
||||||
|
stop = await super().action(action, reraise)
|
||||||
|
if not stop:
|
||||||
|
action_image = await self.action_image(action)
|
||||||
|
self.output.info(f'Commiting {action_image} for {action}')
|
||||||
|
await self.parent.exec(
|
||||||
|
'buildah',
|
||||||
|
'commit',
|
||||||
|
self.ctr,
|
||||||
|
action_image,
|
||||||
|
)
|
||||||
|
self.image_previous = action_image
|
||||||
|
return stop
|
||||||
|
|
||||||
|
async def clean(self, target, result):
|
||||||
|
if self.ctr is not None:
|
||||||
|
for src, dst in self.mounts.items():
|
||||||
|
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)
|
||||||
|
|
||||||
|
if result.status == 'success' and self.ctr:
|
||||||
|
await self.commit()
|
||||||
|
if self.push:
|
||||||
|
await self.image.push(target, self.push)
|
||||||
|
|
||||||
if self.ctr is not None:
|
if self.ctr is not None:
|
||||||
await self.parent.exec('buildah', 'rm', self.ctr)
|
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.mnt / str(dst)[1:]
|
target = self.root / str(dst)[1:]
|
||||||
await self.parent.exec(f'mkdir -p {src} {target}')
|
await self.parent.exec(f'mkdir -p {src} {target}')
|
||||||
await self.parent.exec(f'mount -o bind {src} {target}')
|
await self.parent.exec(f'mount -o bind {src} {target}')
|
||||||
self.mounts[src] = dst
|
self.mounts[src] = dst
|
||||||
@@ -79,24 +143,89 @@ class Buildah(Target):
|
|||||||
return await self.parent.exec(*_args, **kwargs)
|
return await self.parent.exec(*_args, **kwargs)
|
||||||
|
|
||||||
async def commit(self):
|
async def commit(self):
|
||||||
if not self.image:
|
await self.parent.exec(
|
||||||
return
|
|
||||||
|
|
||||||
for key, value in self.config.items():
|
|
||||||
await self.parent.exec(f'buildah config --{key} "{value}" {self.ctr}')
|
|
||||||
|
|
||||||
self.sha = (await self.exec(
|
|
||||||
'buildah',
|
'buildah',
|
||||||
'commit',
|
'commit',
|
||||||
'--format=' + self.image.format,
|
f'--format={self.image.format}',
|
||||||
self.ctr,
|
self.ctr,
|
||||||
buildah=False,
|
f'{self.image.repository}:final',
|
||||||
)).out
|
)
|
||||||
|
if self.image.backend == 'docker':
|
||||||
|
await self.parent.exec(
|
||||||
|
'buildah',
|
||||||
|
'push',
|
||||||
|
f'{self.image.repository}:final',
|
||||||
|
f'docker-daemon:{self.image.repository}:latest'
|
||||||
|
)
|
||||||
|
|
||||||
|
ENV_TAGS = (
|
||||||
|
# gitlab
|
||||||
|
'CI_COMMIT_SHORT_SHA',
|
||||||
|
'CI_COMMIT_REF_NAME',
|
||||||
|
'CI_COMMIT_TAG',
|
||||||
|
# CircleCI
|
||||||
|
'CIRCLE_SHA1',
|
||||||
|
'CIRCLE_TAG',
|
||||||
|
'CIRCLE_BRANCH',
|
||||||
|
# contributions welcome here
|
||||||
|
)
|
||||||
|
|
||||||
|
# figure tags from CI vars
|
||||||
|
for name in ENV_TAGS:
|
||||||
|
value = os.getenv(name)
|
||||||
|
if value:
|
||||||
|
self.image.tags.append(value)
|
||||||
|
|
||||||
if self.image.tags:
|
if self.image.tags:
|
||||||
tags = [f'{self.image.repository}:{tag}' for tag in self.image.tags]
|
tags = [f'{self.image.repository}:{tag}' for tag in self.image.tags]
|
||||||
else:
|
else:
|
||||||
tags = [self.image.repository]
|
tags = [self.image.repository]
|
||||||
|
|
||||||
for tag in tags:
|
await self.parent.exec('buildah', 'tag', self.image.repository + ':final', *tags)
|
||||||
await self.parent.exec('buildah', 'tag', self.sha, tag)
|
|
||||||
|
async def mkdir(self, *paths):
|
||||||
|
return await self.parent.mkdir(*[self.path(path) for path in paths])
|
||||||
|
|
||||||
|
async def copy(self, *args):
|
||||||
|
return await self.parent.exec('buildah', 'copy', self.ctr, *args)
|
||||||
|
|
||||||
|
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)
|
||||||
+13
-5
@@ -1,12 +1,20 @@
|
|||||||
#!/usr/bin/env shlax
|
#!/usr/bin/env python
|
||||||
"""
|
"""
|
||||||
Shlaxfile for shlax itself.
|
Shlaxfile for shlax itself.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from shlax.shortcuts import *
|
from shlax.shortcuts import *
|
||||||
|
|
||||||
build = Buildah(
|
shlax = Container(
|
||||||
Run('echo hi'),
|
build=Buildah(
|
||||||
Packages('python38'),
|
Packages('python38', 'buildah', 'unzip', 'findutils', upgrade=False),
|
||||||
base='quay.io/podman/stable',
|
Copy('setup.py', 'shlax', '/app'),
|
||||||
|
Pip('/app[cli]'),
|
||||||
|
base='quay.io/buildah/stable',
|
||||||
|
commit='docker://docker.io/yourlabs/shlax',
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print(Group(doc=__doc__).load(shlax).entry_point())
|
||||||
|
|||||||
Executable
+18
@@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
|
Shlaxfile for shlax itself.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from shlax.shortcuts import *
|
||||||
|
|
||||||
|
shlax = Container(
|
||||||
|
build=Buildah(
|
||||||
|
Packages('prout', upgrade=False),
|
||||||
|
base='alpine',
|
||||||
|
commit='shlaxfail',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print(Group(doc=__doc__).load(shlax).entry_point())
|
||||||
Executable
+17
@@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
|
Shlaxfile for shlax itself.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from shlax.shortcuts import *
|
||||||
|
|
||||||
|
shlax = Container(
|
||||||
|
build=Buildah(
|
||||||
|
base='alpine',
|
||||||
|
commit='shlaxsuccess',
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print(Group(doc=__doc__).load(shlax).entry_point())
|
||||||
@@ -25,9 +25,3 @@ def test_args(arg, expected):
|
|||||||
im = Image(arg)
|
im = Image(arg)
|
||||||
for k, v in expected.items():
|
for k, v in expected.items():
|
||||||
assert getattr(im, k) == v
|
assert getattr(im, k) == v
|
||||||
|
|
||||||
def test_args_env():
|
|
||||||
os.environ['IMAGE_TEST_ARGS_ENV'] = 'foo'
|
|
||||||
Image.ENV_TAGS = ['IMAGE_TEST_ARGS_ENV']
|
|
||||||
im = Image('re/po:x,y')
|
|
||||||
assert im.tags == ['x', 'y', 'foo']
|
|
||||||
|
|||||||
+32
-1
@@ -68,10 +68,41 @@ async def test_parallel():
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_function():
|
async def test_function():
|
||||||
async def hello(target):
|
async def hello(target):
|
||||||
target.exec('hello')
|
await target.exec('hello')
|
||||||
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