"use client"; import React, { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { useParams, useRouter, useSearchParams } from "next/navigation"; import { api, ApiError, errorText } from "@/lib/api"; import { ago, bytes, fmtDate } from "@/lib/format"; import { useServerPaged } from "@/lib/paging"; import { PRODUCT_NAMES } from "@/lib/products"; import type { Release } from "@/lib/types"; import { Loading, Message, PageTitle, Pager } 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; lastHeartbeatAt: string | null } | null }; fastPanel: string; canRemoveRecord: boolean; }; 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 router = useRouter(); const warn = useSearchParams().get("warn"); const [d, setD] = useState(null); const [msg, setMsg] = useState(warn ? { type: "err", text: warn } : null); const [problems, setProblems] = useState([]); const [upd, setUpd] = useState({ version: "", at: toInput(new Date(Date.now() + 5 * H)), emergency: false, asap: true, reason: "", totp: "" }); const [adopt, setAdopt] = useState({ eventsDir: "", version: "", address: "", at: toInput(new Date(Date.now() + 5 * H)) }); const [un, setUn] = useState({ at: toInput(new Date(Date.now() + 7 * 24 * H)), totp: "", confirm: "" }); const [demo, setDemo] = useState({ profile: "events", confirm: "", markConfirm: "" }); const [releases, setReleases] = useState([]); // Hub 2.6.0: every job of this install, a page at a time (it showed the last 50). const jobsPaged = useServerPaged("/api/admin/jobs", { deploymentId: String(id) }, r => r.jobs as Job[], 20); const load = useCallback(() => { jobsPaged.reload(); return api(`/api/admin/deployments/${id}`).then(setD).catch(e => setMsg({ type: "err", text: errorText(e) })); // eslint-disable-next-line react-hooks/exhaustive-deps -- reload is a fresh function each render }, [id]); useEffect(() => { load(); }, [load]); useEffect(() => { api("/api/admin/releases").then(setReleases).catch(() => {}); }, []); const act = async (path: string, body: Record, 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) { // Hub 2.7.0: an event is on there now: ask once more, then go ahead. if (e instanceof ApiError && e.code === "LIVE_EVENT" && !body.confirmLive && window.confirm(e.message)) return act(path, { ...body, confirmLive: true }, ok); 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) }); } }; const removeRecord = async () => { if (!d || !window.confirm(`Remove the record of ${d.deployment.shortName}? Its install failed, so nothing of it is on the server.`)) return; setMsg(null); try { await api(`/api/admin/deployments/${id}`, { method: "DELETE" }); router.push("/servers/installs"); } catch (e) { setMsg({ type: "err", text: errorText(e) }); } }; if (!d) return
{msg ? : }
; const x = d.deployment; const minAt = upd.emergency ? 15 * 60 * 1000 : 4 * H; return (
{x.domain} · on {x.server.name} · ports web {x.webPort}, api {x.apiPort} · version {x.install?.version || x.version || "–"} } /> {/* Hub 2.6.0: whether the site has registered with the Hub (its banners, licence and heartbeats need it). */}

{x.install ? <>Registered site: {x.install.url}, last heartbeat {x.install.lastHeartbeatAt ? ago(x.install.lastHeartbeatAt) : "never"}. : The site hasn't registered with the Hub yet. It registers by itself with its licence key when it starts, and is tied to this record at its first heartbeat; if that doesn't happen, check its HUB_URL and HUB_LICENCE_KEY.}

