70 lines
2.7 KiB
Python
70 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Create a traceable, unsigned release descriptor from verified build digests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
BUILDER_IMAGE = "ghcr.io/osscontainertools/kaniko:v1.28.5-debug@sha256:c73ca67a3b494bce79a8fb6090c4f73c18997ab272d80bffc460a62343aeb412"
|
|
|
|
|
|
def digest(path: Path) -> str:
|
|
value = path.read_text(encoding="ascii").strip()
|
|
if not re.fullmatch(r"sha256:[0-9a-f]{64}", value):
|
|
raise ValueError(f"invalid Kaniko digest in {path}")
|
|
return value
|
|
|
|
|
|
def create(repository: str, commit: str, source_run_id: str, run_id: str, backend: Path, opensearch: Path) -> dict:
|
|
if not re.fullmatch(r"[a-z0-9][a-z0-9-]*/[a-z0-9][a-z0-9._-]*", repository):
|
|
raise ValueError("repository must be lowercase owner/repository")
|
|
if not re.fullmatch(r"[0-9a-f]{40}", commit):
|
|
raise ValueError("commit must be a 40-character lowercase Git SHA-1")
|
|
if not re.fullmatch(r"[0-9]+", run_id):
|
|
raise ValueError("run_id must be numeric")
|
|
if not re.fullmatch(r"[0-9]+", source_run_id):
|
|
raise ValueError("source_run_id must be numeric")
|
|
registry = "git.vakanaut.ru/" + repository
|
|
return {
|
|
"schema_version": 4,
|
|
"source_ref": "refs/heads/main",
|
|
"repository": repository,
|
|
"commit": commit,
|
|
"workflow": ".gitea/workflows/release-images.yaml",
|
|
"run_id": run_id,
|
|
"source_run_id": source_run_id,
|
|
"builder": {"name": "Kaniko", "image": BUILDER_IMAGE},
|
|
"images": {
|
|
"backend": f"{registry}/backend@{digest(backend)}",
|
|
"opensearch": f"{registry}/opensearch@{digest(opensearch)}",
|
|
},
|
|
}
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--repository", required=True)
|
|
parser.add_argument("--commit", required=True)
|
|
parser.add_argument("--run-id", required=True)
|
|
parser.add_argument("--source-run-id", required=True)
|
|
parser.add_argument("--backend-digest", required=True, type=Path)
|
|
parser.add_argument("--opensearch-digest", required=True, type=Path)
|
|
parser.add_argument("--output", required=True, type=Path)
|
|
args = parser.parse_args()
|
|
try:
|
|
manifest = create(args.repository, args.commit, args.source_run_id, args.run_id, args.backend_digest, args.opensearch_digest)
|
|
args.output.write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
except (OSError, UnicodeError, ValueError) as error:
|
|
print(f"ERROR: {error}", file=sys.stderr)
|
|
return 1
|
|
print(f"PASS release descriptor for {manifest['commit']}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|