|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Package or fetch the native CI executables for an exact VGI main revision. |
| 3 | +
|
| 4 | +SDK development CI must use a client with the same wire contract as its workers. |
| 5 | +The signed community extension is released separately and can still implement |
| 6 | +the previous contract. This helper never falls back to another revision. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import hashlib |
| 13 | +import json |
| 14 | +import os |
| 15 | +from pathlib import Path |
| 16 | +import platform |
| 17 | +import re |
| 18 | +import shutil |
| 19 | +import subprocess |
| 20 | +import tempfile |
| 21 | +import time |
| 22 | + |
| 23 | + |
| 24 | +REPOSITORY = "Query-farm/vgi" |
| 25 | + |
| 26 | + |
| 27 | +def command(*args: str) -> str: |
| 28 | + return subprocess.check_output(args, text=True).strip() |
| 29 | + |
| 30 | + |
| 31 | +def platform_name() -> str: |
| 32 | + machines = {"x86_64": "amd64", "aarch64": "arm64", "arm64": "arm64"} |
| 33 | + if platform.system() != "Linux" or platform.machine() not in machines: |
| 34 | + raise RuntimeError("native SDK CI artifacts require Linux amd64 or arm64") |
| 35 | + return "linux_" + machines[platform.machine()] |
| 36 | + |
| 37 | + |
| 38 | +def digest(path: Path) -> str: |
| 39 | + result = hashlib.sha256() |
| 40 | + with path.open("rb") as source: |
| 41 | + for chunk in iter(lambda: source.read(1024 * 1024), b""): |
| 42 | + result.update(chunk) |
| 43 | + return result.hexdigest() |
| 44 | + |
| 45 | + |
| 46 | +def package(source: Path, build: Path, output: Path, revision: str) -> None: |
| 47 | + cli = build / "haybarn" |
| 48 | + if not cli.is_file(): |
| 49 | + cli = build / "duckdb" |
| 50 | + binaries = {"unittest": build / "test/unittest", "haybarn": cli} |
| 51 | + output.mkdir(parents=True, exist_ok=True) |
| 52 | + hashes = {} |
| 53 | + for name, binary in binaries.items(): |
| 54 | + if not binary.is_file(): |
| 55 | + raise RuntimeError(f"missing native build executable: {binary}") |
| 56 | + shutil.copy2(binary, output / name) |
| 57 | + hashes[name] = digest(output / name) |
| 58 | + header = (source / "src/generated/vgi_protocol_version.hpp").read_text() |
| 59 | + version = re.search(r'VGI_PROTOCOL_VERSION\s*=\s*"([^"]+)"', header) |
| 60 | + if version is None: |
| 61 | + raise RuntimeError("generated protocol version is missing") |
| 62 | + manifest = { |
| 63 | + "revision": revision, |
| 64 | + "engine_revision": command("git", "-C", str(source), "rev-parse", "HEAD:duckdb"), |
| 65 | + "platform": platform_name(), |
| 66 | + "protocol_version": version.group(1), |
| 67 | + "sha256": hashes, |
| 68 | + } |
| 69 | + (output / "manifest.json").write_text(json.dumps(manifest, indent=2) + "\n") |
| 70 | + print(json.dumps(manifest, indent=2), flush=True) |
| 71 | + |
| 72 | + |
| 73 | +def api(path: str) -> dict: |
| 74 | + return json.loads(command("gh", "api", path)) |
| 75 | + |
| 76 | + |
| 77 | +def fetch(revision: str, output: Path, timeout: int) -> None: |
| 78 | + artifact_name = "vgi-native-runner-" + platform_name() |
| 79 | + deadline = time.monotonic() + timeout |
| 80 | + run_id = None |
| 81 | + while True: |
| 82 | + runs = api( |
| 83 | + f"repos/{REPOSITORY}/actions/workflows/integration.yml/runs" |
| 84 | + f"?branch=main&head_sha={revision}&per_page=10" |
| 85 | + )["workflow_runs"] |
| 86 | + for run in runs: |
| 87 | + # Never consume a pull-request merge build or a different revision. |
| 88 | + if run["head_sha"] != revision or run["head_branch"] != "main": |
| 89 | + continue |
| 90 | + if run["event"] not in ("push", "workflow_dispatch"): |
| 91 | + continue |
| 92 | + artifacts = api(f"repos/{REPOSITORY}/actions/runs/{run['id']}/artifacts")["artifacts"] |
| 93 | + if any(item["name"] == artifact_name and not item["expired"] for item in artifacts): |
| 94 | + run_id = run["id"] |
| 95 | + break |
| 96 | + if run_id is not None: |
| 97 | + break |
| 98 | + if time.monotonic() >= deadline: |
| 99 | + raise RuntimeError( |
| 100 | + f"no {artifact_name} artifact for VGI {revision}; " |
| 101 | + "check that revision's native Integration suite build" |
| 102 | + ) |
| 103 | + print(f"Waiting for {artifact_name} at VGI {revision} ...", flush=True) |
| 104 | + time.sleep(min(15, max(0, deadline - time.monotonic()))) |
| 105 | + |
| 106 | + with tempfile.TemporaryDirectory(prefix="vgi-native-download-") as directory: |
| 107 | + staging = Path(directory) |
| 108 | + command( |
| 109 | + "gh", "run", "download", str(run_id), "--repo", REPOSITORY, |
| 110 | + "--name", artifact_name, "--dir", str(staging), |
| 111 | + ) |
| 112 | + manifest = json.loads((staging / "manifest.json").read_text()) |
| 113 | + if manifest["revision"] != revision or manifest["platform"] != platform_name(): |
| 114 | + raise RuntimeError("native artifact provenance does not match the requested revision/platform") |
| 115 | + for name in ("unittest", "haybarn"): |
| 116 | + if digest(staging / name) != manifest["sha256"][name]: |
| 117 | + raise RuntimeError(f"native artifact checksum mismatch: {name}") |
| 118 | + output.mkdir(parents=True, exist_ok=True) |
| 119 | + for name in ("unittest", "haybarn", "manifest.json"): |
| 120 | + shutil.copy2(staging / name, output / name) |
| 121 | + for name in ("unittest", "haybarn"): |
| 122 | + (output / name).chmod(0o755) |
| 123 | + |
| 124 | + values = { |
| 125 | + "HAYBARN_UNITTEST": str(output / "unittest"), |
| 126 | + "HAYBARN_CLI": str(output / "haybarn"), |
| 127 | + "RUNNER_LINKS_EXTENSIONS": "1", |
| 128 | + } |
| 129 | + environment = os.environ.get("GITHUB_ENV") |
| 130 | + if environment: |
| 131 | + with Path(environment).open("a") as target: |
| 132 | + for name, value in values.items(): |
| 133 | + if "\n" in value or "\r" in value: |
| 134 | + raise RuntimeError("native output path cannot contain newlines") |
| 135 | + target.write(f"{name}={value}\n") |
| 136 | + print(f"Using native VGI {revision}, protocol {manifest['protocol_version']}, run {run_id}", flush=True) |
| 137 | + for name, value in values.items(): |
| 138 | + print(f"{name}={value}", flush=True) |
| 139 | + |
| 140 | + |
| 141 | +def main() -> None: |
| 142 | + parser = argparse.ArgumentParser(description=__doc__) |
| 143 | + parser.add_argument("--source", required=True, type=Path) |
| 144 | + parser.add_argument("--output", required=True, type=Path) |
| 145 | + parser.add_argument("--pack", action="store_true") |
| 146 | + parser.add_argument("--build", type=Path, default=Path("build/release")) |
| 147 | + parser.add_argument("--timeout", type=int, default=1800) |
| 148 | + args = parser.parse_args() |
| 149 | + source = args.source.resolve() |
| 150 | + output = args.output.resolve() |
| 151 | + revision = command("git", "-C", str(source), "rev-parse", "HEAD") |
| 152 | + if args.pack: |
| 153 | + build = args.build if args.build.is_absolute() else source / args.build |
| 154 | + package(source, build, output, revision) |
| 155 | + else: |
| 156 | + fetch(revision, output, args.timeout) |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + main() |
0 commit comments