getculpa 0.0.1 → 1.0.2
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/LICENSE +41 -0
- package/README.md +37 -18
- package/assets/culpa-collector.ps1 +137 -0
- package/assets/culpa-compose.yml +144 -0
- package/assets/install-culpa.ps1 +310 -0
- package/assets/launch-culpa.ps1 +350 -0
- package/assets/register.mjs +713 -0
- package/assets/uninstall-culpa.ps1 +227 -0
- package/bin/culpa.js +21 -10
- package/bin/getculpa.js +147 -0
- package/lib/assets.d.mts +2 -0
- package/lib/assets.mjs +36 -0
- package/lib/bootstrap.d.mts +25 -0
- package/lib/bootstrap.mjs +73 -0
- package/lib/docker.d.mts +76 -0
- package/lib/docker.mjs +270 -0
- package/lib/doctor.d.mts +29 -0
- package/lib/doctor.mjs +405 -0
- package/lib/fetch.d.mts +17 -0
- package/lib/fetch.mjs +211 -0
- package/lib/install-summary.d.mts +18 -0
- package/lib/install-summary.mjs +144 -0
- package/lib/paths.d.mts +9 -0
- package/lib/paths.mjs +61 -0
- package/lib/preflight.d.mts +39 -0
- package/lib/preflight.mjs +169 -0
- package/lib/provision.d.mts +54 -0
- package/lib/provision.mjs +318 -0
- package/lib/repair.d.mts +21 -0
- package/lib/repair.mjs +174 -0
- package/lib/start.d.mts +44 -0
- package/lib/start.mjs +479 -0
- package/lib/status.d.mts +23 -0
- package/lib/status.mjs +74 -0
- package/lib/stop.d.mts +15 -0
- package/lib/stop.mjs +46 -0
- package/lib/tty.d.mts +18 -0
- package/lib/tty.mjs +58 -0
- package/lib/uninstall.d.mts +17 -0
- package/lib/uninstall.mjs +99 -0
- package/package.json +14 -25
- package/scripts/install.js +154 -0
- package/scripts/prepack.js +41 -0
- package/index.js +0 -3
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// CF20-T3 — the defer-start provisioner. Lays down everything the Windows
|
|
2
|
+
// installer lays down; starts nothing (compose up, collector start, and a
|
|
3
|
+
// browser open are all launch-time work — CF20-T4).
|
|
4
|
+
//
|
|
5
|
+
// Docker interaction (presence, engine reachability, image pull) and the
|
|
6
|
+
// win32 installer delegation both take injectable dependencies, defaulting
|
|
7
|
+
// to the real ones. Tests MUST override them: this module runs on the
|
|
8
|
+
// founder's own dev machine, and the real win32 delegate writes to the real
|
|
9
|
+
// Desktop, Start Menu and HKCU — never acceptable as a side effect of a test
|
|
10
|
+
// run. Docker-command recording (for the "nothing started" fact) happens
|
|
11
|
+
// through the injected dockerSpawnSync rather than a PATH shim, for the same
|
|
12
|
+
// reason install-culpa.ps1 injects -Probe into Test-DockerInstalled: an
|
|
13
|
+
// explicit dependency is more reliable than relying on OS PATH resolution
|
|
14
|
+
// rules for a fake executable.
|
|
15
|
+
|
|
16
|
+
import { spawnSync as realSpawnSync } from "node:child_process";
|
|
17
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
import { fileURLToPath } from "node:url";
|
|
20
|
+
import { checkDockerEngineReachable, checkDockerPresent, classifyUpgrade, isIncomingComposeOlder } from "./preflight.mjs";
|
|
21
|
+
import { resolveAssetPath } from "./assets.mjs";
|
|
22
|
+
import { canonicalAppDir } from "./paths.mjs";
|
|
23
|
+
|
|
24
|
+
// T-CF29-8: where lib/fetch.mjs stages the vendored binaries. Same derivation
|
|
25
|
+
// lib/repair.mjs already uses, so the two agree on one vendor directory.
|
|
26
|
+
const packageRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
27
|
+
|
|
28
|
+
// The full parity file-set (Windows inventory: culpa-setup.iss:43-54, minus
|
|
29
|
+
// the collectord binary, which lib/fetch.mjs stages separately by triple).
|
|
30
|
+
// This is the WINDOWS inventory and stays the canonical full list — use
|
|
31
|
+
// parityAssetsFor(platform) to get what a given platform should actually
|
|
32
|
+
// receive.
|
|
33
|
+
export const PARITY_ASSETS = [
|
|
34
|
+
"culpa-compose.yml",
|
|
35
|
+
"install-culpa.ps1",
|
|
36
|
+
"launch-culpa.ps1",
|
|
37
|
+
"uninstall-culpa.ps1",
|
|
38
|
+
"culpa-collector.ps1",
|
|
39
|
+
"register.mjs",
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
// T-CF28-6 — four of the six are PowerShell scripts that only win32 can run:
|
|
43
|
+
// lib/start.mjs delegates to launch-culpa.ps1 only when platform === "win32"
|
|
44
|
+
// (start.mjs:457), lib/uninstall.mjs only delegates to uninstall-culpa.ps1 on
|
|
45
|
+
// win32 (uninstall.mjs:80-82), and install-culpa.ps1/culpa-collector.ps1 are
|
|
46
|
+
// invoked by the Windows installer path alone. Staging them on darwin/linux
|
|
47
|
+
// put four inert files in the user's app dir and made `getculpa doctor` grade
|
|
48
|
+
// them as part of a healthy install.
|
|
49
|
+
export const WINDOWS_ONLY_ASSETS = Object.freeze([
|
|
50
|
+
"install-culpa.ps1",
|
|
51
|
+
"launch-culpa.ps1",
|
|
52
|
+
"uninstall-culpa.ps1",
|
|
53
|
+
"culpa-collector.ps1",
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
// The single source of truth for "which parity files belong on this platform".
|
|
57
|
+
// provision(), repair() and doctor() ALL read it — if any one of them used the
|
|
58
|
+
// flat list instead, repair would restore what provision skipped, or doctor
|
|
59
|
+
// would fail an install for a file that is correctly absent.
|
|
60
|
+
export function parityAssetsFor(platform = process.platform) {
|
|
61
|
+
if (platform === "win32") return PARITY_ASSETS;
|
|
62
|
+
return PARITY_ASSETS.filter((name) => !WINDOWS_ONLY_ASSETS.includes(name));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// CF20-T6 — protects the SHIPPED culpa-compose.yml the same way
|
|
66
|
+
// stageLiveComposeIfAbsent already protects the live one, mirroring
|
|
67
|
+
// launch-culpa.ps1's QW-1 one-direction rule: an older incoming pin must
|
|
68
|
+
// never silently overwrite a newer one already staged (the
|
|
69
|
+
// downgrade-package case — npm delivered an older getculpa than what is
|
|
70
|
+
// already here). CULPA_ALLOW_DOWNGRADE=1 is the same escape hatch T4's
|
|
71
|
+
// start.mjs already honors for QW-3 at launch time.
|
|
72
|
+
function stageShippedCompose(appDir, env) {
|
|
73
|
+
const src = resolveAssetPath("culpa-compose.yml");
|
|
74
|
+
const dest = path.join(appDir, "culpa-compose.yml");
|
|
75
|
+
const allowDowngrade = env.CULPA_ALLOW_DOWNGRADE === "1";
|
|
76
|
+
if (existsSync(dest) && !allowDowngrade) {
|
|
77
|
+
const incoming = readFileSync(src, "utf8");
|
|
78
|
+
const staged = readFileSync(dest, "utf8");
|
|
79
|
+
if (isIncomingComposeOlder(incoming, staged)) {
|
|
80
|
+
console.warn(
|
|
81
|
+
"getculpa: the incoming package's culpa-compose.yml pin is older than the one already staged - skipped (set CULPA_ALLOW_DOWNGRADE=1 to override).",
|
|
82
|
+
);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
copyFileSync(src, dest);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// CF20-T5 (repair): exported so lib/repair.mjs can re-stage the parity
|
|
90
|
+
// file-set and (only-if-absent) the live compose without duplicating this
|
|
91
|
+
// logic — same reason the task instructions call out reuse over a second
|
|
92
|
+
// copy. Purely additive: behavior and call sites inside this file are
|
|
93
|
+
// unchanged.
|
|
94
|
+
export function stageParityFiles(appDir, env = process.env, platform = process.platform) {
|
|
95
|
+
mkdirSync(appDir, { recursive: true });
|
|
96
|
+
stageShippedCompose(appDir, env);
|
|
97
|
+
for (const name of parityAssetsFor(platform)) {
|
|
98
|
+
if (name === "culpa-compose.yml") continue;
|
|
99
|
+
copyFileSync(resolveAssetPath(name), path.join(appDir, name));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The live compose is staged ONLY when absent. An existing live compose
|
|
104
|
+
// belongs to launch-time's QW-1 sync (launch-culpa.ps1) — provisioning must
|
|
105
|
+
// never re-pin a running install out from under it.
|
|
106
|
+
export function stageLiveComposeIfAbsent(appDir) {
|
|
107
|
+
const live = path.join(appDir, "docker-compose.yml");
|
|
108
|
+
if (existsSync(live)) return false;
|
|
109
|
+
copyFileSync(path.join(appDir, "culpa-compose.yml"), live);
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// T-CF29-8 — the name culpa-collector.ps1:35 reads from the app dir, and the
|
|
114
|
+
// same name culpa-setup.iss:52 installs there. NOT the Rust target triple
|
|
115
|
+
// lib/fetch.mjs uses for the release asset (x86_64-pc-windows-msvc): this is
|
|
116
|
+
// the Node-style platform-arch pair.
|
|
117
|
+
export function collectorBinaryName(platform = process.platform, arch = process.arch) {
|
|
118
|
+
const exe = platform === "win32" ? ".exe" : "";
|
|
119
|
+
return `culpa-collectord-${platform}-${arch}${exe}`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// T-CF29-8 — capture was silently OFF on every Windows npm install:
|
|
123
|
+
// "culpa-collectord-win32-x64.exe not present in this package - skipping
|
|
124
|
+
// capture setup". lib/fetch.mjs stages the binary into the PACKAGE's vendor/
|
|
125
|
+
// as culpa-collectord.exe, the .ps1 reads it from the APP DIR under the
|
|
126
|
+
// triple-suffixed name, and nothing bridged the two. The Windows installer
|
|
127
|
+
// path has always put it in the app dir, so this is the npm path catching up
|
|
128
|
+
// to installer parity, not a new convention.
|
|
129
|
+
//
|
|
130
|
+
// Returns whether the binary is really there afterwards — never a hopeful
|
|
131
|
+
// echo of the caller's collectorFetched flag, because "capture is on" is
|
|
132
|
+
// exactly the kind of claim that must be backed by a file on disk.
|
|
133
|
+
export function stageCollectorBinary({ appDir, vendorDir, platform, arch }) {
|
|
134
|
+
const exe = platform === "win32" ? ".exe" : "";
|
|
135
|
+
const src = path.join(vendorDir, `culpa-collectord${exe}`);
|
|
136
|
+
if (!existsSync(src)) return false;
|
|
137
|
+
const dest = path.join(appDir, collectorBinaryName(platform, arch));
|
|
138
|
+
try {
|
|
139
|
+
copyFileSync(src, dest);
|
|
140
|
+
} catch (e) {
|
|
141
|
+
console.warn(`getculpa: capture collector could not be staged (${e.message}) - capture OFF, everything else works.`);
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return existsSync(dest);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function detectDockerState(dockerSpawnSync) {
|
|
148
|
+
if (!checkDockerPresent(dockerSpawnSync)) return "missing";
|
|
149
|
+
if (!checkDockerEngineReachable(dockerSpawnSync)) return "installed-not-running";
|
|
150
|
+
return "ready";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Staging only — `pull`, never `up`. This is the defer invariant's other
|
|
154
|
+
// half: even with a reachable engine, provisioning must not start anything.
|
|
155
|
+
function stageImages(appDir, dockerSpawnSync) {
|
|
156
|
+
const shipped = path.join(appDir, "culpa-compose.yml");
|
|
157
|
+
const result = dockerSpawnSync("docker", ["compose", "-f", shipped, "-p", "culpa", "pull"], {
|
|
158
|
+
encoding: "utf8",
|
|
159
|
+
});
|
|
160
|
+
return result.status === 0;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Real win32 delegate: -DeferStart -NonInteractive is what makes this safe
|
|
164
|
+
// to run unattended from postinstall (no dialogs, no engine start, no
|
|
165
|
+
// compose up, no browser) while still producing the same shortcuts + HKCU
|
|
166
|
+
// registration the Windows installer produces.
|
|
167
|
+
function defaultDelegateWindowsInstaller(appDir) {
|
|
168
|
+
const script = path.join(appDir, "install-culpa.ps1");
|
|
169
|
+
const result = realSpawnSync(
|
|
170
|
+
"powershell.exe",
|
|
171
|
+
["-NoProfile", "-ExecutionPolicy", "Bypass", "-File", script, "-DeferStart", "-NonInteractive"],
|
|
172
|
+
{ stdio: "inherit" },
|
|
173
|
+
);
|
|
174
|
+
return result.status === 0;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function writeState(appDir, state) {
|
|
178
|
+
writeFileSync(path.join(appDir, "install-state.json"), `${JSON.stringify(state, null, 2)}\n`);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function readExistingState(appDir) {
|
|
182
|
+
try {
|
|
183
|
+
return JSON.parse(readFileSync(path.join(appDir, "install-state.json"), "utf8"));
|
|
184
|
+
} catch {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// CF20-T6 — the honest, human-facing line for each classifyUpgrade outcome.
|
|
190
|
+
// "upgrade" is explicit that the new version is STAGED, not running yet —
|
|
191
|
+
// it applies at the next `getculpa` (start.mjs's QW-1-equivalent sync + T4's
|
|
192
|
+
// launch orchestration), never here. Postinstall stays silent about
|
|
193
|
+
// migrations by design: those are QW-4's job at that next `getculpa`, not
|
|
194
|
+
// this one.
|
|
195
|
+
function describeClassification(classification, previousVersion, currentVersion) {
|
|
196
|
+
switch (classification) {
|
|
197
|
+
case "fresh":
|
|
198
|
+
return `getculpa: installing Culpa ${currentVersion}.`;
|
|
199
|
+
case "upgrade":
|
|
200
|
+
return `getculpa: upgrading Culpa ${previousVersion} -> ${currentVersion} (staged; applies at next \`getculpa\`).`;
|
|
201
|
+
case "same-version":
|
|
202
|
+
return `getculpa: repairing the existing Culpa ${currentVersion} installation (same version reinstalled).`;
|
|
203
|
+
case "downgrade-package":
|
|
204
|
+
return (
|
|
205
|
+
`getculpa: this package (${currentVersion}) is older than the installed Culpa ${previousVersion} - ` +
|
|
206
|
+
"refusing to downgrade (set CULPA_ALLOW_DOWNGRADE=1 to override)."
|
|
207
|
+
);
|
|
208
|
+
case "repair":
|
|
209
|
+
default:
|
|
210
|
+
return "getculpa: repairing the existing Culpa installation.";
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export async function provision(opts) {
|
|
215
|
+
const {
|
|
216
|
+
appDir,
|
|
217
|
+
currentVersion,
|
|
218
|
+
collectorFetched = false,
|
|
219
|
+
platform = process.platform,
|
|
220
|
+
arch = process.arch,
|
|
221
|
+
vendorDir = path.join(packageRoot, "vendor"),
|
|
222
|
+
dockerSpawnSync = realSpawnSync,
|
|
223
|
+
delegateWindowsInstaller = defaultDelegateWindowsInstaller,
|
|
224
|
+
env = process.env,
|
|
225
|
+
} = opts;
|
|
226
|
+
|
|
227
|
+
// Classify + capture the pre-existing state BEFORE anything below mutates
|
|
228
|
+
// appDir — both read the install-state.json / live compose that staging
|
|
229
|
+
// is about to overwrite.
|
|
230
|
+
const { classification, previousVersion } = classifyUpgrade({ appDir, currentVersion });
|
|
231
|
+
console.log(describeClassification(classification, previousVersion, currentVersion));
|
|
232
|
+
const existingState = readExistingState(appDir);
|
|
233
|
+
|
|
234
|
+
// CF20-R: a REFUSED downgrade (classification === "downgrade-package",
|
|
235
|
+
// CULPA_ALLOW_DOWNGRADE not set) used to still restage every OTHER parity
|
|
236
|
+
// file (the ps1 scripts, register.mjs) with the older package's copies —
|
|
237
|
+
// only culpa-compose.yml was protected (stageShippedCompose's own guard).
|
|
238
|
+
// That left a mixed-version app dir: a newer compose pin beside older
|
|
239
|
+
// scripts. Skip the WHOLE parity re-stage in this case instead, so the app
|
|
240
|
+
// dir stays wholly at the newer version; dir/state handling below is
|
|
241
|
+
// unaffected.
|
|
242
|
+
const allowDowngrade = env.CULPA_ALLOW_DOWNGRADE === "1";
|
|
243
|
+
const refusedDowngrade = classification === "downgrade-package" && !allowDowngrade;
|
|
244
|
+
if (refusedDowngrade) {
|
|
245
|
+
console.warn(
|
|
246
|
+
`getculpa: this package is older than the installed Culpa ${previousVersion} - skipping the parity file re-stage entirely (compose, scripts, register.mjs) to avoid a mixed-version app dir (set CULPA_ALLOW_DOWNGRADE=1 to override).`,
|
|
247
|
+
);
|
|
248
|
+
} else {
|
|
249
|
+
stageParityFiles(appDir, env, platform);
|
|
250
|
+
}
|
|
251
|
+
stageLiveComposeIfAbsent(appDir);
|
|
252
|
+
|
|
253
|
+
// T-CF29-8: only attempt it when the fetch reported success; the result is
|
|
254
|
+
// what gets recorded, not the intent.
|
|
255
|
+
const collectorStaged = collectorFetched && stageCollectorBinary({ appDir, vendorDir, platform, arch });
|
|
256
|
+
if (collectorFetched && !collectorStaged) {
|
|
257
|
+
console.warn(
|
|
258
|
+
"getculpa: the capture collector was downloaded but could not be placed in the app directory - capture OFF, everything else works.",
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const dockerState = detectDockerState(dockerSpawnSync);
|
|
263
|
+
let imagesStaged = false;
|
|
264
|
+
if (dockerState === "ready") {
|
|
265
|
+
imagesStaged = stageImages(appDir, dockerSpawnSync);
|
|
266
|
+
if (!imagesStaged) {
|
|
267
|
+
console.warn("getculpa: image staging failed - images will be pulled at first `getculpa`.");
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
const why = dockerState === "missing" ? "Docker was not found" : "Docker is not running";
|
|
271
|
+
console.warn(`getculpa: ${why} - image staging deferred to first \`getculpa\`.`);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (platform === "win32") {
|
|
275
|
+
// CF20-R: the Windows delegate registers the ONE fixed, per-machine HKCU
|
|
276
|
+
// uninstall key (install-culpa.ps1 / culpa-setup.iss's AppId). A second
|
|
277
|
+
// app dir on the same machine (CULPA_APP_DIR pointed somewhere
|
|
278
|
+
// non-default) must never overwrite that shared key — the gate's
|
|
279
|
+
// historical incident class. Only delegate when this appDir IS the
|
|
280
|
+
// canonical one (CULPA_APP_DIR's override stripped); otherwise skip
|
|
281
|
+
// registration and say so, once, honestly.
|
|
282
|
+
if (appDir === canonicalAppDir({ platform, env })) {
|
|
283
|
+
const ok = delegateWindowsInstaller(appDir);
|
|
284
|
+
if (!ok) {
|
|
285
|
+
console.warn("getculpa: Windows shortcut/registry setup did not complete cleanly - shortcuts may be missing.");
|
|
286
|
+
}
|
|
287
|
+
} else {
|
|
288
|
+
console.log("getculpa: non-canonical app dir: shortcuts/registry registration skipped.");
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// T6 review (Important): a REFUSED downgrade must not record the rejected,
|
|
293
|
+
// older currentVersion as installed — the protected artifacts still carry
|
|
294
|
+
// the newer version, and this field is the ground truth the next run's
|
|
295
|
+
// classification reads. Persist the true installed version instead.
|
|
296
|
+
const recordedPackageVersion =
|
|
297
|
+
classification === "downgrade-package" && previousVersion ? previousVersion : currentVersion;
|
|
298
|
+
writeState(appDir, {
|
|
299
|
+
packageVersion: recordedPackageVersion,
|
|
300
|
+
previousVersion,
|
|
301
|
+
classification,
|
|
302
|
+
// provisionedAt is the FIRST install's timestamp, preserved across every
|
|
303
|
+
// subsequent provision() run; updatedAt is this run's.
|
|
304
|
+
provisionedAt: existingState?.provisionedAt ?? new Date().toISOString(),
|
|
305
|
+
updatedAt: new Date().toISOString(),
|
|
306
|
+
dockerState,
|
|
307
|
+
imagesStaged,
|
|
308
|
+
collectorStaged,
|
|
309
|
+
platform,
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
// T-CF28-5/7: the Docker and staging facts were already computed above and
|
|
313
|
+
// written to install-state.json, but the caller that has to REPORT them
|
|
314
|
+
// (scripts/install.js) could not see them — which is how a Docker-less
|
|
315
|
+
// install still printed "Culpa installed successfully." Returning them
|
|
316
|
+
// changes no behavior here; it just stops postinstall from having to guess.
|
|
317
|
+
return { classification, previousVersion, dockerState, imagesStaged, collectorStaged };
|
|
318
|
+
}
|
package/lib/repair.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { SpawnSyncLike } from "./docker.d.mts";
|
|
2
|
+
import type { FetchResult } from "./fetch.d.mts";
|
|
3
|
+
|
|
4
|
+
export interface RepairOptions {
|
|
5
|
+
appDir: string;
|
|
6
|
+
currentVersion: string;
|
|
7
|
+
platform?: string;
|
|
8
|
+
arch?: string;
|
|
9
|
+
vendorDir?: string;
|
|
10
|
+
dockerSpawnSync?: SpawnSyncLike;
|
|
11
|
+
fetchLauncherFn?: (opts?: { vendorDir?: string }) => Promise<FetchResult>;
|
|
12
|
+
fetchCollectordFn?: (opts?: { vendorDir?: string }) => Promise<FetchResult>;
|
|
13
|
+
log?: (msg: string) => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface RepairResult {
|
|
17
|
+
ok: boolean;
|
|
18
|
+
actions: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function repair(opts: RepairOptions): Promise<RepairResult>;
|
package/lib/repair.mjs
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// CF20-T5 — `getculpa repair`. Re-stages missing/corrupt parity files from
|
|
2
|
+
// the packaged assets, regenerates install-state.json if unreadable,
|
|
3
|
+
// re-fetches a missing launcher/collectord, and does none of this
|
|
4
|
+
// destructively: an EXISTING docker-compose.yml (the live pin) is never
|
|
5
|
+
// touched (the edcf97f invariant this task's reviewer condition names), no
|
|
6
|
+
// `docker volume` command is ever issued (data is never touched), and a
|
|
7
|
+
// second run changes nothing that was already healthy (idempotent).
|
|
8
|
+
//
|
|
9
|
+
// Reuses lib/provision.mjs's own stageParityFiles/stageLiveComposeIfAbsent
|
|
10
|
+
// (exported additively for this purpose — see that file's comment) instead
|
|
11
|
+
// of re-implementing file staging here.
|
|
12
|
+
|
|
13
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import { spawnSync as realSpawnSync } from "node:child_process";
|
|
17
|
+
import {
|
|
18
|
+
collectorBinaryName,
|
|
19
|
+
parityAssetsFor,
|
|
20
|
+
stageCollectorBinary,
|
|
21
|
+
stageLiveComposeIfAbsent,
|
|
22
|
+
stageParityFiles,
|
|
23
|
+
} from "./provision.mjs";
|
|
24
|
+
import { fetchCollectord, fetchLauncher } from "./fetch.mjs";
|
|
25
|
+
import { checkDockerEngineReachable, checkDockerPresent } from "./preflight.mjs";
|
|
26
|
+
|
|
27
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
const packageRoot = path.join(__dirname, "..");
|
|
29
|
+
|
|
30
|
+
function readJsonSafe(filePath) {
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(readFileSync(filePath, "utf8"));
|
|
33
|
+
} catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function detectDockerState(dockerSpawnSync) {
|
|
39
|
+
if (!checkDockerPresent(dockerSpawnSync)) return "missing";
|
|
40
|
+
if (!checkDockerEngineReachable(dockerSpawnSync)) return "installed-not-running";
|
|
41
|
+
return "ready";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function vendorBinaryPath(vendorDir, name, platform) {
|
|
45
|
+
const exe = platform === "win32" ? ".exe" : "";
|
|
46
|
+
return path.join(vendorDir, `${name}${exe}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function repair(opts) {
|
|
50
|
+
const {
|
|
51
|
+
appDir,
|
|
52
|
+
currentVersion,
|
|
53
|
+
platform = process.platform,
|
|
54
|
+
arch = process.arch,
|
|
55
|
+
vendorDir = path.join(packageRoot, "vendor"),
|
|
56
|
+
dockerSpawnSync = realSpawnSync,
|
|
57
|
+
fetchLauncherFn = fetchLauncher,
|
|
58
|
+
fetchCollectordFn = fetchCollectord,
|
|
59
|
+
log = console.log,
|
|
60
|
+
} = opts;
|
|
61
|
+
|
|
62
|
+
const actions = [];
|
|
63
|
+
|
|
64
|
+
if (!existsSync(appDir)) {
|
|
65
|
+
mkdirSync(appDir, { recursive: true });
|
|
66
|
+
actions.push(`created the missing app directory (${appDir})`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 1. Parity files — re-stage anything missing. stageParityFiles never
|
|
70
|
+
// touches docker-compose.yml (it doesn't reference that name at all),
|
|
71
|
+
// so this step alone already satisfies "never touch a live compose".
|
|
72
|
+
// T-CF28-6: platform-scoped, or repair would restore the four Windows .ps1
|
|
73
|
+
// files provisioning deliberately skips on darwin/linux — and report each
|
|
74
|
+
// one as a "restored missing parity file" action while doing it.
|
|
75
|
+
const missingBefore = parityAssetsFor(platform).filter((name) => !existsSync(path.join(appDir, name)));
|
|
76
|
+
stageParityFiles(appDir, process.env, platform);
|
|
77
|
+
for (const name of missingBefore) {
|
|
78
|
+
actions.push(`restored missing parity file: ${name}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 2. Live compose — created ONLY if entirely absent; an existing one is
|
|
82
|
+
// never overwritten (stageLiveComposeIfAbsent's own contract).
|
|
83
|
+
const composeCreated = stageLiveComposeIfAbsent(appDir);
|
|
84
|
+
if (composeCreated) {
|
|
85
|
+
actions.push("live docker-compose.yml was missing - recreated from the shipped culpa-compose.yml");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 3. install-state.json — regenerate only when missing/corrupt. The
|
|
89
|
+
// regenerated state is a conservative snapshot of what is actually on
|
|
90
|
+
// disk right now: dockerState/collectorStaged are re-detected,
|
|
91
|
+
// imagesStaged is set false (safe default — worst case is one extra
|
|
92
|
+
// `docker compose pull` message at the next `getculpa`, never data
|
|
93
|
+
// loss, mirroring deferredPullCatchUp's own contract in lib/start.mjs).
|
|
94
|
+
const existingState = readJsonSafe(path.join(appDir, "install-state.json"));
|
|
95
|
+
if (!existingState) {
|
|
96
|
+
const dockerState = detectDockerState(dockerSpawnSync);
|
|
97
|
+
// Review of 0e4b2e2: this derived collectorStaged from the PACKAGE
|
|
98
|
+
// vendor dir, which is not what culpa-collector.ps1:35 reads. It could
|
|
99
|
+
// therefore regenerate install-state.json saying capture was staged
|
|
100
|
+
// while the app dir held no collector at all — the same false positive
|
|
101
|
+
// T-CF29-8 removed from provision(). One invariant, both writers.
|
|
102
|
+
const collectorStaged = existsSync(path.join(appDir, collectorBinaryName(platform, arch)));
|
|
103
|
+
const regenerated = {
|
|
104
|
+
packageVersion: currentVersion,
|
|
105
|
+
provisionedAt: new Date().toISOString(),
|
|
106
|
+
dockerState,
|
|
107
|
+
imagesStaged: false,
|
|
108
|
+
collectorStaged,
|
|
109
|
+
platform,
|
|
110
|
+
};
|
|
111
|
+
writeFileSync(path.join(appDir, "install-state.json"), `${JSON.stringify(regenerated, null, 2)}\n`);
|
|
112
|
+
actions.push("install-state.json was missing or unreadable - regenerated");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 4. Launcher / collectord — re-fetch only when the vendored binary is
|
|
116
|
+
// genuinely absent. Never re-fetches when present (idempotent, and the
|
|
117
|
+
// task's explicit invocation-recording fact). The launcher is REQUIRED
|
|
118
|
+
// (doctor.mjs's checkVendoredLauncher fails the same way when it's
|
|
119
|
+
// missing) — a failed re-fetch must make repair's own result say so,
|
|
120
|
+
// not just log and report ok:true (CF20-R review). The collector stays
|
|
121
|
+
// optional/non-fatal: capture-off is a valid degraded state.
|
|
122
|
+
const launcherPath = vendorBinaryPath(vendorDir, "culpa-launcher", platform);
|
|
123
|
+
if (!existsSync(launcherPath)) {
|
|
124
|
+
try {
|
|
125
|
+
await fetchLauncherFn({ vendorDir });
|
|
126
|
+
actions.push("the launcher was missing - re-fetched and verified");
|
|
127
|
+
} catch (e) {
|
|
128
|
+
log(`getculpa repair: could not re-fetch the launcher (${e.message}). Run \`getculpa repair\` again once online.`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const collectordPath = vendorBinaryPath(vendorDir, "culpa-collectord", platform);
|
|
133
|
+
if (!existsSync(collectordPath)) {
|
|
134
|
+
try {
|
|
135
|
+
await fetchCollectordFn({ vendorDir });
|
|
136
|
+
actions.push("the capture collector was missing - re-fetched and verified");
|
|
137
|
+
} catch (e) {
|
|
138
|
+
// fetchCollectord's own contract: "not published for this release yet"
|
|
139
|
+
// is an expected, non-fatal outcome (see lib/fetch.mjs) — repair must
|
|
140
|
+
// degrade the same way, never abort over an optional component.
|
|
141
|
+
log(`getculpa repair: capture collector not re-fetched (${e.message}).`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Review of 0e4b2e2: re-fetching only refilled the PACKAGE vendor dir, so
|
|
146
|
+
// "re-fetched and verified" claimed a repair that left capture exactly as
|
|
147
|
+
// broken as it found it. The app-dir copy is the one that matters.
|
|
148
|
+
const appCollector = path.join(appDir, collectorBinaryName(platform, arch));
|
|
149
|
+
if (!existsSync(appCollector) && stageCollectorBinary({ appDir, vendorDir, platform, arch })) {
|
|
150
|
+
actions.push("the capture collector was missing from the app directory - staged it");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// The state file may have been regenerated BEFORE the staging above, so
|
|
154
|
+
// correct it to what is now true rather than leaving a stale answer.
|
|
155
|
+
const statePath = path.join(appDir, "install-state.json");
|
|
156
|
+
const finalState = readJsonSafe(statePath);
|
|
157
|
+
const collectorTruth = existsSync(appCollector);
|
|
158
|
+
// Review of 4e45f81: this used to correct the file and say NOTHING, so a
|
|
159
|
+
// run that really did change something reported "nothing to do - this
|
|
160
|
+
// install is already healthy". Same class of false claim the rest of this
|
|
161
|
+
// commit removes, just relocated into the action log.
|
|
162
|
+
if (finalState && finalState.collectorStaged !== collectorTruth) {
|
|
163
|
+
actions.push(
|
|
164
|
+
`install-state.json claimed capture was ${finalState.collectorStaged ? "staged" : "absent"} - corrected to match the app directory`,
|
|
165
|
+
);
|
|
166
|
+
writeFileSync(statePath, `${JSON.stringify({ ...finalState, collectorStaged: collectorTruth }, null, 2)}\n`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (actions.length === 0) log("getculpa repair: nothing to do - this install is already healthy.");
|
|
170
|
+
else for (const a of actions) log(`getculpa repair: ${a}`);
|
|
171
|
+
|
|
172
|
+
const launcherOk = existsSync(launcherPath);
|
|
173
|
+
return { ok: launcherOk, actions };
|
|
174
|
+
}
|
package/lib/start.d.mts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { SpawnSyncLike, FetchLike } from "./docker.d.mts";
|
|
2
|
+
|
|
3
|
+
export const STAGES: {
|
|
4
|
+
readonly CHECK_INSTALLATION: "CHECK_INSTALLATION";
|
|
5
|
+
readonly CHECK_DOCKER: "CHECK_DOCKER";
|
|
6
|
+
readonly IDEMPOTENCY_CHECK: "IDEMPOTENCY_CHECK";
|
|
7
|
+
readonly PORT_CHECK: "PORT_CHECK";
|
|
8
|
+
readonly START_DOCKER_IF_REQUIRED: "START_DOCKER_IF_REQUIRED";
|
|
9
|
+
readonly WAIT_FOR_DOCKER: "WAIT_FOR_DOCKER";
|
|
10
|
+
readonly PREPARE_RUNTIME: "PREPARE_RUNTIME";
|
|
11
|
+
readonly START_CULPA_DEPENDENCIES: "START_CULPA_DEPENDENCIES";
|
|
12
|
+
readonly RUN_REQUIRED_MIGRATIONS: "RUN_REQUIRED_MIGRATIONS";
|
|
13
|
+
readonly HEALTH_CHECK: "HEALTH_CHECK";
|
|
14
|
+
readonly START_CULPA: "START_CULPA";
|
|
15
|
+
readonly OPEN_UI: "OPEN_UI";
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export interface StartOptions {
|
|
19
|
+
appDir: string;
|
|
20
|
+
currentVersion?: string;
|
|
21
|
+
platform?: string;
|
|
22
|
+
spawnSync?: SpawnSyncLike;
|
|
23
|
+
fetchFn?: FetchLike;
|
|
24
|
+
sleepFn?: (ms: number) => Promise<void>;
|
|
25
|
+
env?: Record<string, string | undefined>;
|
|
26
|
+
log?: (msg: string) => void;
|
|
27
|
+
warn?: (msg: string) => void;
|
|
28
|
+
openBrowser?: (url: string) => void;
|
|
29
|
+
isTTY?: boolean;
|
|
30
|
+
delegateWindowsLaunch?: (appDir: string) => { ok: boolean; output?: string };
|
|
31
|
+
isInteractive?: boolean;
|
|
32
|
+
askYesNo?: (question: string) => Promise<boolean>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface StartResult {
|
|
36
|
+
ok: boolean;
|
|
37
|
+
stage: string;
|
|
38
|
+
message?: string;
|
|
39
|
+
recovery?: string;
|
|
40
|
+
alreadyRunning?: boolean;
|
|
41
|
+
stagesRun: string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function start(opts: StartOptions): Promise<StartResult>;
|