- Map: a Dependencies view showing what would go down with a picked box and what it relies on - Maintenance: lists what depends on a target and can silence those too (same time, audited); the recurring form and update drawer show the same note - Alerts: a headline on where things stand, held-back alerts nested under their cause, severity edge on rows; caused-by now carries the explaining alert's id Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
162 lines
7.1 KiB
TypeScript
162 lines
7.1 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { Link } from "react-router-dom";
|
|
import ImpactNote from "./ImpactNote";
|
|
import { api, type IntegrationSummary, type SemaphoreTemplate, type UpdatesServer } from "../api/client";
|
|
import { confirmDialog } from "../utils/dialogs";
|
|
import { readableError } from "../utils/errors";
|
|
import { toast } from "../utils/toast";
|
|
import DetailDrawer from "./DetailDrawer";
|
|
import { BlockSkeleton } from "./Skeleton";
|
|
import UpdatesBadge from "./UpdatesBadge";
|
|
|
|
interface Loaded {
|
|
integration: IntegrationSummary;
|
|
templates: SemaphoreTemplate[];
|
|
}
|
|
|
|
const WINDOWS = [
|
|
{ minutes: 45, label: "45 minutes" },
|
|
{ minutes: 90, label: "1½ hours" },
|
|
{ minutes: 180, label: "3 hours" },
|
|
{ minutes: 360, label: "6 hours" },
|
|
];
|
|
|
|
const LAST_KEY = "hlm.updateRun.template";
|
|
const remembered = () => {
|
|
try {
|
|
return localStorage.getItem(LAST_KEY) ?? "";
|
|
} catch {
|
|
return "";
|
|
}
|
|
};
|
|
|
|
/**
|
|
* "Update this server now": pick the playbook that applies updates, and Homelab Manager puts the server under maintenance, runs it for this
|
|
* server only, follows it, waits for the server to report again and says what is left and whether a restart is due. It asks before it
|
|
* starts, and says what it will do.
|
|
*/
|
|
export default function UpdateRunDrawer({ server, onClose, onStarted }: { server: UpdatesServer; onClose: () => void; onStarted: () => void }) {
|
|
const [loaded, setLoaded] = useState<Loaded[] | null>(null);
|
|
const [failed, setFailed] = useState(false);
|
|
const [choice, setChoice] = useState(remembered);
|
|
const [limit, setLimit] = useState(server.name);
|
|
const [minutes, setMinutes] = useState(90);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
useEffect(() => {
|
|
let alive = true;
|
|
api.integrations
|
|
.list()
|
|
.then(async (res) => {
|
|
const sems = res.integrations.filter((i) => i.type === "semaphore" && i.enabled);
|
|
const all = await Promise.all(sems.map((integration) => api.integrations.semaphore.templates(integration.id).then((r): Loaded => ({ integration, templates: r.templates })).catch((): Loaded => ({ integration, templates: [] }))));
|
|
if (alive) setLoaded(all);
|
|
})
|
|
.catch(() => alive && setFailed(true));
|
|
return () => {
|
|
alive = false;
|
|
};
|
|
}, []);
|
|
|
|
const options = (loaded ?? []).flatMap((l) =>
|
|
l.templates.map((t) => ({ key: `${l.integration.id}:${t.projectId}:${t.id}`, integration: l.integration, template: t, label: `${t.name} — ${t.projectName}${(loaded ?? []).length > 1 ? ` (${l.integration.name})` : ""}` })),
|
|
);
|
|
const picked = options.find((o) => o.key === choice);
|
|
const count = server.updates?.count ?? null;
|
|
|
|
async function start() {
|
|
if (!picked) return;
|
|
const ok = await confirmDialog({
|
|
title: `Update ${server.name} now?`,
|
|
message: `This silences alerts for ${server.name} for up to ${WINDOWS.find((w) => w.minutes === minutes)?.label ?? `${minutes} minutes`}, runs "${picked.template.name}" in ${picked.integration.name} limited to ${limit.trim()}, and waits for the server to report again. The playbook may restart it.`,
|
|
confirmLabel: "Start the update",
|
|
});
|
|
if (!ok) return;
|
|
setBusy(true);
|
|
try {
|
|
await api.updateRuns.start({ serverId: server.id, integrationId: picked.integration.id, projectId: picked.template.projectId, templateId: picked.template.id, limit: limit.trim(), windowMinutes: minutes });
|
|
try {
|
|
localStorage.setItem(LAST_KEY, picked.key);
|
|
} catch {
|
|
// a remembered choice is only a convenience
|
|
}
|
|
toast.success(`Started updating ${server.name}.`);
|
|
onStarted();
|
|
onClose();
|
|
} catch (err) {
|
|
toast.error(readableError(err));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<DetailDrawer title={`Update ${server.name}`} subtitle={server.osType === "windows" ? "Windows" : "Linux"} onClose={onClose}>
|
|
<div className="mb-3">
|
|
<UpdatesBadge updates={server.updates} />
|
|
{count !== null && count > 0 && server.updates && server.updates.security !== null && <span className="text-secondary small ms-2">{server.updates.security} of them security</span>}
|
|
</div>
|
|
|
|
{failed ? (
|
|
<div className="text-secondary">Couldn't load the integrations.</div>
|
|
) : loaded === null ? (
|
|
<BlockSkeleton lines={3} />
|
|
) : loaded.length === 0 ? (
|
|
<div className="text-secondary">
|
|
There is no Semaphore connected. Add one under <Link to="/integrations">Integrations</Link>, with a playbook that applies updates, and this server can be updated from here.
|
|
</div>
|
|
) : options.length === 0 ? (
|
|
<div className="text-secondary">No templates found in Semaphore.</div>
|
|
) : (
|
|
<>
|
|
<div className="mb-3">
|
|
<ImpactNote targets={[{ type: "server", id: server.id }]} verb="may look down while it restarts" />
|
|
</div>
|
|
<ol className="text-secondary small ps-3 mb-3">
|
|
<li>Alerts for this server are silenced (a maintenance window).</li>
|
|
<li>The playbook runs in Semaphore, for this server only.</li>
|
|
<li>Homelab Manager waits for the server to report again — it may be restarting.</li>
|
|
<li>It tells you what is still waiting and whether a restart is due, and the window ends.</li>
|
|
</ol>
|
|
<div className="mb-3">
|
|
<label className="form-label" htmlFor="ur-template">
|
|
Playbook that applies updates
|
|
</label>
|
|
<select id="ur-template" className="form-select" value={choice} onChange={(e) => setChoice(e.target.value)}>
|
|
<option value="">Choose a template…</option>
|
|
{options.map((o) => (
|
|
<option key={o.key} value={o.key}>
|
|
{o.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
<div className="mb-3">
|
|
<label className="form-label" htmlFor="ur-limit">
|
|
Run it on — the host name as it is in the Semaphore inventory
|
|
</label>
|
|
<input id="ur-limit" className="form-control" value={limit} maxLength={200} onChange={(e) => setLimit(e.target.value)} placeholder="e.g. web01" />
|
|
<div className="form-hint text-secondary small mt-1">An update run is always for this one server, never the whole inventory.</div>
|
|
</div>
|
|
<div className="mb-3">
|
|
<label className="form-label" htmlFor="ur-window">
|
|
Silence its alerts for up to
|
|
</label>
|
|
<select id="ur-window" className="form-select" value={minutes} onChange={(e) => setMinutes(Number(e.target.value))}>
|
|
{WINDOWS.map((w) => (
|
|
<option key={w.minutes} value={w.minutes}>
|
|
{w.label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<div className="form-hint text-secondary small mt-1">The window ends sooner, as soon as the run does.</div>
|
|
</div>
|
|
<button type="button" className="btn btn-primary" disabled={!picked || busy || limit.trim() === ""} onClick={() => void start()}>
|
|
{busy ? "Starting…" : "Update now"}
|
|
</button>
|
|
</>
|
|
)}
|
|
</DetailDrawer>
|
|
);
|
|
}
|