This commit is contained in:
jpic 2020-02-13 05:42:04 +01:00
parent 6e8b2ec2a7
commit 4366ee63f9
14 changed files with 90 additions and 190 deletions

View File

@ -7,10 +7,15 @@ build:
- .cache - .cache
key: cache key: cache
test: py-test:
stage: test stage: test
image: yourlabs/python image: yourlabs/python
script: pip install -e . && cd examples && podctl test -d simple script: pip install -e . && py.test -s
pod-test:
stage: test
image: yourlabs/python
script: pip install -e . && examples/simple && podctl test -d .
pypi: pypi:
stage: deploy stage: deploy

View File

@ -20,34 +20,5 @@ async def test_pod_story2(pod):
async def test_pod_story(pod): async def test_pod_story(pod):
await pod.script('down')() await pod.script('down')()
await pod.script('build')('ex')
await pod.script('up')() await pod.script('up')()
await pod.script('down')() await pod.script('down')()
async def aoeutest_podctl(host):
from podctl.console_script import console_script
console_script.options['debug'] = 'visit'
console_script.options['debug'] = True
from podctl.podfile import Podfile
pod = Podfile.factory(__file__).pod
from podctl.proc import Proc
#await Proc('podctl', 'down')()
#await Proc('podctl', 'build', 'ex')()
#await Proc('podctl', '-d=cmd,out,visit', 'up', 'ex')()
#assert host.podman('simple-ex').is_running
##import time; time.sleep(5)
#await Proc('podctl', 'down')()
#assert 'simple-ex' not in [c.name for c in host.podman.get_containers()]
#return
await pod.script('down')()
await pod.script('build')('ex')
await pod.script('up')('ex')
assert host.podman('ex').is_running
await pod.script('down')()
assert 'simple-ex' not in [c.name for c in host.podman.get_containers()]

View File

@ -4,9 +4,10 @@ import asyncio
import signal import signal
import shlex import shlex
import subprocess import subprocess
import sys
import textwrap import textwrap
from .proc import Proc from .proc import Proc, output
from .script import Script from .script import Script
@ -20,6 +21,8 @@ class Build(Script):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.mounts = dict() self.mounts = dict()
self.ctr = None
self.mnt = None
async def config(self, line): async def config(self, line):
"""Run buildah config.""" """Run buildah config."""
@ -56,7 +59,8 @@ class Build(Script):
async def umount(self): async def umount(self):
"""Unmount the buildah container with buildah unmount.""" """Unmount the buildah container with buildah unmount."""
await self.exec(f'buildah unmount {self.ctr}') if self.ctr:
await self.exec(f'buildah unmount {self.ctr}')
async def paths(self): async def paths(self):
"""Return the list of $PATH directories""" """Return the list of $PATH directories"""
@ -79,3 +83,40 @@ class Build(Script):
def __repr__(self): def __repr__(self):
return f'Build' return f'Build'
async def run(self, *args, **kwargs):
if os.getuid() == 0:
return await super().run(*args, **kwargs)
from podctl.console_script import console_script
# restart under buildah unshare environment !
argv = [
'buildah', 'unshare',
sys.argv[0], # current podctl location
]
if console_script.options.get('debug') is True:
argv.append('-d')
elif isinstance(console_script.options.get('debug'), str):
argv.append('-d=' + console_script.options.get('debug'))
argv += [
type(self).__name__.lower(), # script name ?
]
output(' '.join(argv), 'EXECUTION')
proc = await asyncio.create_subprocess_shell(
shlex.join(argv),
stderr=sys.stderr,
stdin=sys.stdin,
stdout=sys.stdout,
)
await proc.communicate()
console_script.exit_code = proc.returncode
return
pp = subprocess.Popen(
argv,
stderr=sys.stderr,
stdin=sys.stdin,
stdout=sys.stdout,
)
pp.communicate()
console_script.exit_code = pp.returncode

View File

