Add Proxmox snapshots: list, take, roll back, delete

A guest's drawer on the Proxmox page now has a Snapshots section. Everyone
signed in sees them, operators take them, administrators roll back and delete
(each behind a confirmation). Proxmox does each as a task, followed for up to
20 seconds. Audit log and change log entries for all three. An http:// Proxmox
address is now allowed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-10 23:25:47 +01:00
1 parent 12e9fee7af
commit 97df28c6dc
11 files changed
+733 -11

No files matched your search

+80
View File
@@ -871,6 +871,86 @@ integrationsRouter.get("/:id/proxmox/backups", asyncHandler(async (req, res) =>
}
}));
// ── Proxmox snapshots ─────────────────────────────────────────────────────
// Listing is for everyone signed in (like the guests); taking one is an operator's; rolling back and deleting one are an administrator's — a rollback throws away everything the guest has done since.
/** How long a request waits for Proxmox's task to finish before saying it is still going (the page then looks again). */
export const SNAPSHOT_WAIT_MS = 20_000;
/** The wait, which a test (or a very slow storage) can change with PROXMOX_TASK_WAIT_MS. */
const snapshotWaitMs = () => (Number(process.env.PROXMOX_TASK_WAIT_MS) > 0 ? Number(process.env.PROXMOX_TASK_WAIT_MS) : SNAPSHOT_WAIT_MS);
const snapshotBody = z.object({
name: z.string().trim().regex(ID_PATTERNS.proxmoxSnapshot, "A snapshot name starts with a letter, then letters, digits, - and _, at most 40 characters.").optional(),
description: z.string().trim().max(200).optional(),
includeRam: z.boolean().optional(),
});
/** A snapshot name made from the time, when none was given. */
export const autoSnapshotName = (now: Date = new Date()): string => `hlm-${now.toISOString().slice(0, 19).replace(/[-:]/g, "").replace("T", "-")}`;
/** The guest a request is about, checked before anything is asked of Proxmox; null (and a refusal sent) when it is not one. */
function guestFromParams(req: Request, res: Response): { node: string; type: "qemu" | "lxc"; vmid: number } | null {
if (!isValidId(req.params.node, ID_PATTERNS.proxmoxNode)) return (rejectInvalidId(res, "node name"), null);
const vmid = Number(req.params.vmid);
if (!Number.isInteger(vmid) || vmid < 1) return (res.status(400).json({ error: "invalid_vmid" }), null);
const type = req.params.type;
if (type !== "qemu" && type !== "lxc") return (res.status(400).json({ error: "invalid_type" }), null);
return { node: req.params.node, type, vmid };
}
integrationsRouter.get("/:id/proxmox/nodes/:node/:type/:vmid/snapshots", asyncHandler(async (req, res) => {
const found = await requireProxmoxAdapter(req, res);
if (!found) return;
const guest = guestFromParams(req, res);
if (!guest) return;
try {
res.json({ snapshots: await found.adapter.listSnapshots(guest.node, guest.type, guest.vmid) });
} catch (err) {
res.status(502).json({ error: err instanceof Error ? err.message : String(err) });
}
}));
/** Runs one snapshot task, writes it down, and says how it went: a task that ended badly is an error with what Proxmox said, one that is still going is said to be. */
async function snapshotTask(req: Request, res: Response, what: "create" | "rollback" | "delete", run: (found: NonNullable<Awaited<ReturnType<typeof requireProxmoxAdapter>>>, guest: { node: string; type: "qemu" | "lxc"; vmid: number }) => Promise<{ done: boolean; ok: boolean | null; message: string | null; upid: string }>, snapshot: string, extra: Record<string, unknown> = {}) {
const found = await requireProxmoxAdapter(req, res);
if (!found) return;
const guest = guestFromParams(req, res);
if (!guest) return;
let result;
try {
result = await run(found, guest);
} catch (err) {
return res.status(502).json({ error: err instanceof Error ? err.message : String(err) });
}
await recordAudit({
actor: req.currentUser!,
category: "integration",
action: `${what}_snapshot`,
targetType: "proxmox_guest",
targetId: guest.vmid,
detail: { integrationId: found.integration.id, node: guest.node, vmid: guest.vmid, guestType: guest.type, snapshot, done: result.done, ok: result.ok, ...extra },
});
if (result.done && !result.ok) return res.status(502).json({ error: `Proxmox: ${result.message ?? "the task failed"}`, snapshot, upid: result.upid });
res.json({ snapshot, done: result.done, ok: result.ok, upid: result.upid });
}
integrationsRouter.post("/:id/proxmox/nodes/:node/:type/:vmid/snapshots", requireRole("operator"), asyncHandler(async (req, res) => {
const parsed = snapshotBody.safeParse(req.body ?? {});
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "That is not a snapshot." });
const name = parsed.data.name ?? autoSnapshotName();
await snapshotTask(req, res, "create", (f, g) => f.adapter.createSnapshot(g.node, g.type, g.vmid, { name, description: parsed.data.description, includeRam: parsed.data.includeRam }, snapshotWaitMs()), name, { withRam: parsed.data.includeRam === true && req.params.type === "qemu" });
}));
integrationsRouter.post("/:id/proxmox/nodes/:node/:type/:vmid/snapshots/:name/rollback", requireRole("admin"), asyncHandler(async (req, res) => {
if (!isValidId(req.params.name, ID_PATTERNS.proxmoxSnapshot)) return rejectInvalidId(res, "snapshot name");
await snapshotTask(req, res, "rollback", (f, g) => f.adapter.rollbackSnapshot(g.node, g.type, g.vmid, req.params.name, snapshotWaitMs()), req.params.name);
}));
integrationsRouter.delete("/:id/proxmox/nodes/:node/:type/:vmid/snapshots/:name", requireRole("admin"), asyncHandler(async (req, res) => {
if (!isValidId(req.params.name, ID_PATTERNS.proxmoxSnapshot)) return rejectInvalidId(res, "snapshot name");
await snapshotTask(req, res, "delete", (f, g) => f.adapter.deleteSnapshot(g.node, g.type, g.vmid, req.params.name, snapshotWaitMs()), req.params.name);
}));
const proxmoxActions = ["start", "stop", "restart", "shutdown"] as const;
for (const action of proxmoxActions) {