Files
Homelab-manager/web/src/pages/Updates.tsx
T
bobbanandClaude Sonnet 5.5 540ab704b7 Export CSV on every table, edit recurring maintenance, runs-on nothing, and run package checks outside the agent sandbox
- Export CSV for the small tables that had none (reads the table as shown)
- Servers page shows a skeleton while loading instead of "No servers registered yet"
- Recurring maintenance schedules can be edited (PUT /schedules/:id)
- A server can be set to run on nothing (servers.no_parent, migration 0028)
- Linux agent v3: package-manager question runs in ExecStartPre=-+ outside the sandbox,
  with --prepare-updates and --check-updates modes
- Docs: README, DATABASE, AGENT_UPDATE

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-17 22:25:18 +02:00

268 lines
12 KiB
TypeScript

import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { api, type AgentLatest, type DockhandContainer, type UpdatesServer } from "../api/client";
import { agentState, agentVersionWords } from "../utils/agent";
import SortableTh from "../components/SortableTh";
import TableToolbar from "../components/TableToolbar";
import TimeAgo from "../components/TimeAgo";
import UpdatesBadge from "../components/UpdatesBadge";
import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { useSortable } from "../hooks/useSortable";
import { useTableSearch } from "../hooks/useTableSearch";
import { downloadCsv } from "../utils/csv";
import { readableError } from "../utils/errors";
import ExportTableButton from "../components/ExportTableButton";
interface Row {
id: number;
name: string;
os: string;
manager: string;
/** Null: the agent hasn't said. */
count: number | null;
security: number | null;
restart: boolean;
/** ISO — when updates were first seen waiting. */
since: string | null;
reportedAt: string | null;
agentVersion: number | null;
server: UpdatesServer;
}
function toRow(s: UpdatesServer): Row {
return {
id: s.id,
name: s.name,
os: s.osType,
manager: s.updates?.manager ?? "",
count: s.updates ? s.updates.count : null,
security: s.updates ? s.updates.security : null,
restart: s.updates?.rebootRequired ?? false,
since: s.pendingSince,
reportedAt: s.updates?.reportedAt || null,
agentVersion: s.agentVersion,
server: s,
};
}
const DAY_MS = 86_400_000;
/**
* What is waiting to be installed, in one place: the updates each server's agent says are pending (how many, how many are security fixes,
* whether a restart is due, and how long they have been waiting) and the Docker containers that have a newer image. It only looks —
* nothing here installs anything.
*/
export default function Updates() {
const [servers, setServers] = useState<UpdatesServer[] | null>(null);
const [agentLatest, setAgentLatest] = useState<AgentLatest | null>(null);
const [containers, setContainers] = useState<(DockhandContainer & { integrationName: string })[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [onlyWaiting, setOnlyWaiting] = useState(false);
useEffect(() => {
let alive = true;
api.servers
.updates()
.then((res) => {
if (!alive) return;
setServers(res.servers);
setAgentLatest(res.agentLatest);
})
.catch((err) => alive && setError(readableError(err)));
api.integrations
.list()
.then(async (res) => {
const docker = res.integrations.filter((i) => i.type === "dockhand" && i.enabled);
const all = await Promise.all(
docker.map((i) =>
api.integrations.dockhand
.containers(i.id)
.then((r) => r.containers.map((c) => ({ ...c, integrationName: i.name })))
.catch(() => []),
),
);
if (alive) setContainers(docker.length === 0 ? null : all.flat().filter((c) => c.updateAvailable === true));
})
.catch(() => {});
return () => {
alive = false;
};
}, []);
const rows = (servers ?? []).map(toRow);
const waiting = rows.filter((r) => (r.count ?? 0) > 0);
const tableSearch = useTableSearch(onlyWaiting ? waiting : rows);
const { sorted, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, "security", "desc", { url: true });
const reporting = rows.filter((r) => r.count !== null).length;
const securityTotal = rows.reduce((n, r) => n + (r.security ?? 0), 0);
const restarts = rows.filter((r) => r.restart).length;
const oldest = waiting.map((r) => r.since).filter((s): s is string => !!s).sort()[0];
const notReporting = rows.length - reporting;
const behind = rows.filter((r) => agentState(r.server, agentLatest) === "behind").length;
function exportCsv() {
if (!sorted) return;
downloadCsv(
"updates.csv",
["Server", "Package manager", "Waiting", "Security", "Restart needed", "Waiting since", "Reported"],
sorted.map((r) => [r.name, r.manager, r.count ?? "", r.security ?? "", r.restart ? "yes" : "no", r.since ?? "", r.reportedAt ?? ""]),
);
}
const cards: { label: string; value: string | number; note?: string; color: string }[] = [
{ label: "Servers with updates waiting", value: servers ? `${waiting.length} of ${reporting}` : "…", note: notReporting > 0 ? `${notReporting} haven't reported updates` : undefined, color: waiting.length > 0 ? "text-yellow" : "text-green" },
{ label: "Security updates waiting", value: servers ? securityTotal : "…", color: securityTotal > 0 ? "text-red" : "text-green" },
{ label: "Need a restart", value: servers ? restarts : "…", color: restarts > 0 ? "text-orange" : "text-green" },
{ label: "Agents to update", value: servers ? behind : "…", note: behind > 0 ? "run their install command again" : undefined, color: behind > 0 ? "text-yellow" : "text-green" },
{ label: "Longest wait", value: oldest ? `${Math.max(1, Math.floor((Date.now() - Date.parse(oldest)) / DAY_MS))} d` : servers ? "—" : "…", color: "text-secondary" },
];
return (
<>
<div className="d-flex align-items-center mb-3">
<h2 className="page-title mb-0">Updates</h2>
</div>
<p className="text-secondary">
What the agents say is waiting to be installed on each server, and the Docker containers that have a newer image. This page only looks — it installs nothing. To apply updates, run a playbook from the server's page.
</p>
<ErrorNotice error={error} />
<div className="row row-cards mb-3">
{cards.map((c) => (
<div className="col-6 col-lg" key={c.label}>
<div className="card card-sm">
<div className="card-body">
<div className={`h2 mb-0 ${c.color}`}>{c.value}</div>
<div className="text-secondary">{c.label}</div>
{c.note && <div className="text-secondary small">{c.note}</div>}
</div>
</div>
</div>
))}
</div>
<div className="card mb-3">
<TableToolbar
search={tableSearch.query}
onSearch={tableSearch.setQuery}
placeholder="Search servers…"
filters={
<label className="form-check mb-0">
<input type="checkbox" className="form-check-input" checked={onlyWaiting} onChange={(e) => setOnlyWaiting(e.target.checked)} />
<span className="form-check-label">Only those with updates</span>
</label>
}
shown={sorted?.length}
total={onlyWaiting ? waiting.length : rows.length}
onExport={exportCsv}
exportDisabled={!sorted || sorted.length === 0}
/>
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
<tr>
<SortableTh<Row> label="Server" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<Row> label="Updates" sortKeyName="count" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<Row> label="Security" sortKeyName="security" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<Row> label="Waiting since" sortKeyName="since" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<Row> label="Reported" sortKeyName="reportedAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<Row> label="Agent" sortKeyName="agentVersion" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
</tr>
</thead>
<tbody>
{servers === null && !error && (
<tr>
<td colSpan={6}>
<BlockSkeleton lines={3} />
</td>
</tr>
)}
{sorted?.map((r) => (
<tr key={r.id}>
<td>
<Link to={`/servers/${r.id}`}>{r.name}</Link> <span className="text-secondary small">{r.manager}</span>
</td>
<td>
<UpdatesBadge updates={r.server.updates} />
</td>
<td>{r.security === null ? <span className="text-secondary">—</span> : r.security}</td>
<td className="text-secondary">{r.since ? <TimeAgo value={r.since} /> : "—"}</td>
<td className="text-secondary">{r.reportedAt ? <TimeAgo value={r.reportedAt} /> : "—"}</td>
<td>
{agentVersionWords(r.agentVersion)}{" "}
{agentState(r.server, agentLatest) === "behind" && (
<span className="badge bg-yellow-lt text-yellow" title="Older than the agent this Homelab Manager would install now.">
behind
</span>
)}
</td>
</tr>
))}
{servers !== null && sorted?.length === 0 && (
<tr>
<td colSpan={6} className="text-secondary text-center">
{tableSearch.active || onlyWaiting ? "Nothing matches that." : "No server has reported yet."}
</td>
</tr>
)}
</tbody>
</table>
</div>
{notReporting > 0 && (
<div className="card-footer text-secondary small">
“Not reported” means that server's agent hasn't said — it may be an older agent (run its install command again, from Servers → Manage servers, to update it), or its package manager isn't apt, dnf, zypper or Windows Update.
</div>
)}
</div>
{containers !== null && (
<div className="card">
<div className="card-header">
<h3 className="card-title">Docker containers with a newer image</h3>
<div className="card-actions">
<ExportTableButton filename="docker-newer-images" />
</div>
</div>
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
<tr>
<th>Container</th>
<th>Image</th>
<th>Newer version</th>
<th>Where</th>
<th>Checked</th>
</tr>
</thead>
<tbody>
{containers.map((c) => (
<tr key={`${c.integrationName}:${c.environmentId}:${c.id}`}>
<td>
<Link to="/docker">{c.name}</Link>
</td>
<td className="text-secondary">{c.image}</td>
<td>{c.newerVersion ?? "—"}</td>
<td className="text-secondary">{c.environmentName}</td>
<td className="text-secondary">
<TimeAgo value={c.checkedAt} />
</td>
</tr>
))}
{containers.length === 0 && (
<tr>
<td colSpan={5} className="text-secondary text-center">
Every container is on its latest image, as far as the last check could tell.
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
)}
</>
);
}