debian-forge/osbuild
Lars Karlitski 3cca6ccb8a osbuild: factor running systemd-nspawn out of the main loop
Also rename BuildContainer to BuildRoot. It's not a container.
2019-06-12 20:33:00 +02:00

142 lines
5.1 KiB
Python
Executable file

#!/usr/bin/python3
import argparse
import json
import os
import subprocess
import sys
import tempfile
RESET = "\033[0m"
BOLD = "\033[1m"
RED = "\033[31m"
class BuildRoot:
def __init__(self, path=os.getcwd()):
self.buildroot = tempfile.mkdtemp(prefix="osbuild-buildroot-", dir=path)
self.buildroot_mounted = False
self.tree = tempfile.mkdtemp(prefix="osbuild-tree-", dir=path)
self.tree_mounted = False
try:
subprocess.run(["mount", "-o", "bind,ro", "/", self.buildroot], check=True)
self.tree_mounted = True
subprocess.run(["mount", "-t", "tmpfs", "tmpfs", self.tree], check=True)
self.buildroot_mounted = True
except subprocess.CalledProcessError:
self.unmount()
raise
# systemd-nspawn silently removes some characters when choosing a
# machine name from the directory name. The only one relevant for
# us is '_', because all other characters used by
# TemporaryDirectory() are allowed. Replace it with 'L's
# (TemporaryDirectory() only uses lower-case characters)
self.machine_name = os.path.basename(self.buildroot).replace("_", "L")
def unmount(self):
if self.tree:
if self.tree_mounted:
subprocess.run(["umount", "--lazy", self.tree], check=True)
os.rmdir(self.tree)
self.tree = None
if self.buildroot:
if self.buildroot_mounted:
subprocess.run(["umount", "--lazy", self.buildroot], check=True)
os.rmdir(self.buildroot)
self.buildroot = None
def run(self, argv, binds=[], readonly_binds=[], *args, **kwargs):
return subprocess.run([
"systemd-nspawn",
"--as-pid2",
"--link-journal=no",
"--volatile=yes",
f"--machine={self.machine_name}",
f"--directory={self.buildroot}",
f"--bind={self.tree}:/tmp/tree",
*[f"--bind={src}:{dest}" for src, dest in binds],
*[f"--bind-ro={src}:{dest}" for src, dest in readonly_binds]
] + argv, *args, **kwargs)
def __del__(self):
self.unmount()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_tb):
self.unmount()
def main(pipeline_path, input_dir, output_dir, sit):
if output_dir and len(os.listdir(output_dir)) != 0:
print()
print(f"{RESET}{BOLD}{RED}Output directory {output_dir} is not empty{RESET}")
return 1
with open(pipeline_path) as f:
pipeline = json.load(f)
with BuildRoot() as buildroot:
for i, stage in enumerate(pipeline["stages"], start=1):
name = stage["name"]
options = stage.get("options", {})
options["tree"] = "/tmp/tree"
binds = [
(f"{os.getcwd()}/run-stage", "/tmp/run-stage"),
(f"{os.getcwd()}/stages/{name}", "/tmp/stage"),
("/etc/pki", "/etc/pki")
]
readonly_binds = []
if input_dir:
options["input_dir"] = "/tmp/input"
readonly_binds.append((input_dir, "/tmp/input"))
else:
options["input_dir"] = None
if output_dir:
options["output_dir"] = "/tmp/output"
binds.append(output_dir, "/tmp/output")
argv = ["/tmp/run-stage"]
if sit:
argv.append("--sit")
argv.append("/tmp/stage")
options_str = json.dumps(options, indent=2)
print()
print(f"{RESET}{BOLD}{i}. {name}{RESET} {options_str}")
print("Inspect with:")
print(f"\t# nsenter -a --wd=/root -t `machinectl show {buildroot.machine_name} -p Leader --value`")
print()
try:
buildroot.run(argv, binds=binds, readonly_binds=readonly_binds, input=options_str, encoding="utf-8", check=True)
except KeyboardInterrupt:
print()
print(f"{RESET}{BOLD}{RED}Aborted{RESET}")
return 130
except subprocess.CalledProcessError as error:
print()
print(f"{RESET}{BOLD}{RED}{name} failed with code {error.returncode}{RESET}")
return 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Build operating system images")
parser.add_argument("pipeline_path", metavar="PIPELINE",
help="json file containing the pipeline that should be built")
parser.add_argument("--input", dest="input_dir", metavar="DIRECTORY",
help="provide the contents of DIRECTORY to the first stage")
parser.add_argument("--output", dest="output_dir", metavar="DIRECTORY",
help="provide the empty DIRECTORY as output argument to the last stage")
parser.add_argument("--sit", action="store_true",
help="keep the build environment up when a stage failed")
args = parser.parse_args()
sys.exit(main(**vars(args)))