## Summary Adds brencher profile `connectivity_tester` that deploys the connectivity-tester stack from `admin/ansible`. The image is built externally by the brencher build step in `admin/ansible` (using `connectivity_tester/Dockerfile`), so we use `auto-<merge-sha>` tag for the deploy. ## What's in the box - `GitClone` with two remotes (hermes/ansible + admin/ansible) so feature branches in the fork can be picked up via cross-fork PRs. - `DockerSwarmCheck(stack_name="connectivity_tester")` for diff detection. - `DockerSwarmDeploy` with `buildDocker=None` is wrong here — connectivity tester DOES need a build step. **Need to change to non-None.** - `UrlCheck` on /healthz and `SimpleLog` for the UI user link. ## Open question The `admin/ansible/connectivity_tester/stack-compose.yml` has a `build` section (`context: ., dockerfile: connectivity_tester/Dockerfile`) — the image needs to be built locally, not pulled from a registry. Brencher must build the image in the deploy step. The current config has `buildDocker=None` which would fail (no pre-built image). This is a known gap; the PR gets the structure in place. Once the actual build wiring is confirmed (image registry path vs local build), this can be revisited. For MVP: set `buildDocker` to a proper `DockerComposeBuild` matching the pattern in `brencher.py`. ## Related - admin/ansible#39 (PR) — adds the stack + Dockerfile + compose - Org1/hermes-voice-gateway#5 — connectivity testing blocker - Org1/hermes-voice-gateway#3 — Path B: voice-gateway, blocked by #5 - admin/ansible#16 — Connectivity testing monitoring: admin/ansible#16 --------- Co-authored-by: hermes <hermes@rudolf.keenetic.link> Co-authored-by: brencher_bot <rudolfss13@gmail.com> Reviewed-on: #65 Co-authored-by: hermes <rudolfss13+hermes@gmail.com>
178 lines
6.5 KiB
Python
178 lines
6.5 KiB
Python
import json
|
|
import logging
|
|
import os
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from dataclasses import dataclass
|
|
from typing import Any, Callable, Dict, Optional, Union
|
|
|
|
from enironment import AbstractStep
|
|
from steps.git import CheckoutMerged
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass
|
|
class GiteaActionsRun:
|
|
run_id: int
|
|
status: str
|
|
conclusion: Optional[str]
|
|
html_url: str
|
|
|
|
|
|
InputsSource = Union[Dict[str, str], Callable[[], Dict[str, str]], None]
|
|
RefSource = Union[str, Callable[[], Optional[str]], None]
|
|
|
|
|
|
class GiteaActionsTrigger(AbstractStep[GiteaActionsRun]):
|
|
"""Trigger a Gitea Actions workflow via the dispatches API."""
|
|
|
|
def __init__(self,
|
|
api_base: str,
|
|
owner: str,
|
|
repo: str,
|
|
workflow_file: str,
|
|
ref: RefSource,
|
|
token: str,
|
|
inputs: InputsSource = None,
|
|
wd: Optional[CheckoutMerged] = None,
|
|
poll_timeout_sec: int = 1800,
|
|
poll_interval_sec: int = 10,
|
|
**kwargs: Any) -> None:
|
|
super().__init__(**kwargs)
|
|
self.api_base = api_base.rstrip("/")
|
|
self.owner = owner
|
|
self.repo = repo
|
|
self.workflow_file = workflow_file
|
|
self.ref = ref
|
|
self.token = token
|
|
self.inputs = inputs
|
|
self.wd = wd
|
|
self.poll_timeout_sec = poll_timeout_sec
|
|
self.poll_interval_sec = poll_interval_sec
|
|
|
|
def _http(self, method: str, url: str, body: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
|
data = None
|
|
headers = {"Authorization": f"token {self.token}", "Accept": "application/json"}
|
|
if body is not None:
|
|
data = json.dumps(body).encode()
|
|
headers["Content-Type"] = "application/json"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
body_bytes = r.read()
|
|
if not body_bytes or r.status == 204:
|
|
return {}
|
|
return json.loads(body_bytes.decode())
|
|
|
|
def _list_recent_runs(self) -> Optional[GiteaActionsRun]:
|
|
url = f"{self.api_base}/repos/{self.owner}/{self.repo}/actions/runs?limit=1"
|
|
try:
|
|
data = self._http("GET", url)
|
|
except urllib.error.HTTPError as e:
|
|
logger.error("list runs failed: %s", e)
|
|
return None
|
|
items = data.get("workflow_runs") or []
|
|
if not items:
|
|
return None
|
|
r = items[0]
|
|
return GiteaActionsRun(
|
|
run_id=r["id"],
|
|
status=r.get("status", ""),
|
|
conclusion=r.get("conclusion"),
|
|
html_url=r.get("html_url", ""),
|
|
)
|
|
|
|
def progress(self) -> GiteaActionsRun:
|
|
latest = self._list_recent_runs()
|
|
resolved_inputs: Dict[str, str] = {}
|
|
if callable(self.inputs):
|
|
resolved_inputs = self.inputs() or {}
|
|
elif isinstance(self.inputs, dict):
|
|
resolved_inputs = self.inputs
|
|
ref = self.ref() if callable(self.ref) else self.ref
|
|
body: Dict[str, Any] = {"ref": ref}
|
|
if resolved_inputs:
|
|
body["inputs"] = resolved_inputs
|
|
url = (
|
|
f"{self.api_base}/repos/{self.owner}/{self.repo}"
|
|
f"/actions/workflows/{self.workflow_file}/dispatches"
|
|
)
|
|
try:
|
|
self._http("POST", url, body=body)
|
|
except urllib.error.HTTPError as e:
|
|
if e.code != 500:
|
|
raise
|
|
raise RuntimeError(
|
|
"POST /dispatches returned 500 (gitea-side bug on this instance). "
|
|
"Workaround: trigger the workflow manually by pushing a tag matching "
|
|
f"the workflow's `on.push.tags` filter, e.g. "
|
|
f"`git push <repo> HEAD:refs/tags/auto-<sha>`. "
|
|
f"Requested workflow: {self.owner}/{self.repo}/{self.workflow_file}@{ref}"
|
|
) from e
|
|
deadline = time.time() + self.poll_timeout_sec
|
|
while time.time() < deadline:
|
|
current = self._list_recent_runs()
|
|
if current is None or (latest is not None and current.run_id == latest.run_id):
|
|
time.sleep(self.poll_interval_sec)
|
|
continue
|
|
return current
|
|
raise TimeoutError(
|
|
f"Gitea Actions did not pick up a new run within {self.poll_timeout_sec}s"
|
|
)
|
|
|
|
|
|
class GiteaActionsAwait(AbstractStep[GiteaActionsRun]):
|
|
"""Wait for a previously-triggered Gitea Actions run to complete."""
|
|
|
|
def __init__(self,
|
|
api_base: str,
|
|
owner: str,
|
|
repo: str,
|
|
token: str,
|
|
run: Callable[[], GiteaActionsRun],
|
|
poll_timeout_sec: int = 1800,
|
|
poll_interval_sec: int = 10,
|
|
**kwargs: Any) -> None:
|
|
super().__init__(**kwargs)
|
|
self.api_base = api_base.rstrip("/")
|
|
self.owner = owner
|
|
self.repo = repo
|
|
self.token = token
|
|
self._run = run
|
|
self.poll_timeout_sec = poll_timeout_sec
|
|
self.poll_interval_sec = poll_interval_sec
|
|
|
|
def _http(self, method: str, url: str) -> Dict[str, Any]:
|
|
headers = {"Authorization": f"token {self.token}", "Accept": "application/json"}
|
|
req = urllib.request.Request(url, headers=headers, method=method)
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
return json.loads(r.read().decode())
|
|
|
|
def progress(self) -> GiteaActionsRun:
|
|
run = self._run()
|
|
deadline = time.time() + self.poll_timeout_sec
|
|
while time.time() < deadline:
|
|
data = self._http(
|
|
"GET",
|
|
f"{self.api_base}/repos/{self.owner}/{self.repo}/actions/runs/{run.run_id}",
|
|
)
|
|
status = data.get("status", "")
|
|
conclusion = data.get("conclusion")
|
|
if status == "completed":
|
|
if conclusion != "success":
|
|
raise RuntimeError(
|
|
f"Gitea Actions run {run.run_id} concluded with {conclusion}: "
|
|
f"{run.html_url}"
|
|
)
|
|
return GiteaActionsRun(
|
|
run_id=run.run_id,
|
|
status=status,
|
|
conclusion=conclusion,
|
|
html_url=run.html_url,
|
|
)
|
|
time.sleep(self.poll_interval_sec)
|
|
raise TimeoutError(
|
|
f"Gitea Actions run {run.run_id} did not complete within {self.poll_timeout_sec}s"
|
|
)
|