Fold alerts under their host, recurring maintenance, run playbooks from alerts, and an Updates page
- Servers can say what they run on (or it is worked out from the Proxmox node); when the host is down the guests' offline alerts fold under it: held back, not counted, not notified, still recorded. - Recurring maintenance schedules made into ordinary windows a week ahead (maintenance_schedules). - Run a Semaphore template limited to a server from its page or an alert, and watch the run. - Agents report pending updates (apt, dnf, zypper, Windows Update); an Updates page lists them. - Migrations 0024 and 0025. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
44e393f172
commit
171511c6db
45 files changed
+6772
-44
No files matched your search
@@ -23,7 +23,22 @@ const listeningPortSchema = z.object({
|
||||
process: text(100).optional(),
|
||||
});
|
||||
|
||||
// What is waiting to be installed. As lenient as the ports: a malformed answer is dropped, never allowed to cost the agent its report.
|
||||
const updatesSchema = z.object({
|
||||
manager: text(30).optional(),
|
||||
count: z.number().int().min(0).max(100_000),
|
||||
security: z.number().int().min(0).max(100_000).nullable().optional(),
|
||||
reboot_required: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const systemSchema = z.object({
|
||||
updates: z
|
||||
.unknown()
|
||||
.optional()
|
||||
.transform((u) => {
|
||||
const parsed = updatesSchema.safeParse(u);
|
||||
return parsed.success ? parsed.data : undefined;
|
||||
}),
|
||||
ip_addresses: z.array(text(100)).transform(cap(200)).optional(),
|
||||
cpu: z.object({ model: text(200).optional(), cores: z.number().optional(), load_percent: z.number().nullable().optional() }).optional(),
|
||||
memory: z.object({ total_bytes: z.number().optional(), used_bytes: z.number().optional() }).optional(),
|
||||
|
||||
@@ -633,6 +633,14 @@ integrationsRouter.get("/:id/semaphore/templates", asyncHandler(async (req, res)
|
||||
}
|
||||
}));
|
||||
|
||||
// Which hosts a run may be limited to: names, addresses and ansible patterns (commas, colons, wildcards) — and nothing that could be
|
||||
// taken for an option. Semaphore hands the value to ansible as an argument, but it is still only ever shown the characters a host has.
|
||||
const runBodySchema = z
|
||||
.object({
|
||||
limit: z.union([z.literal(""), z.string().trim().regex(/^[A-Za-z0-9][A-Za-z0-9._,:*\-]{0,199}$/, "Use host names or patterns like web01,db*")]).optional(),
|
||||
})
|
||||
.default({});
|
||||
|
||||
integrationsRouter.post(
|
||||
"/:id/semaphore/projects/:projectId/templates/:templateId/run",
|
||||
requireRole("operator"),
|
||||
@@ -645,16 +653,25 @@ integrationsRouter.post(
|
||||
if (!Number.isInteger(projectId) || !Number.isInteger(templateId)) {
|
||||
return res.status(400).json({ error: "invalid_id" });
|
||||
}
|
||||
const body = runBodySchema.safeParse(req.body ?? {});
|
||||
if (!body.success) {
|
||||
return res.status(400).json({ error: "invalid_body", message: "The hosts to limit it to can only be host names or patterns like web01,db*.", details: body.error.flatten() });
|
||||
}
|
||||
|
||||
try {
|
||||
const task = await found.adapter.runTemplate(projectId, templateId);
|
||||
const actor = req.currentUser!;
|
||||
const limit = body.data.limit || undefined;
|
||||
const task = await found.adapter.runTemplate(projectId, templateId, {
|
||||
limit,
|
||||
message: `Started in Homelab Manager by ${actor.name ?? actor.email ?? actor.oidcSub}${limit ? ` (limited to ${limit})` : ""}`,
|
||||
});
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
actor,
|
||||
category: "integration",
|
||||
action: "run_template",
|
||||
targetType: "semaphore_template",
|
||||
targetId: templateId,
|
||||
detail: { integrationId: found.integration.id, projectId, taskId: task.id },
|
||||
detail: { integrationId: found.integration.id, projectId, taskId: task.id, ...(limit ? { limit } : {}) },
|
||||
});
|
||||
res.status(201).json({ task });
|
||||
} catch (err) {
|
||||
@@ -663,6 +680,20 @@ integrationsRouter.post(
|
||||
}),
|
||||
);
|
||||
|
||||
// How a run is getting on: waiting, running, success or error.
|
||||
integrationsRouter.get("/:id/semaphore/projects/:projectId/tasks/:taskId", asyncHandler(async (req, res) => {
|
||||
const found = await requireSemaphoreAdapter(req, res);
|
||||
if (!found) return;
|
||||
const projectId = Number(req.params.projectId);
|
||||
const taskId = Number(req.params.taskId);
|
||||
if (!Number.isInteger(projectId) || !Number.isInteger(taskId)) return res.status(400).json({ error: "invalid_id" });
|
||||
try {
|
||||
res.json({ task: await found.adapter.getTask(projectId, taskId) });
|
||||
} catch (err) {
|
||||
res.status(502).json({ error: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
}));
|
||||
|
||||
// ─── Proxmox ─────────────────────────────────────────────────────────────────
|
||||
|
||||
async function requireProxmoxAdapter(req: Request, res: Response) {
|
||||
|
||||
@@ -2,7 +2,8 @@ import { Router } from "express";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { dnsProviders, integrations, maintenanceAliases, maintenanceTargetTypes, maintenanceWindows, servers } from "../db/schema.js";
|
||||
import { dnsProviders, integrations, maintenanceAliases, maintenanceSchedules, maintenanceTargetTypes, maintenanceWindows, servers } from "../db/schema.js";
|
||||
import { deleteSchedule, isValidTimeZone, materializeSchedules, occurrencesBetween, removeFutureWindows, targetsOf } from "../services/maintenanceSchedules.js";
|
||||
import { hostOfAddress } from "../services/kumaTargets.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
@@ -268,6 +269,105 @@ maintenanceRouter.post("/end-all", requireRole("operator"), asyncHandler(async (
|
||||
res.json({ ended: active.length });
|
||||
}));
|
||||
|
||||
// ─── Recurring schedules ────────────────────────────────────────────────────
|
||||
|
||||
const scheduleSchema = z.object({
|
||||
name: z.string().trim().min(1).max(80),
|
||||
reason: z.string().trim().max(200).optional(),
|
||||
targets: z.array(z.object({ type: z.enum(maintenanceTargetTypes), id: z.number().int() })).min(1).max(100),
|
||||
days: z.array(z.number().int().min(0).max(6)).min(1).max(7),
|
||||
startTime: z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/, "Use a time like 02:00"),
|
||||
durationMinutes: z.number().int().min(15).max(3 * 24 * 60),
|
||||
timezone: z.string().min(1).max(64),
|
||||
});
|
||||
|
||||
maintenanceRouter.get("/schedules", asyncHandler(async (_req, res) => {
|
||||
const rows = await db.select().from(maintenanceSchedules).orderBy(maintenanceSchedules.name);
|
||||
const now = new Date();
|
||||
const out = [];
|
||||
for (const s of rows) {
|
||||
const targets = [];
|
||||
for (const t of targetsOf(s)) {
|
||||
const d = await describeTarget(t.type, t.id);
|
||||
if (d) targets.push({ type: t.type, id: t.id, name: d.name, kind: d.kind });
|
||||
}
|
||||
const next = s.enabled ? occurrencesBetween(s, now, new Date(now.getTime() + 15 * 24 * 3600_000))[0] : undefined;
|
||||
out.push({
|
||||
id: s.id,
|
||||
name: s.name,
|
||||
reason: s.reason,
|
||||
days: JSON.parse(s.days) as number[],
|
||||
startTime: s.startTime,
|
||||
durationMinutes: s.durationMinutes,
|
||||
timezone: s.timezone,
|
||||
enabled: s.enabled,
|
||||
createdBy: s.createdBy,
|
||||
targets,
|
||||
nextStart: next ? next.start.toISOString() : null,
|
||||
});
|
||||
}
|
||||
res.json({ schedules: out });
|
||||
}));
|
||||
|
||||
maintenanceRouter.post("/schedules", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = scheduleSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: "invalid_body", message: "Name it, choose what it covers, the days, a start time and how long.", details: parsed.error.flatten() });
|
||||
}
|
||||
const data = parsed.data;
|
||||
if (!isValidTimeZone(data.timezone)) return res.status(400).json({ error: "invalid_timezone", message: "That isn't a time zone this server knows." });
|
||||
// Only things that exist, once each.
|
||||
const seen = new Set<string>();
|
||||
const targets = [];
|
||||
for (const t of data.targets) {
|
||||
const key = `${t.type}:${t.id}`;
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
if (!(await describeTarget(t.type, t.id))) return res.status(400).json({ error: "unknown_target", message: "One of the things chosen doesn't exist any more." });
|
||||
targets.push(t);
|
||||
}
|
||||
const actor = req.currentUser!;
|
||||
const [created] = await db
|
||||
.insert(maintenanceSchedules)
|
||||
.values({
|
||||
name: data.name,
|
||||
reason: data.reason || null,
|
||||
targets: JSON.stringify(targets),
|
||||
days: JSON.stringify([...new Set(data.days)].sort()),
|
||||
startTime: data.startTime,
|
||||
durationMinutes: data.durationMinutes,
|
||||
timezone: data.timezone,
|
||||
createdBy: actor.name ?? actor.email ?? actor.oidcSub,
|
||||
})
|
||||
.returning();
|
||||
const made = await materializeSchedules(new Date(), undefined, created.id);
|
||||
await recordAudit({ actor, category: "maintenance", action: "create_schedule", targetType: "maintenance_schedule", targetId: created.id, detail: { name: created.name, targets: targets.length, days: data.days, startTime: data.startTime, durationMinutes: data.durationMinutes } });
|
||||
res.status(201).json({ id: created.id, windowsMade: made });
|
||||
}));
|
||||
|
||||
maintenanceRouter.patch("/schedules/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
const parsed = z.object({ enabled: z.boolean() }).safeParse(req.body);
|
||||
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body" });
|
||||
const [row] = await db.select().from(maintenanceSchedules).where(eq(maintenanceSchedules.id, id)).limit(1);
|
||||
if (!row) return res.status(404).json({ error: "not_found" });
|
||||
await db.update(maintenanceSchedules).set({ enabled: parsed.data.enabled }).where(eq(maintenanceSchedules.id, id));
|
||||
if (parsed.data.enabled) await materializeSchedules(new Date(), undefined, id);
|
||||
else await removeFutureWindows(id);
|
||||
await recordAudit({ actor: req.currentUser!, category: "maintenance", action: parsed.data.enabled ? "enable_schedule" : "disable_schedule", targetType: "maintenance_schedule", targetId: id, detail: { name: row.name } });
|
||||
res.json({ enabled: parsed.data.enabled });
|
||||
}));
|
||||
|
||||
maintenanceRouter.delete("/schedules/:id", 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 [row] = await db.select().from(maintenanceSchedules).where(eq(maintenanceSchedules.id, id)).limit(1);
|
||||
if (!row) return res.status(404).json({ error: "not_found" });
|
||||
await deleteSchedule(id);
|
||||
await recordAudit({ actor: req.currentUser!, category: "maintenance", action: "delete_schedule", targetType: "maintenance_schedule", targetId: id, detail: { name: row.name } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
const syncUptimeKumaSchema = z.object({ integrationId: z.number().int().positive() });
|
||||
|
||||
/** Reads Uptime Kuma's maintenance schedule now and brings the mirrored windows in line with it (the same pass that runs every 15 minutes when switched on). */
|
||||
|
||||
@@ -6,6 +6,8 @@ import { servers, serverLinks, integrations, dnsRecordsCache, dnsZonesCache, dns
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { generateApiToken } from "../services/tokens.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { loadParentRows, resolveParents, wouldLoop } from "../services/serverParents.js";
|
||||
import { parsePendingUpdates } from "../services/pendingUpdates.js";
|
||||
import { getServerMetrics, RETENTION_DAYS } from "../services/serverMetrics.js";
|
||||
import { getServerActivity } from "../services/serverActivity.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -21,9 +23,9 @@ import { getSettings } from "../services/settingsStore.js";
|
||||
export const serversRouter = Router();
|
||||
|
||||
/** A server row as the API returns it: no token hash, and tags as an array rather than the stored JSON. */
|
||||
function publicServer<T extends { apiTokenHash: string; tags: string | null }>(row: T) {
|
||||
const { apiTokenHash: _hash, tags, ...rest } = row;
|
||||
return { ...rest, tags: parseTags(tags) };
|
||||
function publicServer<T extends { apiTokenHash: string; tags: string | null; pendingUpdates: string | null }>(row: T) {
|
||||
const { apiTokenHash: _hash, tags, pendingUpdates, ...rest } = row;
|
||||
return { ...rest, tags: parseTags(tags), pendingUpdates: parsePendingUpdates(pendingUpdates) };
|
||||
}
|
||||
|
||||
serversRouter.use(requireAuth);
|
||||
@@ -113,6 +115,8 @@ const updateServerSchema = z
|
||||
proxmoxGuestType: z.enum(["qemu", "lxc"]).nullable().optional(),
|
||||
proxmoxVmid: z.number().int().nullable().optional(),
|
||||
hideProxmoxLink: z.boolean().optional(),
|
||||
// The server this one runs on. Null goes back to working it out from the Proxmox node.
|
||||
parentServerId: z.number().int().nullable().optional(),
|
||||
})
|
||||
.refine(
|
||||
(data) => {
|
||||
@@ -148,6 +152,17 @@ serversRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req, res)
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.data.parentServerId !== undefined && parsed.data.parentServerId !== null) {
|
||||
const parentId = parsed.data.parentServerId;
|
||||
const rows = await loadParentRows();
|
||||
if (parentId === id || !rows.some((r) => r.id === parentId)) {
|
||||
return res.status(400).json({ error: "invalid_parent", message: "Choose another server that exists." });
|
||||
}
|
||||
if (wouldLoop(rows, id, parentId)) {
|
||||
return res.status(400).json({ error: "parent_loop", message: "That server already runs on this one, so each would be waiting for the other." });
|
||||
}
|
||||
}
|
||||
|
||||
const [updated] = await db.update(servers).set(parsed.data).where(eq(servers.id, id)).returning();
|
||||
|
||||
await recordAudit({
|
||||
@@ -156,7 +171,7 @@ serversRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req, res)
|
||||
action: "update",
|
||||
targetType: "server",
|
||||
targetId: id,
|
||||
detail: { name: updated.name },
|
||||
detail: { name: updated.name, ...(parsed.data.parentServerId !== undefined ? { parentServerId: parsed.data.parentServerId } : {}) },
|
||||
});
|
||||
|
||||
const serverOut = publicServer(updated);
|
||||
@@ -173,6 +188,27 @@ serversRouter.get("/summary", asyncHandler(async (_req, res) => {
|
||||
res.json(buildServerSummary(rows, healthChecks, Date.now(), await activeSubjects()));
|
||||
}));
|
||||
|
||||
// What every server that reports through an agent says is waiting to be installed, for the Updates page. Servers whose agent hasn't
|
||||
// said (it predates this, or couldn't find out) are listed with no figures rather than left out.
|
||||
serversRouter.get("/updates", asyncHandler(async (_req, res) => {
|
||||
const rows = await db
|
||||
.select({
|
||||
id: servers.id,
|
||||
name: servers.name,
|
||||
osType: servers.osType,
|
||||
lastSeenAt: servers.lastSeenAt,
|
||||
pendingUpdates: servers.pendingUpdates,
|
||||
updatesPendingSince: servers.updatesPendingSince,
|
||||
})
|
||||
.from(servers)
|
||||
.orderBy(servers.name);
|
||||
res.json({
|
||||
servers: rows
|
||||
.filter((r) => r.lastSeenAt !== null)
|
||||
.map((r) => ({ id: r.id, name: r.name, osType: r.osType, lastSeenAt: r.lastSeenAt, updates: parsePendingUpdates(r.pendingUpdates), pendingSince: r.updatesPendingSince })),
|
||||
});
|
||||
}));
|
||||
|
||||
// For the Operations > Admin Links page — every server's admin bookmarks in one place, instead of visiting
|
||||
// each server's own detail page to find them.
|
||||
serversRouter.get("/links", asyncHandler(async (_req, res) => {
|
||||
@@ -306,10 +342,21 @@ serversRouter.get("/:id/detail", asyncHandler(async (req, res) => {
|
||||
memUsedBytes: _memUsedBytes,
|
||||
listeningPorts: _listeningPorts,
|
||||
lastPortScan: _lastPortScan,
|
||||
pendingUpdates: rawUpdates,
|
||||
...serverOut
|
||||
} = server;
|
||||
|
||||
res.json({ server: { ...serverOut, tags: parseTags(rawTags) }, ipAddresses, hardware, dnsMatches, links });
|
||||
// What this server runs on, and what runs on it (set by hand, or worked out from its Proxmox node).
|
||||
const parentRows = await loadParentRows();
|
||||
const links2 = resolveParents(parentRows);
|
||||
const nameOf = new Map(parentRows.map((r) => [r.id, r.name]));
|
||||
const parentLink = links2.get(id);
|
||||
const relations = {
|
||||
parent: parentLink ? { id: parentLink.parentId, name: nameOf.get(parentLink.parentId) ?? "", auto: parentLink.auto } : null,
|
||||
children: [...links2.entries()].filter(([, l]) => l.parentId === id).map(([childId, l]) => ({ id: childId, name: nameOf.get(childId) ?? "", auto: l.auto })).sort((a, b) => a.name.localeCompare(b.name)),
|
||||
};
|
||||
|
||||
res.json({ server: { ...serverOut, tags: parseTags(rawTags), pendingUpdates: parsePendingUpdates(rawUpdates) }, ipAddresses, hardware, dnsMatches, links, relations });
|
||||
}));
|
||||
|
||||
const tagsSchema = z.object({ tags: z.array(z.string().max(100)).max(50) });
|
||||
@@ -440,6 +487,9 @@ serversRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req, res)
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
|
||||
// Servers that ran on this one go back to working out where they run (the link is a plain column, not a foreign key).
|
||||
await db.update(servers).set({ parentServerId: null }).where(eq(servers.parentServerId, id));
|
||||
|
||||
const deleted = await db.delete(servers).where(eq(servers.id, id)).returning();
|
||||
if (deleted.length === 0) return res.status(404).json({ error: "not_found" });
|
||||
|
||||
|
||||
Reference in new issue
Block a user