from __future__ import annotations from datetime import datetime, timezone from typing import Optional from sqlmodel import Field, SQLModel def _now() -> datetime: return datetime.now(timezone.utc) class AutoUpdate(SQLModel, table=True): """Per-stack auto-update policy (Watchtower-style). When the background image-update check finds a newer registry digest for one of the stack's images, the stack is either pulled + redeployed (``redeploy=True``) or merely notified about (``redeploy=False``). ``agent_id`` None = local host, otherwise a remote agent's stack. """ id: Optional[int] = Field(default=None, primary_key=True) stack_id: str agent_id: Optional[int] = None enabled: bool = True redeploy: bool = True # True = pull + up -d; False = notify only last_run: Optional[datetime] = None last_status: Optional[str] = None # "updated" | "up-to-date" | "update-available" | "skipped" | "error" last_result: Optional[str] = None # detail (stale images / error text) created_at: datetime = Field(default_factory=_now) # --- API schemas --- class AutoUpdateWrite(SQLModel): enabled: bool = True redeploy: bool = True class AutoUpdateRead(SQLModel): id: Optional[int] stack_id: str agent_id: Optional[int] agent_name: Optional[str] = None enabled: bool redeploy: bool last_run: Optional[datetime] last_status: Optional[str] last_result: Optional[str]