fix(ci): qualify on the pinned Python and retain immutable release bundles
Device Core qualification / qualify (push) Successful in 5m51s
Device Core qualification / qualify (push) Successful in 5m51s
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Produce a deterministic data-only release from the exact qualified checkout."""
|
||||
import argparse
|
||||
import gzip
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path, PurePosixPath
|
||||
import re
|
||||
import stat
|
||||
import tarfile
|
||||
|
||||
import qualify
|
||||
|
||||
ENTRY = "deployment/device-core-release-v1.json"
|
||||
ENTRIES = (
|
||||
".dockerignore", "package.json", "package-lock.json",
|
||||
"packages/device-protocol-contract", "packages/device-edge-channel-contract",
|
||||
"packages/infrastructure-telemetry-contract", "services/device-control-core",
|
||||
"services/device-manager", ENTRY,
|
||||
)
|
||||
STEPS = [
|
||||
"design-install", "design-core", "design-react", "device-install",
|
||||
"unit-tests", "ci-contract", "build", "artifact-contracts",
|
||||
]
|
||||
EXCLUDED = {"node_modules", ".git", ".DS_Store", "test", "__pycache__"}
|
||||
DENIED = {"secrets", "credentials", "runtime", "runtime-data", "uploads", "logs", "data"}
|
||||
|
||||
|
||||
def encoded(value):
|
||||
return (json.dumps(value, sort_keys=True, indent=2) + "\n").encode()
|
||||
|
||||
|
||||
def source_files(path, destination):
|
||||
"""Do not traverse ignored trees; reject links and unexpected state elsewhere."""
|
||||
result = {}
|
||||
mode = path.lstat()
|
||||
if stat.S_ISLNK(mode.st_mode) or (stat.S_ISREG(mode.st_mode) and mode.st_nlink != 1):
|
||||
raise ValueError("linked_source:" + destination)
|
||||
if stat.S_ISDIR(mode.st_mode):
|
||||
for child in sorted(path.iterdir()):
|
||||
name = child.name
|
||||
if name in EXCLUDED or name.endswith((".test.mjs", ".map", ".tsbuildinfo")):
|
||||
continue
|
||||
if name in DENIED or name.startswith((".env", "._")) or name.endswith((".pem", ".key", ".sqlite", ".db", ".log")):
|
||||
raise ValueError("runtime_source_rejected:" + destination + "/" + name)
|
||||
result.update(source_files(child, destination + "/" + name))
|
||||
elif stat.S_ISREG(mode.st_mode):
|
||||
if mode.st_size > 16 * 1024 * 1024:
|
||||
raise ValueError("oversized_source:" + destination)
|
||||
result[destination] = path.read_bytes()
|
||||
else:
|
||||
raise ValueError("nonregular_source:" + destination)
|
||||
return result
|
||||
|
||||
|
||||
def bundle(root, report, run_id, run_number):
|
||||
if type(run_id) is not int or type(run_number) is not int or not 0 < run_id < 2**53 or not 0 < run_number < 2**53:
|
||||
raise ValueError("invalid_run_identity")
|
||||
commit = report.get("sourceCommit", "")
|
||||
if not re.fullmatch(r"[0-9a-f]{40}", commit):
|
||||
raise ValueError("invalid_source_commit")
|
||||
lock = qualify.read_lock(root)
|
||||
if (report.get("schema") != "nodedc.device-core.qualification.v1"
|
||||
or report.get("status") != "qualified" or report.get("deployed") is not False
|
||||
or report.get("steps") != STEPS or report.get("sources") != lock):
|
||||
raise ValueError("qualification_required")
|
||||
for name, path in (
|
||||
("sourceLockSha256", root / "infra/ci/sources.lock.json"),
|
||||
("packageLockSha256", root / "package-lock.json"),
|
||||
):
|
||||
if report.get(name) != hashlib.sha256(path.read_bytes()).hexdigest():
|
||||
raise ValueError("qualification_source_changed:" + name)
|
||||
if report.get("managerDistSha256") != qualify.tree_digest(root / "apps/device-manager/dist"):
|
||||
raise ValueError("qualified_dist_changed")
|
||||
|
||||
files = {}
|
||||
for entry in ENTRIES[:-2]:
|
||||
files.update(source_files(root / entry, entry))
|
||||
for source, target in (
|
||||
("services/device-manager/Dockerfile", "services/device-manager/Dockerfile"),
|
||||
("apps/device-manager/server", "services/device-manager/server"),
|
||||
("apps/device-manager/dist", "services/device-manager/dist"),
|
||||
):
|
||||
files.update(source_files(root / source, target))
|
||||
if sum(map(len, files.values())) > 64 * 1024 * 1024:
|
||||
raise ValueError("release_too_large")
|
||||
release_id = "device-core-release-v1-r" + str(run_id) + "-" + commit
|
||||
descriptor = {
|
||||
"schemaVersion": "nodedc.device-core.release.v1",
|
||||
"releaseId": release_id,
|
||||
"repository": "SILVER/NODEDC_DEVICE_CORE",
|
||||
"repositoryId": 38,
|
||||
"branch": "main",
|
||||
"sourceCommit": commit,
|
||||
"runId": run_id,
|
||||
"runNumber": run_number,
|
||||
"qualification": report,
|
||||
"filesSha256": {name: hashlib.sha256(value).hexdigest() for name, value in sorted(files.items())},
|
||||
}
|
||||
files[ENTRY] = encoded(descriptor)
|
||||
members = {
|
||||
"manifest.env": ("id=" + release_id + "\ncomponent=device-core-release\ntype=app-overlay\n").encode(),
|
||||
"files.txt": ("\n".join(ENTRIES) + "\n").encode(),
|
||||
**{"payload/" + name: value for name, value in files.items()},
|
||||
}
|
||||
output = io.BytesIO()
|
||||
with gzip.GzipFile(fileobj=output, mode="wb", filename="", mtime=0) as compressed:
|
||||
with tarfile.open(fileobj=compressed, mode="w", format=tarfile.PAX_FORMAT) as archive:
|
||||
directories = {"payload"}
|
||||
for name in members:
|
||||
directories.update(str(p) for p in PurePosixPath(name).parents if str(p) != ".")
|
||||
for name in sorted(directories):
|
||||
info = tarfile.TarInfo(name)
|
||||
info.type, info.mode = tarfile.DIRTYPE, 0o755
|
||||
archive.addfile(info)
|
||||
for name, value in sorted(members.items()):
|
||||
info = tarfile.TarInfo(name)
|
||||
info.mode, info.size = 0o644, len(value)
|
||||
archive.addfile(info, io.BytesIO(value))
|
||||
return release_id, output.getvalue()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--report", type=Path, required=True)
|
||||
parser.add_argument("--run-id", type=int, required=True)
|
||||
parser.add_argument("--run-number", type=int, required=True)
|
||||
parser.add_argument("--output-dir", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
root = Path(__file__).resolve().parents[2]
|
||||
report = json.loads(args.report.read_text())
|
||||
qualify.verify_sources(root, report["sourceCommit"], qualify.read_lock(root))
|
||||
output = args.output_dir.resolve()
|
||||
for checkout in (root, *(root.parent / name for name in qualify.DEPENDENCIES)):
|
||||
if output == checkout or checkout in output.parents:
|
||||
raise ValueError("release_output_must_be_outside_sources")
|
||||
identity, data = bundle(root, report, args.run_id, args.run_number)
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
path = output / ("nodedc-" + identity + ".tgz")
|
||||
with path.open("xb") as stream:
|
||||
stream.write(data)
|
||||
receipt = {"artifact": path.name, "sha256": hashlib.sha256(data).hexdigest(),
|
||||
"sourceCommit": report["sourceCommit"], "runId": args.run_id,
|
||||
"runNumber": args.run_number, "deployed": False}
|
||||
with (output / "release.json").open("xb") as stream:
|
||||
stream.write(encoded(receipt))
|
||||
print(json.dumps(receipt))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user