|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import csv |
| 6 | +import hashlib |
| 7 | +import json |
| 8 | +import re |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +PINNED_REF_RE = re.compile(r"^[^\s]+@sha256:[0-9a-f]{64}$") |
| 12 | +DIGEST_RE = re.compile(r"^sha256:[0-9a-f]{64}$") |
| 13 | +TIMESTAMP_RE = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$") |
| 14 | +HEADER = [ |
| 15 | + "channel", |
| 16 | + "tag", |
| 17 | + "created", |
| 18 | + "version", |
| 19 | + "revision", |
| 20 | + "arch", |
| 21 | + "platform_contract_digest", |
| 22 | + "platform_profile", |
| 23 | + "platform_images_lock_sha256", |
| 24 | + "artifact_digest", |
| 25 | + "pinned_ref", |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +def load_env(path: Path) -> dict[str, str]: |
| 30 | + data: dict[str, str] = {} |
| 31 | + for raw_line in path.read_text(encoding="utf-8").splitlines(): |
| 32 | + line = raw_line.strip() |
| 33 | + if not line or line.startswith("#"): |
| 34 | + continue |
| 35 | + key, value = line.split("=", 1) |
| 36 | + data[key] = value |
| 37 | + return data |
| 38 | + |
| 39 | + |
| 40 | +def sha256_file(path: Path) -> str: |
| 41 | + return hashlib.sha256(path.read_bytes()).hexdigest() |
| 42 | + |
| 43 | + |
| 44 | +def load_existing_rows(path: Path | None) -> list[dict[str, str]]: |
| 45 | + if path is None or not path.is_file(): |
| 46 | + return [] |
| 47 | + with path.open("r", encoding="utf-8", newline="") as handle: |
| 48 | + reader = csv.DictReader(handle, delimiter="\t") |
| 49 | + if reader.fieldnames != HEADER: |
| 50 | + raise SystemExit(f"{path} does not declare the expected catalog.tsv header") |
| 51 | + return [{key: str(value or "").strip() for key, value in row.items()} for row in reader] |
| 52 | + |
| 53 | + |
| 54 | +def render_row( |
| 55 | + *, |
| 56 | + channel: str, |
| 57 | + immutable_tag: str, |
| 58 | + created: str, |
| 59 | + version: str, |
| 60 | + revision: str, |
| 61 | + arch: str, |
| 62 | + platform_contract_digest: str, |
| 63 | + platform_profile: str, |
| 64 | + images_lock_sha256: str, |
| 65 | + artifact_digest: str, |
| 66 | + pinned_ref: str, |
| 67 | +) -> dict[str, str]: |
| 68 | + if not channel: |
| 69 | + raise SystemExit("channel must be non-empty") |
| 70 | + if not immutable_tag: |
| 71 | + raise SystemExit("tag must be non-empty") |
| 72 | + if not TIMESTAMP_RE.fullmatch(created): |
| 73 | + raise SystemExit(f"created must be an RFC3339 UTC timestamp with Z suffix: {created!r}") |
| 74 | + if not version: |
| 75 | + raise SystemExit("version must be non-empty") |
| 76 | + if not revision: |
| 77 | + raise SystemExit("revision must be non-empty") |
| 78 | + if not arch: |
| 79 | + raise SystemExit("arch must be non-empty") |
| 80 | + if not DIGEST_RE.fullmatch(platform_contract_digest): |
| 81 | + raise SystemExit("platform contract digest must be sha256-pinned") |
| 82 | + if not platform_profile: |
| 83 | + raise SystemExit("platform profile must be non-empty") |
| 84 | + if not re.fullmatch(r"[0-9a-f]{64}", images_lock_sha256): |
| 85 | + raise SystemExit("images lock sha256 must be a plain 64-character hex digest") |
| 86 | + if not DIGEST_RE.fullmatch(artifact_digest): |
| 87 | + raise SystemExit("artifact digest must be sha256-pinned") |
| 88 | + if not PINNED_REF_RE.fullmatch(pinned_ref): |
| 89 | + raise SystemExit("pinned ref must be digest-pinned") |
| 90 | + |
| 91 | + return { |
| 92 | + "channel": channel, |
| 93 | + "tag": immutable_tag, |
| 94 | + "created": created, |
| 95 | + "version": version, |
| 96 | + "revision": revision, |
| 97 | + "arch": arch, |
| 98 | + "platform_contract_digest": platform_contract_digest, |
| 99 | + "platform_profile": platform_profile, |
| 100 | + "platform_images_lock_sha256": images_lock_sha256, |
| 101 | + "artifact_digest": artifact_digest, |
| 102 | + "pinned_ref": pinned_ref, |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | +def main() -> int: |
| 107 | + parser = argparse.ArgumentParser(description="Render installer-browsable catalog rows for an application catalog repo.") |
| 108 | + parser.add_argument("--catalog-json", required=True) |
| 109 | + parser.add_argument("--profile-env", required=True) |
| 110 | + parser.add_argument("--images-lock", required=True) |
| 111 | + parser.add_argument("--existing-catalog") |
| 112 | + parser.add_argument("--out-catalog", required=True) |
| 113 | + parser.add_argument("--channel", default="stable") |
| 114 | + parser.add_argument("--tag", required=True) |
| 115 | + parser.add_argument("--created", required=True) |
| 116 | + parser.add_argument("--version", required=True) |
| 117 | + parser.add_argument("--revision", required=True) |
| 118 | + parser.add_argument("--arch", required=True) |
| 119 | + parser.add_argument("--artifact-digest", required=True) |
| 120 | + parser.add_argument("--pinned-ref", required=True) |
| 121 | + args = parser.parse_args() |
| 122 | + |
| 123 | + catalog_json = Path(args.catalog_json).resolve() |
| 124 | + profile_env = Path(args.profile_env).resolve() |
| 125 | + images_lock = Path(args.images_lock).resolve() |
| 126 | + existing_catalog = Path(args.existing_catalog).resolve() if args.existing_catalog else None |
| 127 | + out_catalog = Path(args.out_catalog).resolve() |
| 128 | + |
| 129 | + catalog = json.loads(catalog_json.read_text(encoding="utf-8")) |
| 130 | + profile = load_env(profile_env) |
| 131 | + catalog_id = str(catalog.get("catalog_id", "")).strip() |
| 132 | + if not catalog_id: |
| 133 | + raise SystemExit(f"{catalog_json} must declare catalog_id") |
| 134 | + if str(profile.get("OURBOX_APPLICATION_CATALOG_ID", "")).strip() != catalog_id: |
| 135 | + raise SystemExit(f"{profile_env} OURBOX_APPLICATION_CATALOG_ID does not match {catalog_id!r}") |
| 136 | + |
| 137 | + existing_rows = load_existing_rows(existing_catalog) |
| 138 | + new_row = render_row( |
| 139 | + channel=str(args.channel).strip(), |
| 140 | + immutable_tag=str(args.tag).strip(), |
| 141 | + created=str(args.created).strip(), |
| 142 | + version=str(args.version).strip(), |
| 143 | + revision=str(args.revision).strip(), |
| 144 | + arch=str(args.arch).strip(), |
| 145 | + platform_contract_digest=str(profile.get("OURBOX_PLATFORM_CONTRACT_DIGEST", "")).strip(), |
| 146 | + platform_profile=catalog_id, |
| 147 | + images_lock_sha256=sha256_file(images_lock), |
| 148 | + artifact_digest=str(args.artifact_digest).strip(), |
| 149 | + pinned_ref=str(args.pinned_ref).strip(), |
| 150 | + ) |
| 151 | + |
| 152 | + merged_rows = [ |
| 153 | + row |
| 154 | + for row in existing_rows |
| 155 | + if not (row.get("channel") == new_row["channel"] and row.get("tag") == new_row["tag"]) |
| 156 | + ] |
| 157 | + merged_rows.append(new_row) |
| 158 | + merged_rows.sort(key=lambda row: row["created"], reverse=True) |
| 159 | + |
| 160 | + out_catalog.parent.mkdir(parents=True, exist_ok=True) |
| 161 | + with out_catalog.open("w", encoding="utf-8", newline="") as handle: |
| 162 | + writer = csv.DictWriter(handle, fieldnames=HEADER, delimiter="\t", lineterminator="\n") |
| 163 | + writer.writeheader() |
| 164 | + for row in merged_rows: |
| 165 | + writer.writerow(row) |
| 166 | + return 0 |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + raise SystemExit(main()) |
0 commit comments