Asyncio rewrite

This commit is contained in:
jpic
2020-02-03 05:25:04 +01:00
parent 7753175d6c
commit dc92b484e8
17 changed files with 188 additions and 137 deletions
+8
View File
@@ -3,3 +3,11 @@
class Base:
def __init__(self, base):
self.base = base
async def init_build(self, script):
ctr = await script.cmd('buildah from ' + self.base)
stdout, stderr = await ctr.communicate()
script.ctr = stdout.decode('utf8').strip()
mnt = await script.cmd('buildah mount ' + script.ctr)
stdout, stderr = await mnt.communicate()
script.mnt = stdout.decode('utf8').strip()
+2 -2
View File
@@ -2,6 +2,6 @@ class Cmd:
def __init__(self, cmd):
self.cmd = cmd
def build(self, script):
async def build(self, script):
# script._run() does not really support sudo code
script.run(self.cmd)
await script.run(self.cmd)
+4 -4
View File
@@ -42,8 +42,8 @@ class Commit:
# filter out tags which resolved to None
self.tags = [t for t in self.tags if t is not None]
def post_build(self, script):
script.append(f'''
async def post_build(self, script):
await script.append(f'''
umounts
buildah commit --format={self.format} $ctr {self.repo}
''')
@@ -53,7 +53,7 @@ class Commit:
if self.tags:
tags = ' '.join([f'{self.repo}:{tag}' for tag in self.tags])
script.append(f'buildah tag {self.repo} {tags}')
await script.append(f'buildah tag {self.repo} {tags}')
if self.push:
user = os.getenv('DOCKER_USER')
@@ -66,4 +66,4 @@ class Commit:
])
for tag in self.tags:
script.append(f'podman push {self.repo}:{tag}')
await script.append(f'podman push {self.repo}:{tag}')
+5 -5
View File
@@ -13,13 +13,13 @@ class Copy:
self.dst, self.mode = self.dst.split(':')
self.owner = script.variable('user')
def build(self, script):
script.run(f'sudo mkdir -p {self.dst}')
async def build(self, script):
await script.run(f'sudo mkdir -p {self.dst}')
for item in self.src:
script.append(f'cp -a {item} $mnt{self.dst}')
await script.append(f'cp -a {item} $mnt{self.dst}')
if self.mode:
script.run(f'sudo chmod {self.mode} $mnt{self.dst}')
await script.run(f'sudo chmod {self.mode} $mnt{self.dst}')
if self.owner:
script.run(f'sudo chown -R {self.owner} $mnt{self.dst}')
await script.run(f'sudo chown -R {self.owner} $mnt{self.dst}')
+2 -2
View File
@@ -3,5 +3,5 @@ class Mount:
self.src = src
self.dst = dst
def build(self, script):
script.mount(self.src, self.dst)
async def build(self, script):
await script.mount(self.src, self.dst)
+3 -3
View File
@@ -2,9 +2,9 @@ class Npm:
def __init__(self, install=None):
self.npm_install = install
def build(self, script):
script.run('sudo npm update -g npm')
script.run(f'''
async def build(self, script):
await script.run('sudo npm update -g npm')
await script.run(f'''
cd {self.npm_install}
npm install
''')
+18 -18
View File
@@ -37,7 +37,7 @@ class Packages:
else:
self.cache = os.path.join(os.getenv('HOME'), '.cache', self.mgr)
def pre_build(self, script):
async def pre_build(self, script):
base = script.container.variable('base')
if self.mgr:
self.cmds = self.mgrs[self.mgr]
@@ -54,23 +54,23 @@ class Packages:
if not self.mgr:
raise Exception('Packages does not yet support this distro')
def build(self, script):
async def build(self, script):
if not getattr(script.container, '_packages_upgraded', None):
# run pkgmgr_setup functions ie. apk_setup
getattr(self, self.mgr + '_setup')(script)
await getattr(self, self.mgr + '_setup')(script)
# first run on container means inject visitor packages
self.packages += script.container.packages
script.run(self.cmds['upgrade'])
await script.run(self.cmds['upgrade'])
script.container._packages_upgraded = True
script.run(' '.join([self.cmds['install']] + self.packages))
await script.run(' '.join([self.cmds['install']] + self.packages))
def apk_setup(self, script):
script.mount(self.cache, f'/var/cache/{self.mgr}')
async def apk_setup(self, script):
await script.mount(self.cache, f'/var/cache/{self.mgr}')
# special step to enable apk cache
script.run('ln -s /var/cache/apk /etc/apk/cache')
script.append(f'''
old="$(find .cache/apk/ -name APKINDEX.* -mtime +3)"
await script.run('ln -s /var/cache/apk /etc/apk/cache')
await script.append(f'''
old="$(find {self.cache} -name APKINDEX.* -mtime +3)"
if [ -n "$old" ] || ! ls .cache/apk/APKINDEX.*; then
{script._run(self.cmds['update'])}
else
@@ -78,18 +78,18 @@ class Packages:
fi
''')
def dnf_setup(self, script):
script.mount(self.cache, f'/var/cache/{self.mgr}')
script.run('sh -c "echo keepcache=True >> /etc/dnf/dnf.conf"')
async def dnf_setup(self, script):
await script.mount(self.cache, f'/var/cache/{self.mgr}')
await script.run('sh -c "echo keepcache=True >> /etc/dnf/dnf.conf"')
def apt_setup(self, script):
async def apt_setup(self, script):
cache = self.cache + '/$(source $mnt/etc/os-release; echo $VERSION_CODENAME)/' # noqa
script.run('sudo rm /etc/apt/apt.conf.d/docker-clean')
await script.run('sudo rm /etc/apt/apt.conf.d/docker-clean')
cache_archives = os.path.join(self.cache, 'archives')
script.mount(cache_archives, f'/var/cache/apt/archives')
await script.mount(cache_archives, f'/var/cache/apt/archives')
cache_lists = os.path.join(self.cache, 'lists')
script.mount(cache_lists, f'/var/lib/apt/lists')
script.append(f'''
await script.mount(cache_lists, f'/var/lib/apt/lists')
await script.append(f'''
old="$(find {cache_lists} -name lastup -mtime +3)"
if [ -n "$old" ] || ! ls {cache_lists}/lastup; then
until [ -z $(lsof /var/lib/dpkg/lock) ]; do sleep 1; done
+14 -19
View File
@@ -1,3 +1,4 @@
from glob import glob
import os
@@ -7,34 +8,28 @@ class Pip:
self.pip = pip
self.requirements = requirements
def build(self, script):
if self.pip:
script.append('_pip=' + self.pip)
else:
script.append(f'''
if {script._run("bash -c 'type pip3'")}; then
_pip=pip3
elif {script._run("bash -c 'type pip'")}; then
_pip=pip
elif {script._run("bash -c 'type pip2'")}; then
_pip=pip2
fi
''')
async def build(self, script):
for pip in ('pip3', 'pip', 'pip2'):
if script.which(pip):
self.pip = pip
break
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')
script.mount(cache, '/root/.cache/pip')
script.run('sudo $_pip install --upgrade pip')
await script.mount(cache, '/root/.cache/pip')
await script.run(f'sudo {self.pip} install --upgrade pip')
source = [p for p in self.pip_packages if p.startswith('/')]
if source:
script.run(
f'sudo $_pip install --upgrade --editable {" ".join(source)}'
await script.run(
f'sudo {self.pip} install --upgrade --editable {" ".join(source)}'
)
nonsource = [p for p in self.pip_packages if not p.startswith('/')]
if nonsource:
script.run(f'sudo $_pip install --upgrade {" ".join(source)}')
await script.run(f'sudo {self.pip} install --upgrade {" ".join(source)}')
if self.requirements:
script.run(f'sudo $_pip install --upgrade -r {self.requirements}')
await script.run(f'sudo {self.pip} install --upgrade -r {self.requirements}')
+2 -2
View File
@@ -2,6 +2,6 @@ class Run:
def __init__(self, *commands):
self.commands = commands
def build(self, script):
async def build(self, script):
for command in self.commands:
script.run(command)
await script.run(command)
+3 -3
View File
@@ -11,10 +11,10 @@ class Template:
self.lines = lines
self.variables = variables
def build(self, script):
async def build(self, script):
self.script = '\n'.join([
dedent(l).strip() for l in self.lines
]).format(**self.variables)
script.run(CMD.strip().format(**self.__dict__))
await script.run(CMD.strip().format(**self.__dict__))
if self.script.startswith('#!'):
script.run('sudo chmod +x ' + self.target)
await script.run('sudo chmod +x ' + self.target)
+5 -14
View File
@@ -3,6 +3,9 @@ from .packages import Packages
class User:
"""Secure the image with a user"""
packages = [
'shadow',
]
def __init__(self, username, uid, home):
self.username = username
@@ -10,20 +13,8 @@ class User:
self.home = home
self.user_created = False
def init_build(self, script):
"""Inject the Packages visitor if necessary."""
packages = script.container.visitor('packages')
if not packages:
index = script.container.visitors.index(self)
script.container.visitors.insert(index, Packages())
def pre_build(self, script):
"""Inject the shadow package for the usermod command"""
if script.container.variable('mgr') == 'apk':
script.container.variable('packages').append('shadow')
def build(self, script):
script.append(f'''
async def build(self, script):
await script.append(f'''
if {script._run('id ' + str(self.uid))}; then
i=$({script._run('id -gn ' + str(self.uid))})
{script._run('usermod -d ' + self.home + ' -l ' + self.username + ' $i')}