From 51691d7b8a83fd16da6dc40c9254812720084131 Mon Sep 17 00:00:00 2001 From: claude Date: Mon, 23 Nov 2026 10:12:12 +0100 Subject: [PATCH] Mise a jour automatique programmee (crontab) par stack - planification crontab par stack (autoupdate.json), incompatible avec l'exclusion - scheduler a la minute ; pull puis down + up -d seulement si une image a change (mode autoapply) - API PUT .../targets/{t}/auto et GET /api/cron (apercu des prochaines executions) - interface : option dans le detail, badge sarcelle, vue de suivi "Mises a jour auto" - TZ dans docker-compose / .env.example (tzdata embarque) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LNfwm6UCYFyvNQNSF5i9Gy --- .env.example | 3 + docker-compose.yml | 2 + frontend/src/AutoUpdate.tsx | 229 +++++++++++++++++++++++++++++ frontend/src/FleetView.tsx | 27 +++- frontend/src/api.ts | 25 ++++ frontend/src/styles.css | 63 ++++++++ internal/api/api.go | 5 + internal/api/auto.go | 58 ++++++++ internal/fleet/autoupdate.go | 236 ++++++++++++++++++++++++++++++ internal/fleet/autoupdate_test.go | 100 +++++++++++++ internal/fleet/cron.go | 158 ++++++++++++++++++++ internal/fleet/cron_test.go | 58 ++++++++ internal/fleet/exclude.go | 4 + internal/fleet/manager.go | 25 +++- internal/fleet/model.go | 3 + internal/fleet/remote.sh | 21 +++ 16 files changed, 1010 insertions(+), 7 deletions(-) create mode 100644 frontend/src/AutoUpdate.tsx create mode 100644 internal/api/auto.go create mode 100644 internal/fleet/autoupdate.go create mode 100644 internal/fleet/autoupdate_test.go create mode 100644 internal/fleet/cron.go create mode 100644 internal/fleet/cron_test.go diff --git a/.env.example b/.env.example index 76402f9..c41639b 100644 --- a/.env.example +++ b/.env.example @@ -34,3 +34,6 @@ TITLE=Stackwatch # Analyse automatique (6h, minimum 5m, 0 : seulement à la demande) et parallélisme CHECK_INTERVAL=6h MAX_PARALLEL=4 + +# Fuseau horaire des planifications de mise a jour automatique (crontab) +TZ=Europe/Paris diff --git a/docker-compose.yml b/docker-compose.yml index fd4a8aa..669785e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,8 @@ services: # Analyse automatique (pull + comparaison des images) ; 0 : seulement à la demande. CHECK_INTERVAL: ${CHECK_INTERVAL:-6h} MAX_PARALLEL: ${MAX_PARALLEL:-4} + # Fuseau des planifications de mise a jour automatique (crontab). + TZ: ${TZ:-Europe/Paris} volumes: # Serveurs, résultats, comptes et clé SSH : chown -R 65532:65532 ./data - ./data:/data diff --git a/frontend/src/AutoUpdate.tsx b/frontend/src/AutoUpdate.tsx new file mode 100644 index 0000000..e5378be --- /dev/null +++ b/frontend/src/AutoUpdate.tsx @@ -0,0 +1,229 @@ +// Mises a jour automatiques programmees (crontab) : badge, editeur dans le +// detail d'une stack et vue de suivi de toutes les programmations. +import { useEffect, useState } from "react"; +import { api, type AutoInfo, type Me, type Scan, type Server, type Stack, type Target } from "./api"; +import { fmtDate, fmtRelative } from "./format"; +import { CheckIcon, ClockIcon, RefreshIcon, WarningIcon } from "./icons"; + +export const AUTO_PRESETS: [string, string][] = [ + ["0 4 * * *", "Chaque nuit a 4 h"], + ["0 4 * * 0", "Chaque dimanche a 4 h"], + ["0 4 1 * *", "Le 1er du mois a 4 h"], + ["0 */6 * * *", "Toutes les 6 heures"], +]; + +const norm = (s: string) => s.trim().split(/\s+/).join(" "); + +/** Badge (couleur sarcelle) a cote du nom d'une stack programmee. */ +export function AutoBadge({ auto }: { auto: AutoInfo }) { + const title = `Mise a jour automatique : ${auto.schedule}${auto.next ? `, prochaine le ${fmtDate(auto.next)}` : ""}`; + return ( + + + + ); +} + +/** Dernier resultat d'une execution programmee. */ +export function AutoLast({ auto }: { auto: AutoInfo }) { + if (auto.pending) return en attente (connexion occupee); + if (!auto.lastRun) return jamais executee; + return ( + + {auto.lastOk ? : } + + {fmtRelative(auto.lastRun)} + {auto.lastMessage ? ` : ${auto.lastMessage}` : ""} + + + ); +} + +interface EditorProps { + stack: Stack; + busy: boolean; + tz?: string; + onSave: (schedule: string) => Promise; +} + +/** Case « Mise a jour automatique programmee » et sa planification crontab. */ +export function AutoEditor({ stack, busy, tz, onSave }: EditorProps) { + const current = stack.auto?.schedule ?? ""; + const [on, setOn] = useState(!!current); + const [expr, setExpr] = useState(current || AUTO_PRESETS[0][0]); + const [preview, setPreview] = useState<{ next?: string[]; error?: string }>({}); + + useEffect(() => { + setOn(!!current); + if (current) setExpr(current); + }, [current]); + + useEffect(() => { + if (!on) return; + let live = true; + const h = setTimeout(() => { + api.cronPreview(expr).then( + (next) => live && setPreview({ next }), + (e: unknown) => live && setPreview({ error: e instanceof Error ? e.message : String(e) }), + ); + }, 300); + return () => { + live = false; + clearTimeout(h); + }; + }, [expr, on]); + + if (stack.excluded) { + return ( + + ); + } + + const toggle = (v: boolean) => { + setOn(v); + if (!v && current) void onSave(""); // desactivation immediate + }; + const dirty = on && norm(expr) !== current; + const preset = AUTO_PRESETS.find(([p]) => p === norm(expr))?.[0] ?? ""; + + return ( +
+ + {on && ( +
+ + setExpr(e.target.value)} placeholder="min heure jour mois jour-semaine" aria-label="Planification crontab" spellCheck={false} /> + +
+ )} + {on && ( +
+ {preview.error + ? preview.error + : preview.next?.length + ? `Prochaines executions : ${preview.next.map(fmtDate).join(", ")}${tz ? ` (heure ${tz})` : ""}` + : " "} +
+ )} + {stack.auto && ( +
+ Derniere execution programmee : +
+ )} +
+ ); +} + +interface Row { + server: Server; + target: Target; + scan: Scan; + stack: Stack; + auto: AutoInfo; +} + +interface PanelProps { + me: Me; + servers: Server[]; + query: string; + tz?: string; + onDisable: (server: Server, target: Target, stack: Stack) => void; +} + +/** Espace de suivi : toutes les stacks programmees, triees par prochaine execution. */ +export function AutoPanel({ me, servers, query, tz, onDisable }: PanelProps) { + const rows: Row[] = servers.flatMap((server) => + server.targets.flatMap((target) => { + const scan = server.scans[target.id]; + return (scan?.stacks ?? []).flatMap((stack) => (stack.auto ? [{ server, target, scan, stack, auto: stack.auto }] : [])); + }), + ); + const q = query.trim().toLowerCase(); + const shown = rows + .filter((r) => !q || r.stack.name.toLowerCase().includes(q) || r.stack.dir.toLowerCase().includes(q) || r.server.name.toLowerCase().includes(q)) + .sort((a, b) => (a.auto.next ?? "9").localeCompare(b.auto.next ?? "9")); + + if (!rows.length) { + return ( +

+ Aucune stack en mise a jour automatique. Pour en programmer une, ouvrir son detail et cocher « Mise a jour automatique programmee ». +

+ ); + } + return ( +
+

+ {rows.length} stack{rows.length > 1 ? "s" : ""} programmee{rows.length > 1 ? "s" : ""}. Heures exprimees dans le fuseau du serveur stackwatch{tz ? ` (${tz})` : ""}. +

+ + + + + + + + + {me.admin && + + + {shown.map(({ server, target, scan, stack, auto }) => { + const running = scan.running && scan.task === "apply" && scan.current === stack.dir; + return ( + + + + + + + {me.admin && ( + + )} + + ); + })} + +
StackServeurPlanificationProchaineDerniere execution} +
+ {stack.name} +
+ {stack.dir} +
+ {server.name} + {!server.enabled && (desactive)} +
+ {target.user} +
+ {auto.schedule} + {auto.next ? fmtDate(auto.next) : jamais} + {running ? ( + + Mise a jour en cours + + ) : ( + + )} + + +
+ {shown.length === 0 &&

Aucune programmation ne correspond a la recherche.

} +
+ ); +} diff --git a/frontend/src/FleetView.tsx b/frontend/src/FleetView.tsx index 1b07da6..e85634b 100644 --- a/frontend/src/FleetView.tsx +++ b/frontend/src/FleetView.tsx @@ -1,10 +1,11 @@ import { useMemo, useState } from "react"; import { api, type Me, type Fleet, type Scan, type Server, type Stack, type StackStatus, type Target } from "./api"; +import { AutoBadge, AutoEditor, AutoPanel } from "./AutoUpdate"; import { ConfirmDialog } from "./Dialogs"; import { fmtRelative, plural } from "./format"; import { ArrowUpIcon, BanIcon, CheckIcon, ChevronDownIcon, ChevronRightIcon, ClockIcon, EditIcon, KeyIcon, PlusIcon, RefreshIcon, SearchIcon, ServerIcon, WarningIcon } from "./icons"; -type Filter = "all" | "updates" | "problems"; +type Filter = "all" | "updates" | "problems" | "auto"; interface Props { me: Me; @@ -70,6 +71,7 @@ export function FleetView({ me, fleet, current, run, onEdit, onAdd, onKey }: Pro const count = (st: StackStatus) => all.filter((x) => x.status === st && !(st === "updates" && x.excluded)).length; const unreachable = servers.reduce((n, s) => n + Object.values(s.scans).filter((sc) => sc.error).length, 0); const scanning = servers.some((s) => Object.values(s.scans).some((sc) => sc.running)); + const autoCount = all.filter((x) => x.auto).length; const q = search.trim().toLowerCase(); const keep = (st: Stack) => { @@ -97,6 +99,7 @@ export function FleetView({ me, fleet, current, run, onEdit, onAdd, onKey }: Pro {count("error") > 0 && {plural(count("error"), "erreur")}} {unreachable > 0 && {plural(unreachable, "connexion")} en échec} {count("stopped") > 0 && {plural(count("stopped"), "arrêtée")}} + {autoCount > 0 && {plural(autoCount, "stack")} en auto} @@ -107,6 +110,7 @@ export function FleetView({ me, fleet, current, run, onEdit, onAdd, onKey }: Pro ["all", "Toutes"], ["updates", "Mises à jour"], ["problems", "Erreurs"], + ["auto", "Mises a jour auto"], ] as [Filter, string][] ).map(([k, label]) => (