forked from longtao.wu/droboticflow
194 lines
6.7 KiB
Python
194 lines
6.7 KiB
Python
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import re
|
|
import subprocess
|
|
from typing import Any
|
|
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
|
CATALOG_PATH = ROOT / "internal" / "api" / "builtin_operators_catalog.json"
|
|
OPERATOR_REPO_ROOT = ROOT / "operator-repos" / "builtin"
|
|
DEFAULT_RELEASE_ENV = "poc"
|
|
RELEASE_SCHEMA_VERSION = "droboticflow.operator.release/v1"
|
|
DIGEST_RE = re.compile(r"^.+@sha256:[a-f0-9]{64}$")
|
|
|
|
|
|
def load_json(path: pathlib.Path) -> Any:
|
|
return json.loads(path.read_text(encoding="utf-8"))
|
|
|
|
|
|
def dump_json(payload: Any) -> str:
|
|
return json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n"
|
|
|
|
|
|
def operator_slug(operator_id: str) -> str:
|
|
return operator_id.removeprefix("operator-builtin-").replace("-", "_")
|
|
|
|
|
|
def release_env() -> str:
|
|
value = os.getenv("OPERATOR_RELEASE_ENV", "").strip() or os.getenv("DEPLOY_ENV", "").strip()
|
|
if value == "dev":
|
|
return DEFAULT_RELEASE_ENV
|
|
return value or DEFAULT_RELEASE_ENV
|
|
|
|
|
|
def release_path(slug: str) -> pathlib.Path:
|
|
return OPERATOR_REPO_ROOT / slug / "release.json"
|
|
|
|
|
|
def git_commit() -> str:
|
|
try:
|
|
return (
|
|
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True)
|
|
.strip()
|
|
)
|
|
except Exception:
|
|
return ""
|
|
|
|
|
|
def image_digest(image_id: str) -> str:
|
|
if "@sha256:" not in image_id:
|
|
return ""
|
|
return "sha256:" + image_id.rsplit("@sha256:", 1)[1]
|
|
|
|
|
|
def image_registry(image: str) -> str:
|
|
return image.split("/", 1)[0] if "/" in image else ""
|
|
|
|
|
|
def image_tag(image: str) -> str:
|
|
tail = image.rsplit("/", 1)[-1]
|
|
if ":" not in tail:
|
|
return ""
|
|
return tail.rsplit(":", 1)[1]
|
|
|
|
|
|
def release_source_files(operator_dir: pathlib.Path) -> list[pathlib.Path]:
|
|
ignored = {"release.json", "repository.json"}
|
|
return sorted(
|
|
path
|
|
for path in operator_dir.rglob("*")
|
|
if path.is_file() and path.name not in ignored
|
|
and "__pycache__" not in path.parts and ".pytest_cache" not in path.parts
|
|
and path.suffix not in {".pyc", ".pyo"}
|
|
)
|
|
|
|
|
|
def source_tree_sha256(slug: str) -> str:
|
|
operator_dir = OPERATOR_REPO_ROOT / slug
|
|
digest = hashlib.sha256()
|
|
for path in release_source_files(operator_dir):
|
|
relative = path.relative_to(operator_dir).as_posix()
|
|
digest.update(relative.encode("utf-8"))
|
|
digest.update(b"\0")
|
|
digest.update(path.read_bytes())
|
|
digest.update(b"\0")
|
|
return digest.hexdigest()
|
|
|
|
|
|
def load_release(slug: str) -> dict[str, Any]:
|
|
path = release_path(slug)
|
|
if not path.exists():
|
|
raise RuntimeError(f"operator_release_manifest_missing: missing {path.relative_to(ROOT)}")
|
|
release = load_json(path)
|
|
validate_release(slug, release)
|
|
return release
|
|
|
|
|
|
def release_environment(release: dict[str, Any], env: str | None = None) -> dict[str, Any]:
|
|
selected_env = env or release_env()
|
|
releases = release.get("environmentReleases")
|
|
if isinstance(releases, dict) and isinstance(releases.get(selected_env), dict):
|
|
env_release = dict(releases[selected_env])
|
|
return {
|
|
**release,
|
|
**env_release,
|
|
"environment": selected_env,
|
|
}
|
|
return {**release, "environment": selected_env}
|
|
|
|
|
|
def release_image_reference(slug: str, env: str | None = None) -> tuple[str, str]:
|
|
release = release_environment(load_release(slug), env)
|
|
image = str(release.get("image") or "").strip()
|
|
image_id = str(release.get("imageId") or "").strip()
|
|
if not image or not DIGEST_RE.match(image_id):
|
|
raise RuntimeError(
|
|
f"operator_release_digest_drift: {slug} release must contain image and digest-pinned imageId"
|
|
)
|
|
return image, image_id
|
|
|
|
|
|
def validate_release(slug: str, release: dict[str, Any]) -> None:
|
|
path_label = release_path(slug).relative_to(ROOT)
|
|
if release.get("schemaVersion") != RELEASE_SCHEMA_VERSION:
|
|
raise RuntimeError(f"operator_release_manifest_missing: {path_label} schemaVersion must be {RELEASE_SCHEMA_VERSION}")
|
|
expected_operator_id = f"operator-builtin-{slug.replace('_', '-')}"
|
|
if release.get("operatorId") != expected_operator_id:
|
|
raise RuntimeError(
|
|
f"operator_release_manifest_missing: {path_label} operatorId must be {expected_operator_id}"
|
|
)
|
|
if release.get("slug") != slug:
|
|
raise RuntimeError(f"operator_release_digest_drift: {path_label} slug must be {slug}")
|
|
image = str(release.get("image") or "").strip()
|
|
image_id = str(release.get("imageId") or "").strip()
|
|
digest = str(release.get("digest") or "").strip()
|
|
if not image:
|
|
raise RuntimeError(f"operator_release_manifest_missing: {path_label} image is required")
|
|
if not DIGEST_RE.match(image_id):
|
|
raise RuntimeError(f"operator_release_digest_drift: {path_label} imageId must be repo@sha256:<64 hex>")
|
|
if digest and digest != image_digest(image_id):
|
|
raise RuntimeError(f"operator_release_digest_drift: {path_label} digest must match imageId")
|
|
source_tree = str(release.get("sourceTreeSha256") or "").strip()
|
|
if not re.fullmatch(r"[a-f0-9]{64}", source_tree):
|
|
raise RuntimeError(f"operator_release_manifest_missing: {path_label} sourceTreeSha256 is required")
|
|
current_source_tree = source_tree_sha256(slug)
|
|
if source_tree != current_source_tree:
|
|
raise RuntimeError(
|
|
f"operator_release_digest_drift: {path_label} sourceTreeSha256 {source_tree} does not match current {current_source_tree}"
|
|
)
|
|
|
|
|
|
def release_from_catalog_item(item: dict[str, Any], *, env: str | None = None) -> dict[str, Any]:
|
|
slug = operator_slug(item["id"])
|
|
image = str(item.get("image") or "").strip()
|
|
image_id = str(item.get("imageId") or "").strip()
|
|
digest = image_digest(image_id)
|
|
selected_env = env or release_env()
|
|
release = {
|
|
"schemaVersion": RELEASE_SCHEMA_VERSION,
|
|
"operatorId": item["id"],
|
|
"slug": slug,
|
|
"sourceCommit": git_commit(),
|
|
"sourceTreeSha256": source_tree_sha256(slug),
|
|
"image": image,
|
|
"imageId": image_id,
|
|
"digest": digest,
|
|
"registry": image_registry(image),
|
|
"tag": image_tag(image),
|
|
"builtAt": "",
|
|
"kanikoJob": "",
|
|
"acceptance": {
|
|
"projectId": "",
|
|
"workflowId": "",
|
|
"runId": "",
|
|
"artifactId": "",
|
|
"verifiedAt": "",
|
|
},
|
|
"environmentReleases": {
|
|
selected_env: {
|
|
"image": image,
|
|
"imageId": image_id,
|
|
"digest": digest,
|
|
"registry": image_registry(image),
|
|
"tag": image_tag(image),
|
|
}
|
|
},
|
|
}
|
|
return release
|