#!/usr/bin/env python3 """Build a deterministic self-contained Linux GuestOps release archive.""" from __future__ import annotations import argparse import gzip import io import json from pathlib import Path import re import shutil import subprocess import sys import tarfile import tempfile import xml.etree.ElementTree as ET from collections.abc import Callable, Sequence SEMVER = re.compile(r"[0-9]+\.[0-9]+\.[0-9]+") FULL_SHA = re.compile(r"[0-9a-f]{40}") LINUX_RUNTIME = "linux-x64" Runner = Callable[[Sequence[str], Path], None] def git(repository: Path, *arguments: str, binary: bool = False) -> bytes | str: result = subprocess.run( ["git", "-C", str(repository), *arguments], check=True, capture_output=True, text=not binary, ) return result.stdout if binary else result.stdout.strip() def committed_text(repository: Path, commit: str, path: str) -> str: return str(git(repository, "show", f"{commit}:{path}")) def versions(repository: Path, commit: str) -> tuple[str, str]: props = ET.fromstring(committed_text(repository, commit, "Directory.Build.props")) version_node = props.find(".//Version") if version_node is None or not version_node.text: raise ValueError("The committed Directory.Build.props has no Version element.") dotnet_version = version_node.text.strip() package = json.loads(committed_text(repository, commit, "web/package.json")) return dotnet_version, str(package.get("version", "")) def run_build(arguments: Sequence[str], working_directory: Path) -> None: command = list(arguments) executable = shutil.which(command[0]) if executable is None: raise FileNotFoundError(f"Required build tool is not installed: {command[0]}") command[0] = executable subprocess.run(command, cwd=working_directory, check=True) def extract_commit(repository: Path, commit: str, destination: Path) -> None: archive = bytes(git(repository, "archive", "--format=tar", commit, binary=True)) with tarfile.open(fileobj=io.BytesIO(archive), mode="r:") as source: source.extractall(destination, filter="data") def add_directory(package: tarfile.TarFile, name: str) -> None: entry = tarfile.TarInfo(name.rstrip("/") + "/") entry.type = tarfile.DIRTYPE entry.mode = 0o755 entry.uid = entry.gid = 0 entry.uname = entry.gname = "root" entry.mtime = 0 package.addfile(entry) def add_file(package: tarfile.TarFile, source: Path, name: str, executable: bool) -> None: content = source.read_bytes() entry = tarfile.TarInfo(name) entry.size = len(content) entry.mode = 0o755 if executable else 0o644 entry.uid = entry.gid = 0 entry.uname = entry.gname = "root" entry.mtime = 0 package.addfile(entry, io.BytesIO(content)) def release_bytes(release: Path, version: str) -> bytes: prefix = f"guestops_{version}" tar_bytes = io.BytesIO() with tarfile.open(fileobj=tar_bytes, mode="w", format=tarfile.PAX_FORMAT) as package: add_directory(package, prefix) for component in ("api", "worker"): root = release / component add_directory(package, f"{prefix}/{component}") for path in sorted(root.rglob("*"), key=lambda item: item.as_posix()): relative = path.relative_to(release).as_posix() target = f"{prefix}/{relative}" if path.is_dir(): add_directory(package, target) elif path.is_file(): add_file( package, path, target, relative in {"api/GuestOps.Api", "worker/GuestOps.Worker"}, ) compressed = io.BytesIO() with gzip.GzipFile(fileobj=compressed, mode="wb", filename="", mtime=0) as stream: stream.write(tar_bytes.getvalue()) return compressed.getvalue() def build_package( repository: Path, ref: str, output_directory: Path, runner: Runner = run_build, ) -> tuple[Path, dict[str, str]]: repository = repository.resolve() output_directory = output_directory.resolve() if not (repository / ".git").exists(): raise ValueError("--repository must be a Git working tree.") commit = str(git(repository, "rev-parse", "--verify", f"{ref}^{{commit}}")).lower() if FULL_SHA.fullmatch(commit) is None: raise ValueError("The selected ref did not resolve to a full Git commit SHA.") dotnet_version, web_version = versions(repository, commit) if dotnet_version != web_version: raise ValueError( f"Committed release versions differ: .NET={dotnet_version}, web={web_version}." ) if SEMVER.fullmatch(dotnet_version) is None: raise ValueError("The committed version must use MAJOR.MINOR.PATCH.") output = output_directory / f"guestops_{dotnet_version}.tar.gz" if output.exists(): raise ValueError("The output package already exists; published versions are not overwritten.") with tempfile.TemporaryDirectory(prefix="guestops-release-") as temporary: root = Path(temporary) source = root / "source" release = root / "release" source.mkdir() (release / "api").mkdir(parents=True) (release / "worker").mkdir(parents=True) extract_commit(repository, commit, source) runner(["npm", "ci", "--no-audit", "--no-fund"], source / "web") runner(["npm", "run", "build"], source / "web") common = [ "--configuration", "Release", "--runtime", LINUX_RUNTIME, "--self-contained", "true", "--no-restore", "-p:UseAppHost=true", "-p:ContinuousIntegrationBuild=true", "-p:Deterministic=true", f"-p:PathMap={source}=/src", ] runner( ["dotnet", "restore", "src/GuestOps.Api/GuestOps.Api.csproj", "--runtime", LINUX_RUNTIME], source, ) runner( ["dotnet", "publish", "src/GuestOps.Api/GuestOps.Api.csproj", *common, "--output", str(release / "api")], source, ) runner( ["dotnet", "restore", "src/GuestOps.Worker/GuestOps.Worker.csproj", "--runtime", LINUX_RUNTIME], source, ) runner( ["dotnet", "publish", "src/GuestOps.Worker/GuestOps.Worker.csproj", *common, "--output", str(release / "worker")], source, ) shutil.copytree(source / "web" / "dist", release / "api" / "wwwroot") for required in (release / "api" / "GuestOps.Api", release / "worker" / "GuestOps.Worker"): if not required.is_file(): raise ValueError(f"Expected native executable was not produced: {required.name}.") if not (release / "api" / "wwwroot" / "index.html").is_file(): raise ValueError("The frontend build did not produce index.html.") content = release_bytes(release, dotnet_version) output_directory.mkdir(parents=True, exist_ok=True) output.write_bytes(content) return output, {"commit": commit, "version": dotnet_version, "runtime": LINUX_RUNTIME} def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--ref", required=True, help="Exact tag, branch, or full commit to package.") parser.add_argument("--repository", type=Path, default=Path(__file__).resolve().parents[1]) parser.add_argument("--output-directory", required=True, type=Path) args = parser.parse_args() try: archive, details = build_package(args.repository, args.ref, args.output_directory) except (OSError, ValueError, ET.ParseError, json.JSONDecodeError, subprocess.SubprocessError) as error: print(f"Release packaging failed: {error}", file=sys.stderr) raise SystemExit(1) print(json.dumps({"archive": str(archive), **details}, indent=2)) if __name__ == "__main__": main()