87 lines
3.7 KiB
Python
87 lines
3.7 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")
|
|
V2_ENTRIES = ("docker-compose.gitea-ci.yml", "runner.yaml", "deployment/bootstrap-v2.json")
|
|
V3_ENTRIES = ("docker-compose.gitea-ci.yml", "runner.yaml", "deployment/bootstrap-v3.json")
|
|
V4_ENTRIES = ("docker-compose.gitea-ci.yml", "runner.yaml", "deployment/bootstrap-v4.json")
|
|
|
|
|
|
def entries_for(patch_id):
|
|
if not re.fullmatch(r"gitea-ci-bootstrap-v[1234]-[a-zA-Z0-9._-]{1,65}", patch_id):
|
|
raise ValueError("invalid_bootstrap_identity")
|
|
if patch_id.startswith("gitea-ci-bootstrap-v4-"):
|
|
return V4_ENTRIES
|
|
if patch_id.startswith("gitea-ci-bootstrap-v3-"):
|
|
return V3_ENTRIES
|
|
return V2_ENTRIES if patch_id.startswith("gitea-ci-bootstrap-v2-") else ENTRIES
|
|
|
|
|
|
def bundle(patch_id, source=SOURCE):
|
|
entries = entries_for(patch_id)
|
|
config_source = source / "synology-v4" if entries == V4_ENTRIES else source / "synology-v3" if entries == V3_ENTRIES else source
|
|
descriptor = json.loads((source / Path(entries[2]).name).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": (config_source / ENTRIES[0]).read_bytes(),
|
|
"payload/runner.yaml": (config_source / ENTRIES[1]).read_bytes(),
|
|
"payload/" + entries[2]: (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_for(args.patch_id),
|
|
"services": ["build-runner"], "publishedPorts": [],
|
|
"productionDeployEnabled": False,
|
|
}))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|