"""Create and verify non-secret release records used by Ansible.""" import argparse import ast import json import re import subprocess from datetime import datetime, timezone from pathlib import Path URLS = {"staging": "https://staging.yebilly.me", "production": "https://app.yebilly.me"} IMAGE = re.compile(r"git\.yebilly\.me/ssw590-group1/pricing@sha256:[0-9a-f]{64}") COMMIT = re.compile(r"[0-9a-f]{40}") def verify(record): required = {"release_image", "expected_commit", "configuration_commit", "expected_version", "expected_total", "proxy_network", "target_env", "public_url"} if not required <= record.keys() or record.keys() - required - {"build_url", "created_at"}: raise ValueError("Missing or unexpected release fields") if not IMAGE.fullmatch(record["release_image"]): raise ValueError("Release requires the team registry image and exact digest") for name in ("expected_commit", "configuration_commit"): value = record[name] if not COMMIT.fullmatch(value): raise ValueError(f"Invalid {name}") subprocess.run(["git", "merge-base", "--is-ancestor", value, "origin/main"], check=True) version = record["expected_version"] if version not in {"v1", "v2"}: raise ValueError("Unsupported application release") if record["expected_total"] != {"v1": 30, "v2": 36}[version]: raise ValueError("Unexpected business result") environment = record["target_env"] if record["public_url"] != URLS[environment] or record["proxy_network"] != "gitea_web": raise ValueError("Unexpected deployment target") def main(): parser = argparse.ArgumentParser() commands = parser.add_subparsers(dest="action", required=True) create = commands.add_parser("create") create.add_argument("--image", required=True) create.add_argument("--build-url", required=True) create.add_argument("--output", required=True) check = commands.add_parser("verify") check.add_argument("record") check.add_argument("--environment", choices=["staging", "production"]) args = parser.parse_args() if args.action == "verify": record = json.loads(Path(args.record).read_text()) verify(record) if args.environment and record["target_env"] != args.environment: raise ValueError("Release environment does not match the authorized pipeline stage") return values = {} for node in ast.parse(Path("delivery/app/app.py").read_text()).body: if isinstance(node, ast.Assign): for target in node.targets: if isinstance(target, ast.Name) and target.id in {"VERSION", "UNIT_PRICE"}: values[target.id] = ast.literal_eval(node.value) commit = subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip() record = { "release_image": args.image, "expected_commit": commit, "configuration_commit": commit, "expected_version": values["VERSION"], "expected_total": values["UNIT_PRICE"] * 3, "proxy_network": "gitea_web", "build_url": args.build_url, "created_at": datetime.now(timezone.utc).isoformat(), } output = Path(args.output) output.mkdir(parents=True, exist_ok=True) for environment, url in URLS.items(): target = dict(record, target_env=environment, public_url=url) verify(target) (output / f"{values['VERSION']}-{environment}.json").write_text(json.dumps(target, indent=2)) if __name__ == "__main__": main()