73 lines
3.0 KiB
Python
73 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Build the immutable, data-only Gitea CI bootstrap artifact from a clean commit."""
|
|
import argparse
|
|
import gzip
|
|
import hashlib
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
import tarfile
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SOURCE = Path(__file__).resolve().parent
|
|
ENTRIES = ("docker-compose.gitea-ci.yml", "runner.yaml", "deployment/bootstrap-v1.json")
|
|
|
|
|
|
def bundle(patch_id, source=SOURCE):
|
|
if not re.fullmatch(r"gitea-ci-bootstrap-v1-[a-zA-Z0-9._-]{1,65}", patch_id):
|
|
raise ValueError("invalid_bootstrap_identity")
|
|
descriptor = json.loads((source / "bootstrap-v1.json").read_text())
|
|
if descriptor["releaseId"] != "__PATCH_ID__":
|
|
raise ValueError("bootstrap_template_identity_mismatch")
|
|
descriptor["releaseId"] = patch_id
|
|
files = {
|
|
"manifest.env": ("id=" + patch_id + "\ncomponent=gitea-ci\ntype=app-overlay\n").encode(),
|
|
"files.txt": ("\n".join(ENTRIES) + "\n").encode(),
|
|
"payload/docker-compose.gitea-ci.yml": (source / ENTRIES[0]).read_bytes(),
|
|
"payload/runner.yaml": (source / ENTRIES[1]).read_bytes(),
|
|
"payload/deployment/bootstrap-v1.json": (json.dumps(descriptor, indent=2) + "\n").encode(),
|
|
}
|
|
result = io.BytesIO()
|
|
with gzip.GzipFile(fileobj=result, mode="wb", filename="", mtime=0) as compressed:
|
|
with tarfile.open(fileobj=compressed, mode="w", format=tarfile.USTAR_FORMAT) as archive:
|
|
for name, content in sorted(files.items()):
|
|
info = tarfile.TarInfo(name)
|
|
info.size = len(content)
|
|
info.mode = 0o644
|
|
info.mtime = 0
|
|
archive.addfile(info, io.BytesIO(content))
|
|
return result.getvalue()
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--patch-id", required=True)
|
|
parser.add_argument("--output-dir", type=Path, required=True)
|
|
parser.add_argument("--source-commit", required=True)
|
|
args = parser.parse_args()
|
|
actual = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip()
|
|
if not re.fullmatch(r"[a-f0-9]{40}", args.source_commit) or actual != args.source_commit:
|
|
raise SystemExit("source_commit_mismatch")
|
|
dirty = subprocess.check_output(["git", "status", "--porcelain"], cwd=ROOT, text=True).strip()
|
|
if dirty:
|
|
raise SystemExit("clean_committed_source_required")
|
|
data = bundle(args.patch_id)
|
|
args.output_dir.mkdir(parents=True, exist_ok=True)
|
|
path = args.output_dir / ("nodedc-gitea-ci-" + args.patch_id + ".tgz")
|
|
with path.open("xb") as output:
|
|
output.write(data)
|
|
print(json.dumps({
|
|
"artifact": str(path.resolve()), "patchId": args.patch_id,
|
|
"component": "gitea-ci", "sourceCommit": actual,
|
|
"sha256": hashlib.sha256(data).hexdigest(), "entries": ENTRIES,
|
|
"services": ["build-runner"], "publishedPorts": [],
|
|
"productionDeployEnabled": False,
|
|
}))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|