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:
1 parent
12e9fee7af
commit
97df28c6dc
11 files changed
+733
-11
No files matched your search
@@ -3524,6 +3524,25 @@ export interface ProxmoxGuest {
|
||||
uptime: number | null;
|
||||
}
|
||||
|
||||
/** One snapshot of a Proxmox guest. */
|
||||
export interface ProxmoxSnapshot {
|
||||
name: string;
|
||||
description: string;
|
||||
takenAt: string | null;
|
||||
/** A VM's snapshot that kept its memory. */
|
||||
withRam: boolean;
|
||||
/** The snapshot it was taken after. */
|
||||
parent: string | null;
|
||||
}
|
||||
|
||||
/** How a snapshot task went: done and worked, still going (done false), or an error (the request fails with what Proxmox said). */
|
||||
export interface ProxmoxSnapshotResult {
|
||||
snapshot: string;
|
||||
done: boolean;
|
||||
ok: boolean | null;
|
||||
upid: string;
|
||||
}
|
||||
|
||||
export interface ProxmoxGuestsResponse {
|
||||
guests: ProxmoxGuest[];
|
||||
summary: { total: number; running: number };
|
||||
@@ -4447,6 +4466,14 @@ export const api = {
|
||||
request<void>(`/api/integrations/${integrationId}/proxmox/nodes/${node}/${type}/${vmid}/restart`, { method: "POST" }),
|
||||
shutdown: (integrationId: number, node: string, type: ProxmoxGuestType, vmid: number) =>
|
||||
request<void>(`/api/integrations/${integrationId}/proxmox/nodes/${node}/${type}/${vmid}/shutdown`, { method: "POST" }),
|
||||
snapshots: (integrationId: number, node: string, type: ProxmoxGuestType, vmid: number) =>
|
||||
request<{ snapshots: ProxmoxSnapshot[] }>(`/api/integrations/${integrationId}/proxmox/nodes/${node}/${type}/${vmid}/snapshots`),
|
||||
createSnapshot: (integrationId: number, node: string, type: ProxmoxGuestType, vmid: number, body: { name?: string; description?: string; includeRam?: boolean }) =>
|
||||
request<ProxmoxSnapshotResult>(`/api/integrations/${integrationId}/proxmox/nodes/${node}/${type}/${vmid}/snapshots`, { method: "POST", body: JSON.stringify(body) }),
|
||||
rollbackSnapshot: (integrationId: number, node: string, type: ProxmoxGuestType, vmid: number, name: string) =>
|
||||
request<ProxmoxSnapshotResult>(`/api/integrations/${integrationId}/proxmox/nodes/${node}/${type}/${vmid}/snapshots/${encodeURIComponent(name)}/rollback`, { method: "POST" }),
|
||||
deleteSnapshot: (integrationId: number, node: string, type: ProxmoxGuestType, vmid: number, name: string) =>
|
||||
request<ProxmoxSnapshotResult>(`/api/integrations/${integrationId}/proxmox/nodes/${node}/${type}/${vmid}/snapshots/${encodeURIComponent(name)}`, { method: "DELETE" }),
|
||||
},
|
||||
synology: {
|
||||
storage: (integrationId: number) => request<SynologyStorageResponse>(`/api/integrations/${integrationId}/synology/storage`),
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { api, type ProxmoxGuest, type ProxmoxSnapshot, type ProxmoxSnapshotResult } from "../api/client";
|
||||
import { confirmDialog } from "../utils/dialogs";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
import { snapshotDepths } from "../utils/proxmoxSnapshots";
|
||||
import { ErrorNotice } from "./Notice";
|
||||
import TimeAgo from "./TimeAgo";
|
||||
|
||||
/**
|
||||
* The snapshots of one guest: what there is (in the order they were taken, each indented under the one it was taken after), a way to take one (an operator's), and to roll back to one or
|
||||
* delete one (an administrator's, because a rollback throws away everything the guest has done since). Proxmox does each as a task: the answer says how it went, or that it is still going, and the list
|
||||
* is looked at again a little later.
|
||||
*/
|
||||
export default function ProxmoxSnapshots({ integrationId, guest, canTake, canRoll }: { integrationId: number; guest: ProxmoxGuest; canTake: boolean; canRoll: boolean }) {
|
||||
const [snapshots, setSnapshots] = useState<ProxmoxSnapshot[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [name, setName] = useState("");
|
||||
const [description, setDescription] = useState("");
|
||||
const [includeRam, setIncludeRam] = useState(false);
|
||||
const [busy, setBusy] = useState<string | null>(null);
|
||||
const timers = useRef<number[]>([]);
|
||||
const key = `${guest.node}:${guest.type}:${guest.vmid}`;
|
||||
const kind = guest.type === "qemu" ? "VM" : "container";
|
||||
|
||||
const load = useCallback(() => {
|
||||
api.integrations.proxmox
|
||||
.snapshots(integrationId, guest.node, guest.type, guest.vmid)
|
||||
.then((res) => {
|
||||
setSnapshots(res.snapshots);
|
||||
setError(null);
|
||||
})
|
||||
.catch((err) => setError(readableError(err)));
|
||||
}, [integrationId, guest.node, guest.type, guest.vmid]);
|
||||
|
||||
useEffect(() => {
|
||||
setSnapshots(null);
|
||||
load();
|
||||
const pending = timers.current;
|
||||
return () => pending.forEach((t) => window.clearTimeout(t));
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [key, integrationId]);
|
||||
|
||||
/** What was done, in words; a task Proxmox is still working on is looked at again in a few seconds, twice. */
|
||||
function report(done: string, result: ProxmoxSnapshotResult) {
|
||||
if (result.done) toast.success(done);
|
||||
else {
|
||||
toast.info("Proxmox is still working on it — the list is looked at again in a moment.");
|
||||
timers.current.push(window.setTimeout(load, 4000), window.setTimeout(load, 12000));
|
||||
}
|
||||
load();
|
||||
}
|
||||
|
||||
async function run(what: string, job: () => Promise<ProxmoxSnapshotResult>, done: string) {
|
||||
setBusy(what);
|
||||
try {
|
||||
report(done, await job());
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
} finally {
|
||||
setBusy(null);
|
||||
}
|
||||
}
|
||||
|
||||
async function take(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
await run("take", () => api.integrations.proxmox.createSnapshot(integrationId, guest.node, guest.type, guest.vmid, { name: name.trim() || undefined, description: description.trim() || undefined, includeRam: includeRam && guest.type === "qemu" && guest.status === "running" }), "Snapshot taken.");
|
||||
setName("");
|
||||
setDescription("");
|
||||
}
|
||||
|
||||
async function rollBack(s: ProxmoxSnapshot) {
|
||||
const ok = await confirmDialog({
|
||||
title: `Roll back ${guest.name}`,
|
||||
message: `Roll back ${kind} "${guest.name}" to "${s.name}"? Everything it has done since ${s.takenAt ? new Date(s.takenAt).toLocaleString() : "then"} is lost${s.withRam ? "" : guest.type === "qemu" ? ", and it comes back stopped (this snapshot kept no memory)" : ""}. Snapshots taken after "${s.name}" are removed by Proxmox.`,
|
||||
confirmLabel: "Roll back",
|
||||
danger: true,
|
||||
});
|
||||
if (ok) await run(`roll:${s.name}`, () => api.integrations.proxmox.rollbackSnapshot(integrationId, guest.node, guest.type, guest.vmid, s.name), `Rolled ${guest.name} back to ${s.name}.`);
|
||||
}
|
||||
|
||||
async function remove(s: ProxmoxSnapshot) {
|
||||
const ok = await confirmDialog({ title: "Delete a snapshot", message: `Delete the snapshot "${s.name}" of ${kind} "${guest.name}"? The guest itself is not touched, but you can no longer roll back to it.`, confirmLabel: "Delete", danger: true });
|
||||
if (ok) await run(`del:${s.name}`, () => api.integrations.proxmox.deleteSnapshot(integrationId, guest.node, guest.type, guest.vmid, s.name), `Deleted ${s.name}.`);
|
||||
}
|
||||
|
||||
const depths = snapshots ? snapshotDepths(snapshots) : {};
|
||||
|
||||
return (
|
||||
<div className="mt-4">
|
||||
<h4>Snapshots</h4>
|
||||
<ErrorNotice error={error} />
|
||||
{canTake && (
|
||||
<form className="row g-2 mb-3" onSubmit={(e) => void take(e)}>
|
||||
<div className="col-sm-5">
|
||||
<input className="form-control form-control-sm" aria-label="Snapshot name" placeholder="Name (leave empty for one from the time)" maxLength={40} pattern="[A-Za-z][A-Za-z0-9_\-]{0,39}" title="Starts with a letter, then letters, digits, - and _" value={name} onChange={(e) => setName(e.target.value)} />
|
||||
</div>
|
||||
<div className="col-sm-7">
|
||||
<input className="form-control form-control-sm" aria-label="Description" placeholder="Why (optional)" maxLength={200} value={description} onChange={(e) => setDescription(e.target.value)} />
|
||||
</div>
|
||||
<div className="col-12 d-flex flex-wrap align-items-center gap-3">
|
||||
{guest.type === "qemu" && guest.status === "running" && (
|
||||
<label className="form-check mb-0">
|
||||
<input type="checkbox" className="form-check-input" checked={includeRam} onChange={(e) => setIncludeRam(e.target.checked)} />
|
||||
<span className="form-check-label">Keep its memory too (a rollback then brings it back running)</span>
|
||||
</label>
|
||||
)}
|
||||
<button className="btn btn-sm btn-primary ms-auto" disabled={busy !== null}>
|
||||
{busy === "take" ? "Taking it…" : "Take a snapshot"}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
{snapshots === null && !error && <div className="text-secondary small">Reading them…</div>}
|
||||
{snapshots?.length === 0 && <div className="text-secondary small">None has been taken.</div>}
|
||||
{snapshots && snapshots.length > 0 && (
|
||||
<div className="table-responsive">
|
||||
<table className="table table-sm table-vcenter mb-0">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Snapshot</th>
|
||||
<th>Taken</th>
|
||||
{canRoll && <th className="w-1" />}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{snapshots.map((s) => (
|
||||
<tr key={s.name}>
|
||||
<td>
|
||||
<div style={{ paddingLeft: `${(depths[s.name] ?? 0) * 1}rem` }}>
|
||||
<strong>{s.name}</strong> {s.withRam && <span className="badge bg-blue-lt text-blue">with memory</span>}
|
||||
{s.description && <div className="text-secondary small text-break">{s.description}</div>}
|
||||
</div>
|
||||
</td>
|
||||
<td className="text-nowrap">{s.takenAt ? <TimeAgo value={s.takenAt} /> : "—"}</td>
|
||||
{canRoll && (
|
||||
<td>
|
||||
<div className="btn-list flex-nowrap">
|
||||
<button className="btn btn-sm" disabled={busy !== null} onClick={() => void rollBack(s)}>
|
||||
Roll back…
|
||||
</button>
|
||||
<button className="btn btn-sm btn-outline-danger" disabled={busy !== null} onClick={() => void remove(s)}>
|
||||
Delete…
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
)}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
<div className="text-secondary small mt-2">Proxmox does each of these as a task and can take a while on a large disk. Rolling back needs an administrator.</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import { useRowDetail } from "../hooks/useRowDetail";
|
||||
import { formatDateTime } from "../utils/date";
|
||||
import ExportTableButton from "../components/ExportTableButton";
|
||||
import { EmptyRow } from "../components/EmptyState";
|
||||
import ProxmoxSnapshots from "../components/ProxmoxSnapshots";
|
||||
|
||||
function proxmoxStatusBadge(status: string) {
|
||||
return status === "running" ? (
|
||||
@@ -609,6 +610,7 @@ export default function Proxmox({ user }: { user: CurrentUser }) {
|
||||
{detail.selected && (
|
||||
<DetailDrawer title={((g) => g.name)(detail.selected)} subtitle={((g) => `${g.type === "qemu" ? "Virtual machine" : "Container"} ${g.vmid} on ${g.node}`)(detail.selected)} onClose={detail.close}>
|
||||
<ObjectDetails data={detail.selected} />
|
||||
{selectedId !== null && <ProxmoxSnapshots integrationId={selectedId} guest={detail.selected} canTake={canEdit} canRoll={user.role === "admin"} />}
|
||||
</DetailDrawer>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { ProxmoxSnapshot } from "../api/client";
|
||||
|
||||
/**
|
||||
* How far in each snapshot is drawn: a chain of snapshots, each taken after the one before, is all at the left edge, and only a branch (several taken after the same one) is indented, one step for each
|
||||
* branch it is down. A parent that is not in the list counts as the top. Pure.
|
||||
*/
|
||||
export function snapshotDepths(snapshots: Pick<ProxmoxSnapshot, "name" | "parent">[]): Record<string, number> {
|
||||
const known = new Set(snapshots.map((s) => s.name));
|
||||
const children = new Map<string, number>();
|
||||
for (const s of snapshots) if (s.parent !== null && known.has(s.parent)) children.set(s.parent, (children.get(s.parent) ?? 0) + 1);
|
||||
const parentOf = new Map(snapshots.map((s) => [s.name, s.parent !== null && known.has(s.parent) ? s.parent : null]));
|
||||
const depth: Record<string, number> = {};
|
||||
for (const s of snapshots) {
|
||||
let d = 0;
|
||||
let at: string = s.name;
|
||||
const seen = new Set<string>([at]);
|
||||
for (let parent = parentOf.get(at) ?? null; parent !== null && !seen.has(parent); parent = parentOf.get(at) ?? null) {
|
||||
if ((children.get(parent) ?? 0) > 1) d++;
|
||||
seen.add(parent);
|
||||
at = parent;
|
||||
}
|
||||
depth[s.name] = d;
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
Reference in new issue
Block a user