forked from longtao.wu/droboticflow
72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Validate immutable base arguments for selected Kaniko operator builds."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import pathlib
|
|
import re
|
|
import sys
|
|
|
|
|
|
BASE_ARGS = {
|
|
"OPERATOR_BASE_IMAGE": "operator_base",
|
|
"OPERATOR_GPU_BASE_IMAGE": "operator_gpu_base",
|
|
"OPERATOR_HOLOBRAIN_BASE_IMAGE": "operator_holobrain_base",
|
|
"OPERATOR_YOLO_BASE_IMAGE": "operator_yolo_base",
|
|
}
|
|
_ARG = re.compile(r"^\s*ARG\s+(OPERATOR(?:_GPU|_HOLOBRAIN|_YOLO)?_BASE_IMAGE)(?:=|\s|$)", re.MULTILINE)
|
|
_DIGEST = re.compile(r"^.+@sha256:[a-f0-9]{64}$")
|
|
|
|
|
|
def required_base_args(dockerfile: pathlib.Path) -> list[str]:
|
|
try:
|
|
text = dockerfile.read_text(encoding="utf-8")
|
|
except OSError as exc:
|
|
raise RuntimeError(f"cannot read operator Dockerfile {dockerfile}: {exc}") from exc
|
|
return sorted(set(_ARG.findall(text)))
|
|
|
|
|
|
def validate(dockerfiles: list[pathlib.Path], values: dict[str, str], require_immutable: bool) -> list[dict[str, object]]:
|
|
result: list[dict[str, object]] = []
|
|
for dockerfile in dockerfiles:
|
|
base_args = required_base_args(dockerfile)
|
|
selected: dict[str, str] = {}
|
|
for arg in base_args:
|
|
value = values[BASE_ARGS[arg]].strip()
|
|
if not value:
|
|
raise RuntimeError(f"{dockerfile}: {arg} is required")
|
|
if require_immutable and not _DIGEST.fullmatch(value):
|
|
raise RuntimeError(f"{dockerfile}: {arg} must be digest-pinned when operator base builds are disabled")
|
|
selected[arg] = value
|
|
result.append({"dockerfile": str(dockerfile), "baseArgs": selected})
|
|
return result
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--dockerfile", action="append", default=[], required=True)
|
|
parser.add_argument("--operator-base", default="")
|
|
parser.add_argument("--operator-gpu-base", default="")
|
|
parser.add_argument("--operator-holobrain-base", default="")
|
|
parser.add_argument("--operator-yolo-base", default="")
|
|
parser.add_argument("--require-immutable", action="store_true")
|
|
args = parser.parse_args()
|
|
values = {
|
|
"operator_base": args.operator_base,
|
|
"operator_gpu_base": args.operator_gpu_base,
|
|
"operator_holobrain_base": args.operator_holobrain_base,
|
|
"operator_yolo_base": args.operator_yolo_base,
|
|
}
|
|
try:
|
|
result = validate([pathlib.Path(value) for value in args.dockerfile], values, args.require_immutable)
|
|
except RuntimeError as exc:
|
|
print(f"ERROR: {exc}", file=sys.stderr)
|
|
return 1
|
|
print(json.dumps(result, sort_keys=True))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|