forked from wrenn/wrenn
Replaces the hardcoded CP_HOST_AGENT_ADDR single-agent setup with a DB-driven registration system supporting multiple host agents (BYOC). Key changes: - Host agents register via one-time token, receive a 7-day JWT + 60-day refresh token; heartbeat loop auto-refreshes on 401/403 and pauses all sandboxes if refresh fails - HostClientPool: lazy Connect RPC client cache keyed by host ID, replacing the single static agent client throughout the API and service layers - RoundRobinScheduler: picks an online host for each new sandbox via ListActiveHosts; extensible for future scheduling strategies - HostMonitor (replaces Reconciler): passive heartbeat staleness check marks hosts unreachable and sandboxes missing after 90s; active reconciliation per online host restores missing-but-alive sandboxes and stops orphans - Graceful host delete: returns 409 with affected sandbox list without ?force=true; force-delete destroys sandboxes then evicts pool client - Snapshot delete broadcasts to all online hosts (templates have no host_id) - sandbox.Manager.PauseAll: pauses all running VMs on CP connectivity loss - New migration: host_refresh_tokens table with token rotation (issue-then- revoke ordering to prevent lockout on mid-rotation crash) - New sandbox status 'missing' (reversible, unlike 'stopped') and host status 'unreachable'; both reflected in OpenAPI spec - Fix: refresh token auth failure now returns 401 (was 400 via generic 'invalid' substring match in serviceErrToHTTP)
85 lines
2.5 KiB
SQL
85 lines
2.5 KiB
SQL
-- name: InsertHost :one
|
|
INSERT INTO hosts (id, type, team_id, provider, availability_zone, created_by)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
RETURNING *;
|
|
|
|
-- name: GetHost :one
|
|
SELECT * FROM hosts WHERE id = $1;
|
|
|
|
-- name: ListHosts :many
|
|
SELECT * FROM hosts ORDER BY created_at DESC;
|
|
|
|
-- name: ListHostsByType :many
|
|
SELECT * FROM hosts WHERE type = $1 ORDER BY created_at DESC;
|
|
|
|
-- name: ListHostsByTeam :many
|
|
SELECT * FROM hosts WHERE team_id = $1 AND type = 'byoc' ORDER BY created_at DESC;
|
|
|
|
-- name: ListHostsByStatus :many
|
|
SELECT * FROM hosts WHERE status = $1 ORDER BY created_at DESC;
|
|
|
|
-- name: RegisterHost :execrows
|
|
UPDATE hosts
|
|
SET arch = $2,
|
|
cpu_cores = $3,
|
|
memory_mb = $4,
|
|
disk_gb = $5,
|
|
address = $6,
|
|
status = 'online',
|
|
last_heartbeat_at = NOW(),
|
|
updated_at = NOW()
|
|
WHERE id = $1 AND status = 'pending';
|
|
|
|
-- name: UpdateHostStatus :exec
|
|
UPDATE hosts SET status = $2, updated_at = NOW() WHERE id = $1;
|
|
|
|
-- name: UpdateHostHeartbeat :exec
|
|
UPDATE hosts SET last_heartbeat_at = NOW(), updated_at = NOW() WHERE id = $1;
|
|
|
|
-- name: DeleteHost :exec
|
|
DELETE FROM hosts WHERE id = $1;
|
|
|
|
-- name: AddHostTag :exec
|
|
INSERT INTO host_tags (host_id, tag) VALUES ($1, $2) ON CONFLICT DO NOTHING;
|
|
|
|
-- name: RemoveHostTag :exec
|
|
DELETE FROM host_tags WHERE host_id = $1 AND tag = $2;
|
|
|
|
-- name: GetHostTags :many
|
|
SELECT tag FROM host_tags WHERE host_id = $1 ORDER BY tag;
|
|
|
|
-- name: ListHostsByTag :many
|
|
SELECT h.* FROM hosts h
|
|
JOIN host_tags ht ON ht.host_id = h.id
|
|
WHERE ht.tag = $1
|
|
ORDER BY h.created_at DESC;
|
|
|
|
-- name: InsertHostToken :one
|
|
INSERT INTO host_tokens (id, host_id, created_by, expires_at)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING *;
|
|
|
|
-- name: MarkHostTokenUsed :exec
|
|
UPDATE host_tokens SET used_at = NOW() WHERE id = $1;
|
|
|
|
-- name: GetHostTokensByHost :many
|
|
SELECT * FROM host_tokens WHERE host_id = $1 ORDER BY created_at DESC;
|
|
|
|
-- name: GetHostByTeam :one
|
|
SELECT * FROM hosts WHERE id = $1 AND team_id = $2;
|
|
|
|
-- name: ListActiveHosts :many
|
|
-- Returns all hosts that have completed registration (not pending/offline).
|
|
SELECT * FROM hosts WHERE status NOT IN ('pending', 'offline') ORDER BY created_at;
|
|
|
|
-- name: UpdateHostHeartbeatAndStatus :exec
|
|
-- Updates last_heartbeat_at and transitions unreachable hosts back to online.
|
|
UPDATE hosts
|
|
SET last_heartbeat_at = NOW(),
|
|
status = CASE WHEN status = 'unreachable' THEN 'online' ELSE status END,
|
|
updated_at = NOW()
|
|
WHERE id = $1;
|
|
|
|
-- name: MarkHostUnreachable :exec
|
|
UPDATE hosts SET status = 'unreachable', updated_at = NOW() WHERE id = $1;
|