Let an administrator set a server's hostname by hand

A Change link on the Identity card sets the hostname (a host name or an
address, written lower case without the trailing dot). It is then kept even
when the agent reports another — marked "set by hand", with what the agent
reports shown beside it — until "Use what the agent reports" gives it up. The
agent's own hostname is always remembered (reported_hostname). PUT and DELETE
/api/servers/:id/hostname, administrators only, audited with from and to.
Migration 0082 adds hostname_pinned and reported_hostname and fills the latter
for servers that have reported. It changes this app's name for the server, not
the machine's.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-01 23:30:31 +01:00
1 parent 6f144b4e36
commit 672c7f2f66
14 files changed
+9020 -7

No files matched your search

+35
View File
@@ -26,6 +26,7 @@ import { activeSubjects } from "../services/maintenance.js";
import { invalidateAlertsCache } from "../services/alerts.js";
import { getSettings, updateSettings } from "../services/settingsStore.js";
import { osSummary } from "../services/osLifecycle.js";
import { normaliseHostname } from "../utils/hostnames.js";
export const serversRouter = Router();
@@ -154,6 +155,40 @@ const updateServerSchema = z
{ message: "proxmoxIntegrationId/proxmoxNode/proxmoxGuestType/proxmoxVmid must be set together or all cleared to null" },
);
const hostnameSchema = z.object({ hostname: z.string().max(300) });
/**
* Sets a server's hostname by hand. It is kept even when the agent reports another (that is still remembered, and shown), until it is given up with DELETE. A host name or an address, in the form
* names are compared (see utils/hostnames.ts); anything else is refused. An administrator's.
*/
serversRouter.put("/:id/hostname", requireRole("admin"), asyncHandler(async (req, res) => {
const id = /^\d+$/.test(req.params.id) ? Number(req.params.id) : Number.NaN;
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const parsed = hostnameSchema.safeParse(req.body);
const hostname = parsed.success ? normaliseHostname(parsed.data.hostname) : null;
if (hostname === null) return res.status(400).json({ error: "invalid_hostname", message: "That is not a host name: use letters, digits, - and _ (a name such as nas.home.lan), or an address." });
const [existing] = await db.select().from(servers).where(eq(servers.id, id)).limit(1);
if (!existing) return res.status(404).json({ error: "not_found" });
const [updated] = await db.update(servers).set({ hostname, hostnamePinned: true }).where(eq(servers.id, id)).returning();
if (existing.hostname !== hostname || !existing.hostnamePinned) {
await recordAudit({ actor: req.currentUser!, category: "server", action: "set_hostname", targetType: "server", targetId: id, detail: { name: existing.name, from: existing.hostname, to: hostname } });
}
res.json({ server: publicServer(updated) });
}));
/** Gives up a hostname that was set by hand: the server's hostname is what its agent reports again — at once, if it has reported one, otherwise as it is until it does. */
serversRouter.delete("/:id/hostname", requireRole("admin"), asyncHandler(async (req, res) => {
const id = /^\d+$/.test(req.params.id) ? Number(req.params.id) : Number.NaN;
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const [existing] = await db.select().from(servers).where(eq(servers.id, id)).limit(1);
if (!existing) return res.status(404).json({ error: "not_found" });
if (!existing.hostnamePinned) return res.json({ server: publicServer(existing) });
const hostname = existing.reportedHostname ?? existing.hostname;
const [updated] = await db.update(servers).set({ hostname, hostnamePinned: false }).where(eq(servers.id, id)).returning();
await recordAudit({ actor: req.currentUser!, category: "server", action: "reset_hostname", targetType: "server", targetId: id, detail: { name: existing.name, from: existing.hostname, to: hostname } });
res.json({ server: publicServer(updated) });
}));
serversRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });