@glassmkr/crucible 0.6.6 → 0.7.1
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.
- package/README.md +25 -0
- package/config/collector.example.yaml +2 -2
- package/dist/__tests__/reboot-marker.test.d.ts +1 -0
- package/dist/__tests__/reboot-marker.test.js +110 -0
- package/dist/__tests__/reboot-marker.test.js.map +1 -0
- package/dist/cli.d.ts +4 -1
- package/dist/cli.js +56 -0
- package/dist/cli.js.map +1 -1
- package/dist/collect/__tests__/system.test.d.ts +1 -0
- package/dist/collect/__tests__/system.test.js +25 -0
- package/dist/collect/__tests__/system.test.js.map +1 -0
- package/dist/collect/system.d.ts +1 -0
- package/dist/collect/system.js +10 -0
- package/dist/collect/system.js.map +1 -1
- package/dist/index.js +54 -16
- package/dist/index.js.map +1 -1
- package/dist/lib/reboot-marker.d.ts +35 -0
- package/dist/lib/reboot-marker.js +95 -0
- package/dist/lib/reboot-marker.js.map +1 -0
- package/dist/lib/types.d.ts +8 -0
- package/dist/lib/version-check.js +1 -1
- package/dist/lib/version-check.js.map +1 -1
- package/dist/lib/version.d.ts +1 -0
- package/dist/lib/version.js +32 -0
- package/dist/lib/version.js.map +1 -0
- package/dist/notify/email.js +2 -1
- package/dist/notify/email.js.map +1 -1
- package/dist/notify/slack.js +2 -1
- package/dist/notify/slack.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/reboot-marker.test.ts +122 -0
- package/src/cli.ts +51 -1
- package/src/collect/__tests__/system.test.ts +29 -0
- package/src/collect/system.ts +11 -0
- package/src/index.ts +54 -17
- package/src/lib/reboot-marker.ts +88 -0
- package/src/lib/types.ts +12 -0
- package/src/lib/version-check.ts +2 -1
- package/src/lib/version.ts +33 -0
- package/src/notify/email.ts +2 -1
- package/src/notify/slack.ts +2 -1
package/dist/lib/types.d.ts
CHANGED
|
@@ -27,6 +27,8 @@ export interface Snapshot {
|
|
|
27
27
|
systemd?: SystemdData;
|
|
28
28
|
ntp?: NtpData;
|
|
29
29
|
file_descriptors?: FileDescriptorData;
|
|
30
|
+
expected_reboot?: boolean;
|
|
31
|
+
expected_reboot_reason?: string;
|
|
30
32
|
}
|
|
31
33
|
export interface ConntrackData {
|
|
32
34
|
available: boolean;
|
|
@@ -98,6 +100,12 @@ export interface SystemInfo {
|
|
|
98
100
|
hostname: string;
|
|
99
101
|
ip: string;
|
|
100
102
|
os: string;
|
|
103
|
+
/** `ID=` from /etc/os-release, lowercased. e.g. "ubuntu", "debian", "rocky", "arch", "alpine". */
|
|
104
|
+
os_id?: string;
|
|
105
|
+
/** `ID_LIKE=` from /etc/os-release, lowercased, space-separated. Used by Forge
|
|
106
|
+
* to pick distro-family-specific fix command variants. e.g. on Rocky this
|
|
107
|
+
* is "rhel centos fedora"; on Ubuntu it is "debian". */
|
|
108
|
+
os_id_like?: string;
|
|
101
109
|
kernel: string;
|
|
102
110
|
uptime_seconds: number;
|
|
103
111
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version-check.js","sourceRoot":"","sources":["../../src/lib/version-check.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"version-check.js","sourceRoot":"","sources":["../../src/lib/version-check.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,IAAI,eAAe,EAAE,MAAM,cAAc,CAAC;AAEnE,IAAI,aAAa,GAAG,CAAC,CAAC;AACtB,IAAI,UAAU,GAA2E,IAAI,CAAC;AAC9F,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,sBAAsB;AAEjE,MAAM,UAAU,iBAAiB;IAC/B,OAAO,eAAe,CAAC;AACzB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,QAAiB;IACrD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACvB,IAAI,GAAG,GAAG,aAAa,GAAG,cAAc;QAAE,OAAO;IACjD,aAAa,GAAG,GAAG,CAAC;IAEpB,MAAM,GAAG,GAAG,QAAQ,IAAI,4BAA4B,CAAC;IACrD,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,GAAG,iBAAiB,EAAE,EAAE,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACxF,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,OAAO;QACpB,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAwF,CAAC;QACpH,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC;QACrC,IAAI,CAAC,MAAM;YAAE,OAAO;QAEpB,IAAI,MAAM,KAAK,eAAe,EAAE,CAAC;YAC/B,OAAO,CAAC,GAAG,CAAC,4CAA4C,MAAM,cAAc,eAAe,GAAG,CAAC,CAAC;YAChG,OAAO,CAAC,GAAG,CAAC,uBAAuB,IAAI,CAAC,QAAQ,EAAE,aAAa,IAAI,+CAA+C,EAAE,CAAC,CAAC;YACtH,OAAO,CAAC,GAAG,CAAC,4FAA4F,CAAC,CAAC;YAC1G,UAAU,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,QAAQ,EAAE,aAAa,IAAI,EAAE,EAAE,CAAC;QAChG,CAAC;aAAM,CAAC;YACN,UAAU,GAAG,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC;QACjE,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,+CAA+C;IACjD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,eAAe;IAC7B,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const CRUCIBLE_VERSION: string;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Single source of truth for the Crucible version string at runtime.
|
|
2
|
+
// Read from package.json so a release bump propagates everywhere
|
|
3
|
+
// (notification footers, version-check log lines, --version flag, the
|
|
4
|
+
// `collector_version` field on every snapshot) without anyone having to
|
|
5
|
+
// remember to update a hardcoded constant.
|
|
6
|
+
//
|
|
7
|
+
// Returns "0.0.0" on read failure rather than throwing; the agent has to
|
|
8
|
+
// keep running even if its package.json is somehow missing.
|
|
9
|
+
import { readFileSync } from "node:fs";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { dirname, join } from "node:path";
|
|
12
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
function readPkgVersion() {
|
|
14
|
+
// src/lib/version.ts -> ../../package.json under src layout, but at
|
|
15
|
+
// runtime (compiled to dist/lib/version.js) it's still ../../package.json.
|
|
16
|
+
// Both paths resolve correctly because package.json sits one level above
|
|
17
|
+
// dist/ AND one level above src/.
|
|
18
|
+
for (const candidate of [
|
|
19
|
+
join(__dirname, "..", "..", "package.json"),
|
|
20
|
+
join(__dirname, "..", "package.json"),
|
|
21
|
+
]) {
|
|
22
|
+
try {
|
|
23
|
+
const pkg = JSON.parse(readFileSync(candidate, "utf8"));
|
|
24
|
+
if (pkg && typeof pkg.version === "string")
|
|
25
|
+
return pkg.version;
|
|
26
|
+
}
|
|
27
|
+
catch { /* try next */ }
|
|
28
|
+
}
|
|
29
|
+
return "0.0.0";
|
|
30
|
+
}
|
|
31
|
+
export const CRUCIBLE_VERSION = readPkgVersion();
|
|
32
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/lib/version.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,iEAAiE;AACjE,sEAAsE;AACtE,wEAAwE;AACxE,2CAA2C;AAC3C,EAAE;AACF,yEAAyE;AACzE,4DAA4D;AAE5D,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAE1D,SAAS,cAAc;IACrB,oEAAoE;IACpE,2EAA2E;IAC3E,yEAAyE;IACzE,kCAAkC;IAClC,KAAK,MAAM,SAAS,IAAI;QACtB,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,cAAc,CAAC;QAC3C,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,cAAc,CAAC;KACtC,EAAE,CAAC;QACF,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;YACxD,IAAI,GAAG,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ;gBAAE,OAAO,GAAG,CAAC,OAAO,CAAC;QACjE,CAAC;QAAC,MAAM,CAAC,CAAC,cAAc,CAAC,CAAC;IAC5B,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG,cAAc,EAAE,CAAC"}
|
package/dist/notify/email.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { execFile } from "child_process";
|
|
2
2
|
import { promisify } from "util";
|
|
3
|
+
import { CRUCIBLE_VERSION } from "../lib/version.js";
|
|
3
4
|
const execFileAsync = promisify(execFile);
|
|
4
5
|
export async function sendEmail(config, newAlerts, resolvedAlerts, serverName) {
|
|
5
6
|
if (!config.to)
|
|
@@ -49,7 +50,7 @@ function buildBody(newAlerts, resolvedAlerts, serverName) {
|
|
|
49
50
|
lines.push("");
|
|
50
51
|
}
|
|
51
52
|
lines.push("---");
|
|
52
|
-
lines.push(
|
|
53
|
+
lines.push(`Glassmkr Crucible v${CRUCIBLE_VERSION}`);
|
|
53
54
|
return lines.join("\n");
|
|
54
55
|
}
|
|
55
56
|
//# sourceMappingURL=email.js.map
|
package/dist/notify/email.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"email.js","sourceRoot":"","sources":["../../src/notify/email.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"email.js","sourceRoot":"","sources":["../../src/notify/email.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAEjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD,MAAM,aAAa,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;AAE1C,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,MAAsB,EACtB,SAAwB,EACxB,cAA6B,EAC7B,UAAkB;IAElB,IAAI,CAAC,MAAM,CAAC,EAAE;QAAE,OAAO,KAAK,CAAC;IAE7B,MAAM,OAAO,GAAG,YAAY,CAAC,SAAS,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC;IACpE,MAAM,IAAI,GAAG,SAAS,CAAC,SAAS,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC;IAE9D,MAAM,KAAK,GAAG;QACZ,OAAO,MAAM,CAAC,EAAE,EAAE;QAClB,2BAA2B,UAAU,EAAE;QACvC,YAAY,OAAO,EAAE;QACrB,yCAAyC;QACzC,EAAE;QACF,IAAI;KACL,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAEb,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,aAAa,CAAC,oBAAoB,EAAE,CAAC,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;QAC9E,KAAK,CAAC,KAAK,CAAC,KAAK,EA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KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,cAAc,EAAE,CAAC,CAAC;QAC1C,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjB,CAAC;IAED,KAAK,MAAM,CAAC,IAAI,cAAc,EAAE,CAAC;QAC/B,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC;QACpC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAClB,KAAK,CAAC,IAAI,CAAC,sBAAsB,gBAAgB,EAAE,CAAC,CAAC;IACrD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
package/dist/notify/slack.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CRUCIBLE_VERSION } from "../lib/version.js";
|
|
1
2
|
export async function sendSlack(webhookUrl, newAlerts, resolvedAlerts, serverName) {
|
|
2
3
|
const blocks = [];
|
|
3
4
|
if (newAlerts.length > 0) {
|
|
@@ -20,7 +21,7 @@ export async function sendSlack(webhookUrl, newAlerts, resolvedAlerts, serverNam
|
|
|
20
21
|
if (blocks.length === 0)
|
|
21
22
|
return true;
|
|
22
23
|
blocks.push({ type: "divider" });
|
|
23
|
-
blocks.push({ type: "context", elements: [{ type: "mrkdwn", text:
|
|
24
|
+
blocks.push({ type: "context", elements: [{ type: "mrkdwn", text: `Glassmkr Crucible v${CRUCIBLE_VERSION}` }] });
|
|
24
25
|
try {
|
|
25
26
|
const res = await fetch(webhookUrl, {
|
|
26
27
|
method: "POST",
|
package/dist/notify/slack.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slack.js","sourceRoot":"","sources":["../../src/notify/slack.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"slack.js","sourceRoot":"","sources":["../../src/notify/slack.ts"],"names":[],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H,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { mkdtempSync, existsSync, writeFileSync, statSync, rmSync, chmodSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
consumeRebootMarker,
|
|
7
|
+
writeRebootMarker,
|
|
8
|
+
parseDuration,
|
|
9
|
+
} from "../lib/reboot-marker.js";
|
|
10
|
+
import { parseCliArgs } from "../cli.js";
|
|
11
|
+
|
|
12
|
+
let tmpDir: string;
|
|
13
|
+
let path: string;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
tmpDir = mkdtempSync(join(tmpdir(), "crucible-test-"));
|
|
17
|
+
path = join(tmpDir, "reboot-expected");
|
|
18
|
+
});
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
try { rmSync(tmpDir, { recursive: true, force: true }); } catch {}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("consumeRebootMarker", () => {
|
|
24
|
+
it("7. marker present, not expired: returns flag, deletes file", () => {
|
|
25
|
+
const future = new Date(Date.now() + 5 * 60_000).toISOString();
|
|
26
|
+
writeFileSync(path, JSON.stringify({ expires_at: future, reason: "kernel update" }));
|
|
27
|
+
const out = consumeRebootMarker(path);
|
|
28
|
+
expect(out).toEqual({ expected: true, reason: "kernel update" });
|
|
29
|
+
expect(existsSync(path)).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("8. marker present, expired: returns null, deletes file", () => {
|
|
33
|
+
const past = new Date(Date.now() - 60_000).toISOString();
|
|
34
|
+
writeFileSync(path, JSON.stringify({ expires_at: past, reason: "stale" }));
|
|
35
|
+
expect(consumeRebootMarker(path)).toBeNull();
|
|
36
|
+
expect(existsSync(path)).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("9. marker absent: returns null, no throw", () => {
|
|
40
|
+
expect(consumeRebootMarker(path)).toBeNull();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("15. malformed JSON: returns null, file deleted, no crash", () => {
|
|
44
|
+
writeFileSync(path, "{not json at all");
|
|
45
|
+
expect(consumeRebootMarker(path)).toBeNull();
|
|
46
|
+
expect(existsSync(path)).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("invalid expires_at (missing): returns null, file deleted", () => {
|
|
50
|
+
writeFileSync(path, JSON.stringify({ reason: "oops" }));
|
|
51
|
+
expect(consumeRebootMarker(path)).toBeNull();
|
|
52
|
+
expect(existsSync(path)).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("consumed marker cannot be re-read (single-use)", () => {
|
|
56
|
+
const future = new Date(Date.now() + 60_000).toISOString();
|
|
57
|
+
writeFileSync(path, JSON.stringify({ expires_at: future }));
|
|
58
|
+
expect(consumeRebootMarker(path)).not.toBeNull();
|
|
59
|
+
expect(consumeRebootMarker(path)).toBeNull();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("writeRebootMarker", () => {
|
|
64
|
+
it("13. writes file at given path with correct TTL and reason, 0600 mode", () => {
|
|
65
|
+
const now = new Date("2026-04-21T22:00:00Z");
|
|
66
|
+
const res = writeRebootMarker({ path, reason: "kernel update", ttlMs: 10 * 60_000, now });
|
|
67
|
+
expect(res.path).toBe(path);
|
|
68
|
+
expect(res.expires_at).toBe("2026-04-21T22:10:00.000Z");
|
|
69
|
+
expect(existsSync(path)).toBe(true);
|
|
70
|
+
const mode = statSync(path).mode & 0o777;
|
|
71
|
+
expect(mode).toBe(0o600);
|
|
72
|
+
const round = consumeRebootMarker(path, new Date("2026-04-21T22:05:00Z"));
|
|
73
|
+
expect(round).toEqual({ expected: true, reason: "kernel update" });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("default TTL is 10 minutes", () => {
|
|
77
|
+
const now = new Date("2026-04-21T22:00:00Z");
|
|
78
|
+
const res = writeRebootMarker({ path, now });
|
|
79
|
+
expect(res.expires_at).toBe("2026-04-21T22:10:00.000Z");
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("parseDuration", () => {
|
|
84
|
+
it.each([
|
|
85
|
+
["10m", 600_000],
|
|
86
|
+
["2h", 7_200_000],
|
|
87
|
+
["600s", 600_000],
|
|
88
|
+
["500ms", 500],
|
|
89
|
+
["30", 30_000], // bare number -> seconds
|
|
90
|
+
])("%s -> %d ms", (input, ms) => {
|
|
91
|
+
expect(parseDuration(input)).toBe(ms);
|
|
92
|
+
});
|
|
93
|
+
it("rejects garbage", () => {
|
|
94
|
+
expect(parseDuration("forever")).toBeNull();
|
|
95
|
+
expect(parseDuration("-5m")).toBeNull();
|
|
96
|
+
expect(parseDuration("")).toBeNull();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe("CLI parseCliArgs subcommands", () => {
|
|
101
|
+
it("14. `reboot` subcommand captured with flags", () => {
|
|
102
|
+
const { result } = parseCliArgs(["reboot", "--reason", "kernel update"], "1.0.0");
|
|
103
|
+
expect(result.mode).toBe("reboot");
|
|
104
|
+
expect(result.reason).toBe("kernel update");
|
|
105
|
+
});
|
|
106
|
+
it("`mark-reboot` with --ttl parsed through", () => {
|
|
107
|
+
const { result } = parseCliArgs(["mark-reboot", "--ttl=5m", "--reason=test"], "1.0.0");
|
|
108
|
+
expect(result.mode).toBe("mark-reboot");
|
|
109
|
+
expect(result.ttl).toBe("5m");
|
|
110
|
+
expect(result.reason).toBe("test");
|
|
111
|
+
});
|
|
112
|
+
it("`mark-reboot --help` returns help output without running", () => {
|
|
113
|
+
const { result, output } = parseCliArgs(["mark-reboot", "--help"], "1.0.0");
|
|
114
|
+
expect(result.mode).toBe("help");
|
|
115
|
+
expect(output).toContain("mark-reboot");
|
|
116
|
+
});
|
|
117
|
+
it("top-level help lists the new subcommands", () => {
|
|
118
|
+
const { output } = parseCliArgs(["--help"], "1.0.0");
|
|
119
|
+
expect(output).toMatch(/mark-reboot/);
|
|
120
|
+
expect(output).toMatch(/reboot/);
|
|
121
|
+
});
|
|
122
|
+
});
|
package/src/cli.ts
CHANGED
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
// or collector initialization so --version and --help exit cleanly even when
|
|
3
3
|
// the config file is missing or the host lacks the tools the collectors need.
|
|
4
4
|
|
|
5
|
+
export type CliMode = "version" | "help" | "run" | "mark-reboot" | "reboot";
|
|
6
|
+
|
|
5
7
|
export interface CliArgs {
|
|
6
|
-
mode:
|
|
8
|
+
mode: CliMode;
|
|
7
9
|
configPath: string;
|
|
10
|
+
reason?: string;
|
|
11
|
+
ttl?: string; // raw duration string, parsed by caller
|
|
8
12
|
}
|
|
9
13
|
|
|
10
14
|
export const DEFAULT_CONFIG_PATH = "/etc/glassmkr/collector.yaml";
|
|
@@ -13,6 +17,25 @@ export function parseCliArgs(argv: string[], version: string): { result: CliArgs
|
|
|
13
17
|
// argv is typically process.argv.slice(2)
|
|
14
18
|
let configPath = DEFAULT_CONFIG_PATH;
|
|
15
19
|
|
|
20
|
+
// Subcommand dispatch: `mark-reboot` and `reboot` take their own flags
|
|
21
|
+
// (--reason, --ttl) but re-use --help.
|
|
22
|
+
if (argv[0] === "mark-reboot" || argv[0] === "reboot") {
|
|
23
|
+
const mode: "mark-reboot" | "reboot" = argv[0];
|
|
24
|
+
let reason: string | undefined;
|
|
25
|
+
let ttl: string | undefined;
|
|
26
|
+
for (let i = 1; i < argv.length; i++) {
|
|
27
|
+
const a = argv[i];
|
|
28
|
+
if (a === "--help" || a === "-h") {
|
|
29
|
+
return { result: { mode: "help", configPath: "" }, output: subcommandHelp(mode, version) };
|
|
30
|
+
}
|
|
31
|
+
if (a === "--reason") { reason = argv[++i]; continue; }
|
|
32
|
+
if (a.startsWith("--reason=")) { reason = a.slice("--reason=".length); continue; }
|
|
33
|
+
if (a === "--ttl") { ttl = argv[++i]; continue; }
|
|
34
|
+
if (a.startsWith("--ttl=")) { ttl = a.slice("--ttl=".length); continue; }
|
|
35
|
+
}
|
|
36
|
+
return { result: { mode, configPath: "", reason, ttl }, output: null };
|
|
37
|
+
}
|
|
38
|
+
|
|
16
39
|
for (let i = 0; i < argv.length; i++) {
|
|
17
40
|
const arg = argv[i];
|
|
18
41
|
if (arg === "--version" || arg === "-v") {
|
|
@@ -50,13 +73,40 @@ export function helpText(version: string): string {
|
|
|
50
73
|
"",
|
|
51
74
|
"Usage:",
|
|
52
75
|
" glassmkr-crucible [options]",
|
|
76
|
+
" glassmkr-crucible mark-reboot [--reason TEXT] [--ttl DURATION]",
|
|
77
|
+
" glassmkr-crucible reboot [--reason TEXT] [--ttl DURATION]",
|
|
53
78
|
"",
|
|
54
79
|
"Options:",
|
|
55
80
|
" -v, --version Print version and exit",
|
|
56
81
|
" -h, --help Print this help and exit",
|
|
57
82
|
` -c, --config Path to config file (default: ${DEFAULT_CONFIG_PATH})`,
|
|
58
83
|
"",
|
|
84
|
+
"Subcommands:",
|
|
85
|
+
" mark-reboot Write a planned-reboot marker so the next boot",
|
|
86
|
+
" does not fire `server_rebooted_unexpectedly`.",
|
|
87
|
+
" You run the reboot yourself afterwards.",
|
|
88
|
+
" reboot Write the marker, then invoke `systemctl reboot`.",
|
|
89
|
+
"",
|
|
59
90
|
"Without options, starts the collector daemon using the config file.",
|
|
60
91
|
"Docs: https://github.com/glassmkr/crucible",
|
|
61
92
|
].join("\n");
|
|
62
93
|
}
|
|
94
|
+
|
|
95
|
+
function subcommandHelp(mode: "mark-reboot" | "reboot", version: string): string {
|
|
96
|
+
const action = mode === "reboot"
|
|
97
|
+
? "Write a planned-reboot marker and invoke `systemctl reboot`."
|
|
98
|
+
: "Write a planned-reboot marker; operator triggers the reboot.";
|
|
99
|
+
return [
|
|
100
|
+
`glassmkr-crucible ${mode} - ${action}`,
|
|
101
|
+
"",
|
|
102
|
+
"Usage:",
|
|
103
|
+
` glassmkr-crucible ${mode} [--reason TEXT] [--ttl DURATION]`,
|
|
104
|
+
"",
|
|
105
|
+
"Options:",
|
|
106
|
+
' --reason TEXT Free-text reason (e.g. "kernel update")',
|
|
107
|
+
" --ttl DURATION Expiry window; e.g. 5m, 10m, 1h (default 10m)",
|
|
108
|
+
"",
|
|
109
|
+
`Marker path: /var/lib/crucible/reboot-expected (requires root).`,
|
|
110
|
+
`v${version}`,
|
|
111
|
+
].join("\n");
|
|
112
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { readOsReleaseField } from "../system.js";
|
|
3
|
+
|
|
4
|
+
describe("readOsReleaseField", () => {
|
|
5
|
+
it("parses unquoted Ubuntu values", () => {
|
|
6
|
+
const s = 'NAME="Ubuntu"\nID=ubuntu\nID_LIKE=debian\nVERSION_ID="24.04"';
|
|
7
|
+
expect(readOsReleaseField(s, "ID")).toBe("ubuntu");
|
|
8
|
+
expect(readOsReleaseField(s, "ID_LIKE")).toBe("debian");
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("parses quoted RHEL-family values", () => {
|
|
12
|
+
const s = 'NAME="Rocky Linux"\nID="rocky"\nID_LIKE="rhel centos fedora"';
|
|
13
|
+
expect(readOsReleaseField(s, "ID")).toBe("rocky");
|
|
14
|
+
expect(readOsReleaseField(s, "ID_LIKE")).toBe("rhel centos fedora");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("lowercases the result (some distros uppercase their ID)", () => {
|
|
18
|
+
expect(readOsReleaseField("ID=Alpine", "ID")).toBe("alpine");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("returns undefined for a missing key", () => {
|
|
22
|
+
expect(readOsReleaseField("ID=arch", "ID_LIKE")).toBeUndefined();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("does not confuse ID with VERSION_ID", () => {
|
|
26
|
+
const s = 'VERSION_ID="24.04"\nID=ubuntu';
|
|
27
|
+
expect(readOsReleaseField(s, "ID")).toBe("ubuntu");
|
|
28
|
+
});
|
|
29
|
+
});
|
package/src/collect/system.ts
CHANGED
|
@@ -3,9 +3,18 @@ import { readProcFile } from "../lib/parse.js";
|
|
|
3
3
|
import { run } from "../lib/exec.js";
|
|
4
4
|
import type { SystemInfo } from "../lib/types.js";
|
|
5
5
|
|
|
6
|
+
// Matches KEY=value with optional surrounding double quotes. Handles both
|
|
7
|
+
// `ID=ubuntu` and `ID="rocky"` styles found in the wild.
|
|
8
|
+
export function readOsReleaseField(osRelease: string, key: string): string | undefined {
|
|
9
|
+
const m = osRelease.match(new RegExp(`^${key}=("?)(.+?)\\1$`, "m"));
|
|
10
|
+
return m ? m[2].toLowerCase() : undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
6
13
|
export async function collectSystem(): Promise<SystemInfo> {
|
|
7
14
|
const osRelease = readProcFile("/etc/os-release") || "";
|
|
8
15
|
const osName = osRelease.match(/PRETTY_NAME="(.+?)"/)?.[1] || "Unknown";
|
|
16
|
+
const os_id = readOsReleaseField(osRelease, "ID");
|
|
17
|
+
const os_id_like = readOsReleaseField(osRelease, "ID_LIKE");
|
|
9
18
|
const kernel = (await run("uname", ["-r"]))?.trim() || "unknown";
|
|
10
19
|
const uptimeRaw = readProcFile("/proc/uptime") || "0";
|
|
11
20
|
const uptimeSeconds = Math.floor(parseFloat(uptimeRaw.split(" ")[0]));
|
|
@@ -15,6 +24,8 @@ export async function collectSystem(): Promise<SystemInfo> {
|
|
|
15
24
|
hostname: hostname(),
|
|
16
25
|
ip,
|
|
17
26
|
os: osName,
|
|
27
|
+
...(os_id ? { os_id } : {}),
|
|
28
|
+
...(os_id_like ? { os_id_like } : {}),
|
|
18
29
|
kernel,
|
|
19
30
|
uptime_seconds: uptimeSeconds,
|
|
20
31
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,28 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { readFileSync } from "node:fs";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
import { dirname, join } from "node:path";
|
|
6
3
|
import { parseCliArgs } from "./cli.js";
|
|
4
|
+
import { CRUCIBLE_VERSION as PKG_VERSION } from "./lib/version.js";
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return pkg.version || "0.0.0";
|
|
13
|
-
} catch {
|
|
14
|
-
return "0.0.0";
|
|
15
|
-
}
|
|
16
|
-
})();
|
|
17
|
-
|
|
18
|
-
// Handle --version and --help before importing collectors, loading config, or
|
|
19
|
-
// starting the Prometheus server. This keeps the CLI responsive even on hosts
|
|
20
|
-
// missing the config file or external tools.
|
|
6
|
+
// Handle --version, --help, and planned-reboot subcommands before
|
|
7
|
+
// importing collectors, loading config, or starting the Prometheus
|
|
8
|
+
// server. Keeps the CLI responsive even on hosts missing the config
|
|
9
|
+
// file or external tools.
|
|
21
10
|
const { result: cliArgs, output: cliOutput } = parseCliArgs(process.argv.slice(2), PKG_VERSION);
|
|
22
|
-
if (cliArgs.mode
|
|
11
|
+
if (cliArgs.mode === "version" || cliArgs.mode === "help") {
|
|
23
12
|
console.log(cliOutput);
|
|
24
13
|
process.exit(0);
|
|
25
14
|
}
|
|
15
|
+
if (cliArgs.mode === "mark-reboot" || cliArgs.mode === "reboot") {
|
|
16
|
+
const { writeRebootMarker, parseDuration, DEFAULT_TTL_MS } = await import("./lib/reboot-marker.js");
|
|
17
|
+
const ttlMs = cliArgs.ttl ? parseDuration(cliArgs.ttl) : DEFAULT_TTL_MS;
|
|
18
|
+
if (ttlMs === null) {
|
|
19
|
+
console.error(`[mark-reboot] invalid --ttl value: ${cliArgs.ttl}. Use e.g. 10m, 2h, 600s.`);
|
|
20
|
+
process.exit(2);
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const { path, expires_at } = writeRebootMarker({
|
|
24
|
+
reason: cliArgs.reason, ttlMs,
|
|
25
|
+
});
|
|
26
|
+
console.log(`[${cliArgs.mode}] marker written: ${path} (expires ${expires_at}${cliArgs.reason ? `, reason: ${cliArgs.reason}` : ""})`);
|
|
27
|
+
} catch (err: any) {
|
|
28
|
+
console.error(`[${cliArgs.mode}] failed to write marker: ${err?.message || err}`);
|
|
29
|
+
console.error(` Most likely cause: need root privileges to write under /var/lib/crucible/.`);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
if (cliArgs.mode === "reboot") {
|
|
33
|
+
const { execFileSync } = await import("node:child_process");
|
|
34
|
+
console.log("[reboot] invoking systemctl reboot");
|
|
35
|
+
try {
|
|
36
|
+
execFileSync("systemctl", ["reboot"], { stdio: "inherit" });
|
|
37
|
+
} catch (err: any) {
|
|
38
|
+
console.error(`[reboot] systemctl reboot failed: ${err?.message || err}`);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
process.exit(0);
|
|
43
|
+
}
|
|
26
44
|
|
|
27
45
|
import { loadConfig } from "./config.js";
|
|
28
46
|
import { checkForUpdates } from "./lib/version-check.js";
|
|
@@ -51,6 +69,17 @@ import { collectSystemd } from "./collect/systemd.js";
|
|
|
51
69
|
import { collectNtp } from "./collect/ntp.js";
|
|
52
70
|
import { collectFileDescriptors } from "./collect/fd.js";
|
|
53
71
|
import type { Snapshot, IpmiInfo } from "./lib/types.js";
|
|
72
|
+
import { consumeRebootMarker, type PlannedReboot } from "./lib/reboot-marker.js";
|
|
73
|
+
|
|
74
|
+
// Consume the planned-reboot marker once at startup. If the operator ran
|
|
75
|
+
// `crucible-agent mark-reboot` / `reboot` before this boot, the marker
|
|
76
|
+
// exists, we flag it on the first snapshot, and we delete the file (so
|
|
77
|
+
// subsequent snapshots don't keep claiming the reboot was planned).
|
|
78
|
+
const plannedRebootFlag: PlannedReboot | null = consumeRebootMarker();
|
|
79
|
+
if (plannedRebootFlag) {
|
|
80
|
+
console.log(`[collector] Planned reboot acknowledged${plannedRebootFlag.reason ? `: ${plannedRebootFlag.reason}` : ""}`);
|
|
81
|
+
}
|
|
82
|
+
let plannedRebootConsumed = false;
|
|
54
83
|
|
|
55
84
|
const config = loadConfig(cliArgs.configPath);
|
|
56
85
|
|
|
@@ -106,6 +135,14 @@ async function collect() {
|
|
|
106
135
|
security: cachedSecurity,
|
|
107
136
|
};
|
|
108
137
|
|
|
138
|
+
// Single-shot: the very first snapshot after a marked reboot carries
|
|
139
|
+
// the flag, subsequent snapshots do not.
|
|
140
|
+
if (plannedRebootFlag && !plannedRebootConsumed) {
|
|
141
|
+
(snapshot as any).expected_reboot = true;
|
|
142
|
+
if (plannedRebootFlag.reason) (snapshot as any).expected_reboot_reason = plannedRebootFlag.reason;
|
|
143
|
+
plannedRebootConsumed = true;
|
|
144
|
+
}
|
|
145
|
+
|
|
109
146
|
// ZFS and I/O errors: collect every cycle (lightweight checks)
|
|
110
147
|
try { snapshot.zfs = await collectZfs() ?? undefined; } catch { /* skip if ZFS not available */ }
|
|
111
148
|
try { snapshot.io_errors = await collectIoErrors() ?? undefined; } catch { /* skip on error */ }
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Planned-reboot marker handling.
|
|
2
|
+
//
|
|
3
|
+
// An operator signals "the next reboot is expected, don't page me"
|
|
4
|
+
// by writing a short-lived JSON file to disk BEFORE rebooting. The
|
|
5
|
+
// collector reads and deletes it on agent startup; the first
|
|
6
|
+
// post-boot snapshot then carries `expected_reboot: true` so Forge's
|
|
7
|
+
// unexpected_reboot rule stays quiet.
|
|
8
|
+
//
|
|
9
|
+
// Single-use (deleted on read regardless of validity) and TTL-guarded
|
|
10
|
+
// (default 10 min) so a forgotten marker cannot silence a genuine
|
|
11
|
+
// crash reboot weeks later.
|
|
12
|
+
|
|
13
|
+
import { existsSync, readFileSync, unlinkSync, writeFileSync, mkdirSync, chmodSync } from "node:fs";
|
|
14
|
+
import { dirname } from "node:path";
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_MARKER_PATH = "/var/lib/crucible/reboot-expected";
|
|
17
|
+
export const DEFAULT_TTL_MS = 10 * 60 * 1000;
|
|
18
|
+
|
|
19
|
+
export interface PlannedReboot {
|
|
20
|
+
expected: true;
|
|
21
|
+
reason?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RebootMarker {
|
|
25
|
+
expires_at: string; // ISO timestamp
|
|
26
|
+
reason?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Read and delete the marker at `path`. Returns the resolved reboot flag
|
|
31
|
+
* if the file existed, was parseable JSON, and hasn't expired; otherwise
|
|
32
|
+
* returns null. The file is unlinked in every branch where it existed,
|
|
33
|
+
* so a malformed or stale marker is one-shot (can't linger).
|
|
34
|
+
*/
|
|
35
|
+
export function consumeRebootMarker(
|
|
36
|
+
path: string = DEFAULT_MARKER_PATH,
|
|
37
|
+
now: Date = new Date(),
|
|
38
|
+
): PlannedReboot | null {
|
|
39
|
+
if (!existsSync(path)) return null;
|
|
40
|
+
let raw: string;
|
|
41
|
+
try { raw = readFileSync(path, "utf-8"); } catch { try { unlinkSync(path); } catch {} return null; }
|
|
42
|
+
// Always delete after read, regardless of validity.
|
|
43
|
+
try { unlinkSync(path); } catch {}
|
|
44
|
+
|
|
45
|
+
let parsed: RebootMarker;
|
|
46
|
+
try { parsed = JSON.parse(raw); } catch { return null; }
|
|
47
|
+
if (!parsed || typeof parsed !== "object" || typeof parsed.expires_at !== "string") return null;
|
|
48
|
+
const expiresAt = new Date(parsed.expires_at);
|
|
49
|
+
if (isNaN(expiresAt.getTime())) return null;
|
|
50
|
+
if (expiresAt.getTime() <= now.getTime()) return null; // stale
|
|
51
|
+
return { expected: true, reason: parsed.reason };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Write a planned-reboot marker. Used by the `mark-reboot` and `reboot`
|
|
56
|
+
* CLI subcommands. `ttlMs` defaults to 10 minutes. Creates the parent
|
|
57
|
+
* directory if needed. Chmod 600 so other users on the host can't read
|
|
58
|
+
* or modify it.
|
|
59
|
+
*/
|
|
60
|
+
export function writeRebootMarker(opts: {
|
|
61
|
+
reason?: string;
|
|
62
|
+
ttlMs?: number;
|
|
63
|
+
path?: string;
|
|
64
|
+
now?: Date;
|
|
65
|
+
}): { path: string; expires_at: string } {
|
|
66
|
+
const path = opts.path ?? DEFAULT_MARKER_PATH;
|
|
67
|
+
const now = opts.now ?? new Date();
|
|
68
|
+
const ttlMs = opts.ttlMs ?? DEFAULT_TTL_MS;
|
|
69
|
+
const expiresAt = new Date(now.getTime() + ttlMs);
|
|
70
|
+
const body: RebootMarker = { expires_at: expiresAt.toISOString() };
|
|
71
|
+
if (opts.reason) body.reason = opts.reason;
|
|
72
|
+
try { mkdirSync(dirname(path), { recursive: true, mode: 0o700 }); } catch {}
|
|
73
|
+
writeFileSync(path, JSON.stringify(body), { mode: 0o600 });
|
|
74
|
+
try { chmodSync(path, 0o600); } catch {}
|
|
75
|
+
return { path, expires_at: body.expires_at };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Parse a duration like "10m", "2h", "600s" into milliseconds. Used by
|
|
79
|
+
* the CLI for the `--ttl` flag. */
|
|
80
|
+
export function parseDuration(s: string): number | null {
|
|
81
|
+
const m = /^(\d+)\s*(ms|s|m|h)?$/.exec(s.trim());
|
|
82
|
+
if (!m) return null;
|
|
83
|
+
const n = parseInt(m[1], 10);
|
|
84
|
+
if (!Number.isFinite(n) || n < 0) return null;
|
|
85
|
+
const unit = m[2] ?? "s";
|
|
86
|
+
const mult = unit === "ms" ? 1 : unit === "s" ? 1000 : unit === "m" ? 60_000 : 3_600_000;
|
|
87
|
+
return n * mult;
|
|
88
|
+
}
|
package/src/lib/types.ts
CHANGED
|
@@ -18,6 +18,12 @@ export interface Snapshot {
|
|
|
18
18
|
systemd?: SystemdData;
|
|
19
19
|
ntp?: NtpData;
|
|
20
20
|
file_descriptors?: FileDescriptorData;
|
|
21
|
+
// Planned-reboot flag: set only on the first snapshot after a reboot
|
|
22
|
+
// that was marked with `crucible-agent mark-reboot` / `reboot`. Forge
|
|
23
|
+
// reads this to suppress the `unexpected_reboot` rule. Single-use:
|
|
24
|
+
// subsequent snapshots don't carry it.
|
|
25
|
+
expected_reboot?: boolean;
|
|
26
|
+
expected_reboot_reason?: string;
|
|
21
27
|
}
|
|
22
28
|
|
|
23
29
|
export interface ConntrackData {
|
|
@@ -73,6 +79,12 @@ export interface SystemInfo {
|
|
|
73
79
|
hostname: string;
|
|
74
80
|
ip: string;
|
|
75
81
|
os: string;
|
|
82
|
+
/** `ID=` from /etc/os-release, lowercased. e.g. "ubuntu", "debian", "rocky", "arch", "alpine". */
|
|
83
|
+
os_id?: string;
|
|
84
|
+
/** `ID_LIKE=` from /etc/os-release, lowercased, space-separated. Used by Forge
|
|
85
|
+
* to pick distro-family-specific fix command variants. e.g. on Rocky this
|
|
86
|
+
* is "rhel centos fedora"; on Ubuntu it is "debian". */
|
|
87
|
+
os_id_like?: string;
|
|
76
88
|
kernel: string;
|
|
77
89
|
uptime_seconds: number;
|
|
78
90
|
}
|
package/src/lib/version-check.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { CRUCIBLE_VERSION as CURRENT_VERSION } from "./version.js";
|
|
2
|
+
|
|
2
3
|
let lastCheckTime = 0;
|
|
3
4
|
let lastResult: { updateAvailable: boolean; latest: string; changelog: string } | null = null;
|
|
4
5
|
const CHECK_INTERVAL = 6 * 60 * 60 * 1000; // check every 6 hours
|