Garage UI : interface web d'administration pour Garage (API v2 + CLI)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
cedricandClaude Sonnet 5.5 committed 2026-11-20 20:18:24 +01:00
commit fdd5555100
36 files changed
+8486

No files matched your search

+284
View File
@@ -0,0 +1,284 @@
import { useState } from "react";
import { garage, type ClusterLayout, type ClusterStatus, type NodeInfo } from "./api";
import { fmtAgo, fmtSize } from "./format";
import { EditIcon, PlusIcon, RefreshIcon, TrashIcon } from "./icons";
import { Confirm, Loading, Modal, Mono, PageHead, Status, UsageBar, useAct, useLoad } from "./ui";
const GB = 1e9; // Garage raisonne en octets ; on saisit des Go décimaux, comme dans la doc
export function Cluster() {
const act = useAct();
const { data, error, loading, reload } = useLoad(async () => {
const [status, layout] = await Promise.all([garage.get<ClusterStatus>("GetClusterStatus"), garage.get<ClusterLayout>("GetClusterLayout")]);
return { status, layout };
});
const [edit, setEdit] = useState<NodeInfo | null>(null);
const [connect, setConnect] = useState(false);
const [preview, setPreview] = useState<string[] | null>(null);
const [confirmRevert, setConfirmRevert] = useState(false);
const [removing, setRemoving] = useState<NodeInfo | null>(null);
const run = (fn: () => Promise<unknown>, ok?: string) => act(fn, ok).then((r) => r && reload());
const staged = data?.layout.stagedRoleChanges ?? [];
const stagedOf = (id: string) => staged.find((s) => s.id === id);
const showPreview = async () => {
const r = await garage.post<{ error?: string; message?: string[] }>("PreviewClusterLayoutChanges");
if (r.error) throw new Error(r.error);
setPreview(r.message ?? ["Aucun détail."]);
};
return (
<div className="page">
<PageHead title="Cluster">
<button onClick={reload} disabled={loading} aria-label="Actualiser" title="Actualiser">
<RefreshIcon />
</button>
<button onClick={() => setConnect(true)}>Connecter un nœud</button>
</PageHead>
<Loading loading={loading && !data} error={error} onRetry={reload} />
{data && (
<>
{staged.length > 0 && (
<div className="notice staged">
<strong>{staged.length} changement(s) de layout en attente.</strong> Ils ne sont appliqués qu'après validation : Garage redistribue alors les données entre les nœuds.
<div className="row" style={{ marginTop: "0.5rem" }}>
<button onClick={() => act(showPreview)}>Prévisualiser</button>
<button className="primary" onClick={() => run(() => garage.post("ApplyClusterLayout", undefined, { version: data.layout.version + 1 }), "Layout appliqué")}>
Appliquer
</button>
<button onClick={() => setConfirmRevert(true)}>Annuler les changements</button>
</div>
</div>
)}
<div className="table-wrap">
<table className="table">
<thead>
<tr>
<th>Nœud</th>
<th>État</th>
<th>Zone</th>
<th className="num">Capacité</th>
<th>Données</th>
<th>Version</th>
<th />
</tr>
</thead>
<tbody>
{data.status.nodes.map((n) => {
const s = stagedOf(n.id);
const used = n.dataPartition ? n.dataPartition.total - n.dataPartition.available : 0;
return (
<tr key={n.id}>
<td>
<strong>{n.hostname || "(sans nom)"}</strong>
<div>
<Mono value={n.id} short={16} /> <small className="muted">{n.addr}</small>
</div>
</td>
<td>
<Status kind={n.isUp ? "ok" : "bad"}>{n.isUp ? "en ligne" : "hors ligne"}</Status>
{!n.isUp && <small className="muted"> {fmtAgo(n.lastSeenSecsAgo)}</small>}
{n.draining && <Status kind="warn">vidange</Status>}
</td>
<td>
{n.role ? n.role.zone : <span className="muted">sans rôle</span>}
{n.role?.tags?.length ? <small className="muted"> [{n.role.tags.join(", ")}]</small> : null}
{s && <Status kind="warn">{s.remove ? "retrait prévu" : "modifié"}</Status>}
</td>
<td className="num">{n.role ? (n.role.capacity != null ? fmtSize(n.role.capacity) : "passerelle") : "-"}</td>
<td className="quota">
{n.dataPartition ? (
<>
<UsageBar used={used} total={n.dataPartition.total} />
<small className="muted">
{fmtSize(used)} / {fmtSize(n.dataPartition.total)}
</small>
</>
) : (
<span className="muted">-</span>
)}
</td>
<td className="muted">{n.garageVersion ?? "-"}</td>
<td className="actions-cell">
<button className="icon-btn" onClick={() => setEdit(n)} aria-label="Modifier le rôle" title={n.role ? "Modifier le rôle" : "Assigner un rôle"}>
{n.role ? <EditIcon /> : <PlusIcon />}
</button>
{n.role && (
<button className="icon-btn danger" onClick={() => setRemoving(n)} aria-label="Retirer du layout" title="Retirer du layout">
<TrashIcon />
</button>
)}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
<p className="muted small-text">
Version du layout : {data.layout.version}
{data.layout.partitionSize ? ` · partitions de ${fmtSize(data.layout.partitionSize)}` : ""}
</p>
</>
)}
{edit && (
<RoleForm
node={edit}
staged={stagedOf(edit.id)}
onClose={() => setEdit(null)}
onSave={async (role) => {
if (await act(() => garage.post("UpdateClusterLayout", undefined, { roles: [{ id: edit.id, ...role }] }), "Changement mis en attente")) {
setEdit(null);
reload();
}
}}
/>
)}
{connect && <ConnectForm onClose={() => setConnect(false)} onDone={() => (setConnect(false), reload())} />}
{preview && (
<Modal title="Effet du layout" onClose={() => setPreview(null)}>
<pre className="out">{preview.join("\n")}</pre>
<div className="modal-actions">
<span className="spacer" />
<button className="primary" onClick={() => setPreview(null)}>
Fermer
</button>
</div>
</Modal>
)}
{confirmRevert && (
<Confirm
title="Annuler les changements"
message={<p>Les changements de layout en attente seront abandonnés.</p>}
confirmLabel="Annuler les changements"
onClose={() => setConfirmRevert(false)}
onConfirm={async () => {
setConfirmRevert(false);
await run(() => garage.post("RevertClusterLayout"), "Changements abandonnés");
}}
/>
)}
{removing && (
<Confirm
title="Retirer le nœud du layout"
message={
<p>
<strong>{removing.hostname || removing.id.slice(0, 12)}</strong> n'aura plus de rôle de stockage. Le changement est mis en attente : il faudra l'appliquer, et Garage redistribuera alors ses données sur les autres nœuds.
</p>
}
confirmLabel="Mettre en attente"
onClose={() => setRemoving(null)}
onConfirm={async () => {
const id = removing.id;
setRemoving(null);
await run(() => garage.post("UpdateClusterLayout", undefined, { roles: [{ id, remove: true }] }), "Retrait mis en attente");
}}
/>
)}
</div>
);
}
function RoleForm({
node,
staged,
onClose,
onSave,
}: {
node: NodeInfo;
staged?: { zone?: string; capacity?: number | null; tags?: string[]; remove?: boolean };
onClose: () => void;
onSave: (r: { zone: string; capacity: number | null; tags: string[] }) => Promise<void>;
}) {
const cur = staged && !staged.remove ? staged : node.role;
const [zone, setZone] = useState(cur?.zone ?? "dc1");
const [gateway, setGateway] = useState(cur ? cur.capacity == null : false);
const [cap, setCap] = useState(cur?.capacity ? String(cur.capacity / GB) : "");
const [tags, setTags] = useState((cur?.tags ?? []).join(", "));
const valid = zone.trim() !== "" && (gateway || parseFloat(cap) > 0);
return (
<Modal title={`Rôle de ${node.hostname || node.id.slice(0, 12)}`} onClose={onClose}>
<form
className="form"
onSubmit={(e) => {
e.preventDefault();
onSave({
zone: zone.trim(),
capacity: gateway ? null : Math.round(parseFloat(cap) * GB),
tags: tags.split(",").map((t) => t.trim()).filter(Boolean),
});
}}
>
<label className="field">
<span>Zone</span>
<input type="text" value={zone} onChange={(e) => setZone(e.target.value)} autoFocus />
</label>
<label className="check-row">
<input type="checkbox" checked={gateway} onChange={(e) => setGateway(e.target.checked)} />
<span>Nœud passerelle (aucun stockage de données)</span>
</label>
{!gateway && (
<label className="field">
<span>Capacité (Go)</span>
<input type="number" min="0" step="any" value={cap} onChange={(e) => setCap(e.target.value)} placeholder="ex. 500" />
</label>
)}
<label className="field">
<span>Étiquettes (séparées par des virgules)</span>
<input type="text" value={tags} onChange={(e) => setTags(e.target.value)} />
</label>
<div className="modal-actions">
<span className="spacer" />
<button type="button" onClick={onClose}>
Annuler
</button>
<button className="primary" disabled={!valid}>
Mettre en attente
</button>
</div>
</form>
</Modal>
);
}
function ConnectForm({ onClose, onDone }: { onClose: () => void; onDone: () => void }) {
const act = useAct();
const [addr, setAddr] = useState("");
return (
<Modal title="Connecter un nœud" onClose={onClose} small>
<form
className="form"
onSubmit={async (e) => {
e.preventDefault();
const r = await garage.post<{ success: boolean; error?: string }[]>("ConnectClusterNodes", undefined, [addr]).catch((er) => {
act(async () => {
throw er;
});
return null;
});
if (!r) return;
if (r[0]?.success) onDone();
else act(async () => { throw new Error(r[0]?.error || "Connexion refusée"); });
}}
>
<label className="field">
<span>Identifiant@adresse:port RPC</span>
<input type="text" value={addr} onChange={(e) => setAddr(e.target.value.trim())} placeholder="563e1ac8…@10.0.0.2:3901" autoFocus />
</label>
<div className="modal-actions">
<span className="spacer" />
<button type="button" onClick={onClose}>
Annuler
</button>
<button className="primary" disabled={!addr.includes("@")}>
Connecter
</button>
</div>
</form>
</Modal>
);
}