Phase 8: back up & restore remote (agent) stacks (0.8.0)
- Agent: GET /agent/stacks/{id}/backup + POST /agent/stacks/restore (reuse
backup_service). backup_service gains backup_basename/backup_filename helpers.
- Main proxy streams agent <-> main <-> destination (creds stay central):
agent_service download_to_file/upload_file; routers/agents.py backup download,
backup/push, restore upload, restore-from.
- Schedules: BackupSchedule.agent_id; schedule_service downloads from the agent
when set; per-host filename prefix isolates retention across hosts.
- Frontend: agents api backup/restore; BackupButton/RestoreButton agent-aware
(Backup on remote stack detail, Restore per host section); schedule form host
selector (local or an online agent) + host shown on schedule rows.
Rough-verified (per request): py_compile, frontend tsc build, image imports
(main 99 / agent 16 routes). Full live agent round-trip to be tested post-deploy.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
84ef3df59e
commit
5cd55382ed
@@ -6,6 +6,7 @@ from sqlmodel import Session, select
|
||||
|
||||
from auth import require_admin
|
||||
from database import get_session
|
||||
from models.agent import Agent
|
||||
from models.backup_destination import BackupDestination
|
||||
from models.backup_schedule import (
|
||||
FREQUENCIES,
|
||||
@@ -27,11 +28,14 @@ def _ip(request: Request) -> str:
|
||||
|
||||
def _to_read(session: Session, s: BackupSchedule) -> ScheduleRead:
|
||||
dest = session.get(BackupDestination, s.destination_id)
|
||||
agent = session.get(Agent, s.agent_id) if s.agent_id is not None else None
|
||||
return ScheduleRead(
|
||||
id=s.id,
|
||||
stack_id=s.stack_id,
|
||||
destination_id=s.destination_id,
|
||||
destination_name=dest.name if dest else None,
|
||||
agent_id=s.agent_id,
|
||||
agent_name=agent.name if agent else None,
|
||||
frequency=s.frequency,
|
||||
hour=s.hour,
|
||||
minute=s.minute,
|
||||
@@ -54,13 +58,17 @@ def _get_or_404(session: Session, schedule_id: int) -> BackupSchedule:
|
||||
return s
|
||||
|
||||
|
||||
def _validate(session: Session, stack_id: str, destination_id: int, frequency: str) -> None:
|
||||
if frequency not in FREQUENCIES:
|
||||
raise HTTPException(status_code=400, detail=f"Unknown frequency '{frequency}'")
|
||||
if not session.get(Stack, stack_id):
|
||||
raise HTTPException(status_code=404, detail=f"Stack '{stack_id}' not found")
|
||||
if not session.get(BackupDestination, destination_id):
|
||||
raise HTTPException(status_code=404, detail=f"Destination {destination_id} not found")
|
||||
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 schedule.agent_id is not None:
|
||||
# Remote stack: validate the agent exists; the stack is checked at run time.
|
||||
if not session.get(Agent, schedule.agent_id):
|
||||
raise HTTPException(status_code=404, detail=f"Agent {schedule.agent_id} not found")
|
||||
elif 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])
|
||||
@@ -79,8 +87,8 @@ def create_schedule(
|
||||
session: Session = Depends(get_session),
|
||||
user: User = Depends(require_admin),
|
||||
) -> ScheduleRead:
|
||||
_validate(session, body.stack_id, body.destination_id, body.frequency)
|
||||
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()
|
||||
)
|
||||
@@ -106,7 +114,7 @@ def update_schedule(
|
||||
data = body.model_dump(exclude_unset=True)
|
||||
for k, v in data.items():
|
||||
setattr(s, k, v)
|
||||
_validate(session, s.stack_id, s.destination_id, s.frequency)
|
||||
_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(
|
||||
|
||||
Reference in New Issue
Block a user