overvues 0.0.1 → 0.4.10
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 +58 -2
- package/dist/ansi.js +24 -0
- package/dist/attach-client.js +586 -0
- package/dist/auth-connect-browser.js +112 -0
- package/dist/auth-connect.js +306 -0
- package/dist/browser-bootstrap.js +190 -0
- package/dist/browser-loopback.js +289 -0
- package/dist/build-identity.js +6 -0
- package/dist/codex-reviewer-apparmor.json +4 -0
- package/dist/codex-reviewer-seccomp.json +1238 -0
- package/dist/codex-runtime-protocol.js +1 -0
- package/dist/codex-subscription.js +53 -0
- package/dist/config.js +297 -0
- package/dist/connect-comparison-code.js +26 -0
- package/dist/development-runner.js +109 -0
- package/dist/dns-doctor.js +390 -0
- package/dist/docker-names.js +47 -0
- package/dist/doctor.js +114 -0
- package/dist/gadget-job.js +804 -0
- package/dist/health-check.js +112 -0
- package/dist/help.js +71 -0
- package/dist/host-auth.js +22 -0
- package/dist/host-diagnostics.js +20 -0
- package/dist/host-runtime-identity.js +39 -0
- package/dist/host-runtime-metadata.js +56 -0
- package/dist/host-service.js +377 -0
- package/dist/host-stack.js +484 -0
- package/dist/host-supervisor.js +237 -0
- package/dist/host-update.js +387 -0
- package/dist/hub-address.js +100 -0
- package/dist/hub-client.js +459 -0
- package/dist/hub-kind.js +88 -0
- package/dist/identity.js +73 -0
- package/dist/index.js +185 -0
- package/dist/init.js +295 -0
- package/dist/isolated-run-cache.js +116 -0
- package/dist/local-stack.js +685 -0
- package/dist/machine-capacity.js +128 -0
- package/dist/managed-runner-image.js +13 -0
- package/dist/os-acted-record.js +49 -0
- package/dist/os-adapter.js +8190 -0
- package/dist/os-command.js +292 -0
- package/dist/os-handoff.js +274 -0
- package/dist/os-keychain.js +247 -0
- package/dist/os-machine.js +818 -0
- package/dist/os-owner.js +61 -0
- package/dist/os-private-file.js +98 -0
- package/dist/os-protocol.js +468 -0
- package/dist/os-session-store.js +133 -0
- package/dist/os-status.js +102 -0
- package/dist/picker.js +863 -0
- package/dist/portable-workspace-capability.js +60 -0
- package/dist/project-environment-capability.js +62 -0
- package/dist/project-environment.js +28 -0
- package/dist/project-preparation.js +134 -0
- package/dist/readiness.js +28 -0
- package/dist/redaction.js +27 -0
- package/dist/reviewer-isolation-capability.js +61 -0
- package/dist/runner-api-auth.js +80 -0
- package/dist/runner-command-fence.js +536 -0
- package/dist/runner-inspection.js +57 -0
- package/dist/runner-inventory.js +587 -0
- package/dist/runner-server.js +2520 -0
- package/dist/runner-session.js +470 -0
- package/dist/runner-slug-folder.js +39 -0
- package/dist/runner-slug.js +40 -0
- package/dist/setup.js +139 -0
- package/dist/shutdown.js +102 -0
- package/dist/splash.js +65 -0
- package/dist/staged-workspaces.js +441 -0
- package/dist/staging-disk.js +97 -0
- package/dist/terminal-lane.js +339 -0
- package/dist/terminal-recovery.js +502 -0
- package/licenses/capnweb.txt +23 -0
- package/licenses/moby-profiles.txt +212 -0
- package/licenses/ws.txt +22 -0
- package/package.json +28 -7
- package/cli.js +0 -2
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { hostname } from "node:os";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { ansi } from "./ansi.js";
|
|
6
|
+
import { HUB_URL, LOCAL_STATE_DIR, MACHINE_ID, hubControlHeaders, ensureHostAuth, isMachineUuid, loadConfig, reloadConfig, savedCredentialsAreFor, writeConfig, } from "./config.js";
|
|
7
|
+
import { hubParentDomainsText, isAllowedHubUrl } from "./hub-address.js";
|
|
8
|
+
import { runBrowserBootstrap } from "./browser-bootstrap.js";
|
|
9
|
+
import { fetchHubSetupStatus } from "./codex-subscription.js";
|
|
10
|
+
import { readSetupStatusWithValidatedCredential } from "./host-auth.js";
|
|
11
|
+
import { collectHubHostChecks } from "./host-diagnostics.js";
|
|
12
|
+
import { selectHostSupervisor, } from "./host-supervisor.js";
|
|
13
|
+
import { dockerInstalled, dockerReady, getLocalStackStatus, localImageExists, prepareLocalRunnerImage, startHostRunner, stopHostRunner, } from "./local-stack.js";
|
|
14
|
+
import { hasWarnings, printCheckReport } from "./readiness.js";
|
|
15
|
+
import { collectSetupChecks } from "./setup.js";
|
|
16
|
+
import { fetchHostUpdateCheck, resolveHostUpdateTargetImage, } from "./host-runtime-metadata.js";
|
|
17
|
+
import { CLI } from "./identity.js";
|
|
18
|
+
import { credentialHeaders, hubKindAtStart, requireHubKind } from "./hub-kind.js";
|
|
19
|
+
const HOST_PID_PATH = resolve(LOCAL_STATE_DIR, "host.pid.json");
|
|
20
|
+
const HOST_SHUTDOWN_DEADLINE_MS = 10_000;
|
|
21
|
+
function hostLog(message) {
|
|
22
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} ${message}\n`);
|
|
23
|
+
}
|
|
24
|
+
function readHostPidRecord() {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(readFileSync(HOST_PID_PATH, "utf-8"));
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function readActiveHostPidRecord() {
|
|
33
|
+
const record = readHostPidRecord();
|
|
34
|
+
if (!record?.pid || !processIsRunning(record.pid)) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return record;
|
|
38
|
+
}
|
|
39
|
+
function writeHostPidRecord(pid) {
|
|
40
|
+
mkdirSync(LOCAL_STATE_DIR, { recursive: true });
|
|
41
|
+
writeFileSync(HOST_PID_PATH, JSON.stringify({ pid, startedAt: new Date().toISOString() }, null, 2));
|
|
42
|
+
}
|
|
43
|
+
function processIsRunning(pid) {
|
|
44
|
+
try {
|
|
45
|
+
process.kill(pid, 0);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function waitForProcessExit(pid, timeoutMs = 10_000) {
|
|
53
|
+
const deadline = Date.now() + timeoutMs;
|
|
54
|
+
while (Date.now() < deadline) {
|
|
55
|
+
if (!processIsRunning(pid)) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
await new Promise((resolvePromise) => setTimeout(resolvePromise, 200));
|
|
59
|
+
}
|
|
60
|
+
return !processIsRunning(pid);
|
|
61
|
+
}
|
|
62
|
+
function renderHealth(ok) {
|
|
63
|
+
return ok ? `${ansi.green}ok${ansi.reset}` : `${ansi.red}down${ansi.reset}`;
|
|
64
|
+
}
|
|
65
|
+
function renderCheckLevel(level) {
|
|
66
|
+
if (level === "ok")
|
|
67
|
+
return `${ansi.green}ok${ansi.reset}`;
|
|
68
|
+
if (level === "warn")
|
|
69
|
+
return `${ansi.yellow}degraded${ansi.reset}`;
|
|
70
|
+
return `${ansi.red}down${ansi.reset}`;
|
|
71
|
+
}
|
|
72
|
+
function resolveInstallServiceScope(requested) {
|
|
73
|
+
if (requested === "user" || requested === "system") {
|
|
74
|
+
return requested;
|
|
75
|
+
}
|
|
76
|
+
return "system";
|
|
77
|
+
}
|
|
78
|
+
function normalizeUrl(value) {
|
|
79
|
+
return (value ?? "").trim().replace(/\/+$/, "");
|
|
80
|
+
}
|
|
81
|
+
export async function prepareLocalHostRuntime() {
|
|
82
|
+
const dockerAvailable = dockerInstalled();
|
|
83
|
+
const dockerOk = dockerAvailable && dockerReady();
|
|
84
|
+
const collect = async (blockingMessage) => {
|
|
85
|
+
const report = await collectSetupChecks({ local: true });
|
|
86
|
+
return {
|
|
87
|
+
report,
|
|
88
|
+
ready: !blockingMessage && report.ready,
|
|
89
|
+
...(blockingMessage ? { blockingMessage } : {}),
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
if (!dockerAvailable || !dockerOk) {
|
|
93
|
+
return collect("Docker is not ready. Fix the items above, then rerun setup from the workers.dev URL.");
|
|
94
|
+
}
|
|
95
|
+
if (dockerOk && !localImageExists()) {
|
|
96
|
+
try {
|
|
97
|
+
await prepareLocalRunnerImage();
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
return collect(`the local sandbox image could not be prepared: ${error instanceof Error ? error.message : String(error)}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return collect();
|
|
104
|
+
}
|
|
105
|
+
async function verifySavedMachineCredential(hubUrl) {
|
|
106
|
+
const existing = loadConfig();
|
|
107
|
+
// Saved credentials are tried only against the Hub they were saved for.
|
|
108
|
+
if (!savedCredentialsAreFor(hubUrl, existing))
|
|
109
|
+
return false;
|
|
110
|
+
if (!existing.clientId?.trim() || !existing.clientSecret?.trim() || !existing.controlSecret?.trim())
|
|
111
|
+
return false;
|
|
112
|
+
try {
|
|
113
|
+
await fetchHubSetupStatus(hubUrl, {
|
|
114
|
+
"X-Tiller-Client-Id": existing.clientId.trim(),
|
|
115
|
+
"X-Tiller-Client-Secret": existing.clientSecret.trim(),
|
|
116
|
+
"X-Tiller-Capability": existing.controlSecret.trim(),
|
|
117
|
+
});
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function connectMachineCredential(hubUrl) {
|
|
125
|
+
if (await verifySavedMachineCredential(hubUrl)) {
|
|
126
|
+
const existing = loadConfig();
|
|
127
|
+
return {
|
|
128
|
+
clientId: existing.clientId.trim(),
|
|
129
|
+
clientSecret: existing.clientSecret.trim(),
|
|
130
|
+
controlSecret: existing.controlSecret.trim(),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// A machine connecting again names its id, so the owner approves this credential for it.
|
|
134
|
+
const connected = await runBrowserBootstrap(hubUrl, { machineId: loadConfig().machineId ?? null });
|
|
135
|
+
if (connected.protectionMode !== "sign-in"
|
|
136
|
+
|| normalizeUrl(connected.hubUrl) !== hubUrl) {
|
|
137
|
+
throw new Error("This hub's sign-in is not set up, so this machine cannot connect to it. Its operator has to finish it.");
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
clientId: connected.clientId,
|
|
141
|
+
clientSecret: connected.clientSecret,
|
|
142
|
+
controlSecret: connected.controlSecret,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
export async function resolveCompatibleRuntimeImage(hubUrl, credential) {
|
|
146
|
+
const metadata = await fetchHostUpdateCheck(hubUrl, {
|
|
147
|
+
"X-Tiller-Client-Id": credential.clientId,
|
|
148
|
+
"X-Tiller-Client-Secret": credential.clientSecret,
|
|
149
|
+
"X-Tiller-Capability": credential.controlSecret,
|
|
150
|
+
});
|
|
151
|
+
return resolveHostUpdateTargetImage(metadata.currentRelease);
|
|
152
|
+
}
|
|
153
|
+
export async function waitForHealthyMachineAdvertisement(hubUrl, machineId, timeoutMs = 20_000) {
|
|
154
|
+
const deadline = Date.now() + timeoutMs;
|
|
155
|
+
let lastError = "";
|
|
156
|
+
while (Date.now() < deadline) {
|
|
157
|
+
try {
|
|
158
|
+
const response = await fetch(`${hubUrl}/api/machines/${encodeURIComponent(machineId)}/execution-status`, {
|
|
159
|
+
headers: { Accept: "application/json", ...hubControlHeaders },
|
|
160
|
+
});
|
|
161
|
+
if (!response.ok) {
|
|
162
|
+
lastError = `HTTP ${response.status}`;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
const status = await response.json();
|
|
166
|
+
if (status.state === "ready" && status.machineId === machineId) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (status.state === "incompatible" && status.machineId === machineId) {
|
|
170
|
+
throw new Error(`The execution-machine advertisement is incompatible (${status.code ?? "unknown"}).`);
|
|
171
|
+
}
|
|
172
|
+
lastError = status.state === "not_connected"
|
|
173
|
+
? "the Hub has not accepted a live advertisement"
|
|
174
|
+
: "the Hub returned an unexpected machine status";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
if (error instanceof Error && error.message.includes("advertisement is incompatible")) {
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
182
|
+
}
|
|
183
|
+
await new Promise((resolvePromise) => setTimeout(resolvePromise, 500));
|
|
184
|
+
}
|
|
185
|
+
throw new Error(`The execution-machine service did not advertise healthy state to the Hub${lastError ? ` (${lastError})` : ""}.`);
|
|
186
|
+
}
|
|
187
|
+
export async function runHostSetupCommand(options = {}, deps = {}) {
|
|
188
|
+
const readConfig = deps.loadConfig ?? loadConfig;
|
|
189
|
+
const saveConfig = deps.writeConfig ?? writeConfig;
|
|
190
|
+
const refreshConfig = deps.reloadConfig ?? reloadConfig;
|
|
191
|
+
const log = deps.log ?? hostLog;
|
|
192
|
+
const config = readConfig();
|
|
193
|
+
const overrideHubUrl = normalizeUrl(options.hubUrlOverride);
|
|
194
|
+
const configuredHubUrl = normalizeUrl(config.hubUrl || HUB_URL);
|
|
195
|
+
const setupHubUrl = overrideHubUrl || configuredHubUrl;
|
|
196
|
+
if (!isAllowedHubUrl(setupHubUrl)) {
|
|
197
|
+
throw new Error(`An exact https Hub URL under ${hubParentDomainsText()} is required. Run \`${CLI.command} host setup --hub-url https://<exact-host>.workers.dev\`.`);
|
|
198
|
+
}
|
|
199
|
+
const credential = await (deps.connectMachineCredential ?? connectMachineCredential)(setupHubUrl);
|
|
200
|
+
// Before anything is saved: the `overvues` build connects only to an Overvues Dev hub. The
|
|
201
|
+
// credential is sent only to the hub it was issued by.
|
|
202
|
+
await (deps.requireHubKind ?? requireHubKind)(setupHubUrl, credentialHeaders(credential));
|
|
203
|
+
const runtimeImage = await (deps.resolveCompatibleRuntimeImage ?? resolveCompatibleRuntimeImage)(setupHubUrl, credential);
|
|
204
|
+
const storedMachineId = config.machineId?.trim() ?? "";
|
|
205
|
+
const machineId = isMachineUuid(storedMachineId)
|
|
206
|
+
? storedMachineId
|
|
207
|
+
: (deps.machineId ?? randomUUID)();
|
|
208
|
+
const displayName = config.displayName?.trim() || (deps.displayName ?? hostname)();
|
|
209
|
+
const previousHubUrlEnv = process.env[CLI.hubUrlEnv];
|
|
210
|
+
if (overrideHubUrl)
|
|
211
|
+
process.env[CLI.hubUrlEnv] = setupHubUrl;
|
|
212
|
+
try {
|
|
213
|
+
saveConfig({
|
|
214
|
+
...config,
|
|
215
|
+
hubUrl: setupHubUrl,
|
|
216
|
+
clientId: credential.clientId,
|
|
217
|
+
clientSecret: credential.clientSecret,
|
|
218
|
+
controlSecret: credential.controlSecret,
|
|
219
|
+
machineId,
|
|
220
|
+
displayName,
|
|
221
|
+
localRunnerImage: runtimeImage,
|
|
222
|
+
});
|
|
223
|
+
refreshConfig();
|
|
224
|
+
log(`Saved execution machine ${displayName} (${machineId}).`);
|
|
225
|
+
const prep = await (deps.prepareLocalHostRuntime ?? prepareLocalHostRuntime)();
|
|
226
|
+
(deps.printCheckReport ?? printCheckReport)("Execution machine setup", prep.report.checks);
|
|
227
|
+
if (!prep.ready) {
|
|
228
|
+
throw new Error(prep.blockingMessage ?? "Execution machine checks failed.");
|
|
229
|
+
}
|
|
230
|
+
const supervisor = (deps.createHostSupervisor ?? selectHostSupervisor)();
|
|
231
|
+
const installed = supervisor.installOrUpdate();
|
|
232
|
+
if (!installed.started && !installed.running) {
|
|
233
|
+
throw new Error(`The ${installed.platform} service was installed but did not start.`);
|
|
234
|
+
}
|
|
235
|
+
if (installed.platform === "systemd"
|
|
236
|
+
&& installed.scope === "user"
|
|
237
|
+
&& installed.lingerEnabled === false) {
|
|
238
|
+
try {
|
|
239
|
+
supervisor.stop();
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
// The actionable persistence failure below is the setup outcome.
|
|
243
|
+
}
|
|
244
|
+
const user = process.env.USER?.trim() || "$USER";
|
|
245
|
+
throw new Error(`The user systemd service is not persistent because login linger is disabled. Run \`sudo loginctl enable-linger ${user}\` and rerun \`${CLI.command} host setup\`, or install the system service with \`sudo ${CLI.command} host install-service --system\`.`);
|
|
246
|
+
}
|
|
247
|
+
log(`Installed and started ${installed.serviceName}.`);
|
|
248
|
+
await (deps.waitForHealthyMachineAdvertisement ?? waitForHealthyMachineAdvertisement)(setupHubUrl, machineId);
|
|
249
|
+
log("Your machine is ready for selection in Settings.");
|
|
250
|
+
const settingsUrl = `${setupHubUrl}/settings`;
|
|
251
|
+
if (deps.printSettingsLink) {
|
|
252
|
+
deps.printSettingsLink(settingsUrl);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} ${settingsUrl}\n`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
finally {
|
|
259
|
+
if (overrideHubUrl) {
|
|
260
|
+
if (previousHubUrlEnv === undefined)
|
|
261
|
+
delete process.env[CLI.hubUrlEnv];
|
|
262
|
+
else
|
|
263
|
+
process.env[CLI.hubUrlEnv] = previousHubUrlEnv;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
export async function runHostInstallServiceCommand(options = {}) {
|
|
268
|
+
if (process.platform === "darwin") {
|
|
269
|
+
ensureHostAuth();
|
|
270
|
+
const installed = selectHostSupervisor().installOrUpdate();
|
|
271
|
+
hostLog(`Installed ${installed.serviceName} at ${installed.definitionPath}.`);
|
|
272
|
+
if (!installed.started && !installed.running) {
|
|
273
|
+
throw new Error("The launchd service was installed but did not start.");
|
|
274
|
+
}
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (process.platform !== "linux") {
|
|
278
|
+
throw new Error("Persistent execution-machine services are supported on Linux and macOS.");
|
|
279
|
+
}
|
|
280
|
+
const scope = resolveInstallServiceScope(options.scope);
|
|
281
|
+
if (scope === "user") {
|
|
282
|
+
ensureHostAuth();
|
|
283
|
+
}
|
|
284
|
+
const installed = selectHostSupervisor(process.platform, {
|
|
285
|
+
preferredSystemdScope: scope,
|
|
286
|
+
}).installOrUpdate();
|
|
287
|
+
if (scope === "system") {
|
|
288
|
+
hostLog(`Installed systemd unit at ${installed.definitionPath} for ${installed.serviceUser}.`);
|
|
289
|
+
hostLog(`The service will read host config from ${installed.configPath}.`);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
hostLog(`Installed user systemd unit at ${installed.definitionPath}.`);
|
|
293
|
+
}
|
|
294
|
+
if (installed.daemonReloaded && installed.enabled && installed.started) {
|
|
295
|
+
hostLog(scope === "system"
|
|
296
|
+
? `Enabled and started ${installed.serviceName} in the system service manager.`
|
|
297
|
+
: `Enabled and started ${installed.serviceName} in the user systemd manager.`);
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
hostLog(`${ansi.yellow}The service file was written, but systemd could not fully enable/start it from this shell.${ansi.reset}`);
|
|
301
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} Run these commands on the Pi:\n`);
|
|
302
|
+
if (scope === "system") {
|
|
303
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} sudo systemctl daemon-reload\n`);
|
|
304
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} sudo systemctl enable --now ${installed.serviceName}\n`);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} systemctl --user daemon-reload\n`);
|
|
308
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} systemctl --user enable --now ${installed.serviceName}\n`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (scope === "user" && installed.lingerEnabled === false && process.env.USER) {
|
|
312
|
+
hostLog(`To start your execution machine automatically after reboot without logging in first, run \`sudo loginctl enable-linger ${process.env.USER}\` once.`);
|
|
313
|
+
hostLog(`If you want unattended boot without linger, use \`sudo ${CLI.command} host install-service --system\` instead.`);
|
|
314
|
+
}
|
|
315
|
+
if (scope === "user") {
|
|
316
|
+
const report = await collectSetupChecks({ local: true });
|
|
317
|
+
if (!report.ready || hasWarnings(report.checks)) {
|
|
318
|
+
hostLog("The service is installed, but this machine still has setup warnings.");
|
|
319
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} Run \`${CLI.command} host setup\` and \`${CLI.command} doctor\` if the service does not stay healthy.\n`);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} Run \`sudo -u ${installed.serviceUser} ${CLI.command} doctor\` if the service does not stay healthy.\n`);
|
|
324
|
+
}
|
|
325
|
+
process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} View logs with \`${scope === "system" ? "journalctl" : "journalctl --user"} -u ${installed.serviceName} -f\`\n`);
|
|
326
|
+
}
|
|
327
|
+
export async function runHostCommand() {
|
|
328
|
+
// A hub of a kind this program does not connect to is refused; a build that does not connect
|
|
329
|
+
// to every kind waits until the hub says its kind rather than starting on no answer (hub-kind.ts).
|
|
330
|
+
await hubKindAtStart(() => readSetupStatusWithValidatedCredential(), HUB_URL, { log: hostLog });
|
|
331
|
+
const existing = readHostPidRecord();
|
|
332
|
+
if (existing?.pid && existing.pid !== process.pid && processIsRunning(existing.pid)) {
|
|
333
|
+
hostLog(`Your execution machine is already running ${ansi.dim}[pid ${existing.pid}]${ansi.reset}`);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
writeHostPidRecord(process.pid);
|
|
337
|
+
let cleanedUp = false;
|
|
338
|
+
let shutdownSignal = null;
|
|
339
|
+
const cleanup = async () => {
|
|
340
|
+
if (cleanedUp)
|
|
341
|
+
return;
|
|
342
|
+
cleanedUp = true;
|
|
343
|
+
await stopHostRunner().catch(() => false);
|
|
344
|
+
rmSync(HOST_PID_PATH, { force: true });
|
|
345
|
+
};
|
|
346
|
+
const cleanupAfterSignal = async () => {
|
|
347
|
+
const forceExit = setTimeout(() => {
|
|
348
|
+
hostLog(`Shutdown cleanup exceeded ${HOST_SHUTDOWN_DEADLINE_MS}ms after ${shutdownSignal ?? "signal"}; forcing exit.`);
|
|
349
|
+
process.exit(0);
|
|
350
|
+
}, HOST_SHUTDOWN_DEADLINE_MS);
|
|
351
|
+
try {
|
|
352
|
+
await cleanup();
|
|
353
|
+
}
|
|
354
|
+
finally {
|
|
355
|
+
clearTimeout(forceExit);
|
|
356
|
+
}
|
|
357
|
+
process.exit(0);
|
|
358
|
+
};
|
|
359
|
+
try {
|
|
360
|
+
await startHostRunner();
|
|
361
|
+
hostLog("Your execution machine is running.");
|
|
362
|
+
await new Promise((resolvePromise) => {
|
|
363
|
+
const onSignal = (signal) => {
|
|
364
|
+
shutdownSignal = signal;
|
|
365
|
+
resolvePromise();
|
|
366
|
+
};
|
|
367
|
+
process.once("SIGTERM", () => onSignal("SIGTERM"));
|
|
368
|
+
process.once("SIGINT", () => onSignal("SIGINT"));
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
finally {
|
|
372
|
+
if (shutdownSignal) {
|
|
373
|
+
await cleanupAfterSignal();
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
await cleanup();
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
export async function runHostStatusCommand() {
|
|
381
|
+
const [stack, setupStatus] = await Promise.all([
|
|
382
|
+
getLocalStackStatus(),
|
|
383
|
+
readSetupStatusWithValidatedCredential(),
|
|
384
|
+
]);
|
|
385
|
+
const hostPid = readHostPidRecord();
|
|
386
|
+
const hostRunning = Boolean(hostPid?.pid && processIsRunning(hostPid.pid));
|
|
387
|
+
hostLog(`host process ${hostRunning ? renderHealth(true) : renderHealth(false)}${hostPid?.pid ? ` ${ansi.dim}[pid ${hostPid.pid}]${ansi.reset}` : ""}`);
|
|
388
|
+
hostLog(`docker ${renderHealth(stack.dockerReady)}`);
|
|
389
|
+
hostLog(`hub reachable ${renderHealth(stack.hubReachable)}`);
|
|
390
|
+
if (setupStatus) {
|
|
391
|
+
for (const check of collectHubHostChecks(setupStatus)) {
|
|
392
|
+
hostLog(`${check.label.toLowerCase()} ${renderCheckLevel(check.level)} ${ansi.dim}${check.detail ?? ""}${ansi.reset}`);
|
|
393
|
+
if (check.fixHint && check.level !== "ok") {
|
|
394
|
+
hostLog(`${ansi.dim}${check.fixHint}${ansi.reset}`);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
hostLog(`runner ${renderHealth(stack.runner.healthy)} ${ansi.dim}${stack.runner.detail ?? ""}${ansi.reset}`);
|
|
399
|
+
}
|
|
400
|
+
export async function waitForMachineAdvertisementWithdrawal(hubUrl, machineId, timeoutMs = 10_000, fetchImpl = fetch, headers = hubControlHeaders) {
|
|
401
|
+
const deadline = Date.now() + timeoutMs;
|
|
402
|
+
let lastError = "the machine is still advertised";
|
|
403
|
+
while (Date.now() < deadline) {
|
|
404
|
+
try {
|
|
405
|
+
const response = await fetchImpl(`${normalizeUrl(hubUrl)}/api/machines/${encodeURIComponent(machineId)}/execution-status`, {
|
|
406
|
+
headers: {
|
|
407
|
+
Accept: "application/json",
|
|
408
|
+
"Cache-Control": "no-store",
|
|
409
|
+
...headers,
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
if (!response.ok) {
|
|
413
|
+
lastError = `Hub returned HTTP ${response.status}`;
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
const status = await response.json();
|
|
417
|
+
if (status.state === "not_connected")
|
|
418
|
+
return;
|
|
419
|
+
lastError = `Hub still reports ${status.state ?? "an unknown state"}`;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
424
|
+
}
|
|
425
|
+
await new Promise((resolvePromise) => setTimeout(resolvePromise, 250));
|
|
426
|
+
}
|
|
427
|
+
throw new Error(`The execution machine stopped locally, but its Hub advertisement did not withdraw (${lastError}).`);
|
|
428
|
+
}
|
|
429
|
+
export async function runHostDownCommand(deps = {}) {
|
|
430
|
+
const platform = deps.platform ?? process.platform;
|
|
431
|
+
const log = deps.log ?? hostLog;
|
|
432
|
+
const readPid = deps.readHostPidRecord ?? readHostPidRecord;
|
|
433
|
+
const isRunning = deps.processIsRunning ?? processIsRunning;
|
|
434
|
+
const removePid = deps.removePidRecord ?? (() => rmSync(HOST_PID_PATH, { force: true }));
|
|
435
|
+
const waitForExit = deps.waitForProcessExit ?? waitForProcessExit;
|
|
436
|
+
const hubUrl = deps.hubUrl ?? HUB_URL;
|
|
437
|
+
const machineId = deps.machineId ?? MACHINE_ID;
|
|
438
|
+
const waitForWithdrawal = deps.waitForMachineAdvertisementWithdrawal
|
|
439
|
+
?? ((url, id) => waitForMachineAdvertisementWithdrawal(url, id));
|
|
440
|
+
const supervisor = deps.hostSupervisor === undefined
|
|
441
|
+
? (platform === "linux" || platform === "darwin"
|
|
442
|
+
? selectHostSupervisor(platform)
|
|
443
|
+
: null)
|
|
444
|
+
: deps.hostSupervisor;
|
|
445
|
+
const supervisorState = supervisor?.inspect() ?? null;
|
|
446
|
+
const hostPid = readPid();
|
|
447
|
+
if (supervisorState?.running) {
|
|
448
|
+
supervisor.stop();
|
|
449
|
+
if (hostPid?.pid && isRunning(hostPid.pid)) {
|
|
450
|
+
const stopped = await waitForExit(hostPid.pid);
|
|
451
|
+
if (!stopped) {
|
|
452
|
+
throw new Error(`Timed out waiting for the execution machine [pid ${hostPid.pid}] to stop`);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
removePid();
|
|
456
|
+
if (!hubUrl || !isMachineUuid(machineId)) {
|
|
457
|
+
throw new Error(`The service stopped, but Hub withdrawal could not be verified because the saved machine configuration is invalid. Run \`${CLI.command} host setup\`.`);
|
|
458
|
+
}
|
|
459
|
+
await waitForWithdrawal(hubUrl, machineId);
|
|
460
|
+
log("Stopped the execution-machine service and verified its Hub advertisement was withdrawn.");
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (hostPid?.pid && isRunning(hostPid.pid)) {
|
|
464
|
+
try {
|
|
465
|
+
(deps.signalProcess ?? process.kill)(hostPid.pid, "SIGTERM");
|
|
466
|
+
}
|
|
467
|
+
catch {
|
|
468
|
+
// The process may have exited between the liveness check and signal.
|
|
469
|
+
}
|
|
470
|
+
const stopped = await waitForExit(hostPid.pid);
|
|
471
|
+
if (!stopped) {
|
|
472
|
+
throw new Error(`Timed out waiting for the execution machine [pid ${hostPid.pid}] to stop`);
|
|
473
|
+
}
|
|
474
|
+
removePid();
|
|
475
|
+
if (hubUrl && isMachineUuid(machineId)) {
|
|
476
|
+
await waitForWithdrawal(hubUrl, machineId);
|
|
477
|
+
}
|
|
478
|
+
log("Stopped the execution machine.");
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
removePid();
|
|
482
|
+
await (deps.stopHostRunner ?? stopHostRunner)().catch(() => false);
|
|
483
|
+
log("The execution machine is not running.");
|
|
484
|
+
}
|