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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LNfwm6UCYFyvNQNSF5i9Gy
This commit is contained in:
claude BotandClaude Opus 5.5 committed 2026-11-23 10:12:12 +01:00
1 parent 54cbab17cb
commit 51691d7b8a
16 files changed
+1010 -7

No files matched your search

+229
View File
@@ -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 (
<span className="auto-badge" title={title} aria-label="Mise a jour automatique programmee">
<ClockIcon size={12} />
</span>
);
}
/** Dernier resultat d'une execution programmee. */
export function AutoLast({ auto }: { auto: AutoInfo }) {
if (auto.pending) return <span className="auto-last muted">en attente (connexion occupee)</span>;
if (!auto.lastRun) return <span className="auto-last muted">jamais executee</span>;
return (
<span className={`auto-last ${auto.lastOk ? "ok" : "ko"}`} title={`${fmtDate(auto.lastRun)} : ${auto.lastMessage ?? ""}`}>
{auto.lastOk ? <CheckIcon size={13} /> : <WarningIcon size={13} />}
<span>
{fmtRelative(auto.lastRun)}
{auto.lastMessage ? ` : ${auto.lastMessage}` : ""}
</span>
</span>
);
}
interface EditorProps {
stack: Stack;
busy: boolean;
tz?: string;
onSave: (schedule: string) => Promise<unknown>;
}
/** 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 (
<label className="check-row muted">
<input type="checkbox" checked={false} disabled />
<span>Mise a jour automatique indisponible : la stack est exclue des mises a jour (la reactiver d'abord)</span>
</label>
);
}
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 (
<div className="auto-editor">
<label className="check-row">
<input type="checkbox" checked={on} disabled={busy} onChange={(e) => toggle(e.target.checked)} />
<span>Mise a jour automatique programmee (pull, puis down + up -d seulement si une image a change)</span>
</label>
{on && (
<div className="auto-form">
<select value={preset} onChange={(e) => e.target.value && setExpr(e.target.value)} aria-label="Planification predefinie">
<option value="">Personnalisee</option>
{AUTO_PRESETS.map(([p, label]) => (
<option key={p} value={p}>
{label}
</option>
))}
</select>
<input className="mono" value={expr} onChange={(e) => setExpr(e.target.value)} placeholder="min heure jour mois jour-semaine" aria-label="Planification crontab" spellCheck={false} />
<button className="primary" disabled={!dirty || busy || !!preview.error} onClick={() => void onSave(norm(expr))}>
<ClockIcon />
{current ? "Modifier" : "Programmer"}
</button>
</div>
)}
{on && (
<div className={`small-text ${preview.error ? "error-text" : "muted"}`}>
{preview.error
? preview.error
: preview.next?.length
? `Prochaines executions : ${preview.next.map(fmtDate).join(", ")}${tz ? ` (heure ${tz})` : ""}`
: " "}
</div>
)}
{stack.auto && (
<div className="small-text muted">
Derniere execution programmee : <AutoLast auto={stack.auto} />
</div>
)}
</div>
);
}
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 (
<p className="muted pad">
Aucune stack en mise a jour automatique. Pour en programmer une, ouvrir son detail et cocher « Mise a jour automatique programmee ».
</p>
);
}
return (
<div className="auto-panel">
<p className="muted small-text">
{rows.length} stack{rows.length > 1 ? "s" : ""} programmee{rows.length > 1 ? "s" : ""}. Heures exprimees dans le fuseau du serveur stackwatch{tz ? ` (${tz})` : ""}.
</p>
<table className="services auto-table">
<thead>
<tr>
<th>Stack</th>
<th>Serveur</th>
<th>Planification</th>
<th>Prochaine</th>
<th>Derniere execution</th>
{me.admin && <th />}
</tr>
</thead>
<tbody>
{shown.map(({ server, target, scan, stack, auto }) => {
const running = scan.running && scan.task === "apply" && scan.current === stack.dir;
return (
<tr key={`${server.id}/${target.id}:${stack.dir}`}>
<td>
<strong>{stack.name}</strong>
<br />
<small className="muted">{stack.dir}</small>
</td>
<td>
{server.name}
{!server.enabled && <span className="muted"> (desactive)</span>}
<br />
<small className="muted">{target.user}</small>
</td>
<td>
<code>{auto.schedule}</code>
</td>
<td>{auto.next ? fmtDate(auto.next) : <span className="muted">jamais</span>}</td>
<td>
{running ? (
<span className="status-pill st-applying">
<RefreshIcon size={13} className="spin" /> Mise a jour en cours
</span>
) : (
<AutoLast auto={auto} />
)}
</td>
{me.admin && (
<td>
<button onClick={() => onDisable(server, target, stack)} disabled={running}>
Desactiver
</button>
</td>
)}
</tr>
);
})}
</tbody>
</table>
{shown.length === 0 && <p className="muted pad">Aucune programmation ne correspond a la recherche.</p>}
</div>
);
}
+22 -5
View File
@@ -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 && <span className="stat err">{plural(count("error"), "erreur")}</span>}
{unreachable > 0 && <span className="stat err">{plural(unreachable, "connexion")} en échec</span>}
{count("stopped") > 0 && <span className="stat">{plural(count("stopped"), "arrêtée")}</span>}
{autoCount > 0 && <span className="stat auto">{plural(autoCount, "stack")} en auto</span>}
</div>
</div>
@@ -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]) => (
<button key={k} className={filter === k ? "on" : ""} onClick={() => setFilter(k)} aria-pressed={filter === k}>
@@ -136,7 +140,9 @@ export function FleetView({ me, fleet, current, run, onEdit, onAdd, onKey }: Pro
</div>
<div className="scroll">
{servers.map((s) => (
{filter === "auto" ? (
<AutoPanel me={me} servers={servers} query={search} tz={fleet.tz} onDisable={(srv, t, st) => run(() => api.autoStack(srv.id, t.id, st.dir, ""))} />
) : servers.map((s) => (
<ServerBlock
key={s.id}
server={s}
@@ -148,6 +154,8 @@ export function FleetView({ me, fleet, current, run, onEdit, onAdd, onKey }: Pro
onCheck={(t) => run(() => api.check(s.id, t))}
onApply={(target, stack) => setApplying({ server: s, target, stack })}
onExclude={(target, stack, on) => run(() => api.excludeStack(s.id, target.id, stack.dir, on))}
onAuto={(target, stack, schedule) => run(() => api.autoStack(s.id, target.id, stack.dir, schedule))}
tz={fleet.tz}
/>
))}
</div>
@@ -183,9 +191,11 @@ interface BlockProps {
onCheck: (targetId?: string) => void;
onApply: (t: Target, s: Stack) => void;
onExclude: (t: Target, s: Stack, on: boolean) => void;
onAuto: (t: Target, s: Stack, schedule: string) => Promise<void>;
tz?: string;
}
function ServerBlock({ server, single, me, keep, filtering, onEdit, onCheck, onApply, onExclude }: BlockProps) {
function ServerBlock({ server, single, me, keep, filtering, onEdit, onCheck, onApply, onExclude, onAuto, tz }: BlockProps) {
const scans = Object.values(server.scans);
const running = scans.some((s) => s.running);
const last = scans.map((s) => s.finished).filter(Boolean).sort().pop();
@@ -217,13 +227,13 @@ function ServerBlock({ server, single, me, keep, filtering, onEdit, onCheck, onA
</header>
)}
{server.targets.map((t) => (
<TargetBlock key={t.id} server={server} target={t} scan={server.scans[t.id]} me={me} keep={keep} filtering={filtering} onCheck={() => onCheck(t.id)} onApply={(s) => onApply(t, s)} onExclude={(s, on) => onExclude(t, s, on)} />
<TargetBlock key={t.id} server={server} target={t} scan={server.scans[t.id]} me={me} keep={keep} filtering={filtering} onCheck={() => onCheck(t.id)} onApply={(s) => onApply(t, s)} onExclude={(s, on) => onExclude(t, s, on)} onAuto={(s, x) => onAuto(t, s, x)} tz={tz} />
))}
</section>
);
}
function TargetBlock({ server, target, scan, me, keep, filtering, onCheck, onApply, onExclude }: { server: Server; target: Target; scan?: Scan; me: Me; keep: (s: Stack) => boolean; filtering: boolean; onCheck: () => void; onApply: (s: Stack) => void; onExclude: (s: Stack, on: boolean) => void }) {
function TargetBlock({ server, target, scan, me, keep, filtering, onCheck, onApply, onExclude, onAuto, tz }: { server: Server; target: Target; scan?: Scan; me: Me; keep: (s: Stack) => boolean; filtering: boolean; onCheck: () => void; onApply: (s: Stack) => void; onExclude: (s: Stack, on: boolean) => void; onAuto: (s: Stack, schedule: string) => Promise<void>; tz?: string }) {
const [open, setOpen] = useState<Record<string, boolean>>({});
const stacks = (scan?.stacks ?? []).filter(keep);
const hidden = (scan?.stacks.length ?? 0) - stacks.length;
@@ -306,6 +316,7 @@ function TargetBlock({ server, target, scan, me, keep, filtering, onCheck, onApp
<BanIcon size={12} />
</span>
)}
{st.auto && <AutoBadge auto={st.auto} />}
</strong>
<small className="muted">{st.dir}</small>
</span>
@@ -330,6 +341,12 @@ function TargetBlock({ server, target, scan, me, keep, filtering, onCheck, onApp
<span>Exclure cette stack des mises à jour (elle reste analysée et affichée, sans bouton « Mettre à jour »)</span>
</label>
)}
{me.admin && <AutoEditor stack={st} busy={applying} tz={tz} onSave={(x) => onAuto(st, x)} />}
{!me.admin && st.auto && (
<div className="small-text muted">
Mise a jour automatique : <code>{st.auto.schedule}</code>
</div>
)}
{st.error && <div className="error static">{st.error}</div>}
{total > 0 && (
<table className="services">
+25
View File
@@ -71,6 +71,21 @@ export interface Stack {
applied?: Applied;
/** Exclue des mises à jour par un administrateur (reste analysée et affichée). */
excluded?: boolean;
/** Mise a jour automatique programmee (crontab). */
auto?: AutoInfo;
}
export interface AutoInfo {
schedule: string;
by?: string;
since: string;
/** Prochaine execution (absente : jamais). */
next?: string;
lastRun?: string;
lastOk: boolean;
lastMessage?: string;
/** Echeance atteinte, connexion occupee : nouvel essai a la minute suivante. */
pending?: boolean;
}
export interface Scan {
@@ -119,6 +134,8 @@ export interface Fleet {
summary: Summary;
/** Secondes entre deux analyses automatiques (0 : désactivées). */
interval: number;
/** Fuseau horaire des planifications. */
tz?: string;
/** Administrateur : clé publique SSH à autoriser sur les serveurs. */
publicKey?: string;
}
@@ -214,6 +231,14 @@ export const api = {
await request(`/api/servers/${q(serverId)}/targets/${q(targetId)}/exclude`, json({ dir, excluded: on }, "PUT"));
},
/** Programme la mise a jour automatique d'une stack (crontab) ; schedule vide : la desactive. */
autoStack: async (serverId: string, targetId: string, dir: string, schedule: string): Promise<void> => {
await request(`/api/servers/${q(serverId)}/targets/${q(targetId)}/auto`, json({ dir, schedule }, "PUT"));
},
/** Valide une planification et renvoie ses prochaines executions. */
cronPreview: async (expr: string): Promise<string[]> => (await (await request(`/api/cron?expr=${q(expr)}`)).json()).next,
// --- Compte ------------------------------------------------------------------
changePassword: async (current: string, next: string): Promise<void> => {
await request("/api/account/password", json({ current, new: next }));
+63
View File
@@ -1285,3 +1285,66 @@ button.copy-ko {
align-items: center;
min-width: 0;
}
/* Mises a jour automatiques programmees : sarcelle, distincte du magenta (exclusion) et du bleu (en cours) */
:root {
--auto: #0c8599;
}
:root[data-theme="dark"] {
--auto: #3bc9db;
}
.auto-badge {
display: inline-flex;
align-items: center;
justify-content: center;
width: 1.25rem;
height: 1.25rem;
flex: none;
margin-left: 0.4rem;
border-radius: 50%;
color: var(--auto);
background: color-mix(in srgb, var(--auto) 15%, transparent);
}
.stat.auto {
color: var(--auto);
}
.auto-editor {
display: flex;
flex-direction: column;
gap: 0.35rem;
margin: 0.4rem 0;
}
.auto-editor > .small-text,
.auto-form {
padding-left: 1.6rem;
}
.auto-form {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.4rem;
}
.auto-form input.mono {
font-family: var(--mono);
flex: 1;
min-width: 12rem;
max-width: 22rem;
}
.auto-last {
display: inline-flex;
align-items: center;
gap: 0.3rem;
}
.auto-last.ok svg {
color: var(--ok);
}
.auto-last.ko svg {
color: var(--danger);
}
.auto-panel {
padding: 0.5rem 0;
overflow-x: auto;
}
.auto-table td {
vertical-align: top;
}