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
157 lines
5.0 KiB
Python
157 lines
5.0 KiB
Python
"""Scheduled backup management."""
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request
|
|
from sqlmodel import Session, select
|
|
|
|
from auth import require_admin
|
|
from database import get_session
|
|
from models.backup_destination import BackupDestination
|
|
from models.backup_schedule import (
|
|
FREQUENCIES,
|
|
BackupSchedule,
|
|
ScheduleCreate,
|
|
ScheduleRead,
|
|
ScheduleUpdate,
|
|
)
|
|
from models.stack import Stack
|
|
from models.user import User
|
|
from services import audit_service, schedule_service
|
|
|
|
router = APIRouter(prefix="/api/backups/schedules", tags=["backups"])
|
|
|
|
|
|
def _ip(request: Request) -> str:
|
|
return request.client.host if request.client else "unknown"
|
|
|
|
|
|
def _to_read(session: Session, s: BackupSchedule) -> ScheduleRead:
|
|
dest = session.get(BackupDestination, s.destination_id)
|
|
return ScheduleRead(
|
|
id=s.id,
|
|
stack_id=s.stack_id,
|
|
destination_id=s.destination_id,
|
|
destination_name=dest.name if dest else None,
|
|
frequency=s.frequency,
|
|
hour=s.hour,
|
|
minute=s.minute,
|
|
weekday=s.weekday,
|
|
include_volumes=s.include_volumes,
|
|
stop_first=s.stop_first,
|
|
keep=s.keep,
|
|
enabled=s.enabled,
|
|
last_run=s.last_run,
|
|
last_status=s.last_status,
|
|
next_run=s.next_run,
|
|
created_at=s.created_at,
|
|
)
|
|
|
|
|
|
def _get_or_404(session: Session, schedule_id: int) -> BackupSchedule:
|
|
s = session.get(BackupSchedule, schedule_id)
|
|
if not s:
|
|
raise HTTPException(status_code=404, detail=f"Schedule {schedule_id} not found")
|
|
return s
|
|
|
|
|
|
def _validate(session: Session, schedule: BackupSchedule) -> None:
|
|
if schedule.frequency not in FREQUENCIES:
|
|
raise HTTPException(status_code=400, detail=f"Unknown frequency '{schedule.frequency}'")
|
|
if not session.get(BackupDestination, schedule.destination_id):
|
|
raise HTTPException(status_code=404, detail=f"Destination {schedule.destination_id} not found")
|
|
if not session.get(Stack, schedule.stack_id):
|
|
raise HTTPException(status_code=404, detail=f"Stack '{schedule.stack_id}' not found")
|
|
|
|
|
|
@router.get("", response_model=list[ScheduleRead])
|
|
def list_schedules(
|
|
session: Session = Depends(get_session),
|
|
_user: User = Depends(require_admin),
|
|
) -> list[ScheduleRead]:
|
|
rows = session.exec(select(BackupSchedule).order_by(BackupSchedule.id)).all()
|
|
return [_to_read(session, s) for s in rows]
|
|
|
|
|
|
@router.post("", response_model=ScheduleRead, status_code=201)
|
|
def create_schedule(
|
|
body: ScheduleCreate,
|
|
request: Request,
|
|
session: Session = Depends(get_session),
|
|
user: User = Depends(require_admin),
|
|
) -> ScheduleRead:
|
|
s = BackupSchedule(**body.model_dump())
|
|
_validate(session, s)
|
|
s.next_run = schedule_service.compute_next_run(
|
|
s.frequency, s.hour, s.minute, s.weekday, schedule_service._now()
|
|
)
|
|
session.add(s)
|
|
session.commit()
|
|
session.refresh(s)
|
|
audit_service.record(
|
|
session, user=user.username, action="schedule.create", target=s.stack_id,
|
|
detail=f"{s.frequency} → dest {s.destination_id}", ip=_ip(request),
|
|
)
|
|
return _to_read(session, s)
|
|
|
|
|
|
@router.put("/{schedule_id}", response_model=ScheduleRead)
|
|
def update_schedule(
|
|
schedule_id: int,
|
|
body: ScheduleUpdate,
|
|
request: Request,
|
|
session: Session = Depends(get_session),
|
|
user: User = Depends(require_admin),
|
|
) -> ScheduleRead:
|
|
s = _get_or_404(session, schedule_id)
|
|
data = body.model_dump(exclude_unset=True)
|
|
for k, v in data.items():
|
|
setattr(s, k, v)
|
|
_validate(session, s)
|
|
# Recompute next run when timing fields change.
|
|
if {"frequency", "hour", "minute", "weekday"} & set(data) or s.next_run is None:
|
|
s.next_run = schedule_service.compute_next_run(
|
|
s.frequency, s.hour, s.minute, s.weekday, schedule_service._now()
|
|
)
|
|
session.add(s)
|
|
session.commit()
|
|
session.refresh(s)
|
|
audit_service.record(
|
|
session, user=user.username, action="schedule.update", target=s.stack_id,
|
|
ip=_ip(request),
|
|
)
|
|
return _to_read(session, s)
|
|
|
|
|
|
@router.delete("/{schedule_id}")
|
|
def delete_schedule(
|
|
schedule_id: int,
|
|
request: Request,
|
|
session: Session = Depends(get_session),
|
|
user: User = Depends(require_admin),
|
|
) -> dict:
|
|
s = _get_or_404(session, schedule_id)
|
|
stack_id = s.stack_id
|
|
session.delete(s)
|
|
session.commit()
|
|
audit_service.record(
|
|
session, user=user.username, action="schedule.delete", target=stack_id,
|
|
ip=_ip(request),
|
|
)
|
|
return {"ok": True}
|
|
|
|
|
|
@router.post("/{schedule_id}/run")
|
|
async def run_now(
|
|
schedule_id: int,
|
|
request: Request,
|
|
session: Session = Depends(get_session),
|
|
user: User = Depends(require_admin),
|
|
) -> dict:
|
|
s = _get_or_404(session, schedule_id)
|
|
result = await schedule_service.run_schedule(session, s)
|
|
audit_service.record(
|
|
session, user=user.username, action="schedule.run", target=s.stack_id,
|
|
detail="ok" if result.get("ok") else result.get("error", "error"), ip=_ip(request),
|
|
)
|
|
return result
|