Stale-backup alerts, per-server alert limits, and update runs from the Updates page
- Backups: a guest whose newest backup is older than its limit is an alert (backup kind), in the daily backup message, the digest and /metrics (homelab_backup_guests, ..._last_age_seconds) - Per-server thresholds (servers.thresholds, migration 0030): offline minutes, disk percent, forecast window, backup age, and per-disk limits or "leave alone"; applied everywhere the global ones were; saving clears the alerts cache - Update runs (update_runs, migration 0031): Update... on the Updates page puts the server under maintenance, runs a Semaphore playbook limited to it, waits for the host to report, and reports what is left and whether a restart is due; poller survives restarts - Notification toggle for update-run results; tests and docs Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5dc6cea69c
commit
7a5b623444
42 files changed
+7561
-57
No files matched your search
@@ -0,0 +1,78 @@
|
||||
import { Router } from "express";
|
||||
import { z } from "zod";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
import { ACTIVE_STATUSES, cancelUpdateRun, listUpdateRuns, startUpdateRun, UpdateRunError } from "../services/updateRuns.js";
|
||||
|
||||
export const updateRunsRouter = Router();
|
||||
|
||||
// Anyone signed in can see how updates are going, like the Updates page itself. Starting or stopping one is for operators.
|
||||
updateRunsRouter.use(requireAuth);
|
||||
|
||||
// Which hosts the playbook may touch: names, addresses and ansible patterns — and nothing that could be taken for an option. The same rule the
|
||||
// Semaphore page uses, except that it may not be left out: an update run is for one server.
|
||||
const startSchema = z.object({
|
||||
serverId: z.number().int().positive(),
|
||||
integrationId: z.number().int().positive(),
|
||||
projectId: z.number().int().positive(),
|
||||
templateId: z.number().int().positive(),
|
||||
limit: z.string().trim().regex(/^[A-Za-z0-9][A-Za-z0-9._,:*\-]{0,199}$/, "Use the host name as it is in the Semaphore inventory, like web01"),
|
||||
windowMinutes: z.number().int().min(15).max(480).default(90),
|
||||
});
|
||||
|
||||
const shape = (r: Awaited<ReturnType<typeof listUpdateRuns>>[number]) => ({
|
||||
id: r.id,
|
||||
serverId: r.serverId,
|
||||
serverName: r.serverName,
|
||||
integrationId: r.integrationId,
|
||||
template: r.templateName,
|
||||
limit: r.hostLimit,
|
||||
taskId: r.taskId,
|
||||
taskStatus: r.taskStatus,
|
||||
status: r.status,
|
||||
active: ACTIVE_STATUSES.includes(r.status as (typeof ACTIVE_STATUSES)[number]),
|
||||
message: r.message,
|
||||
startedBy: r.startedBy,
|
||||
startedAt: r.startedAt,
|
||||
taskEndedAt: r.taskEndedAt,
|
||||
finishedAt: r.finishedAt,
|
||||
updatesBefore: r.updatesBefore,
|
||||
updatesAfter: r.updatesAfter,
|
||||
rebootRequired: r.rebootRequired,
|
||||
});
|
||||
|
||||
updateRunsRouter.get("/", asyncHandler(async (req, res) => {
|
||||
const serverId = Number(req.query.serverId);
|
||||
const runs = await listUpdateRuns({ serverId: Number.isInteger(serverId) && serverId > 0 ? serverId : undefined, limit: 30 });
|
||||
res.json({ runs: runs.map(shape) });
|
||||
}));
|
||||
|
||||
updateRunsRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = startSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "Choose a server, a template and the host name it runs on.", details: parsed.error.flatten() });
|
||||
}
|
||||
const actor = req.currentUser!;
|
||||
try {
|
||||
const run = await startUpdateRun({ ...parsed.data, actor: actor.name ?? actor.email ?? actor.oidcSub });
|
||||
const [shaped] = (await listUpdateRuns({ serverId: run.serverId, limit: 1 })).map(shape);
|
||||
res.status(201).json({ run: shaped });
|
||||
} catch (err) {
|
||||
if (err instanceof UpdateRunError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
|
||||
updateRunsRouter.post("/:id/cancel", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const actor = req.currentUser!;
|
||||
try {
|
||||
const run = await cancelUpdateRun(id, actor.name ?? actor.email ?? actor.oidcSub);
|
||||
if (!run) return res.status(404).json({ error: "not_found" });
|
||||
res.json({ status: run.status });
|
||||
} catch (err) {
|
||||
if (err instanceof UpdateRunError) return res.status(err.status).json({ error: err.code, message: err.message });
|
||||
throw err;
|
||||
}
|
||||
}));
|
||||
Reference in new issue
Block a user