{!!problems.length &&
    {problems.map(p =>
  • {p}
  • )}
}
{(x.status === "waiting_for_proxy" || x.status === "installing") && (

Set up the site in FastPanel

{d.fastPanel}
{x.status === "installing" &&

Waiting for the install to finish (see its job below).

}
)} {x.status === "live" && (
{ e.preventDefault(); const asap = upd.emergency && upd.asap; act("/api/admin/updates", { deploymentIds: [x.id], product: x.product, version: upd.version || undefined, at: asap ? undefined : fromInput(upd.at), asap, emergency: upd.emergency, reason: upd.reason || null, totp: upd.totp || null }, asap ? "Started: the agent picks it up within a minute. The site admin was emailed and sees a banner." : "Planned. The site admin was emailed and sees a banner."); }}>

Schedule an update

{upd.emergency && upd.asap ?
When

As soon as possible: the agent starts within a minute.

:
setUpd({ ...upd, at: e.target.value })} />

At least {upd.emergency ? "15 minutes" : "4 hours"} away, so the church gets notice.

} {upd.emergency && <>
When the emergency update runs
setUpd({ ...upd, reason: e.target.value })} />
setUpd({ ...upd, totp: e.target.value })} />
}
)} {x.status === "live" && x.install?.version?.startsWith("2.1.") && (
{ e.preventDefault(); act(`/api/admin/deployments/${x.id}/adopt`, { eventsDir: adopt.eventsDir, version: adopt.version || undefined, address: adopt.address || undefined, at: fromInput(adopt.at) }, "Adopt planned. The site admin was emailed."); }}>

Move onto the monorepo (crosscode adopt)

Moves the site from its old folder to /var/www/sites/{x.shortName}/events. Same database, uploads, .env and ports; the old folder is kept 14 days for crosscode adopt {x.shortName} --undo.

setAdopt({ ...adopt, eventsDir: e.target.value })} />
setAdopt({ ...adopt, address: e.target.value })} />
setAdopt({ ...adopt, version: e.target.value })} />
setAdopt({ ...adopt, at: e.target.value })} />
)} {x.product === "events" && ["live", "waiting_for_proxy"].includes(x.status) && (

Demo site

{!x.isDemo ? (
{ e.preventDefault(); act(`/api/admin/deployments/${x.id}/demo`, { isDemo: true, confirm: demo.markConfirm }, "Marked as a demo site."); }}>

A demo site's data can be replaced with demo data from here. Only mark sites used for showing Cross Events, never a church's real site.

setDemo({ ...demo, markConfirm: e.target.value })} />
) : ( <>
{ e.preventDefault(); if (window.confirm(`Replace ALL of ${x.shortName}'s data with the "${demo.profile}" demo? A backup is taken first; one admin account is kept.`)) act(`/api/admin/deployments/${x.id}/seed-demo`, { profile: demo.profile, confirm: demo.confirm }, "Queued: the agent loads the demo data on its next poll (see Jobs below). You get an email when it's done."); }}>

Replaces everything (events, people, payments) with a demo set, keeping the site settings and {x.adminEmail || "the site's first admin"}. Demo logins use the password Demo@1234.

setDemo({ ...demo, confirm: e.target.value })} />
)}
)}

Jobs

{(jobsPaged.rows ?? x.jobs).map(j => ( ))}
JobWhenStatusResult
{j.type}{j.emergency && emergency} {fmtDate(j.notBefore)}{j.message} {["queued", "scheduled"].includes(j.status) && }
{!!x.exports.length && (

Exports

{x.exports.map(e =>

{e.id}: {bytes(e.bytes)}, {e.deletedAt ? `deleted ${fmtDate(e.deletedAt)}` : `until ${fmtDate(e.expiresAt)}`}{e.downloadedAt ? `, downloaded ${fmtDate(e.downloadedAt)}` : ", not downloaded yet"}

)}
)} {d.canRemoveRecord && (

Remove the record

This install failed and the server already removed what it made, so there's nothing to export or uninstall. Removing the record frees the short name.

)} {["live", "waiting_for_proxy"].includes(x.status) && (
{ 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."); }}>

Uninstall

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.

setUn({ ...un, at: e.target.value })} />
setUn({ ...un, confirm: e.target.value })} />
setUn({ ...un, totp: e.target.value })} />
)}
); }