@ -35,7 +35,19 @@ async def test(*args, **kwargs):
'\n\x1b[1;38;5;160;48;5;118m BUILD START \x1b[0m' '\n\x1b[1;38;5;160;48;5;118m BUILD START \x1b[0m'
+ ' ' + podfile.path + '\n' + ' ' + podfile.path + '\n'
) )
podfile.pod.script('build')()
old_exit_code = console_script.exit_code
console_script.exit_code = 0
try:
await podfile.pod.script('build')()
except Exception as e:
report.append(('build ' + candidate, False))
continue
if console_script.exit_code != 0:
report.append(('build ' + candidate, False))
continue
console_script.exit_code = old_exit_code
for name, test in podfile.tests.items(): for name, test in podfile.tests.items():
name = '::'.join([podfile.path, name]) name = '::'.join([podfile.path, name])

View File

@ -43,7 +43,6 @@ class Container(Visitable):
await script.exec('podman', 'inspect', self.container_name) await script.exec('podman', 'inspect', self.container_name)
except WrongResult as ee: except WrongResult as ee:
output('Container creating', self.name) output('Container creating', self.name)
breakpoint()
await script.exec( await script.exec(
'podman', 'run', '-d', '--name', self.container_name, 'podman', 'run', '-d', '--name', self.container_name,
self.image_name, self.image_name,

View File

@ -104,25 +104,6 @@ class Script:
sys.stdout.flush() sys.stdout.flush()
async def run(self, *args, **kwargs): async def run(self, *args, **kwargs):
if self.unshare and os.getuid() != 0:
from podctl.console_script import console_script
# restart under buildah unshare environment !
argv = [
'buildah', 'unshare',
sys.argv[0], # current podctl location
] + console_script.parser.argv + [
type(self).__name__.lower() # script name ?
] + list(args)
print('Executing', ' '.join(argv))
pp = subprocess.Popen(
argv,
stderr=sys.stderr,
stdin=sys.stdin,
stdout=sys.stdout,
)
pp.communicate()
return pp.returncode
for key, value in kwargs.items(): for key, value in kwargs.items():
setattr(self, key, value) setattr(self, key, value)

View File

@ -12,7 +12,8 @@ class Base:
async def clean_build(self, script): async def clean_build(self, script):
await script.umounts() await script.umounts()
await script.umount() await script.umount()
proc = await script.exec('buildah', 'rm', script.ctr, raises=False) if script.ctr:
proc = await script.exec('buildah', 'rm', script.ctr, raises=False)
def __repr__(self): def __repr__(self):
return f'Base({self.base})' return f'Base({self.base})'

View File

@ -20,20 +20,24 @@ class Commit:
self.repo = repo self.repo = repo
self.registry = registry or 'localhost' self.registry = registry or 'localhost'
self.push = push or os.getenv('CI') self.push = push or os.getenv('CI')
self.tags = tags or []
# figure out registry host # figure out registry host
if '/' in self.repo and not registry: if '/' in self.repo and not registry:
first = self.repo.split('/')[0] first = self.repo.split('/')[0]
if '.' in first or ':' in first: if '.' in first or ':' in first:
self.registry = self.repo.split('/')[0] self.registry = self.repo.split('/')[0]
self.repo = '/'.join(self.repo.split('/')[1:])
if ':' in self.repo and not tags:
self.tags = [self.repo.split(':')[1]]
self.repo = self.repo.split(':')[0]
# docker.io currently has issues with oci format # docker.io currently has issues with oci format
self.format = format or 'oci' self.format = format or 'oci'
if self.registry == 'docker.io': if self.registry == 'docker.io':
self.format = 'docker' self.format = 'docker'
self.tags = tags or []
# figure tags from CI vars # figure tags from CI vars
if not self.tags: if not self.tags:
for name in CI_VARS: for name in CI_VARS:

View File

@ -1,19 +0,0 @@
#/usr/bin/env bash
base="alpine"
repo="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
mounts=("${mounts[@]/$i}")
done
buildah unmount $ctr
trap - 0
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
echo "Copy.init_build"
echo "Copy.build"
buildah run --user root $ctr -- mkdir -p /app
cp -a /test $mnt/app

View File

@ -1,15 +0,0 @@
#/usr/bin/env bash
base="alpine"
repo="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
mounts=("${mounts[@]/$i}")
done
buildah unmount $ctr
trap - 0
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)

View File

@ -1,30 +0,0 @@
#/usr/bin/env bash
base="alpine"
repo="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
mounts=("${mounts[@]/$i}")
done
buildah unmount $ctr
trap - 0
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
echo "Packages.pre_build"
echo "Packages.build"
buildah run --user root $ctr -- mkdir -p /var/cache/apk
mkdir -p /test/apk
mount -o bind /test/apk $mnt/var/cache/apk
mounts=("$mnt/var/cache/apk" "${mounts[@]}")
buildah run $ctr -- ln -s /var/cache/apk /etc/apk/cache
old="$(find .cache/apk/ -name APKINDEX.* -mtime +3)"
if [ -n "$old" ] || ! ls .cache/apk/APKINDEX.*; then
buildah run --user root $ctr -- apk update
else
echo Cache recent enough, skipping index update.
fi
buildah run --user root $ctr -- apk upgrade
buildah run --user root $ctr -- apk add bash

View File

@ -1,27 +0,0 @@
#/usr/bin/env bash
base="alpine"
repo="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
mounts=("${mounts[@]/$i}")
done
buildah unmount $ctr
trap - 0
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
echo "Run.build"
buildah run $ctr -- foo
echo "Run.build"
buildah run --user root $ctr -- bar
echo "Run.build"
buildah run --user root $ctr -- bash -eux <<__EOF
bar > test
__EOF
echo "Run.build"
buildah run $ctr -- bash -eux <<__EOF
bar
__EOF

View File

@ -1,42 +0,0 @@
#/usr/bin/env bash
base="alpine"
repo="None"
mounts=()
umounts() {
for i in "${mounts[@]}"; do
umount $i
mounts=("${mounts[@]/$i}")
done
buildah unmount $ctr
trap - 0
}
trap umounts 0
ctr=$(buildah from $base)
mnt=$(buildah mount $ctr)
echo "User.init_build"
echo "User.init_build"
echo "Packages.pre_build"
echo "User.pre_build"
echo "Packages.build"
buildah run --user root $ctr -- mkdir -p /var/cache/apk
mkdir -p /test/apk
mount -o bind /test/apk $mnt/var/cache/apk
mounts=("$mnt/var/cache/apk" "${mounts[@]}")
buildah run $ctr -- ln -s /var/cache/apk /etc/apk/cache
old="$(find .cache/apk/ -name APKINDEX.* -mtime +3)"
if [ -n "$old" ] || ! ls .cache/apk/APKINDEX.*; then
buildah run --user root $ctr -- apk update
else
echo Cache recent enough, skipping index update.
fi
buildah run --user root $ctr -- apk upgrade
buildah run --user root $ctr -- apk add shadow
echo "User.build"
if buildah run $ctr -- id 1000; then
i=$(buildah run $ctr -- id -gn 1000)
buildah run $ctr -- usermod -d /app -l app $i
else
buildah run $ctr -- useradd -d /app -u 1000 app
fi
echo "User.post_build"
buildah config --user app $ctr

19
tests/test_commit.py Normal file
View File

@ -0,0 +1,19 @@
from podctl.visitors.commit import Commit
def test_name_parse():
commit = Commit('foo.ee/bar/test:y')
assert commit.registry == 'foo.ee'
assert commit.repo == 'bar/test'
assert commit.tags == ['y']
commit = Commit('foo.ee/bar/test')
assert commit.registry == 'foo.ee'
assert commit.repo == 'bar/test'
commit = Commit('bar/test')
assert commit.repo == 'bar/test'
commit = Commit('bar/test:y')
assert commit.repo == 'bar/test'
assert commit.tags == ['y']