Fold alerts under their host, recurring maintenance, run playbooks from alerts, and an Updates page

- Servers can say what they run on (or it is worked out from the Proxmox node); when the host is
  down the guests' offline alerts fold under it: held back, not counted, not notified, still recorded.
- Recurring maintenance schedules made into ordinary windows a week ahead (maintenance_schedules).
- Run a Semaphore template limited to a server from its page or an alert, and watch the run.
- Agents report pending updates (apt, dnf, zypper, Windows Update); an Updates page lists them.
- Migrations 0024 and 0025.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-17 01:35:15 +02:00
1 parent 44e393f172
commit 171511c6db
45 files changed
+6772 -44

No files matched your search

+143
View File
@@ -0,0 +1,143 @@
/**
* Running a Semaphore template from the app, limited to one host, and watching how the run is getting on.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
const dir = mkdtempSync(join(tmpdir(), "hm-sem-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, integrations, auditLog } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { integrationsRouter } = await import("../src/routes/integrations.js");
await runMigrations();
describe("running a Semaphore template", () => {
let semaphore: http.Server;
let app: http.Server;
let base = "";
let who = "operator";
let integrationId = 0;
const received: { method: string; url: string; body: Record<string, unknown> | null; auth: string | undefined }[] = [];
before(async () => {
semaphore = await new Promise<http.Server>((resolve) => {
const s = http.createServer((req, res) => {
let raw = "";
req.on("data", (c) => (raw += c));
req.on("end", () => {
received.push({ method: req.method ?? "", url: req.url ?? "", body: raw ? JSON.parse(raw) : null, auth: req.headers.authorization });
res.setHeader("content-type", "application/json");
if (req.method === "POST" && req.url === "/api/project/3/tasks") return res.end(JSON.stringify({ id: 77, status: "waiting", created: "2026-10-17T10:00:00Z" }));
if (req.method === "GET" && req.url === "/api/project/3/tasks/77") return res.end(JSON.stringify({ id: 77, status: "running", start: "2026-10-17T10:00:05Z" }));
res.statusCode = 404;
res.end(JSON.stringify({ error: "not here" }));
});
});
s.listen(0, "127.0.0.1", () => resolve(s));
});
const url = `http://127.0.0.1:${(semaphore.address() as AddressInfo).port}`;
await db.insert(users).values([
{ oidcSub: "operator", name: "Olga", role: "operator" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
]);
const [row] = await db.insert(integrations).values({ type: "semaphore", name: "Semaphore", baseUrl: url, config: JSON.stringify({ url, token: "secret-token" }) }).returning();
integrationId = row.id;
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/integrations", integrationsRouter);
app = await new Promise<http.Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/integrations/${integrationId}/semaphore`;
});
after(() => {
semaphore.close();
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const run = (body?: unknown) =>
fetch(`${base}/projects/3/templates/9/run`, { method: "POST", headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
test("a run limited to a host tells Semaphore which, and who started it", async () => {
const res = await run({ limit: "elsa" });
assert.equal(res.status, 201);
assert.equal((await res.json()).task.id, 77);
const sent = received.find((r) => r.method === "POST")!;
assert.equal(sent.url, "/api/project/3/tasks");
assert.equal(sent.auth, "Bearer secret-token");
assert.equal(sent.body?.template_id, 9);
assert.equal(sent.body?.limit, "elsa");
assert.match(String(sent.body?.message), /Olga/);
assert.match(String(sent.body?.message), /limited to elsa/);
});
test("a run with no limit sends none, and the audit log records what ran and where", async () => {
received.length = 0;
assert.equal((await run()).status, 201);
assert.equal((await run({})).status, 201);
for (const r of received) {
assert.equal(r.body?.limit, undefined);
assert.doesNotMatch(String(r.body?.message), /limited/);
}
const entries = (await db.select().from(auditLog)).filter((e) => e.action === "run_template");
assert.equal(entries.length, 3);
assert.match(entries[0].detail ?? "", /"limit":"elsa"/);
assert.doesNotMatch(entries[1].detail ?? "", /limit/);
assert.ok(entries.every((e) => !(e.detail ?? "").includes("secret-token")));
});
test("anything that isn't a host or a pattern is refused before it reaches Semaphore", async () => {
received.length = 0;
for (const limit of ["--extra-vars x=1", "-e foo", "elsa; reboot", "$(id)", "a b", "", "x".repeat(201), "../etc"]) {
const res = await run({ limit });
assert.equal(res.status, limit === "" ? 201 : 400, JSON.stringify(limit).slice(0, 40));
}
// the empty one is "no limit"; nothing else was sent on
assert.equal(received.filter((r) => r.method === "POST").length, 1);
for (const ok of ["web01,db*", "elsa.lab.example", "10.0.0.5", "group:&web"]) {
if (ok.includes("&")) continue; // not a pattern this accepts
assert.equal((await run({ limit: ok })).status, 201, ok);
}
});
test("a viewer can't run anything", async () => {
who = "viewer";
received.length = 0;
assert.equal((await run({ limit: "elsa" })).status, 403);
assert.equal(received.length, 0);
who = "operator";
});
test("how a run is getting on can be asked for, and a missing run is reported", async () => {
const res = await fetch(`${base}/projects/3/tasks/77`);
assert.equal(res.status, 200);
assert.deepEqual((await res.json()).task, { id: 77, status: "running", created: null, start: "2026-10-17T10:00:05Z", end: null });
assert.equal((await fetch(`${base}/projects/3/tasks/999`)).status, 502);
assert.equal((await fetch(`${base}/projects/x/tasks/77`)).status, 400);
});
});