Add pinned Device Core CI qualification for main
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Qualify exact sources in disposable CI checkouts. No production access."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
SHA = re.compile(r"[0-9a-f]{40}")
|
||||
DEPENDENCIES = {
|
||||
"NODEDC_DESIGN_GUIDELINE": "https://git.dcserve.ru/SILVER/NODEDC_DESIGN_GUIDELINE.git",
|
||||
"platform": "https://git.dcserve.ru/SILVER/NODEDC_PLATFORM.git",
|
||||
}
|
||||
|
||||
|
||||
def run(args, cwd, capture=False):
|
||||
return subprocess.run(
|
||||
args, cwd=cwd, check=True, text=True,
|
||||
stdout=subprocess.PIPE if capture else None,
|
||||
env={**os.environ, "GIT_TERMINAL_PROMPT": "0", "CI": "true"},
|
||||
).stdout
|
||||
|
||||
|
||||
def read_lock(root):
|
||||
lock = json.loads((root / "infra/ci/sources.lock.json").read_text())
|
||||
if lock.get("schema") != "nodedc.device-core.ci-sources.v1":
|
||||
raise ValueError("unsupported_source_lock")
|
||||
if set(lock.get("dependencies", {})) != set(DEPENDENCIES):
|
||||
raise ValueError("unexpected_source_dependencies")
|
||||
for name, url in DEPENDENCIES.items():
|
||||
item = lock["dependencies"][name]
|
||||
if item.get("url") != url or not SHA.fullmatch(item.get("commit", "")):
|
||||
raise ValueError("invalid_dependency_identity:" + name)
|
||||
if not re.fullmatch(r"v22\.\d+\.\d+", lock.get("node", "")):
|
||||
raise ValueError("invalid_node_version")
|
||||
if not re.fullmatch(r"10\.\d+\.\d+", lock.get("npm", "")):
|
||||
raise ValueError("invalid_npm_version")
|
||||
return lock
|
||||
|
||||
|
||||
def verify_checkout(path, commit):
|
||||
if not SHA.fullmatch(commit):
|
||||
raise ValueError("full_commit_sha_required")
|
||||
if path.is_symlink() or not path.is_dir():
|
||||
raise ValueError("regular_checkout_required:" + path.name)
|
||||
top = Path(run(["git", "rev-parse", "--show-toplevel"], path, True).strip())
|
||||
if top.resolve() != path.resolve():
|
||||
raise ValueError("independent_checkout_required:" + path.name)
|
||||
actual = run(["git", "rev-parse", "HEAD"], path, True).strip()
|
||||
if actual != commit:
|
||||
raise ValueError("source_commit_mismatch:" + path.name)
|
||||
if run(["git", "status", "--porcelain", "--untracked-files=normal"], path, True).strip():
|
||||
raise ValueError("dirty_source_checkout:" + path.name)
|
||||
|
||||
|
||||
def verify_sources(root, commit, lock):
|
||||
verify_checkout(root, commit)
|
||||
for name, item in lock["dependencies"].items():
|
||||
verify_checkout(root.parent / name, item["commit"])
|
||||
|
||||
|
||||
def fetch_dependencies(root, lock):
|
||||
# Never reset an operator checkout or reuse an earlier job's workspace.
|
||||
for name in DEPENDENCIES:
|
||||
target = root.parent / name
|
||||
if target.exists() or target.is_symlink():
|
||||
raise ValueError("dependency_destination_exists:" + name)
|
||||
for name, item in lock["dependencies"].items():
|
||||
target = root.parent / name
|
||||
target.mkdir()
|
||||
run(["git", "init", "--quiet"], target)
|
||||
run(["git", "remote", "add", "origin", item["url"]], target)
|
||||
run(["git", "fetch", "--quiet", "--depth=1", "origin", item["commit"]], target)
|
||||
run(["git", "checkout", "--quiet", "--detach", "FETCH_HEAD"], target)
|
||||
verify_checkout(target, item["commit"])
|
||||
|
||||
|
||||
def tree_digest(root):
|
||||
"""Digest paths + lengths + bytes, independent of mtime, owner and checkout path."""
|
||||
digest = hashlib.sha256()
|
||||
if not root.is_dir() or root.is_symlink():
|
||||
raise ValueError("build_output_missing")
|
||||
count = 0
|
||||
for path in sorted(root.rglob("*")):
|
||||
if path.is_symlink():
|
||||
raise ValueError("symlink_in_build_output")
|
||||
if path.is_dir():
|
||||
continue
|
||||
if not path.is_file():
|
||||
raise ValueError("nonregular_build_output")
|
||||
data = path.read_bytes()
|
||||
name = path.relative_to(root).as_posix().encode()
|
||||
digest.update(len(name).to_bytes(8, "big") + name)
|
||||
digest.update(len(data).to_bytes(8, "big") + data)
|
||||
count += 1
|
||||
if count == 0:
|
||||
raise ValueError("empty_build_output")
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def qualify(root, commit, lock, report):
|
||||
if report.exists():
|
||||
raise ValueError("qualification_report_already_exists")
|
||||
verify_sources(root, commit, lock)
|
||||
for tool in ("node", "npm"):
|
||||
if run([tool, "--version"], root, True).strip() != lock[tool]:
|
||||
raise ValueError("toolchain_version_mismatch:" + tool)
|
||||
design = root.parent / "NODEDC_DESIGN_GUIDELINE"
|
||||
steps = [
|
||||
("design-install", design, ["npm", "ci", "--ignore-scripts", "--no-audit", "--no-fund"]),
|
||||
("design-core", design, ["npm", "run", "build", "--workspace", "@nodedc/ui-core"]),
|
||||
("design-react", design, ["npm", "run", "build", "--workspace", "@nodedc/ui-react"]),
|
||||
("device-install", root, ["npm", "ci", "--ignore-scripts", "--no-audit", "--no-fund"]),
|
||||
("unit-tests", root, ["npm", "test"]),
|
||||
("ci-contract", root, [sys.executable, "-m", "unittest", "discover", "-s", "infra/ci", "-p", "test_*.py"]),
|
||||
("build", root, ["npm", "run", "build"]),
|
||||
("artifact-contracts", root, ["npm", "run", "test:deploy"]),
|
||||
]
|
||||
completed = []
|
||||
for name, cwd, command in steps:
|
||||
print("ci_step=" + name, flush=True)
|
||||
run(command, cwd)
|
||||
completed.append(name)
|
||||
verify_sources(root, commit, lock)
|
||||
result = {
|
||||
"schema": "nodedc.device-core.qualification.v1",
|
||||
"status": "qualified",
|
||||
"deployed": False,
|
||||
"sourceCommit": commit,
|
||||
"sources": lock,
|
||||
"sourceLockSha256": hashlib.sha256((root / "infra/ci/sources.lock.json").read_bytes()).hexdigest(),
|
||||
"packageLockSha256": hashlib.sha256((root / "package-lock.json").read_bytes()).hexdigest(),
|
||||
"managerDistSha256": tree_digest(root / "apps/device-manager/dist"),
|
||||
"steps": completed,
|
||||
}
|
||||
report.parent.mkdir(parents=True, exist_ok=True)
|
||||
with report.open("x") as stream:
|
||||
json.dump(result, stream, indent=2)
|
||||
stream.write("\n")
|
||||
print(json.dumps(result), flush=True)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("action", choices=("fetch-dependencies", "verify", "check"))
|
||||
parser.add_argument("--commit", required=True)
|
||||
parser.add_argument("--report", type=Path)
|
||||
args = parser.parse_args()
|
||||
root = Path(__file__).resolve().parents[2]
|
||||
verify_checkout(root, args.commit)
|
||||
lock = read_lock(root)
|
||||
if args.action == "fetch-dependencies":
|
||||
fetch_dependencies(root, lock)
|
||||
elif args.action == "verify":
|
||||
verify_sources(root, args.commit, lock)
|
||||
else:
|
||||
if args.report is None:
|
||||
parser.error("check requires --report outside the source checkouts")
|
||||
report = args.report.resolve()
|
||||
for checkout in (root, *(root.parent / name for name in DEPENDENCIES)):
|
||||
if report.is_relative_to(checkout):
|
||||
raise ValueError("report_must_be_outside_sources")
|
||||
qualify(root, args.commit, lock, report)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user