Servers and the agent, dashboard (2.1.0): servers, installs, updates, jobs, ports, download page
- Servers: add with a one-time join command, each server's CPU / RAM charts (24 h, 7 days, 90 days, spikes over 85% marked, table view), configuration, PM2 processes, sites, jobs and alerts. - Installs: New install (server chooser with free RAM, CPU and sites), Link existing, FastPanel steps and Proxy done, schedule / emergency update, adopt, uninstall (2FA and the short name). - Updates: one release to many installs (a batch), planned jobs. - Jobs with their reports; Ports & defaults; the public download page that decrypts an export in the browser. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
"use client";
|
||||
|
||||
import React, { useCallback, useEffect, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { useParams, useSearchParams } from "next/navigation";
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import { bytes, fmtDate } from "@/lib/format";
|
||||
import { Loading, Message, PageTitle } from "@/components/ui";
|
||||
import { ServersNav, StatusPill, toInput, fromInput, type Deployment, type Job, type ServerSummary } from "@/components/servers";
|
||||
|
||||
type Msg = { type: "ok" | "err"; text: string } | null;
|
||||
type Detail = {
|
||||
deployment: Deployment & { server: ServerSummary; jobs: Job[]; exports: { id: string; bytes: number; expiresAt: string; downloadedAt: string | null; deletedAt: string | null }[]; install: { version: string | null; url: string } | null };
|
||||
fastPanel: string;
|
||||
};
|
||||
|
||||
const H = 60 * 60 * 1000;
|
||||
|
||||
/** One install on a server: proxy, updates, adopt, uninstall (§7–9). */
|
||||
export default function DeploymentPage() {
|
||||
const { id } = useParams<{ id: string }>();
|
||||
const warn = useSearchParams().get("warn");
|
||||
const [d, setD] = useState<Detail | null>(null);
|
||||
const [msg, setMsg] = useState<Msg>(warn ? { type: "err", text: warn } : null);
|
||||
const [problems, setProblems] = useState<string[]>([]);
|
||||
const [upd, setUpd] = useState({ version: "", at: toInput(new Date(Date.now() + 5 * H)), emergency: false, reason: "", totp: "" });
|
||||
const [adopt, setAdopt] = useState({ eventsDir: "", version: "", at: toInput(new Date(Date.now() + 5 * H)) });
|
||||
const [un, setUn] = useState({ at: toInput(new Date(Date.now() + 7 * 24 * H)), totp: "", confirm: "" });
|
||||
|
||||
const load = useCallback(() => api<Detail>(`/api/admin/deployments/${id}`).then(setD).catch(e => setMsg({ type: "err", text: errorText(e) })), [id]);
|
||||
useEffect(() => { load(); }, [load]);
|
||||
|
||||
const act = async (path: string, body: unknown, ok: string) => {
|
||||
setMsg(null);
|
||||
try {
|
||||
const r = await api<{ warnings?: string[] }>(path, { method: "POST", body });
|
||||
setMsg({ type: "ok", text: `${ok}${r?.warnings?.length ? ` Note: ${r.warnings.join(" ")}` : ""}` });
|
||||
load();
|
||||
} catch (e) { setMsg({ type: "err", text: errorText(e) }); }
|
||||
};
|
||||
const proxyDone = async () => {
|
||||
setMsg(null);
|
||||
try {
|
||||
const r = await api<{ live: boolean; problems: string[] }>(`/api/admin/deployments/${id}/proxy-done`, { method: "POST" });
|
||||
setProblems(r.problems);
|
||||
setMsg(r.live ? { type: "ok", text: "Live. The site admin got the welcome email." } : { type: "err", text: "The site isn't answering as expected yet:" });
|
||||
load();
|
||||
} catch (e) { setMsg({ type: "err", text: errorText(e) }); }
|
||||
};
|
||||
|
||||
if (!d) return <div><ServersNav active="/servers/installs" />{msg ? <Message msg={msg} /> : <Loading />}</div>;
|
||||
const x = d.deployment;
|
||||
const minAt = upd.emergency ? 15 * 60 * 1000 : 4 * H;
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<PageTitle title={`${x.customer?.name} (${x.shortName})`} subtitle={<>{x.domain} · on <Link className="text-indigo-700 hover:underline" href={`/servers/${x.serverId}`}>{x.server.name}</Link> · ports web {x.webPort}, api {x.apiPort} · version {x.install?.version || x.version || "–"} <StatusPill status={x.status} /></>} />
|
||||
<ServersNav active="/servers/installs" />
|
||||
<Message msg={msg} />
|
||||
{!!problems.length && <ul className="mt-2 list-disc pl-6 text-sm text-red-800">{problems.map(p => <li key={p}>{p}</li>)}</ul>}
|
||||
</div>
|
||||
|
||||
{(x.status === "waiting_for_proxy" || x.status === "installing") && (
|
||||
<div className="card">
|
||||
<h2 className="font-medium">Set up the site in FastPanel</h2>
|
||||
<pre className="mt-3 overflow-x-auto whitespace-pre-wrap rounded-lg bg-slate-50 p-3 text-xs">{d.fastPanel}</pre>
|
||||
<button className="btn-primary mt-3" disabled={x.status !== "waiting_for_proxy"} onClick={proxyDone}>Proxy done</button>
|
||||
{x.status === "installing" && <p className="mt-2 text-xs text-slate-500">Waiting for the install to finish (see its job below).</p>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{x.status === "live" && (
|
||||
<form className="card grid gap-3 md:grid-cols-2" onSubmit={e => { e.preventDefault(); act("/api/admin/updates", { deploymentIds: [x.id], version: upd.version || undefined, at: fromInput(upd.at), emergency: upd.emergency, reason: upd.reason || null, totp: upd.totp || null }, "Planned. The site admin was emailed and sees a banner."); }}>
|
||||
<h2 className="font-medium md:col-span-2">Schedule an update</h2>
|
||||
<div><label className="label" htmlFor="u-v">Release</label><input id="u-v" className="input" placeholder="latest" value={upd.version} onChange={e => setUpd({ ...upd, version: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="u-at">When</label><input id="u-at" type="datetime-local" className="input" value={upd.at} min={toInput(new Date(Date.now() + minAt))} onChange={e => setUpd({ ...upd, at: e.target.value })} /><p className="mt-1 text-xs text-slate-500">At least {upd.emergency ? "15 minutes" : "4 hours"} away, so the church gets notice.</p></div>
|
||||
<label className="flex items-center gap-2 text-sm md:col-span-2"><input type="checkbox" checked={upd.emergency} onChange={e => setUpd({ ...upd, emergency: e.target.checked })} /> Emergency (security fix or broken release): 15 minutes' notice, a reason and your two-factor code</label>
|
||||
{upd.emergency && <>
|
||||
<div><label className="label" htmlFor="u-r">Reason (shown to the church)</label><input id="u-r" className="input" required value={upd.reason} onChange={e => setUpd({ ...upd, reason: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="u-t">Two-factor code</label><input id="u-t" className="input" inputMode="numeric" autoComplete="one-time-code" required value={upd.totp} onChange={e => setUpd({ ...upd, totp: e.target.value })} /></div>
|
||||
</>}
|
||||
<div className="md:col-span-2"><button className="btn-primary">Schedule</button></div>
|
||||
</form>
|
||||
)}
|
||||
|
||||
{x.status === "live" && x.install?.version?.startsWith("2.1.") && (
|
||||
<form className="card grid gap-3 md:grid-cols-3" onSubmit={e => { e.preventDefault(); act(`/api/admin/deployments/${x.id}/adopt`, { eventsDir: adopt.eventsDir, version: adopt.version || undefined, at: fromInput(adopt.at) }, "Adopt planned. The site admin was emailed."); }}>
|
||||
<h2 className="font-medium md:col-span-3">Move onto the monorepo (crosscode adopt)</h2>
|
||||
<div><label className="label" htmlFor="a-d">The old install's folder</label><input id="a-d" className="input font-mono" required placeholder="/var/www/hope-events" value={adopt.eventsDir} onChange={e => setAdopt({ ...adopt, eventsDir: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="a-v">Release</label><input id="a-v" className="input" placeholder="latest" value={adopt.version} onChange={e => setAdopt({ ...adopt, version: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="a-at">When</label><input id="a-at" type="datetime-local" className="input" value={adopt.at} onChange={e => setAdopt({ ...adopt, at: e.target.value })} /></div>
|
||||
<div className="md:col-span-3"><button className="btn-primary">Plan the move</button></div>
|
||||
</form>
|
||||
)}
|
||||
|
||||
<div className="card overflow-x-auto">
|
||||
<h2 className="font-medium">Jobs</h2>
|
||||
<table className="mt-3 w-full text-sm">
|
||||
<thead><tr className="text-left text-xs text-slate-500"><th className="py-1">Job</th><th>When</th><th>Status</th><th>Result</th><th /></tr></thead>
|
||||
<tbody>{x.jobs.map(j => (
|
||||
<tr key={j.id} className="border-t">
|
||||
<td className="py-1.5"><Link className="text-indigo-700 hover:underline" href={`/jobs/${j.id}`}>{j.type}</Link>{j.emergency && <span className="ml-1 text-xs text-red-700">emergency</span>}</td>
|
||||
<td>{fmtDate(j.notBefore)}</td><td><StatusPill status={j.status} /></td><td className="max-w-md truncate text-xs text-slate-500">{j.message}</td>
|
||||
<td>{["queued", "scheduled"].includes(j.status) && <button className="btn-secondary px-2 py-1 text-xs" onClick={() => act(`/api/admin/jobs/${j.id}/cancel`, {}, "Cancelled; the church was told.")}>Cancel</button>}</td>
|
||||
</tr>
|
||||
))}</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{!!x.exports.length && (
|
||||
<div className="card text-sm">
|
||||
<h2 className="font-medium">Exports</h2>
|
||||
{x.exports.map(e => <p key={e.id} className="mt-2">{e.id}: {bytes(e.bytes)}, {e.deletedAt ? `deleted ${fmtDate(e.deletedAt)}` : `until ${fmtDate(e.expiresAt)}`}{e.downloadedAt ? `, downloaded ${fmtDate(e.downloadedAt)}` : ", not downloaded yet"}</p>)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{x.status !== "removed" && x.status !== "uninstalling" && (
|
||||
<form className="card grid gap-3 border-red-200 md:grid-cols-3" onSubmit={e => { e.preventDefault(); if (window.confirm(`Uninstall ${x.shortName} on ${un.at.replace("T", " ")}? The church gets its data first.`)) act(`/api/admin/deployments/${x.id}/uninstall`, { at: fromInput(un.at), totp: un.totp, confirm: un.confirm }, "Uninstall planned: the site admin was emailed and sees a banner. The export runs first."); }}>
|
||||
<h2 className="font-medium text-red-800 md:col-span-3">Uninstall</h2>
|
||||
<p className="text-sm text-slate-500 md:col-span-3">At the time: the church's data is exported (encrypted; the link goes to the site admin), then the site, its tunnels and database are removed. The final backup stays on the server for 30 days.</p>
|
||||
<div><label className="label" htmlFor="x-at">When</label><input id="x-at" type="datetime-local" className="input" value={un.at} onChange={e => setUn({ ...un, at: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="x-c">Type {x.shortName}</label><input id="x-c" className="input" required value={un.confirm} onChange={e => setUn({ ...un, confirm: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="x-t">Two-factor code</label><input id="x-t" className="input" inputMode="numeric" autoComplete="one-time-code" required value={un.totp} onChange={e => setUn({ ...un, totp: e.target.value })} /></div>
|
||||
<div className="md:col-span-3"><button className="btn-danger" disabled={un.confirm !== x.shortName}>Plan the uninstall</button></div>
|
||||
</form>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
"use client";
|
||||
|
||||
import React, { useCallback, useEffect, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Link2, Plus } from "lucide-react";
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import { ago } from "@/lib/format";
|
||||
import { Loading, Message, PageTitle } from "@/components/ui";
|
||||
import { ServersNav, StatusPill, pct, type Deployment, type ServerSummary } from "@/components/servers";
|
||||
|
||||
type Msg = { type: "ok" | "err"; text: string } | null;
|
||||
type Customer = { id: string; name: string; domain?: string | null };
|
||||
type InstallRow = { id: string; name: string; url: string; customer: { id: string; name: string } | null; version: string | null };
|
||||
|
||||
const emptyNew = { customerId: "", serverId: "", shortName: "", domain: "", timezone: "", adminName: "", adminEmail: "", version: "" };
|
||||
const emptyLink = { installId: "", serverId: "", shortName: "", webPort: "", apiPort: "" };
|
||||
|
||||
/** Installs on servers: New install (§7) and linking existing installs (§14). */
|
||||
export default function InstallsPage() {
|
||||
const router = useRouter();
|
||||
const [list, setList] = useState<Deployment[] | null>(null);
|
||||
const [srv, setSrv] = useState<ServerSummary[]>([]);
|
||||
const [customers, setCustomers] = useState<Customer[]>([]);
|
||||
const [installs, setInstalls] = useState<InstallRow[]>([]);
|
||||
const [mode, setMode] = useState<"none" | "new" | "link">("none");
|
||||
const [form, setForm] = useState(emptyNew);
|
||||
const [link, setLink] = useState(emptyLink);
|
||||
const [msg, setMsg] = useState<Msg>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
const load = useCallback(() => {
|
||||
api<{ deployments: Deployment[] }>("/api/admin/deployments").then(r => setList(r.deployments)).catch(e => setMsg({ type: "err", text: errorText(e) }));
|
||||
api<{ servers: ServerSummary[] }>("/api/admin/servers").then(r => setSrv(r.servers.filter(s => s.mode === "church"))).catch(() => {});
|
||||
}, []);
|
||||
useEffect(() => { load(); }, [load]);
|
||||
useEffect(() => {
|
||||
if (mode === "new" && !customers.length) api<Customer[]>("/api/admin/customers").then(setCustomers).catch(() => {});
|
||||
if (mode === "link" && !installs.length) api<InstallRow[]>("/api/admin/installs").then(setInstalls).catch(() => {});
|
||||
}, [mode, customers.length, installs.length]);
|
||||
|
||||
const submitNew = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setBusy(true);
|
||||
setMsg(null);
|
||||
try {
|
||||
const r = await api<{ deployment: Deployment; warnings: string[] }>("/api/admin/deployments", { method: "POST", body: { ...form, product: "events", version: form.version || undefined } });
|
||||
router.push(`/servers/installs/${r.deployment.id}${r.warnings.length ? `?warn=${encodeURIComponent(r.warnings.join(" "))}` : ""}`);
|
||||
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
|
||||
setBusy(false);
|
||||
};
|
||||
const submitLink = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setBusy(true);
|
||||
setMsg(null);
|
||||
try {
|
||||
const r = await api<{ deployment: Deployment }>("/api/admin/deployments/link", { method: "POST", body: link });
|
||||
router.push(`/servers/installs/${r.deployment.id}`);
|
||||
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
|
||||
setBusy(false);
|
||||
};
|
||||
const f = (k: keyof typeof emptyNew) => ({ value: form[k], onChange: (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => setForm({ ...form, [k]: e.target.value }) });
|
||||
const l = (k: keyof typeof emptyLink) => ({ value: link[k], onChange: (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => setLink({ ...link, [k]: e.target.value }) });
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageTitle title="Installs on servers" subtitle="Sites the Hub installs, updates and uninstalls through each server's agent."
|
||||
actions={<div className="flex gap-2"><button className="btn-secondary" onClick={() => setMode("link")}><Link2 className="h-4 w-4" /> Link existing</button><button className="btn-primary" onClick={() => setMode("new")}><Plus className="h-4 w-4" /> New install</button></div>} />
|
||||
<ServersNav active="/servers/installs" />
|
||||
<Message msg={msg} />
|
||||
|
||||
{mode === "new" && (
|
||||
<form onSubmit={submitNew} className="card mb-6 grid gap-3 md:grid-cols-2">
|
||||
<h2 className="font-medium md:col-span-2">New Cross Events install</h2>
|
||||
<div><label className="label" htmlFor="n-cust">Customer</label><select id="n-cust" className="input" required {...f("customerId")}><option value="">Choose…</option>{customers.map(c => <option key={c.id} value={c.id}>{c.name}</option>)}</select></div>
|
||||
<div><label className="label" htmlFor="n-short">Short name</label><input id="n-short" className="input" required pattern="[a-z][a-z0-9-]{1,30}" placeholder="hope" {...f("shortName")} /><p className="mt-1 text-xs text-slate-500">Folders, database and tunnels: a-z, 0-9 and -.</p></div>
|
||||
<div><label className="label" htmlFor="n-domain">Domain</label><input id="n-domain" className="input" required placeholder="events.hope.co.za" {...f("domain")} /></div>
|
||||
<div><label className="label" htmlFor="n-tz">Time zone</label><input id="n-tz" className="input" placeholder="Africa/Johannesburg" {...f("timezone")} /></div>
|
||||
<div><label className="label" htmlFor="n-an">Site admin name</label><input id="n-an" className="input" {...f("adminName")} /></div>
|
||||
<div><label className="label" htmlFor="n-ae">Site admin email</label><input id="n-ae" type="email" className="input" {...f("adminEmail")} /><p className="mt-1 text-xs text-slate-500">Gets the welcome email and update notices.</p></div>
|
||||
<div className="md:col-span-2">
|
||||
<span className="label">Server</span>
|
||||
<div className="grid gap-2 md:grid-cols-2">
|
||||
{srv.map(s => {
|
||||
const low = (s.ram?.availableBytes ?? 0) < 1.5 * 1024 ** 3;
|
||||
return (
|
||||
<label key={s.id} className={`flex cursor-pointer gap-2 rounded-lg border p-3 text-sm ${form.serverId === s.id ? "border-indigo-500 bg-indigo-50" : "border-slate-200"}`}>
|
||||
<input type="radio" name="server" value={s.id} checked={form.serverId === s.id} onChange={() => setForm({ ...form, serverId: s.id })} disabled={!s.online} />
|
||||
<span>
|
||||
<span className="font-medium">{s.name}</span> <StatusPill status={s.online ? "online" : "offline"} />
|
||||
<span className="block text-xs text-slate-500">RAM free {s.ram ? `${(s.ram.availableBytes / 1024 ** 3).toFixed(1)} GB` : "?"} · CPU day avg {pct(s.lastDay?.cpuAvg)} · disk {pct(s.disk.reduce((m, d) => Math.max(m, d.totalBytes ? (d.usedBytes / d.totalBytes) * 100 : 0), 0))} · {s.deployments ?? 0} site(s)</span>
|
||||
{low && <span className="block text-xs font-medium text-amber-800">Not recommended: under 1.5 GB RAM free</span>}
|
||||
</span>
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div><label className="label" htmlFor="n-ver">Release</label><input id="n-ver" className="input" placeholder="latest" {...f("version")} /></div>
|
||||
<div className="flex items-end gap-2 md:col-span-2">
|
||||
<button className="btn-primary" disabled={busy || !form.serverId}>{busy ? "Starting…" : "Install"}</button>
|
||||
<button type="button" className="btn-secondary" onClick={() => setMode("none")}>Cancel</button>
|
||||
<p className="text-xs text-slate-500">The Hub makes the licence, picks two ports, and the agent installs within a minute. You get the report and the FastPanel steps by email.</p>
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
|
||||
{mode === "link" && (
|
||||
<form onSubmit={submitLink} className="card mb-6 grid gap-3 md:grid-cols-2">
|
||||
<h2 className="font-medium md:col-span-2">Link an install that's already on a server</h2>
|
||||
<p className="text-sm text-slate-500 md:col-span-2">For sites installed by hand or moved with <code>crosscode adopt</code>. Nothing on the server changes; the Hub records the server, folder name and ports (see the server's church.env and frpc.ini).</p>
|
||||
<div><label className="label" htmlFor="l-inst">Install</label><select id="l-inst" className="input" required {...l("installId")}><option value="">Choose…</option>{installs.map(i => <option key={i.id} value={i.id}>{i.customer?.name || i.name} ({i.url})</option>)}</select></div>
|
||||
<div><label className="label" htmlFor="l-srv">Server</label><select id="l-srv" className="input" required {...l("serverId")}><option value="">Choose…</option>{srv.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}</select></div>
|
||||
<div><label className="label" htmlFor="l-short">Short name (its folder in /var/www/sites)</label><input id="l-short" className="input" required {...l("shortName")} /></div>
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<div><label className="label" htmlFor="l-web">Web port</label><input id="l-web" className="input" required inputMode="numeric" {...l("webPort")} /></div>
|
||||
<div><label className="label" htmlFor="l-api">API port</label><input id="l-api" className="input" required inputMode="numeric" {...l("apiPort")} /></div>
|
||||
</div>
|
||||
<div className="flex gap-2 md:col-span-2"><button className="btn-primary" disabled={busy}>Link</button><button type="button" className="btn-secondary" onClick={() => setMode("none")}>Cancel</button></div>
|
||||
</form>
|
||||
)}
|
||||
|
||||
{!list ? <Loading /> : (
|
||||
<div className="card overflow-x-auto">
|
||||
<table className="w-full text-sm">
|
||||
<thead><tr className="text-left text-xs text-slate-500"><th className="py-1">Church</th><th>Domain</th><th>Server</th><th>Version</th><th>Last heartbeat</th><th>Status</th></tr></thead>
|
||||
<tbody>{list.map(d => (
|
||||
<tr key={d.id} className="border-t"><td className="py-1.5"><Link className="text-indigo-700 hover:underline" href={`/servers/installs/${d.id}`}>{d.customer?.name} ({d.shortName})</Link></td><td>{d.domain}</td><td>{d.server?.name}</td><td>{d.install?.version || d.version || "–"}</td><td>{ago(d.install?.lastHeartbeatAt)}</td><td><StatusPill status={d.status} /></td></tr>
|
||||
))}</tbody>
|
||||
</table>
|
||||
{!list.length && <p className="mt-2 text-sm text-slate-500">No installs on servers yet.</p>}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user