kupferbootstrap/wrapper.py

114 lines
3.5 KiB
Python
Raw Normal View History

2021-08-04 18:36:37 +02:00
import atexit
import os
import subprocess
import sys
import uuid
import click
import logging
from config import config, dump_file as dump_config_file
DOCKER_PATHS = {
'chroots': '/chroot',
'jumpdrive': '/var/cache/jumpdrive',
'pacman': '/var/cache/pacman/pkg',
}
2021-08-04 18:36:37 +02:00
def wrap_docker():
def _docker_volumes(volume_mappings: dict[str, str]) -> list[str]:
result = []
for source, destination in volume_mappings.items():
result += ['-v', f'{source}:{destination}:z']
return result
2021-08-04 18:36:37 +02:00
script_path = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(script_path, 'version.txt')) as version_file:
version = version_file.read().replace('\n', '')
2021-08-08 18:16:39 +02:00
tag = f'registry.gitlab.com/kupfer/kupferbootstrap:{version}'
2021-08-04 18:36:37 +02:00
if version == 'dev':
logging.info(f'Building docker image "{tag}"')
cmd = [
'docker',
'build',
'.',
'-t',
tag,
] + (['-q'] if not config.runtime['verbose'] else [])
result = subprocess.run(cmd, cwd=script_path, capture_output=True)
2021-08-04 18:36:37 +02:00
if result.returncode != 0:
logging.fatal('Failed to build docker image:\n' + result.stderr.decode())
2021-08-04 18:36:37 +02:00
exit(1)
else:
# Check if the image for the version already exists
2021-08-08 18:32:42 +02:00
result = subprocess.run(
[
'docker',
'images',
'-q',
tag,
],
capture_output=True,
)
2021-08-04 18:36:37 +02:00
if result.stdout == b'':
logging.info(f'Pulling kupferbootstrap docker image version \'{version}\'')
2021-08-08 18:32:42 +02:00
subprocess.run([
'docker',
'pull',
tag,
])
container_name = f'kupferbootstrap-{str(uuid.uuid4())}'
wrapped_config = f'/tmp/kupfer/{container_name}_wrapped.toml'
2021-08-04 18:36:37 +02:00
def at_exit():
2021-08-08 18:32:42 +02:00
subprocess.run(
[
'docker',
'kill',
container_name,
2021-08-08 18:32:42 +02:00
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
os.remove(wrapped_config)
2021-08-08 18:32:42 +02:00
2021-08-04 18:36:37 +02:00
atexit.register(at_exit)
dump_config_file(file_path=wrapped_config, config=(config.file | {'paths': DOCKER_PATHS}))
volumes = {
'/dev': '/dev',
os.getcwd(): '/src',
wrapped_config: '/root/.config/kupfer/kupferbootstrap.toml',
}
volumes |= dict({(config.file['paths'][vol_name], vol_dest) for vol_name, vol_dest in DOCKER_PATHS.items()})
if os.getenv('KUPFERBOOTSTRAP_PREBUILTS'):
volumes |= {os.getenv("KUPFERBOOTSTRAP_PREBUILTS"): '/prebuilts'}
cmd = [
2021-08-08 18:32:42 +02:00
'docker',
'run',
'--name',
container_name,
2021-08-08 18:32:42 +02:00
'--rm',
'--interactive',
'--tty',
'--privileged',
] + _docker_volumes(volumes) + [tag, 'kupferbootstrap'] + sys.argv[1:]
logging.debug('Wrapping in docker:' + repr(cmd))
result = subprocess.run(cmd)
exit(result.returncode)
def enforce_wrap(no_wrapper=False):
if os.getenv('KUPFERBOOTSTRAP_DOCKER') != '1' and not no_wrapper:
wrap_docker()
nowrapper_option = click.option(
'--no-wrapper',
'no_wrapper',
is_flag=True,
default=False,
help='Disable the docker wrapper. Defaults to autodetection.',
)