|
| 1 | +"""Scenario-driven install command. |
| 2 | +
|
| 3 | +Reads a `scenarios.yaml` (or standalone `instance.yaml`) and installs every |
| 4 | +artifact referenced — platform service binary, per-KAS binaries (each at |
| 5 | +its own pinned version), and encrypt/decrypt SDK CLIs. Writes |
| 6 | +`installed.json` next to the manifest so downstream tools (`otdf-local`, |
| 7 | +plugin skills) can locate the dist paths without re-resolving. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import json |
| 13 | +from pathlib import Path |
| 14 | +from typing import Annotated |
| 15 | + |
| 16 | +import typer |
| 17 | + |
| 18 | +from otdf_sdk_mgr.installers import InstallError, install_release |
| 19 | +from otdf_sdk_mgr.platform_installer import ( |
| 20 | + PlatformInstallError, |
| 21 | + install_helper_scripts, |
| 22 | + install_platform_release, |
| 23 | + install_platform_source, |
| 24 | +) |
| 25 | +from otdf_sdk_mgr.schema import KasPin, PlatformPin, Scenario, load_instance, load_scenario |
| 26 | + |
| 27 | + |
| 28 | +def _install_platform_pin(pin: PlatformPin | KasPin, label: str) -> dict[str, str]: |
| 29 | + if pin.image is not None: |
| 30 | + raise typer.BadParameter( |
| 31 | + f"{label}: container-image platform pins are not supported in v1; use dist or source" |
| 32 | + ) |
| 33 | + if pin.dist is not None: |
| 34 | + dist_dir = install_platform_release(pin.dist) |
| 35 | + return {"kind": "dist", "version": pin.dist, "path": str(dist_dir)} |
| 36 | + assert pin.source is not None # by schema invariant |
| 37 | + dist_dir = install_platform_source(pin.source.ref) |
| 38 | + return {"kind": "source", "ref": pin.source.ref, "path": str(dist_dir)} |
| 39 | + |
| 40 | + |
| 41 | +def install_scenario_cmd( |
| 42 | + path: Annotated[Path, typer.Argument(help="Path to scenarios.yaml or instance.yaml")], |
| 43 | + skip_scripts: Annotated[ |
| 44 | + bool, |
| 45 | + typer.Option("--skip-scripts", help="Skip refreshing helper scripts from main"), |
| 46 | + ] = False, |
| 47 | +) -> None: |
| 48 | + """Install every artifact declared by a scenarios.yaml or instance.yaml.""" |
| 49 | + if not path.exists(): |
| 50 | + typer.echo(f"Error: {path} not found", err=True) |
| 51 | + raise typer.Exit(1) |
| 52 | + |
| 53 | + raw_kind = _peek_kind(path) |
| 54 | + scenario: Scenario | None = None |
| 55 | + if raw_kind == "Scenario": |
| 56 | + scenario = load_scenario(path) |
| 57 | + instance = scenario.instance |
| 58 | + elif raw_kind == "Instance": |
| 59 | + instance = load_instance(path) |
| 60 | + else: |
| 61 | + typer.echo(f"Error: {path} has unknown kind {raw_kind!r}", err=True) |
| 62 | + raise typer.Exit(1) |
| 63 | + |
| 64 | + installed: dict[str, object] = {"manifest": str(path), "platform": None, "kas": {}, "sdks": {}} |
| 65 | + |
| 66 | + try: |
| 67 | + installed["platform"] = _install_platform_pin(instance.platform, "platform") |
| 68 | + for kas_name, kas_pin in instance.kas.items(): |
| 69 | + installed["kas"][kas_name] = _install_platform_pin(kas_pin, f"kas.{kas_name}") |
| 70 | + if not skip_scripts: |
| 71 | + install_helper_scripts() |
| 72 | + except PlatformInstallError as e: |
| 73 | + typer.echo(f"Error installing platform artifacts: {e}", err=True) |
| 74 | + raise typer.Exit(1) |
| 75 | + |
| 76 | + if scenario is not None: |
| 77 | + sdks = scenario.sdks.union() |
| 78 | + for sdk_name, sdk_pin in sdks.items(): |
| 79 | + try: |
| 80 | + dist_dir = install_release(sdk_name, sdk_pin.version, source=sdk_pin.source) |
| 81 | + installed["sdks"][sdk_name] = { |
| 82 | + "version": sdk_pin.version, |
| 83 | + "source": sdk_pin.source, |
| 84 | + "path": str(dist_dir), |
| 85 | + } |
| 86 | + except InstallError as e: |
| 87 | + typer.echo(f"Error installing SDK {sdk_name}: {e}", err=True) |
| 88 | + raise typer.Exit(1) |
| 89 | + |
| 90 | + out = path.parent / f"{path.stem}.installed.json" |
| 91 | + out.write_text(json.dumps(installed, indent=2) + "\n") |
| 92 | + typer.echo(f" Wrote {out}") |
| 93 | + |
| 94 | + |
| 95 | +def _peek_kind(path: Path) -> str | None: |
| 96 | + """Cheap pre-validation read so we can dispatch to the right model loader.""" |
| 97 | + from ruamel.yaml import YAML |
| 98 | + |
| 99 | + y = YAML(typ="safe") |
| 100 | + raw = y.load(path.read_text()) |
| 101 | + if isinstance(raw, dict): |
| 102 | + kind = raw.get("kind") |
| 103 | + return kind if isinstance(kind, str) else None |
| 104 | + return None |
0 commit comments