from __future__ import annotations import os import subprocess from pathlib import Path from typing import Sequence def _repo_root() -> Path: return Path(__file__).resolve().parents[2] def _ansible_playbook() -> str: return os.environ.get("ANSIBLE_PLAYBOOK_BIN", "ansible-playbook") def provision_runner( *, inventory: Path, registration_token: str, limit: str | None = None, extra_vars: Sequence[str] = (), force_reregister: bool = False, ) -> None: repo_root = _repo_root() playbook = repo_root / "ansible" / "playbooks" / "gitea-runner.yml" command = [ _ansible_playbook(), "-i", str(inventory), str(playbook), "--extra-vars", f"gitea_runner_registration_token={registration_token}", ] if force_reregister: command.extend( [ "--extra-vars", "gitea_runner_force_reregister=true", ] ) for value in extra_vars: command.extend(["--extra-vars", value]) if limit: command.extend(["--limit", limit]) subprocess.run(command, cwd=repo_root, check=True) def remove_runner( *, inventory: Path, limit: str | None = None, remove_data: bool = False, extra_vars: Sequence[str] = (), ) -> None: repo_root = _repo_root() playbook = repo_root / "ansible" / "playbooks" / "gitea-runner-remove.yml" command = [ _ansible_playbook(), "-i", str(inventory), str(playbook), ] if remove_data: command.extend( [ "--extra-vars", "gitea_runner_remove_data=true", ] ) for value in extra_vars: command.extend(["--extra-vars", value]) if limit: command.extend(["--limit", limit]) subprocess.run(command, cwd=repo_root, check=True)