Repository navigation
Expand file tree
/
Copy pathprepare-artifacts.py
More file actions
87 lines (79 loc) · 4.04 KB
/
Copy pathprepare-artifacts.py
File metadata and controls
87 lines (79 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
"""Print an explicit OpenTofu artifact manifest; never upload or modify inputs."""
import argparse
import hashlib
import json
from pathlib import Path
import re
def safe_name(name):
if not isinstance(name, str) or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._+-]*", name):
raise ValueError(f"Not a safe artifact basename: {name!r}")
if any(word in name.lower() for word in ("lan-test", "-test", "localhost")):
raise ValueError(f"Refusing test artifact: {name}")
return name
def generate(serve, bootstrap, previous=None):
serve = serve.resolve()
bootstrap = bootstrap.resolve()
script = bootstrap.read_text()
for forbidden in ("IPSW_BASE", "localhost", "127.0.0.1", "lan-test", "installer-dev", "--insecure", "SSL_CERT_FILE"):
if forbidden in script:
raise ValueError(f"Bootstrap contains test/local override: {forbidden}")
for value in ("https://cdn.gravitylinux.org/installer/latest",
"https://cdn.gravitylinux.org/installer/installer_data.json",
"https://cdn.gravitylinux.org/installer"):
if value not in script:
raise ValueError(f"Missing production bootstrap URL: {value}")
version = safe_name((serve / "installer/latest").read_text().strip())
data = json.loads((serve / "installer/installer_data.json").read_text())
entries = data["os_list"]
if not entries:
raise ValueError("Installer OS list is empty")
paths = {
"index.sh": bootstrap,
"installer/latest": serve / "installer/latest",
"installer/installer_data.json": serve / "installer/installer_data.json",
f"installer/installer-{version}.tar.gz": serve / f"installer/installer-{version}.tar.gz",
"os/boot.bin": serve / "os/boot.bin",
}
for entry in entries:
name = safe_name(entry["package"])
if not name.endswith(".zip"):
raise ValueError("OS packages must be ZIP files")
if "hardware test" in entry.get("name", "").lower():
raise ValueError("Installer data still describes a hardware-test image")
paths[f"os/{name}"] = serve / "os" / name
result = dict(previous or {})
for key, path in paths.items():
if key != "index.sh" and not path.resolve().is_relative_to(serve):
raise ValueError(f"Artifact escapes serve directory: {path}")
if not path.is_file() or path.stat().st_size == 0:
raise ValueError(f"Missing/empty artifact: {path}")
with path.open("rb") as stream:
digest = hashlib.file_digest(stream, "sha256").hexdigest()
mutable = key in ("index.sh", "installer/latest", "installer/installer_data.json", "os/boot.bin")
if not mutable and key in result and result[key]["sha256"] != digest:
raise ValueError(f"Immutable artifact changed: {key}; use a new filename")
content_type = "application/octet-stream"
if key.endswith(".zip"):
content_type = "application/zip"
elif key.endswith(".tar.gz"):
content_type = "application/gzip"
result[key] = {"source": str(path.resolve()), "sha256": digest, "content_type": content_type}
# Historical payloads stay in the manifest; historical control pointers don't.
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--serve", type=Path, required=True)
parser.add_argument("--bootstrap", type=Path, required=True,
help="Use installer/scripts/bootstrap-prod.sh, NOT the LAN index.sh")
parser.add_argument("--previous", type=Path,
help="Previous manifest, to retain immutable releases")
args = parser.parse_args()
previous = json.loads(args.previous.read_text()) if args.previous else None
try:
result = generate(args.serve, args.bootstrap, previous)
except (ValueError, KeyError, OSError) as error:
parser.exit(1, f"Artifact validation failed: {error}\n")
print(json.dumps(result, indent=2, sort_keys=True))
if __name__ == "__main__":
main()