|
|
@@ -0,0 +1,174 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+"""Build the self-contained hth Linux amd64 release archive."""
|
|
|
+
|
|
|
+from __future__ import annotations
|
|
|
+
|
|
|
+import argparse
|
|
|
+import gzip
|
|
|
+import hashlib
|
|
|
+import json
|
|
|
+import os
|
|
|
+import shutil
|
|
|
+import subprocess
|
|
|
+import tarfile
|
|
|
+import tempfile
|
|
|
+from datetime import datetime, timezone
|
|
|
+from pathlib import Path
|
|
|
+
|
|
|
+BINARIES = ("hthd", "hthctl", "hth-capture")
|
|
|
+EXECUTABLES = {"install.sh", "uninstall.sh", "selftest.sh", *BINARIES}
|
|
|
+
|
|
|
+
|
|
|
+def sha256(path: Path) -> str:
|
|
|
+ digest = hashlib.sha256()
|
|
|
+ with path.open("rb") as stream:
|
|
|
+ for chunk in iter(lambda: stream.read(1024 * 1024), b""):
|
|
|
+ digest.update(chunk)
|
|
|
+ return digest.hexdigest()
|
|
|
+
|
|
|
+
|
|
|
+def git(repo: Path, *args: str) -> str:
|
|
|
+ return subprocess.check_output(
|
|
|
+ ["git", *args], cwd=repo, text=True, stderr=subprocess.DEVNULL
|
|
|
+ ).strip()
|
|
|
+
|
|
|
+
|
|
|
+def copy_file(source: Path, target: Path, mode: int = 0o644) -> None:
|
|
|
+ if not source.is_file():
|
|
|
+ raise FileNotFoundError(source)
|
|
|
+ target.parent.mkdir(parents=True, exist_ok=True)
|
|
|
+ shutil.copyfile(source, target)
|
|
|
+ target.chmod(mode)
|
|
|
+
|
|
|
+
|
|
|
+def build_binaries(host_root: Path, binary_dir: Path, version: str) -> None:
|
|
|
+ binary_dir.mkdir(parents=True, exist_ok=True)
|
|
|
+ env = os.environ.copy()
|
|
|
+ env.update({"GOOS": "linux", "GOARCH": "amd64", "CGO_ENABLED": "0"})
|
|
|
+ for name in BINARIES:
|
|
|
+ subprocess.run(
|
|
|
+ [
|
|
|
+ "go",
|
|
|
+ "build",
|
|
|
+ "-trimpath",
|
|
|
+ "-ldflags",
|
|
|
+ "-s -w -X hth/internal/app.APIVersion=hth.api.v1",
|
|
|
+ "-o",
|
|
|
+ str(binary_dir / name),
|
|
|
+ f"./cmd/{name}",
|
|
|
+ ],
|
|
|
+ cwd=host_root,
|
|
|
+ env=env,
|
|
|
+ check=True,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def write_inner_sums(stage: Path) -> None:
|
|
|
+ names = sorted(
|
|
|
+ path.relative_to(stage).as_posix()
|
|
|
+ for path in stage.rglob("*")
|
|
|
+ if path.is_file() and path.name != "SHA256SUMS"
|
|
|
+ )
|
|
|
+ lines = [f"{sha256(stage / name)} {name}" for name in names]
|
|
|
+ (stage / "SHA256SUMS").write_text("\n".join(lines) + "\n", encoding="ascii")
|
|
|
+
|
|
|
+
|
|
|
+def add_tree(tar: tarfile.TarFile, stage: Path, top: str) -> None:
|
|
|
+ for path in sorted(stage.rglob("*"), key=lambda item: item.relative_to(stage).as_posix()):
|
|
|
+ rel = path.relative_to(stage).as_posix()
|
|
|
+ info = tar.gettarinfo(str(path), arcname=f"{top}/{rel}")
|
|
|
+ info.uid = info.gid = 0
|
|
|
+ info.uname = info.gname = "root"
|
|
|
+ info.mtime = 0
|
|
|
+ if path.name in EXECUTABLES:
|
|
|
+ info.mode = 0o755
|
|
|
+ elif path.is_file():
|
|
|
+ info.mode = 0o644
|
|
|
+ if path.is_file():
|
|
|
+ with path.open("rb") as stream:
|
|
|
+ tar.addfile(info, stream)
|
|
|
+ else:
|
|
|
+ tar.addfile(info)
|
|
|
+
|
|
|
+
|
|
|
+def create_archive(stage: Path, archive: Path, top: str) -> None:
|
|
|
+ archive.parent.mkdir(parents=True, exist_ok=True)
|
|
|
+ with archive.open("wb") as raw:
|
|
|
+ with gzip.GzipFile(filename="", mode="wb", fileobj=raw, mtime=0) as zipped:
|
|
|
+ with tarfile.open(fileobj=zipped, mode="w") as tar:
|
|
|
+ add_tree(tar, stage, top)
|
|
|
+
|
|
|
+
|
|
|
+def main() -> int:
|
|
|
+ parser = argparse.ArgumentParser()
|
|
|
+ parser.add_argument("--version", required=True, help="Release version, for example v0.1.0")
|
|
|
+ parser.add_argument("--output-dir", type=Path, required=True)
|
|
|
+ parser.add_argument("--binary-dir", type=Path, help="Use prebuilt Linux amd64 binaries")
|
|
|
+ parser.add_argument("--source-commit", help="Override commit only for detached release staging")
|
|
|
+ args = parser.parse_args()
|
|
|
+
|
|
|
+ host_root = Path(__file__).resolve().parents[1]
|
|
|
+ repo_root = host_root.parent
|
|
|
+ version = args.version
|
|
|
+ if not version.startswith("v") or any(ch.isspace() for ch in version):
|
|
|
+ parser.error("--version must start with v and contain no whitespace")
|
|
|
+ source_commit = args.source_commit or git(repo_root, "rev-parse", "HEAD")
|
|
|
+ project = repo_root / "projects" / "prod2u-door-temp" / "project.json"
|
|
|
+ profile = host_root / "profiles" / "prod2u-door-temp" / "profile.json"
|
|
|
+ release_src = host_root / "deploy" / "amd64-release"
|
|
|
+
|
|
|
+ with tempfile.TemporaryDirectory(prefix="hth-amd64-release-") as temp:
|
|
|
+ temp_root = Path(temp)
|
|
|
+ stage = temp_root / "stage"
|
|
|
+ binaries = args.binary_dir.resolve() if args.binary_dir else temp_root / "bin"
|
|
|
+ if args.binary_dir is None:
|
|
|
+ build_binaries(host_root, binaries, version)
|
|
|
+
|
|
|
+ for name in BINARIES:
|
|
|
+ copy_file(binaries / name, stage / "bin" / name, 0o755)
|
|
|
+ for name in ("install.sh", "uninstall.sh", "selftest.sh"):
|
|
|
+ copy_file(release_src / name, stage / name, 0o755)
|
|
|
+ copy_file(release_src / "README.md", stage / "README.md")
|
|
|
+ copy_file(profile, stage / "profile.json")
|
|
|
+ copy_file(release_src / "config" / "hthd.mock.yaml", stage / "config" / "hthd.mock.yaml")
|
|
|
+ copy_file(release_src / "config" / "hthd.serial.yaml", stage / "config" / "hthd.serial.yaml")
|
|
|
+ copy_file(host_root / "deploy" / "systemd" / "hthd.service", stage / "service" / "hthd.service")
|
|
|
+ copy_file(host_root / "deploy" / "sysusers.d" / "hth.conf", stage / "service" / "hth.conf")
|
|
|
+ copy_file(host_root / "deploy" / "tmpfiles" / "hth.conf", stage / "service" / "hthd-tmpfiles.conf")
|
|
|
+ copy_file(
|
|
|
+ host_root / "deploy" / "udev" / "99-hth-controller.rules.template",
|
|
|
+ stage / "service" / "99-hth-controller.rules.template",
|
|
|
+ )
|
|
|
+
|
|
|
+ metadata = {
|
|
|
+ "schema": "hth-linux-amd64-release-v1",
|
|
|
+ "version": version,
|
|
|
+ "target": {"os": "linux", "arch": "amd64", "machine": "x86_64"},
|
|
|
+ "source_commit": source_commit,
|
|
|
+ "project_sha256": sha256(project),
|
|
|
+ "profile_sha256": sha256(profile),
|
|
|
+ "api_version": "hth.api.v1",
|
|
|
+ "profile_id": "prod2u-normal-multiproject-115200-v1",
|
|
|
+ "hardware_validation": "not_run",
|
|
|
+ "built_at_utc": datetime.now(timezone.utc).replace(microsecond=0).isoformat(),
|
|
|
+ "binaries": {name: sha256(stage / "bin" / name) for name in BINARIES},
|
|
|
+ }
|
|
|
+ (stage / "RELEASE-METADATA.json").write_text(
|
|
|
+ json.dumps(metadata, indent=2, sort_keys=True) + "\n", encoding="utf-8"
|
|
|
+ )
|
|
|
+ write_inner_sums(stage)
|
|
|
+
|
|
|
+ top = f"hth-linux-amd64-{version}"
|
|
|
+ archive = args.output_dir.resolve() / f"{top}.tar.gz"
|
|
|
+ create_archive(stage, archive, top)
|
|
|
+ digest = sha256(archive)
|
|
|
+ outer_sum = archive.with_suffix(archive.suffix + ".sha256")
|
|
|
+ outer_sum.write_text(f"{digest} {archive.name}\n", encoding="ascii")
|
|
|
+ print(f"archive={archive}")
|
|
|
+ print(f"sha256={digest}")
|
|
|
+ print(f"source_commit={source_commit}")
|
|
|
+ return 0
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ raise SystemExit(main())
|