Remove the remote-host (agent) integration (0.48.0)
StackPilot now manages exactly one Docker host: the one it runs on. The
stackpilot-agent sidecar and everything that proxied to it are gone — 4721
lines deleted against 657 added.
Deleted outright: agent/ (image, compose, env), agent_app.py, models/agent.py,
routers/agents.py (1200 lines), services/agent_service.py, the agent API client,
RemoteStackDetail, the host components and AgentStacksSection. That removes 57
API routes and the three /ws/agent-* proxies.
Threaded out everywhere else, which was the bulk of the work. Every API module
carried an optional agentId that switched the base path; every page that listed
Docker objects rendered one section per host behind a HostHeader; Files had a
host switcher; the New Stack editor and the template dialog had host selectors;
schedules, auto-update policies and stack summaries carried agent_id. All of it
is gone, and the typechecker drove the sweep — 85 files touched, tsc and the
build clean.
Two things the removal exposed as dead weight rather than merely unused:
compose_service kept an in-process busy set purely because the agent needed a
lock and has no database. With the agent gone that was a second source of truth
next to the real DB lock, so it is deleted; compute_status now reports only what
the containers say and the two callers that want "updating" overlay the lock.
StacksTable's linkBase prop only ever existed to point at /hosts/{id}/stacks.
The dashboard's "Hosts 1/1 online" KPI can no longer say anything else, so the
tile and the KPIs behind it are gone and the row is five wide.
Upgrading matters here. An existing install still has an agent table holding
each remote host's URL and bearer token — full Docker control of that host,
sitting in the database with nothing left to use it. _drop_removed_schema drops
it on first start, and drops the agent_id columns where the SQLite build
supports DROP COLUMN. Each statement runs in its own transaction on purpose: a
failed DDL poisons the transaction it is in, so sharing one would let an
unsupported column drop take the table drop down with it. test_agent_removal
covers both branches plus the fresh-install and idempotent cases, and an
end-to-end run against a seeded pre-0.48 database confirms the table is gone and
every /api/agents route answers 404.
Docstrings that justified a design by "shared with the agent, which has no
database" were rewritten rather than left lying: update_service's persistence
callback and image_status_store are still the right split (registry logic stays
testable without a database), but for that reason now, not the old one. The
README's multi-host sections are removed and an upgrade note explains what to do
with running agent containers; ROADMAP keeps its history behind a note saying
the feature it describes no longer exists.
CI no longer builds or pushes stackpilot-agent.
735 tests pass, ruff and tsc clean.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dk43rmEeRfYi5wsLDbmfyG
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
"""Fleet aggregate for the dashboard cockpit.
|
||||
"""Host aggregate for the dashboard cockpit.
|
||||
|
||||
One read-only call rolls up every host (local + agents) into a "needs
|
||||
attention" list, headline KPIs and a per-host resource view. It is cheap by
|
||||
construction: a single container *summary* list (no per-container inspect)
|
||||
drives the local figures, image freshness comes from the cache the
|
||||
update-service background loop already maintains, and per agent it makes a
|
||||
small bounded set of HTTP fetches that degrade gracefully on failure.
|
||||
One read-only call rolls the host up into a "needs attention" list, headline
|
||||
KPIs and a resource view. It is cheap by construction: a single container
|
||||
*summary* list (no per-container inspect) drives the figures, and image
|
||||
freshness comes from the cache the update-service background loop already
|
||||
maintains.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -15,15 +14,13 @@ from datetime import datetime, timedelta, timezone
|
||||
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from database import engine
|
||||
from docker_client import DockerError, get_client, safe_call
|
||||
from models.agent import Agent
|
||||
from models.backup_schedule import BackupSchedule
|
||||
from services import agent_service, compose_service, update_service
|
||||
from services import compose_service, update_service
|
||||
|
||||
COMPOSE_LABEL = compose_service.COMPOSE_LABEL
|
||||
|
||||
# Fleet aggregate: cached briefly because each call may fan out to every agent.
|
||||
# Cached briefly: the dashboard polls this and the rollup is not free.
|
||||
FLEET_TTL = 25.0
|
||||
DISK_PRESSURE = 0.85 # disk used fraction above which a host needs attention
|
||||
MEM_PRESSURE = 0.90 # memory used fraction above which a host needs attention
|
||||
@@ -73,14 +70,12 @@ def _is_updated(containers: list[dict], cache: dict[str, dict]) -> bool:
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Fleet aggregate (one call → "needs attention" + KPIs across every host)
|
||||
# Host aggregate (one call → "needs attention" + KPIs)
|
||||
#
|
||||
# The dashboard used to poll each agent individually and recombine the numbers
|
||||
# client-side. ``compute_fleet`` does the fan-out server-side instead: one local
|
||||
# Docker pass plus, per online agent, a small set of system/stacks/updates
|
||||
# fetches — all wrapped so a slow or broken agent degrades to "offline" rather
|
||||
# than stalling the whole view. Cached for FLEET_TTL because the fan-out is not
|
||||
# free.
|
||||
# The dashboard used to fetch these numbers per stack and recombine them
|
||||
# client-side. ``compute_fleet`` rolls them up server-side instead, off one
|
||||
# Docker pass plus the update cache, and holds the result for FLEET_TTL because
|
||||
# the dashboard polls it.
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
# Stack-status buckets the status bar / KPIs are built from. Anything reporting
|
||||
@@ -189,98 +184,12 @@ def _local_host() -> tuple[dict, list[dict]]:
|
||||
return host, attention
|
||||
|
||||
|
||||
def _offline_host(agent_id: int, name: str, status: str) -> dict:
|
||||
return {"id": agent_id, "name": name, "online": False, "status": status,
|
||||
"cpu_cores": 0, "mem_used": 0, "mem_total": 0, "disk_used": 0, "disk_total": 0,
|
||||
"stacks": _bucket_statuses([]), "containers_running": 0, "containers_total": 0,
|
||||
"unhealthy": 0, "updates_available": 0}
|
||||
|
||||
|
||||
async def _agent_host(agent_id: int) -> tuple[dict, list[dict]]:
|
||||
"""One agent's host card + attention items, tolerant of partial failure.
|
||||
|
||||
Opens its own Session so the fan-out across agents stays concurrency-safe
|
||||
(a shared SQLModel session is not), and fetches sequentially within the
|
||||
agent because :func:`agent_service.call` commits a status update each time.
|
||||
"""
|
||||
with Session(engine) as session:
|
||||
agent = session.get(Agent, agent_id)
|
||||
if agent is None:
|
||||
return _offline_host(agent_id, str(agent_id), "unknown"), []
|
||||
name = agent.name
|
||||
# Agent stacks live in the host section of the dashboard, not a route
|
||||
# of their own, so aggregate agent items deep-link back to it.
|
||||
link = "/"
|
||||
|
||||
if agent.status != "online":
|
||||
return _offline_host(agent_id, name, agent.status), [
|
||||
_attn("error", "agent_offline", name,
|
||||
f"{name} is {agent.status}", "Check it under Settings → Remote hosts.",
|
||||
"/settings")]
|
||||
|
||||
async def _fetch(path: str):
|
||||
try:
|
||||
return await agent_service.call(session, agent, "GET", path)
|
||||
except Exception: # AgentError or transport — degrade gracefully
|
||||
return None
|
||||
|
||||
sys_data = await _fetch("/agent/system")
|
||||
stacks = await _fetch("/agent/stacks")
|
||||
updates = await _fetch("/agent/stacks/updates")
|
||||
|
||||
if sys_data is None and stacks is None:
|
||||
# Couldn't reach it at all — agent_service.call already marked it offline.
|
||||
return _offline_host(agent_id, name, "offline"), [
|
||||
_attn("error", "agent_offline", name,
|
||||
f"{name} is unreachable", "Check it under Settings → Remote hosts.",
|
||||
"/settings")]
|
||||
|
||||
sys_data = sys_data or {}
|
||||
statuses = [s.get("status", "") for s in (stacks or [])]
|
||||
buckets = _bucket_statuses(statuses)
|
||||
update_count = sum(1 for v in (updates or {}).values()
|
||||
if isinstance(v, dict) and v.get("update_available"))
|
||||
|
||||
host = {
|
||||
"id": agent_id,
|
||||
"name": name,
|
||||
"online": True,
|
||||
"status": "online",
|
||||
"cpu_cores": sys_data.get("cpu_cores", 0),
|
||||
"mem_used": sys_data.get("mem_used", 0), "mem_total": sys_data.get("mem_total", 0),
|
||||
"disk_used": sys_data.get("disk_used", 0), "disk_total": sys_data.get("disk_total", 0),
|
||||
"stacks": buckets,
|
||||
"containers_running": sys_data.get("compose_running", sys_data.get("containers_running", 0)),
|
||||
"containers_total": sys_data.get("containers_total", 0),
|
||||
"unhealthy": buckets["error"], # agents expose no healthcheck rollup; error is the proxy
|
||||
"updates_available": update_count,
|
||||
}
|
||||
|
||||
attention: list[dict] = []
|
||||
if buckets["error"]:
|
||||
attention.append(_attn("error", "stack_error", name,
|
||||
f"{name}: {buckets['error']} stack(s) in error",
|
||||
"Containers are dead.", link))
|
||||
if buckets["partial"]:
|
||||
attention.append(_attn("warn", "stack_partial", name,
|
||||
f"{name}: {buckets['partial']} stack(s) partially running",
|
||||
"Some services are down.", link))
|
||||
if update_count:
|
||||
attention.append(_attn("warn", "updates", name,
|
||||
f"{name}: {update_count} stack(s) have image updates",
|
||||
"Pull the newer images.", link))
|
||||
attention += _resource_attention(name, link,
|
||||
host["mem_used"], host["mem_total"],
|
||||
host["disk_used"], host["disk_total"])
|
||||
return host, attention
|
||||
|
||||
|
||||
def _backup_attention(session: Session, agent_names: dict[int, str]) -> list[dict]:
|
||||
def _backup_attention(session: Session) -> list[dict]:
|
||||
"""Flag enabled backup schedules whose last run failed or is overdue."""
|
||||
now = datetime.now(timezone.utc)
|
||||
items: list[dict] = []
|
||||
for sch in session.exec(select(BackupSchedule).where(BackupSchedule.enabled == True)).all(): # noqa: E712
|
||||
host = agent_names.get(sch.agent_id, "local") if sch.agent_id else "local"
|
||||
host = "local"
|
||||
status = (sch.last_status or "").lower()
|
||||
if status and not status.startswith("ok"):
|
||||
items.append(_attn("error", "backup_failed", host,
|
||||
@@ -307,23 +216,12 @@ async def compute_fleet(session: Session, refresh: bool = False) -> dict:
|
||||
return _fleet_cache["data"]
|
||||
|
||||
local_host, attention = await asyncio.to_thread(_local_host)
|
||||
|
||||
agents = session.exec(select(Agent)).all()
|
||||
agent_names = {a.id: a.name for a in agents}
|
||||
agent_ids = [a.id for a in agents]
|
||||
agent_results = await asyncio.gather(*[_agent_host(aid) for aid in agent_ids])
|
||||
|
||||
hosts = [local_host]
|
||||
for host, items in agent_results:
|
||||
hosts.append(host)
|
||||
attention += items
|
||||
attention += _backup_attention(session, agent_names)
|
||||
attention += _backup_attention(session)
|
||||
|
||||
attention.sort(key=lambda a: _SEVERITY_ORDER.get(a["severity"], 9))
|
||||
|
||||
kpis = {
|
||||
"hosts_online": sum(1 for h in hosts if h["online"]),
|
||||
"hosts_total": len(hosts),
|
||||
"stacks_running": sum(h["stacks"]["running"] for h in hosts),
|
||||
"stacks_partial": sum(h["stacks"]["partial"] for h in hosts),
|
||||
"stacks_total": sum(h["stacks"]["total"] for h in hosts),
|
||||
|
||||
Reference in New Issue
Block a user