Files
tracer-cloud--opensre/surfaces/cli/wizard/probes.py
T
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

77 lines
2.5 KiB
Python

"""Reachability probes for the quickstart wizard."""
from __future__ import annotations
import os
from dataclasses import asdict, dataclass
from pathlib import Path
from urllib.error import URLError
from urllib.parse import urlparse
from urllib.request import Request, urlopen
from config.config import get_tracer_base_url
from surfaces.cli.wizard.config import PROJECT_ENV_PATH
@dataclass(frozen=True)
class ProbeResult:
"""A lightweight reachability result."""
target: str
reachable: bool
detail: str
def as_dict(self) -> dict[str, object]:
"""Convert the result to a JSON-friendly dict."""
return asdict(self)
def _is_writable(path: Path) -> bool:
if path.exists():
return os.access(path, os.W_OK)
parent = path.parent
while not parent.exists() and parent != parent.parent:
parent = parent.parent
return os.access(parent, os.W_OK)
def _open_probe_request(request: Request, timeout_seconds: float):
return urlopen(request, timeout=timeout_seconds) # nosemgrep
def probe_local_target(store_path: Path) -> ProbeResult:
"""Check whether the local wizard targets are writable."""
writable = _is_writable(store_path) and _is_writable(PROJECT_ENV_PATH)
detail = f"Local config targets: {store_path} and {PROJECT_ENV_PATH}"
if not writable:
detail = f"Local config is not writable: {store_path} or {PROJECT_ENV_PATH}"
return ProbeResult(target="local", reachable=writable, detail=detail)
def probe_remote_target(timeout_seconds: float = 3.0) -> ProbeResult:
"""Probe the hosted Tracer base URL used for future remote setup."""
url = get_tracer_base_url()
parsed = urlparse(url)
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
return ProbeResult(
target="remote",
reachable=False,
detail=f"Tracer remote target has an invalid URL: {url}",
)
request = Request(url, method="GET")
try:
with _open_probe_request(request, timeout_seconds) as response:
status = getattr(response, "status", 200)
return ProbeResult(
target="remote",
reachable=200 <= status < 500,
detail=f"Tracer remote target reachable at {url} (HTTP {status})",
)
except URLError as err:
reason = getattr(err, "reason", err)
return ProbeResult(
target="remote",
reachable=False,
detail=f"Tracer remote target unreachable at {url}: {reason}",
)