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:
1 parent
6f144b4e36
commit
672c7f2f66
14 files changed
+9020
-7
No files matched your search
@@ -750,6 +750,10 @@ export interface ServerRecord {
|
||||
id: number;
|
||||
name: string;
|
||||
hostname: string | null;
|
||||
/** Set by hand: a report from the agent does not replace it. */
|
||||
hostnamePinned: boolean;
|
||||
/** What the agent last said its hostname is; null if it never said. */
|
||||
reportedHostname: string | null;
|
||||
osType: string;
|
||||
description: string | null;
|
||||
apiTokenPrefix: string;
|
||||
@@ -4011,6 +4015,8 @@ export const api = {
|
||||
request<{ server: ServerRecord }>(`/api/servers/${id}`, { method: "PATCH", body: JSON.stringify(data) }),
|
||||
summary: () => request<ServerSummary>("/api/servers/summary"),
|
||||
updates: () => request<{ servers: UpdatesServer[]; agentLatest: AgentLatest }>("/api/servers/updates"),
|
||||
setHostname: (id: number, hostname: string) => request<{ server: ServerRecord }>(`/api/servers/${id}/hostname`, { method: "PUT", body: JSON.stringify({ hostname }) }),
|
||||
resetHostname: (id: number) => request<{ server: ServerRecord }>(`/api/servers/${id}/hostname`, { method: "DELETE" }),
|
||||
detail: (id: number) => request<ServerDetail>(`/api/servers/${id}/detail`),
|
||||
activity: (id: number) => request<{ events: ServerActivityEvent[] }>(`/api/servers/${id}/activity`),
|
||||
metrics: (id: number, days: number) => request<{ days: number; series: ServerMetricSeries[] }>(`/api/servers/${id}/metrics?days=${days}`),
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { useState, type FormEvent } from "react";
|
||||
import { api, type ServerRecord } from "../api/client";
|
||||
import { ErrorNotice } from "./Notice";
|
||||
import { readableError } from "../utils/errors";
|
||||
|
||||
/**
|
||||
* A server's hostname, with (for an administrator) a way to change it by hand. A hostname set by hand is kept even when the agent reports another — the page says so, shows what the agent
|
||||
* reports, and can go back to it. It is the name this app uses to match the server to a DNS name or a monitor and to reach it; it does not rename the machine itself.
|
||||
*/
|
||||
export default function ServerHostname({ server, isAdmin, onChanged }: { server: Pick<ServerRecord, "id" | "hostname" | "hostnamePinned" | "reportedHostname">; isAdmin: boolean; onChanged: () => void | Promise<void> }) {
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [value, setValue] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
function start() {
|
||||
setValue(server.hostname ?? "");
|
||||
setError(null);
|
||||
setEditing(true);
|
||||
}
|
||||
|
||||
async function save(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
if (busy) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
await api.servers.setHostname(server.id, value);
|
||||
setEditing(false);
|
||||
await onChanged();
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function giveUp() {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
await api.servers.resetHostname(server.id);
|
||||
await onChanged();
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
const differs = server.hostnamePinned && server.reportedHostname && server.reportedHostname !== server.hostname;
|
||||
|
||||
return (
|
||||
<>
|
||||
<dt className="col-5">Hostname</dt>
|
||||
<dd className="col-7">
|
||||
{editing ? (
|
||||
<form onSubmit={save}>
|
||||
<ErrorNotice error={error} />
|
||||
<input className="form-control form-control-sm mb-1" autoFocus required maxLength={300} autoComplete="off" aria-label="Hostname" placeholder="nas.home.lan" value={value} onChange={(e) => setValue(e.target.value)} />
|
||||
<div className="text-secondary small mb-1">A host name or an address. It is kept even if the agent reports another.</div>
|
||||
<div className="d-flex gap-2">
|
||||
<button type="submit" className="btn btn-sm btn-primary" disabled={busy || value.trim() === ""}>
|
||||
{busy ? "Saving…" : "Save"}
|
||||
</button>
|
||||
<button type="button" className="btn btn-sm btn-link text-secondary" onClick={() => setEditing(false)} disabled={busy}>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
) : (
|
||||
<>
|
||||
<span>{server.hostname ?? "—"}</span>
|
||||
{server.hostnamePinned && (
|
||||
<span className="badge bg-blue-lt text-blue ms-2" title="Set by hand: a report from the agent does not replace it.">
|
||||
set by hand
|
||||
</span>
|
||||
)}
|
||||
{isAdmin && (
|
||||
<button type="button" className="btn btn-sm btn-link py-0" onClick={start}>
|
||||
Change
|
||||
</button>
|
||||
)}
|
||||
{differs && (
|
||||
<div className="small text-secondary">
|
||||
The agent reports <strong>{server.reportedHostname}</strong>.
|
||||
</div>
|
||||
)}
|
||||
{isAdmin && server.hostnamePinned && (
|
||||
<div>
|
||||
<button type="button" className="btn btn-sm btn-link px-0 py-0" onClick={() => void giveUp()} disabled={busy}>
|
||||
{server.reportedHostname ? "Use what the agent reports" : "Stop keeping it by hand"}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{!editing && error && <ErrorNotice error={error} />}
|
||||
</>
|
||||
)}
|
||||
</dd>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
type TaskRecord,
|
||||
} from "../api/client";
|
||||
import ServerPorts from "../components/ServerPorts";
|
||||
import ServerHostname from "../components/ServerHostname";
|
||||
import ServerTrends from "../components/ServerTrends";
|
||||
import UptimeHistory from "../components/UptimeHistory";
|
||||
import NotesCard from "../components/NotesCard";
|
||||
@@ -704,8 +705,7 @@ export default function ServerDetail({ user }: { user: CurrentUser }) {
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<dl className="row mb-0">
|
||||
<dt className="col-5">Hostname</dt>
|
||||
<dd className="col-7">{server.hostname ?? "—"}</dd>
|
||||
<ServerHostname server={server} isAdmin={isAdmin} onChanged={loadDetail} />
|
||||
<dt className="col-5">OS type</dt>
|
||||
<dd className="col-7">{server.osType}</dd>
|
||||
<dt className="col-5">Token</dt>
|
||||
|
||||
Reference in new issue
Block a user