acupedia-ops/acupedia_ops/deployment/gitea_runner.py

46 lines
1.6 KiB
Python

from __future__ import annotations
from pathlib import Path
from typing import Any, Sequence
import yaml
from acupedia_ops.common.paths import repo_root
from acupedia_ops.ansible.runner import run_playbook
def _repo_root() -> Path:
"""Backward-compatible repository root helper."""
return repo_root()
def _parse_extra_vars(values: Sequence[str]) -> dict[str, Any]:
result: dict[str, Any] = {}
for value in values:
parsed = yaml.safe_load(value)
if isinstance(parsed, dict):
result.update(parsed)
continue
if "=" not in value:
raise ValueError(f"Invalid --extra-var {value!r}; expected key=value or YAML mapping")
key, raw = value.split("=", 1)
result[key] = yaml.safe_load(raw)
return result
def provision_runner(*, inventory: Path, registration_token: str, limit: str | None = None,
extra_vars: Sequence[str] = (), force_reregister: bool = False) -> None:
variables = _parse_extra_vars(extra_vars)
variables["gitea_runner_registration_token"] = registration_token
if force_reregister:
variables["gitea_runner_force_reregister"] = True
run_playbook(playbook="gitea-runner.yml", inventory=inventory, extra_vars=variables, limit=limit)
def remove_runner(*, inventory: Path, limit: str | None = None, remove_data: bool = False,
extra_vars: Sequence[str] = ()) -> None:
variables = _parse_extra_vars(extra_vars)
if remove_data:
variables["gitea_runner_remove_data"] = True
run_playbook(playbook="gitea-runner-remove.yml", inventory=inventory, extra_vars=variables, limit=limit)