Add remote autoscaler daemon endpoint support
All checks were successful
buildbot/nix-eval Build done.
buildbot/nix-build Build done.
buildbot/nix-effects Build done.

This commit is contained in:
Abel Luck 2026-03-05 15:47:57 +01:00
parent 95021a4253
commit 679b5c8d07
11 changed files with 291 additions and 22 deletions

View file

@ -7,6 +7,7 @@ import socket
import time
from dataclasses import dataclass
from typing import Any
from urllib.parse import urlparse
@dataclass(frozen=True)
@ -45,10 +46,39 @@ class UnixSocketHTTPConnection(http.client.HTTPConnection):
class DaemonClient:
def __init__(self, socket_path: str, retry_policy: RetryPolicy) -> None:
def __init__(
self,
*,
retry_policy: RetryPolicy,
socket_path: str | None = None,
base_url: str | None = None,
auth_token: str | None = None,
) -> None:
if (socket_path is None) == (base_url is None):
raise ValueError("exactly one of socket_path or base_url must be set")
self._socket_path = socket_path
self._base_url = base_url
self._auth_token = auth_token.strip() if auth_token is not None else None
self._retry = retry_policy
self._base_path = ""
self._http_scheme = "http"
self._http_host = "localhost"
self._http_port = 80
if base_url is not None:
parsed = urlparse(base_url)
if parsed.scheme not in {"http", "https"}:
raise ValueError("base_url must use http or https scheme")
if parsed.hostname is None:
raise ValueError("base_url must include a hostname")
self._http_scheme = parsed.scheme
self._http_host = parsed.hostname
if parsed.port is not None:
self._http_port = parsed.port
elif parsed.scheme == "https":
self._http_port = 443
self._base_path = parsed.path.rstrip("/")
def post_json(
self,
path: str,
@ -136,12 +166,31 @@ class DaemonClient:
timeout_seconds: float,
payload: bytes | None,
) -> tuple[bytes, int]:
conn = UnixSocketHTTPConnection(self._socket_path, timeout=timeout_seconds)
request_path = path if path.startswith("/") else f"/{path}"
if self._base_path != "":
request_path = f"{self._base_path}{request_path}"
headers = {"Accept": "application/json"}
if payload is not None:
headers["Content-Type"] = "application/json"
if self._auth_token is not None:
headers["Authorization"] = f"Bearer {self._auth_token}"
conn: http.client.HTTPConnection
if self._socket_path is not None:
conn = UnixSocketHTTPConnection(self._socket_path, timeout=timeout_seconds)
else:
conn = (
http.client.HTTPSConnection(
self._http_host, self._http_port, timeout=timeout_seconds
)
if self._http_scheme == "https"
else http.client.HTTPConnection(
self._http_host, self._http_port, timeout=timeout_seconds
)
)
try:
conn.request(method=method, url=path, body=payload, headers=headers)
conn.request(method=method, url=request_path, body=payload, headers=headers)
response = conn.getresponse()
data = response.read()
return data, response.status