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
167 lines
5.5 KiB
Python
167 lines
5.5 KiB
Python
"""Host device detection (USB, serial/TTY, DRI) and host filesystem browser."""
|
|
from __future__ import annotations
|
|
|
|
import glob
|
|
import os
|
|
from dataclasses import asdict, dataclass
|
|
|
|
from config import settings
|
|
|
|
|
|
@dataclass
|
|
class HostDevice:
|
|
path: str
|
|
kind: str # "usb" | "tty" | "dri" | "other"
|
|
name: str = ""
|
|
|
|
def to_dict(self) -> dict:
|
|
return asdict(self)
|
|
|
|
|
|
def _read(path: str) -> str:
|
|
try:
|
|
with open(path, "r", encoding="utf-8", errors="replace") as fh:
|
|
return fh.read().strip()
|
|
except OSError:
|
|
return ""
|
|
|
|
|
|
def _usb_devices() -> list[HostDevice]:
|
|
devices: list[HostDevice] = []
|
|
# /dev/bus/usb/<bus>/<dev>
|
|
for path in sorted(glob.glob("/dev/bus/usb/*/*")):
|
|
name = ""
|
|
# Best-effort name lookup via sysfs is non-trivial to map; leave generic.
|
|
devices.append(HostDevice(path=path, kind="usb", name=name or "USB device"))
|
|
return devices
|
|
|
|
|
|
def _usb_names_from_sysfs() -> list[HostDevice]:
|
|
"""Richer USB list from sysfs with product/manufacturer strings."""
|
|
out: list[HostDevice] = []
|
|
for dev in sorted(glob.glob("/sys/bus/usb/devices/*")):
|
|
busnum = _read(os.path.join(dev, "busnum"))
|
|
devnum = _read(os.path.join(dev, "devnum"))
|
|
if not busnum or not devnum:
|
|
continue
|
|
product = _read(os.path.join(dev, "product"))
|
|
manufacturer = _read(os.path.join(dev, "manufacturer"))
|
|
label = " ".join(p for p in (manufacturer, product) if p) or "USB device"
|
|
path = f"/dev/bus/usb/{int(busnum):03d}/{int(devnum):03d}"
|
|
out.append(HostDevice(path=path, kind="usb", name=label))
|
|
return out
|
|
|
|
|
|
def _tty_devices() -> list[HostDevice]:
|
|
devices: list[HostDevice] = []
|
|
patterns = ["/dev/ttyUSB*", "/dev/ttyACM*", "/dev/ttyAMA*", "/dev/serial/by-id/*"]
|
|
for pattern in patterns:
|
|
for path in sorted(glob.glob(pattern)):
|
|
base = os.path.basename(path)
|
|
driver = _read(f"/sys/class/tty/{base}/device/driver/module/name") or ""
|
|
devices.append(
|
|
HostDevice(path=path, kind="tty", name=driver or "Serial device")
|
|
)
|
|
return devices
|
|
|
|
|
|
def _dri_devices() -> list[HostDevice]:
|
|
return [
|
|
HostDevice(path=p, kind="dri", name="GPU render node")
|
|
for p in sorted(glob.glob("/dev/dri/*"))
|
|
]
|
|
|
|
|
|
def detect_devices() -> dict:
|
|
usb = _usb_names_from_sysfs() or _usb_devices()
|
|
return {
|
|
"usb": [d.to_dict() for d in usb],
|
|
"tty": [d.to_dict() for d in _tty_devices()],
|
|
"dri": [d.to_dict() for d in _dri_devices()],
|
|
}
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# Host filesystem browser (sandboxed)
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
class BrowseError(Exception):
|
|
pass
|
|
|
|
|
|
def _real_root(path: str) -> str:
|
|
"""Map a logical host path into the container view (HOST_ROOT_PREFIX).
|
|
|
|
Refuses anything that resolves inside StackPilot's own ``DATA_DIR``. That
|
|
directory holds ``stackpilot.db`` — users, password hashes and
|
|
backup-destination credentials — and the API deliberately never hands those
|
|
out (destination secrets come back masked). Without this the file browser
|
|
would be a way around that, for admins too. Note this only bites when ``HOST_ROOT_PREFIX`` is empty: with a
|
|
prefix set, no logical path can reach the container's own ``/data`` at all.
|
|
"""
|
|
prefix = settings.HOST_ROOT_PREFIX.rstrip("/")
|
|
real = prefix + path if prefix else path
|
|
data_dir = os.path.normpath(settings.DATA_DIR)
|
|
norm = os.path.normpath(real)
|
|
if norm == data_dir or norm.startswith(data_dir + os.sep):
|
|
raise BrowseError("Path is inside StackPilot's own data directory")
|
|
return real
|
|
|
|
|
|
def _is_allowed(path: str) -> bool:
|
|
norm = os.path.normpath(path)
|
|
for root in settings.ALLOWED_BROWSE_ROOTS:
|
|
root = os.path.normpath(root)
|
|
if root == "/" or norm == root or norm.startswith(root + os.sep):
|
|
return True
|
|
return False
|
|
|
|
|
|
def browse(path: str = "/", show_hidden: bool = False) -> dict:
|
|
path = os.path.normpath(path or "/")
|
|
if not path.startswith("/"):
|
|
raise BrowseError("Path must be absolute")
|
|
if not _is_allowed(path):
|
|
raise BrowseError("Path is outside the allowed browse roots")
|
|
|
|
real = _real_root(path)
|
|
if not os.path.isdir(real):
|
|
raise BrowseError(f"Not a directory: {path}")
|
|
|
|
entries = []
|
|
try:
|
|
names = os.listdir(real)
|
|
except PermissionError as exc:
|
|
raise BrowseError(f"Permission denied: {path}") from exc
|
|
|
|
for name in sorted(names):
|
|
if not show_hidden and name.startswith("."):
|
|
continue
|
|
full_real = os.path.join(real, name)
|
|
try:
|
|
st = os.lstat(full_real)
|
|
is_dir = os.path.isdir(full_real)
|
|
entries.append(
|
|
{
|
|
"name": name,
|
|
"type": "dir" if is_dir else "file",
|
|
"size": st.st_size if not is_dir else None,
|
|
"permissions": oct(st.st_mode & 0o777),
|
|
"mtime": st.st_mtime,
|
|
"symlink": os.path.islink(full_real),
|
|
}
|
|
)
|
|
except OSError:
|
|
continue
|
|
|
|
# Sort: dirs first, then files, both alphabetical.
|
|
entries.sort(key=lambda e: (e["type"] != "dir", e["name"].lower()))
|
|
parent = os.path.dirname(path) if path != "/" else None
|
|
return {
|
|
"path": path,
|
|
"parent": parent,
|
|
"roots": settings.ALLOWED_BROWSE_ROOTS,
|
|
"entries": entries,
|
|
}
|