Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ docker compose --env-file .env.example config --quiet
docker compose up --build
```

The probe validates `PROBE_ID`, `BACKEND_URL`, and `PROBE_TOKEN`, prints `config valid`, and exits. For a non-Docker check, run `uv run --locked python -m probe` with those variables set.
The probe validates `PROBE_ID`, `BACKEND_URL`, and `PROBE_TOKEN`, then runs the check-in, test, and upload loop until stopped. For a non-Docker check, run `uv run --locked python -m probe` with those variables set.

Probe unit tests live in `probe/tests/`. `uv` reads `probe/.python-version`, so run the commands from `probe/`. Dependencies are declared in `probe/pyproject.toml`; add new Python dependencies with `uv add` and commit `probe/pyproject.toml` and `probe/uv.lock` together.

Expand Down
2 changes: 1 addition & 1 deletion probe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Bootstrap starter for the probe.

The current implementation validates `PROBE_ID`, `BACKEND_URL`, and `PROBE_TOKEN` when run as `python -m probe`, prints `config valid`, and exits. Upcoming tickets cover polling, SQLite storage, test execution, and testing.
When run as `python -m probe`, it validates `PROBE_ID`, `BACKEND_URL`, and `PROBE_TOKEN`, then loops: check in, download and hash-check the assigned `run.sh` and `test.json`, run `bash run.sh` (30 s deadline, without `PROBE_TOKEN` in its environment), and upload the result. With no assignment it waits 30 s and checks again. A server or download error stops the probe with a message. `SIGTERM` or Ctrl-C stops it after the current pass. Measurement intervals, retries, caching, and queues come later.

## Configuration

Expand Down
2 changes: 2 additions & 0 deletions probe/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ services:
probe:
build:
context: .
# Let a pass in progress (script deadline plus upload) finish on stop.
stop_grace_period: 1m
env_file:
- path: .env
required: false
Expand Down
15 changes: 13 additions & 2 deletions probe/src/probe/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""Probe bootstrap configuration check."""
"""Probe entry point: validate configuration, then run the probe loop."""

from __future__ import annotations

import os
import signal
import sys
import threading
import uuid
from urllib.parse import urlsplit

from probe.runner import Config, ProbeError, run_loop


def validate_config() -> list[str]:
"""Return configuration errors without exposing secret values."""
Expand Down Expand Up @@ -50,7 +54,14 @@ def main() -> int:
print(error, file=sys.stderr)
return 1

print("config valid")
stop = threading.Event()
signal.signal(signal.SIGTERM, lambda *_: stop.set())
signal.signal(signal.SIGINT, lambda *_: stop.set())
try:
run_loop(Config.from_env(), stop)
except ProbeError as error:
print(f"probe stopped: {error}", file=sys.stderr)
return 1
return 0


Expand Down
181 changes: 181 additions & 0 deletions probe/src/probe/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
"""Probe loop: check in, run the assigned test, and upload its measurement."""

from __future__ import annotations

import hashlib
import json
import os
import signal
import subprocess
import sys
import tempfile
import threading
import uuid
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen

CHECK_IN_PATH = "/functions/v1/probe-check-in"
MEASUREMENTS_PATH = "/functions/v1/probe-measurements"
IDLE_WAIT_SECONDS = 30
SCRIPT_TIMEOUT_SECONDS = 30
HTTP_TIMEOUT_SECONDS = 30


class ProbeError(Exception):
"""A server or download failure that ends the run."""


@dataclass(frozen=True)
class Config:
probe_id: str
backend_url: str
probe_token: str

@classmethod
def from_env(cls) -> Config:
return cls(
probe_id=os.environ["PROBE_ID"].strip(),
backend_url=os.environ["BACKEND_URL"].strip().rstrip("/"),
probe_token=os.environ["PROBE_TOKEN"].strip(),
)


def _post(config: Config, path: str, body: dict) -> object:
request = Request(
config.backend_url + path,
data=json.dumps(body).encode(),
method="POST",
headers={"Content-Type": "application/json", "X-Probe-Token": config.probe_token},
)
try:
with urlopen(request, timeout=HTTP_TIMEOUT_SECONDS) as response:
return json.load(response)
except HTTPError as error:
raise ProbeError(f"{path} returned HTTP {error.code}") from None
except (URLError, OSError) as error:
raise ProbeError(f"{path} failed: {error}") from None
except ValueError:
# No error text: a rejected header value would echo the token.
raise ProbeError(f"{path} returned a malformed response or bad request") from None


def _download(config: Config, file: dict) -> bytes:
"""Fetch a signed path without the probe token and verify its hash."""
try:
path, expected = file["download_path"], file["sha256"].lower()
except (KeyError, TypeError, AttributeError):
raise ProbeError("check-in returned a malformed download") from None
# The signed query token is not the probe token, but keep it out of logs anyway.
name = path.split("?", 1)[0]
try:
with urlopen(config.backend_url + path, timeout=HTTP_TIMEOUT_SECONDS) as response:
data = response.read()
except (URLError, OSError) as error:
raise ProbeError(f"download {name} failed: {error}") from None
if hashlib.sha256(data).hexdigest() != expected:
raise ProbeError(f"download {name} failed its hash check")
return data


def check_in(config: Config) -> dict | None:
body = _post(config, CHECK_IN_PATH, {"probe_id": config.probe_id})
if not isinstance(body, dict) or "assignment" not in body:
raise ProbeError("check-in returned a malformed response")
return body["assignment"]


def run_script(test_dir: str, timeout: float) -> tuple[str, int | None, dict]:
"""Run run.sh and return (status, result_code, result_data)."""
env = {k: v for k, v in os.environ.items() if k != "PROBE_TOKEN"}
try:
proc = subprocess.Popen(
["bash", "run.sh"],
cwd=test_dir,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
start_new_session=True,
)
except OSError as error:
print(f"could not start test: {error}", file=sys.stderr)
return "failed", None, {}
with proc:
try:
stdout, stderr = proc.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
# Kill the whole group so children like ping don't hold the pipes open.
if hasattr(os, "killpg"):
os.killpg(proc.pid, signal.SIGKILL)
else:
proc.kill()
proc.communicate()
return "timeout", None, {}
if stderr:
sys.stderr.write(stderr.decode(errors="replace"))
if proc.returncode == 0:
try:
output = json.loads(stdout)
except ValueError:
output = None
if isinstance(output, dict) and isinstance(output.get("result_data"), dict):
return "completed", 0, output["result_data"]
return "failed", proc.returncode, {}


def _now() -> str:
return datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")


def run_once(config: Config, script_timeout: float = SCRIPT_TIMEOUT_SECONDS) -> bool:
"""Run one pass. Return False when there was no assignment."""
assignment = check_in(config)
if assignment is None:
return False
try:
revision_id = assignment["test_revision_id"]
script, definition = assignment["script"], assignment["definition"]
except (KeyError, TypeError):
raise ProbeError("check-in returned a malformed assignment") from None

# On Windows the folder can stay locked for a moment after bash exits.
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as test_dir:
Path(test_dir, "run.sh").write_bytes(_download(config, script))
Path(test_dir, "test.json").write_bytes(_download(config, definition))
run_id = str(uuid.uuid4())
started_at = _now()
status, result_code, result_data = run_script(test_dir, script_timeout)
finished_at = _now()

_post(
config,
MEASUREMENTS_PATH,
{
"schema_version": 1,
"id": run_id,
"probe_id": config.probe_id,
"test_revision_id": revision_id,
"started_at": started_at,
"finished_at": finished_at,
"status": status,
"result_code": result_code,
"result_data": result_data,
"location": None,
},
)
print(f"run {run_id} of revision {revision_id}: {status}")
return True


def run_loop(
config: Config,
stop: threading.Event,
idle_wait: float = IDLE_WAIT_SECONDS,
script_timeout: float = SCRIPT_TIMEOUT_SECONDS,
) -> None:
"""Run passes until stop is set; a pass in progress finishes first."""
while not stop.is_set():
if not run_once(config, script_timeout):
stop.wait(idle_wait)
Loading
Loading