@astrofoundry/pi-astro 0.40.34 → 0.40.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -62,15 +62,20 @@ describe("morning status lines", () => {
62
62
  expect(edgeStatus(null, null)).toMatchObject({ line: "the check failed (vps status)", problem: "Edge: the status check failed" });
63
63
  });
64
64
 
65
- it("reports both Proxmox hosts and their waiting host packages without flagging them", () => {
66
- const held = `${PULSAR_UPDATES}\nwazuh-agent/stable 4.14.9-1 amd64 [upgradable from: 4.14.7-1]`;
67
- expect(proxmoxStatus([{ name: "Pulsar", status: PULSAR, updates: held }])).toEqual({ area: "Proxmox", line: "Pulsar up 59 days, RAM 47Gi of 60Gi used, root disk 13%, 2 host package updates waiting (1 security), applied by hand" });
68
- const both = proxmoxStatus([
69
- { name: "Pulsar", status: PULSAR, updates: PULSAR_UPDATES },
70
- { name: "SPACE-X", status: null, updates: null },
65
+ it("reports both Proxmox hosts, the morning upgrade, a held major, and a pending reboot with its button", () => {
66
+ const held = "Listing...\nwazuh-agent/stable 4.14.9-1 amd64 [upgradable from: 4.14.7-1]";
67
+ expect(proxmoxStatus([{ name: "Pulsar", id: "pulsar", status: `${PULSAR}\nreboot-needed: no`, updates: held }])).toEqual({ area: "Proxmox", line: "Pulsar up 59 days, RAM 47Gi of 60Gi used, root disk 13%, up to date" });
68
+ const upgraded = proxmoxStatus([
69
+ { name: "Pulsar", id: "pulsar", status: `${PULSAR}\nreboot-needed: kernel 7.0.14-25-pve -> 7.0.14-26-pve`, updates: held, upgrade: "3 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.\nreboot-needed: kernel 7.0.14-25-pve -> 7.0.14-26-pve" },
70
+ { name: "SPACE-X", id: "space-x", status: null, updates: null },
71
71
  ]);
72
- expect(both.line).toBe("Pulsar up 59 days, RAM 47Gi of 60Gi used, root disk 13%, 2 host package updates waiting (1 security), applied by hand; SPACE-X status unknown");
73
- expect(both).toMatchObject({ problem: "Proxmox: the status check failed for SPACE-X", route: "admin" });
72
+ expect(upgraded.line).toBe("Pulsar up 59 days, RAM 47Gi of 60Gi used, root disk 13%, 3 host updates installed, reboot needed; SPACE-X status unknown");
73
+ expect(upgraded.problem).toBe("Proxmox: Pulsar needs a reboot (kernel 7.0.14-25-pve -> 7.0.14-26-pve); every guest on it restarts for a few minutes; the status check failed for SPACE-X");
74
+ expect(upgraded.buttons).toEqual([{ label: "Reboot Pulsar", customId: "proxmox:reboot:pulsar" }]);
75
+ const major = proxmoxStatus([{ name: "SPACE-X", id: "space-x", status: `${PULSAR}\nreboot-needed: no`, updates: PULSAR_UPDATES, upgrade: "held: Proxmox VE 9.2.24 -> 10.0.1 is a major upgrade" }]);
76
+ expect(major).toMatchObject({ problem: "Proxmox: SPACE-X waits for you: Proxmox VE 9.2.24 -> 10.0.1 is a major upgrade", route: "admin" });
77
+ expect(major.line).toContain("a Proxmox VE major upgrade waits for you");
78
+ expect(proxmoxStatus([{ name: "Pulsar", id: "pulsar", status: PULSAR, updates: PULSAR_UPDATES, upgrade: null }]).problem).toBe("Proxmox: the host upgrade on Pulsar failed");
74
79
  });
75
80
 
76
81
  it("flags updates the nightly run left behind, reading guest-agent JSON too, and ignores the held Wazuh agent", () => {
@@ -6,12 +6,19 @@ import type { ToolsReport } from "./tools-report.ts";
6
6
  /** Where an area's problems tag the owners: specialists without a channel of their own use the admin channel. */
7
7
  export type Route = "admin" | "media";
8
8
 
9
+ /** An owner-only button posted with an area's problem. */
10
+ export interface AreaButton {
11
+ label: string;
12
+ customId: string;
13
+ }
14
+
9
15
  /** One area of the morning summary: a line, and a problem that needs an owner, if any. */
10
16
  export interface AreaStatus {
11
17
  area: string;
12
18
  line: string;
13
19
  problem?: string;
14
20
  route?: Route;
21
+ buttons?: AreaButton[];
15
22
  }
16
23
 
17
24
  const HOUR = 3600;
@@ -50,35 +57,65 @@ export function edgeStatus(status: string | null, updates: string | null): AreaS
50
57
  return { area: "Edge", line: `front door: ${parts.join(", ")}`, ...(problem ? { problem, route: "admin" as const } : {}) };
51
58
  }
52
59
 
53
- /** One Proxmox host's `host-status` and `updates` output. */
60
+ /** One Proxmox host as the morning run saw it. */
54
61
  export interface ProxmoxHost {
55
62
  name: string;
63
+ /** `pulsar` or `space-x`: the target of the Reboot button. */
64
+ id: string;
56
65
  status: string | null;
66
+ /** `updates` after any upgrade. */
57
67
  updates: string | null;
68
+ /** What `host-upgrade` printed this morning: undefined when nothing was waiting, null when it failed. */
69
+ upgrade?: string | null;
70
+ }
71
+
72
+ /** The custom id of the Reboot button for one Proxmox host. */
73
+ export function rebootButtonId(id: string): string {
74
+ return `proxmox:reboot:${id}`;
58
75
  }
59
76
 
60
- function proxmoxHostLine(h: ProxmoxHost): string {
61
- if (h.status === null) return `${h.name} status unknown`;
77
+ interface ProxmoxFacts {
78
+ line: string;
79
+ problems: string[];
80
+ button?: AreaButton;
81
+ }
82
+
83
+ function proxmoxHostFacts(h: ProxmoxHost): ProxmoxFacts {
84
+ if (h.status === null) return { line: `${h.name} status unknown`, problems: [`the status check failed for ${h.name}`] };
62
85
  const up = /up\s+([^,]+(?:,\s*\d+:\d+)?)/.exec(h.status)?.[1]?.split(",")[0]?.trim();
63
86
  const mem = /^Mem:\s+(\S+)\s+(\S+)/m.exec(h.status);
64
87
  const root = /\s(\d+)%\s+\/\s*$/m.exec(h.status)?.[1];
65
- const lines = h.updates === null ? null : pendingPackageLines(h.updates);
66
- const security = lines?.filter((l) => /-security\s/.test(l)).length ?? 0;
67
- return [
68
- up ? `${h.name} up ${up}` : `${h.name} up`,
69
- mem ? `RAM ${mem[2]} of ${mem[1]} used` : null,
70
- root ? `root disk ${root}%` : null,
71
- lines === null ? "updates unknown" : `${lines.length} host package update${lines.length === 1 ? "" : "s"} waiting${security > 0 ? ` (${security} security)` : ""}, applied by hand`,
72
- ]
73
- .filter(Boolean)
74
- .join(", ");
88
+ const reboot = /^reboot-needed:\s*(.*)$/m.exec(h.status)?.[1]?.trim();
89
+ const waiting = h.updates === null ? null : pendingPackageLines(h.updates).length;
90
+ const held = h.upgrade ? /^held:\s*(.*)$/m.exec(h.upgrade)?.[1] : undefined;
91
+ const installed = h.upgrade ? /^(\d+) upgraded/m.exec(h.upgrade)?.[1] : undefined;
92
+ const updates =
93
+ h.upgrade === null
94
+ ? "the host upgrade failed"
95
+ : held
96
+ ? "a Proxmox VE major upgrade waits for you"
97
+ : [installed ? `${installed} host update${installed === "1" ? "" : "s"} installed` : null, waiting === null ? "updates unknown" : waiting > 0 ? `${waiting} still waiting` : installed ? null : "up to date"].filter(Boolean).join(", ");
98
+ const rebootNeeded = reboot !== undefined && reboot !== "no";
99
+ const line = [up ? `${h.name} up ${up}` : `${h.name} up`, mem ? `RAM ${mem[2]} of ${mem[1]} used` : null, root ? `root disk ${root}%` : null, updates, rebootNeeded ? "reboot needed" : null].filter(Boolean).join(", ");
100
+ const problems = [
101
+ h.upgrade === null ? `the host upgrade on ${h.name} failed` : null,
102
+ held ? `${h.name} waits for you: ${held}` : null,
103
+ rebootNeeded ? `${h.name} needs a reboot (${reboot}); every guest on it restarts for a few minutes` : null,
104
+ ].filter((p): p is string => p !== null);
105
+ return { line, problems, ...(rebootNeeded ? { button: { label: `Reboot ${h.name}`, customId: rebootButtonId(h.id) } } : {}) };
75
106
  }
76
107
 
77
- /** `proxmox [space-x] host-status` and `updates`: each host's uptime, memory, root disk, and pending packages (report only). */
108
+ /** Both Proxmox hosts: uptime, memory, root disk, the morning's automatic upgrade, a held major, and a pending reboot with its button. */
78
109
  export function proxmoxStatus(hosts: readonly ProxmoxHost[]): AreaStatus {
79
- const unknown = hosts.filter((h) => h.status === null).map((h) => h.name);
80
- const line = hosts.map(proxmoxHostLine).join("; ");
81
- return { area: "Proxmox", line, ...(unknown.length > 0 ? { problem: `Proxmox: the status check failed for ${unknown.join(" and ")}`, route: "admin" as const } : {}) };
110
+ const facts = hosts.map(proxmoxHostFacts);
111
+ const problems = facts.flatMap((f) => f.problems);
112
+ const buttons = facts.flatMap((f) => (f.button ? [f.button] : []));
113
+ return {
114
+ area: "Proxmox",
115
+ line: facts.map((f) => f.line).join("; "),
116
+ ...(problems.length > 0 ? { problem: `Proxmox: ${problems.join("; ")}`, route: "admin" as const } : {}),
117
+ ...(buttons.length > 0 ? { buttons } : {}),
118
+ };
82
119
  }
83
120
 
84
121
  /** Packages the nightly run leaves alone on purpose: a Wazuh agent must never be newer than the manager. */
@@ -78,7 +78,7 @@ import { ArcaneWatch } from "./arcane-watch-monitor.ts";
78
78
  import { parseSnapshot } from "./arcane-watch.ts";
79
79
  import { type Applied, autoFailureText, foremanTask, stackProblems, stackUpdateAnswer, SUPERVISE_DELAY_MS, SUPERVISE_LIMIT_MS } from "./supervise.ts";
80
80
  import { stackPlan, toolPlan } from "./update-policy.ts";
81
- import { type AreaStatus, arcaneSummary, backupStatus, dnsStatus, edgeStatus, identityStatus, inferenceStatus, mediaStatus, networkStatus, problemText, proxmoxStatus, type ReportLine, type Route, summaryText, toolsSummary, updatesStatus } from "./daily-run.ts";
81
+ import { type AreaStatus, arcaneSummary, backupStatus, dnsStatus, edgeStatus, identityStatus, inferenceStatus, mediaStatus, networkStatus, pendingPackageLines, problemText, type ProxmoxHost, proxmoxStatus, type ReportLine, type Route, summaryText, toolsSummary, updatesStatus } from "./daily-run.ts";
82
82
  import { type InvestigationRequest, SecurityMonitor } from "./security-monitor.ts";
83
83
  import { nasOutage, readSecurityState } from "./security.ts";
84
84
 
@@ -174,6 +174,13 @@ const DNS_NODES = { technitium2: "technitium2.dns.home.arpa", technitium3: "tech
174
174
 
175
175
  /** The audit caller of updates the morning run applies on its own. */
176
176
  const AUTO_CALLER = "daily-run";
177
+ /** The Proxmox hosts the morning run upgrades, with the wrapper prefix that selects each. */
178
+ const PROXMOX_HOSTS: readonly { name: string; id: string; prefix: string[] }[] = [
179
+ { name: "Pulsar", id: "pulsar", prefix: [] },
180
+ { name: "SPACE-X", id: "space-x", prefix: ["space-x"] },
181
+ ];
182
+ /** The wrapper allows a host upgrade 20 minutes; the bridge waits a little longer for its answer. */
183
+ const HOST_UPGRADE_MS = 21 * 60_000;
177
184
 
178
185
  function pause(ms: number): Promise<void> {
179
186
  return new Promise((resolve) => setTimeout(resolve, Math.max(0, ms)).unref());
@@ -230,6 +237,8 @@ class Bridge {
230
237
  private readonly mediaMessages = new Map<string, MessageComponent[]>();
231
238
  /** Tool updates started from the Cortex Tools Report, by line key; they count as open work. */
232
239
  private readonly toolRuns = new Map<string, { channelId: string; label: string }>();
240
+ /** Proxmox hosts that an owner's Reboot click is restarting. */
241
+ private readonly rebooting = new Set<string>();
233
242
  private readonly toolClicks = new KeyedQueue();
234
243
  private gateway: GatewayClient | undefined;
235
244
  private stopping = false;
@@ -588,7 +597,8 @@ class Bridge {
588
597
  const routes = this.routes();
589
598
  const owners = this.config.owners;
590
599
  for (const a of areas.filter((x) => x.problem)) {
591
- await this.rest.createMessage(routes[a.route ?? "admin"], { content: problemText(a, owners), mentionUsers: owners }).catch((err) => log(`daily ${date}: ${a.area} problem message failed: ${err instanceof Error ? err.message : String(err)}`));
600
+ const components = a.buttons ? [{ type: 1, components: a.buttons.map((b) => ({ type: 2, style: 4, label: b.label, custom_id: b.customId })) }] : undefined;
601
+ await this.rest.createMessage(routes[a.route ?? "admin"], { content: problemText(a, owners), mentionUsers: owners, ...(components ? { components } : {}) }).catch((err) => log(`daily ${date}: ${a.area} problem message failed: ${err instanceof Error ? err.message : String(err)}`));
592
602
  }
593
603
  const label = new Intl.DateTimeFormat("en-GB", { timeZone: schedule.timeZone, weekday: "short", day: "numeric", month: "short" }).format(new Date());
594
604
  const text = summaryText({ date: label, guildId: this.config.guildId, reports, areas, routes });
@@ -623,11 +633,17 @@ class Bridge {
623
633
  if (present.has("edge")) areas.push((async () => edgeStatus(await text("edge", ["vps", "status"]), await text("edge", ["vps", "updates"])))());
624
634
  if (present.has("proxmox")) {
625
635
  areas.push(
626
- (async () =>
627
- proxmoxStatus([
628
- { name: "Pulsar", status: await text("proxmox", ["host-status"]), updates: await text("proxmox", ["updates"]) },
629
- { name: "SPACE-X", status: await text("proxmox", ["space-x", "host-status"]), updates: await text("proxmox", ["space-x", "updates"]) },
630
- ]))(),
636
+ (async () => {
637
+ const hosts: ProxmoxHost[] = [];
638
+ for (const h of PROXMOX_HOSTS) {
639
+ const before = await text("proxmox", [...h.prefix, "updates"]);
640
+ // Host packages install on their own; a new Proxmox VE major is held by the entry script for the owner.
641
+ const upgrade = before !== null && pendingPackageLines(before).length > 0 ? await this.runEntry("proxmox", AUTO_CALLER, [...h.prefix, "host-upgrade"], HOST_UPGRADE_MS).catch((err) => (log(`daily: ${h.name} host-upgrade failed: ${err instanceof Error ? err.message : String(err)}`), null)) : undefined;
642
+ const updates = upgrade === undefined ? before : await text("proxmox", [...h.prefix, "updates"]);
643
+ hosts.push({ name: h.name, id: h.id, status: await text("proxmox", [...h.prefix, "host-status"]), updates, upgrade });
644
+ }
645
+ return proxmoxStatus(hosts);
646
+ })(),
631
647
  );
632
648
  areas.push(
633
649
  (async () => {
@@ -1350,6 +1366,63 @@ class Bridge {
1350
1366
  await this.restartService(p, userId);
1351
1367
  }
1352
1368
 
1369
+ /**
1370
+ * Reboots one Proxmox host for an owner, when its status still says a reboot is needed,
1371
+ * then reports in the problem's channel whether it came back within ten minutes.
1372
+ */
1373
+ private async onRebootButton(interaction: Interaction, id: string): Promise<void> {
1374
+ const host = PROXMOX_HOSTS.find((h) => h.id === id);
1375
+ const userId = interaction.member?.user?.id ?? interaction.user?.id ?? "";
1376
+ this.refreshConfig();
1377
+ if (!host) {
1378
+ await this.rest.ephemeralReply(interaction.id, interaction.token, "This button is no longer active.");
1379
+ return;
1380
+ }
1381
+ if (!isOwner(this.config, userId)) {
1382
+ await this.rest.ephemeralReply(interaction.id, interaction.token, "Only owners reboot Proxmox hosts.");
1383
+ return;
1384
+ }
1385
+ if (this.rebooting.has(host.id)) {
1386
+ await this.rest.ephemeralReply(interaction.id, interaction.token, `${host.name} is already rebooting.`);
1387
+ return;
1388
+ }
1389
+ const status = await this.runEntry("proxmox", userId, [...host.prefix, "host-status"], 3 * 60_000).catch(() => null);
1390
+ if (status !== null && /^reboot-needed:\s*no\s*$/m.test(status)) {
1391
+ await this.rest.ephemeralReply(interaction.id, interaction.token, `${host.name} no longer needs a reboot.`);
1392
+ return;
1393
+ }
1394
+ await this.rest.deferUpdate(interaction.id, interaction.token);
1395
+ const channelId = interaction.channel_id ?? this.config.adminChannelId;
1396
+ const messageId = interaction.message?.id;
1397
+ this.rebooting.add(host.id);
1398
+ try {
1399
+ log(`reboot ${host.name} by ${userId}`);
1400
+ await this.runEntry("proxmox", userId, [...host.prefix, "host-reboot", "--confirm"], 2 * 60_000);
1401
+ const note = `♻️ ${host.name} reboots in one minute, started by <@${userId}>; its guests restart with it.`;
1402
+ if (messageId) await this.rest.editMessage(channelId, messageId, `${interaction.message?.content ?? ""}\n${note}`.slice(0, 1900), []).catch(() => undefined);
1403
+ // The host goes down after one minute; ask again from minute four until minute fourteen.
1404
+ await new Promise((r) => setTimeout(r, 4 * 60_000));
1405
+ let back: string | null = null;
1406
+ for (let i = 0; i < 20 && back === null && !this.stopping; i++) {
1407
+ back = await this.runEntry("proxmox", AUTO_CALLER, [...host.prefix, "host-status"], 60_000).catch(() => null);
1408
+ if (back !== null && !/up\s+(\d+ min|0:0?\d)/.test(back)) back = null;
1409
+ if (back === null) await new Promise((r) => setTimeout(r, 30_000));
1410
+ }
1411
+ const kernel = back ? /running kernel: ([^)]+)\)/.exec(back)?.[1] : undefined;
1412
+ const reboot = back ? /^reboot-needed:\s*(.*)$/m.exec(back)?.[1] : undefined;
1413
+ await this.rest.createMessage(channelId, {
1414
+ content: back
1415
+ ? `✅ ${host.name} is back${kernel ? ` on kernel ${kernel}` : ""}${reboot && reboot !== "no" ? `; it still reports reboot-needed: ${reboot}` : ""}.`
1416
+ : `❌ ${host.name} did not answer within ten minutes of the reboot. <@${userId}>`,
1417
+ ...(back ? {} : { mentionUsers: [userId] }),
1418
+ });
1419
+ } catch (err) {
1420
+ await this.rest.createMessage(channelId, { content: `❌ The reboot of ${host.name} did not start: ${(err instanceof Error ? err.message : String(err)).slice(0, 300)}` }).catch(() => undefined);
1421
+ } finally {
1422
+ this.rebooting.delete(host.id);
1423
+ }
1424
+ }
1425
+
1353
1426
  /** Runs one specialist wrapper through the sudo entry point, without a model; rejects with the wrapper's stderr. */
1354
1427
  private runEntry(service: string, caller: string, args: string[], timeoutMs: number): Promise<string> {
1355
1428
  const gate = loadSpecialists().config;
@@ -1754,6 +1827,10 @@ class Bridge {
1754
1827
  await this.onArcaneButton(interaction, customId);
1755
1828
  return;
1756
1829
  }
1830
+ if (customId.startsWith("proxmox:reboot:")) {
1831
+ await this.onRebootButton(interaction, customId.slice("proxmox:reboot:".length));
1832
+ return;
1833
+ }
1757
1834
  if (customId.startsWith("security:")) {
1758
1835
  this.refreshConfig();
1759
1836
  const userId = interaction.member?.user?.id ?? interaction.user?.id ?? "";
@@ -48,6 +48,9 @@ describe("approvals", () => {
48
48
  expect(isRisky("proxmox", ["snapshot-create", "107", "before", "-h"])).toBe(true);
49
49
  expect(isRisky("dns", ["--help"])).toBe(false);
50
50
  expect(isRisky("proxmox", ["guests"])).toBe(false);
51
+ expect(isRisky("proxmox", ["space-x", "host-upgrade"])).toBe(true);
52
+ expect(isRisky("proxmox", ["host-reboot", "--confirm"])).toBe(true);
53
+ expect(isRisky("proxmox", ["space-x", "host-status"])).toBe(false);
51
54
  expect(isRisky("dns", ["technitium", "host", "technitium1", "upgrade"])).toBe(true);
52
55
  expect(isRisky("dns", ["technitium", "host", "technitium3", "restart"])).toBe(true);
53
56
  expect(isRisky("dns", ["technitium", "host", "technitium2", "status"])).toBe(false);
@@ -43,7 +43,7 @@ export const RISKY: Readonly<Record<string, readonly RegExp[]>> = {
43
43
  ],
44
44
  edge: [/^vps\s+deploy-nginx\b/],
45
45
  backup: [/^(vzdump-run|host-backup-run|arcane-backup-run|gcs-run|restic-check-run|restic-restore|restore-clean)\b/],
46
- proxmox: [/^(space-x\s+)?(set|snapshot-create|start)\b/],
46
+ proxmox: [/^(space-x\s+)?(set|snapshot-create|start|host-upgrade|host-reboot)\b/],
47
47
  inference: [/^(nexus-restart|nexus-start)\b/],
48
48
  // media has no entry: trendarr cannot delete anything, and adds and queue toggles run on the caller's request.
49
49
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrofoundry/pi-astro",
3
- "version": "0.40.34",
3
+ "version": "0.40.35",
4
4
  "description": "Personal pi customizations (extensions, subagents, skills, prompts, themes) for the pi coding agent.",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -41,6 +41,8 @@ Power actions and snapshot changes return `{ upid, exitstatus }`; `OK` means don
41
41
  | `host-status` | `pveversion`, uptime, memory, root and `/var/lib/vz` usage, CPU and NVMe temperatures |
42
42
  | `host-journal <since>` | warnings and errors from the host journal |
43
43
  | `updates` | upgradable host packages |
44
+ | `host-upgrade` | apt full-upgrade of the host (risky, needs approval); a new Proxmox VE major is held and printed as `held:`; ends with `reboot-needed:`. The morning run does this on its own each day |
45
+ | `host-reboot --confirm` | reboot the host in one minute (risky): every guest on it restarts. Only on the owner's explicit request; the morning run offers a Reboot button instead |
44
46
  | `guest-exec <vmid> <status|journal|df|updates>` | fixed read inside a guest: failed units and uptime, warning journal of 24 h, disk usage, upgradable packages |
45
47
 
46
48
  ## Guest change procedure
@@ -55,7 +57,7 @@ Memory and CPU changes on a running guest apply at the next start unless hotplug
55
57
 
56
58
  ## Not available
57
59
 
58
- Creating, cloning, or destroying guests, disk resize or removal, storage changes, host reboot or updates, backups (backup specialist), and anything inside a guest beyond the fixed reads. Describe the exact operator command (`qm`, `pct`, `pvesh`) and stop.
60
+ Creating, cloning, or destroying guests, disk resize or removal, storage changes, backups (backup specialist), and anything inside a guest beyond the fixed reads. Describe the exact operator command (`qm`, `pct`, `pvesh`) and stop.
59
61
 
60
62
  ## When to stop and report
61
63
 
@@ -12,6 +12,19 @@ rest=""
12
12
  [ "$cmd" != "$name" ] && rest="${cmd#* }"
13
13
  since_ok() { printf '%s' "$1" | grep -Eq '^[A-Za-z0-9 :+-]{1,40}$'; }
14
14
 
15
+ # A newer Proxmox kernel than the running one, or a package's reboot flag, means the host needs a reboot.
16
+ reboot_needed() {
17
+ running="$(uname -r)"
18
+ newest="$(ls /boot/vmlinuz-*-pve 2>/dev/null | sed 's#^/boot/vmlinuz-##' | sort -V | tail -n 1)"
19
+ if [ -n "$newest" ] && [ "$newest" != "$running" ]; then
20
+ echo "reboot-needed: kernel $running -> $newest"
21
+ elif [ -f /run/reboot-required ]; then
22
+ echo "reboot-needed: $(tr '\n' ' ' < /run/reboot-required.pkgs 2>/dev/null)"
23
+ else
24
+ echo "reboot-needed: no"
25
+ fi
26
+ }
27
+
15
28
  guest_cmd() {
16
29
  case "$1" in
17
30
  status) echo 'systemctl --failed --no-pager; uptime' ;;
@@ -28,9 +41,27 @@ case "$name" in
28
41
  uptime
29
42
  free -h
30
43
  df -h / /var/lib/vz
31
- sensors 2>/dev/null | grep -E 'Tctl|Composite|edge' || true ;;
44
+ sensors 2>/dev/null | grep -E 'Tctl|Composite|edge' || true
45
+ reboot_needed ;;
32
46
  host-journal) since_ok "$rest" || { echo "bad since" >&2; exit 2; }; journalctl -p warning --since "$rest" --no-pager -n 200 ;;
33
47
  updates) apt list --upgradable 2>/dev/null ;;
48
+ host-upgrade)
49
+ [ -z "$rest" ] || { echo "host-upgrade takes no arguments" >&2; exit 2; }
50
+ export DEBIAN_FRONTEND=noninteractive
51
+ apt-get update -q >/dev/null
52
+ # A new Proxmox VE major waits for the owner.
53
+ now="$(dpkg-query -W -f='${Version}' pve-manager)"
54
+ next="$(apt-get -s full-upgrade | sed -n 's/^Inst pve-manager \[[^]]*\] (\([^ ]*\) .*/\1/p')"
55
+ if [ -n "$next" ] && [ "${next%%.*}" != "${now%%.*}" ]; then
56
+ echo "held: Proxmox VE $now -> $next is a major upgrade"
57
+ exit 0
58
+ fi
59
+ apt-get -y -q -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold full-upgrade 2>&1 | grep -E '^[0-9]+ upgraded|^E:' || true
60
+ reboot_needed ;;
61
+ host-reboot)
62
+ [ "$rest" = "--confirm" ] || { echo "host-reboot needs --confirm" >&2; exit 2; }
63
+ shutdown -r +1 "reboot requested through the proxmox specialist" 2>&1
64
+ echo "reboot in 1 minute" ;;
34
65
  guest-exec)
35
66
  vmid="${rest%% *}"; action="${rest#* }"
36
67
  case "$vmid" in 1[0-9][0-9]) ;; *) echo "bad vmid" >&2; exit 2 ;; esac
@@ -12,6 +12,19 @@ rest=""
12
12
  [ "$cmd" != "$name" ] && rest="${cmd#* }"
13
13
  since_ok() { printf '%s' "$1" | grep -Eq '^[A-Za-z0-9 :+-]{1,40}$'; }
14
14
 
15
+ # A newer Proxmox kernel than the running one, or a package's reboot flag, means the host needs a reboot.
16
+ reboot_needed() {
17
+ running="$(uname -r)"
18
+ newest="$(ls /boot/vmlinuz-*-pve 2>/dev/null | sed 's#^/boot/vmlinuz-##' | sort -V | tail -n 1)"
19
+ if [ -n "$newest" ] && [ "$newest" != "$running" ]; then
20
+ echo "reboot-needed: kernel $running -> $newest"
21
+ elif [ -f /run/reboot-required ]; then
22
+ echo "reboot-needed: $(tr '\n' ' ' < /run/reboot-required.pkgs 2>/dev/null)"
23
+ else
24
+ echo "reboot-needed: no"
25
+ fi
26
+ }
27
+
15
28
  guest_cmd() {
16
29
  case "$1" in
17
30
  status) echo 'systemctl --failed --no-pager; uptime' ;;
@@ -28,9 +41,27 @@ case "$name" in
28
41
  uptime
29
42
  free -h
30
43
  df -h / /var/lib/vz
31
- sensors 2>/dev/null | grep -E 'Tctl|Composite|edge' || true ;;
44
+ sensors 2>/dev/null | grep -E 'Tctl|Composite|edge' || true
45
+ reboot_needed ;;
32
46
  host-journal) since_ok "$rest" || { echo "bad since" >&2; exit 2; }; journalctl -p warning --since "$rest" --no-pager -n 200 ;;
33
47
  updates) apt list --upgradable 2>/dev/null ;;
48
+ host-upgrade)
49
+ [ -z "$rest" ] || { echo "host-upgrade takes no arguments" >&2; exit 2; }
50
+ export DEBIAN_FRONTEND=noninteractive
51
+ apt-get update -q >/dev/null
52
+ # A new Proxmox VE major waits for the owner.
53
+ now="$(dpkg-query -W -f='${Version}' pve-manager)"
54
+ next="$(apt-get -s full-upgrade | sed -n 's/^Inst pve-manager \[[^]]*\] (\([^ ]*\) .*/\1/p')"
55
+ if [ -n "$next" ] && [ "${next%%.*}" != "${now%%.*}" ]; then
56
+ echo "held: Proxmox VE $now -> $next is a major upgrade"
57
+ exit 0
58
+ fi
59
+ apt-get -y -q -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold full-upgrade 2>&1 | grep -E '^[0-9]+ upgraded|^E:' || true
60
+ reboot_needed ;;
61
+ host-reboot)
62
+ [ "$rest" = "--confirm" ] || { echo "host-reboot needs --confirm" >&2; exit 2; }
63
+ shutdown -r +1 "reboot requested through the proxmox specialist" 2>&1
64
+ echo "reboot in 1 minute" ;;
34
65
  guest-exec)
35
66
  vmid="${rest%% *}"; action="${rest#* }"
36
67
  case "$vmid" in 1[0-9][0-9]) ;; *) echo "bad vmid" >&2; exit 2 ;; esac
@@ -85,9 +85,13 @@ export const HOST_COMMANDS: Readonly<Record<string, { args: number; help: string
85
85
  "host-status": { args: 0, help: "pveversion, uptime, load, memory, root disk, temperatures" },
86
86
  "host-journal": { args: 1, help: "host-journal <since> warnings and errors of the host journal" },
87
87
  updates: { args: 0, help: "upgradable packages on the host" },
88
+ "host-upgrade": { args: 0, help: "apt full-upgrade of the host; a new Proxmox VE major is held (prints held:); ends with reboot-needed" },
89
+ "host-reboot": { args: 1, help: "host-reboot --confirm reboot the host in one minute; every guest on it restarts" },
88
90
  "guest-exec": { args: 2, help: "guest-exec <vmid> <status|journal|df|updates> fixed read inside a guest (pct exec or qm guest exec)" },
89
91
  };
90
92
  export const GUEST_EXEC = ["status", "journal", "df", "updates"];
93
+ /** A full upgrade with a new kernel and pve-manager can take several minutes on the host. */
94
+ export const HOST_UPGRADE_TIMEOUT_MS = 20 * 60_000;
91
95
  const SINCE = /^[A-Za-z0-9 :\-+]{1,40}$/;
92
96
 
93
97
  const HELP = `proxmox specialist (Proxmox VE on Pulsar at 37 and SPACE-X at Mira)
@@ -328,6 +332,7 @@ export function buildHostRemote(args: string[]): string {
328
332
  if (!spec) throw new UsageError(`unknown host command: ${name ?? "(none)"}`);
329
333
  if (rest.length !== spec.args) throw new UsageError(`${name} takes ${spec.args} argument(s)`);
330
334
  if (name === "host-journal" && !SINCE.test(rest[0])) throw new UsageError("since: letters, digits, spaces, colon, plus, minus only");
335
+ if (name === "host-reboot" && rest[0] !== "--confirm") throw new UsageError("host-reboot restarts every guest on the host; add --confirm when the task asks for it explicitly");
331
336
  if (name === "guest-exec") {
332
337
  parseVmid(rest[0]);
333
338
  if (!GUEST_EXEC.includes(rest[1])) throw new UsageError(`guest-exec action must be one of ${GUEST_EXEC.join(", ")}`);
@@ -345,7 +350,7 @@ async function host(config: Target, args: string[]): Promise<number> {
345
350
  knownHostsFile: join(specialistsHome(), "config", "known_hosts"),
346
351
  },
347
352
  remote,
348
- 120_000,
353
+ args[0] === "host-upgrade" ? HOST_UPGRADE_TIMEOUT_MS : 120_000,
349
354
  );
350
355
  if (result.stdout.length > 0) printRaw(result.stdout);
351
356
  if (result.code !== 0) throw new ServiceError(result.stderr.trim() || `${args[0]} exited ${result.code}`);
@@ -653,6 +653,10 @@ describe("proxmox wrapper", () => {
653
653
  expect(buildHostRemote(["guest-exec", "101", "df"])).toBe("guest-exec 101 df");
654
654
  expect(() => buildHostRemote(["guest-exec", "101", "rm"])).toThrow(/action/);
655
655
  expect(() => buildHostRemote(["host-journal", "1 hour ago; id"])).toThrow(/since/);
656
+ expect(buildHostRemote(["host-upgrade"])).toBe("host-upgrade");
657
+ expect(() => buildHostRemote(["host-upgrade", "now"])).toThrow(UsageError);
658
+ expect(buildHostRemote(["host-reboot", "--confirm"])).toBe("host-reboot --confirm");
659
+ expect(() => buildHostRemote(["host-reboot", "now"])).toThrow(/--confirm/);
656
660
  expect(() => buildHostRemote(["shell"])).toThrow(/unknown host command/);
657
661
  });
658
662