File size: 11,339 Bytes
f778c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | // Node-host daemon lifecycle commands for install, status, start, stop, and restart.
import { colorize } from "../../../packages/terminal-core/src/theme.js";
import {
DEFAULT_GATEWAY_DAEMON_RUNTIME,
isGatewayDaemonRuntime,
} from "../../commands/daemon-runtime.js";
import { buildNodeInstallPlan } from "../../commands/node-daemon-install-helpers.js";
import {
resolveNodeLaunchAgentLabel,
resolveNodeSystemdServiceName,
resolveNodeWindowsTaskName,
} from "../../daemon/constants.js";
import { resolveNodeService } from "../../daemon/node-service.js";
import {
buildPlatformRuntimeLogHints,
buildPlatformServiceStartHints,
} from "../../daemon/runtime-hints.js";
import type { GatewayServiceRuntime } from "../../daemon/service-runtime.js";
import {
isSystemdUserServiceAvailable,
readSystemdUserLingerStatus,
resolveSystemdUserServiceAccount,
} from "../../daemon/systemd.js";
import { formatErrorMessage } from "../../infra/errors.js";
import { loadNodeHostConfig } from "../../node-host/config.js";
import { defaultRuntime } from "../../runtime.js";
import { formatCliCommand } from "../command-format.js";
import {
runServiceRestart,
runServiceStart,
runServiceStop,
runServiceUninstall,
} from "../daemon-cli/lifecycle-core.js";
import { buildDaemonServiceSnapshot, installDaemonServiceAndEmit } from "../daemon-cli/response.js";
import {
createCliStatusTextStyles,
createDaemonInstallActionContext,
resolveDaemonInstallBlockMessage,
formatRuntimeStatus,
projectDaemonServiceForJson,
resolveRuntimeStatusColor,
} from "../daemon-cli/shared.js";
import { formatInvalidConfigPort, formatInvalidPortOption } from "../error-format.js";
import { resolveNodeGatewayOptions } from "./gateway-options.js";
type NodeDaemonInstallOptions = {
host?: string;
port?: string | number;
contextPath?: string;
tls?: boolean;
tlsFingerprint?: string;
nodeId?: string;
displayName?: string;
shareInstalledApps?: boolean;
commands?: string[];
allCommands?: boolean;
runtime?: string;
force?: boolean;
json?: boolean;
};
type NodeDaemonLifecycleOptions = {
json?: boolean;
};
type NodeDaemonStatusOptions = {
json?: boolean;
};
function renderNodeServiceStartHints(): string[] {
return buildPlatformServiceStartHints({
installHint: formatCliCommand("openclaw node install"),
startCommand: formatCliCommand("openclaw node start"),
launchAgentPlistPath: `~/Library/LaunchAgents/${resolveNodeLaunchAgentLabel()}.plist`,
systemdServiceName: resolveNodeSystemdServiceName(),
windowsTaskName: resolveNodeWindowsTaskName(),
});
}
function buildNodeRuntimeHints(env: NodeJS.ProcessEnv = process.env): string[] {
return buildPlatformRuntimeLogHints({
env,
systemdServiceName: resolveNodeSystemdServiceName(),
windowsTaskName: resolveNodeWindowsTaskName(),
});
}
/**
* Warns (does NOT auto-enable) when systemd user lingering is disabled.
* The installed user-level node service stops when the last SSH session ends
* unless `loginctl enable-linger <user>` has been run. Read-only: this never
* changes host state, matching the operator-consent policy used elsewhere.
*/
async function warnIfSystemdUserLingerDisabled(warn: (message: string) => void): Promise<void> {
if (process.platform !== "linux") {
return;
}
if (!(await isSystemdUserServiceAvailable())) {
return;
}
const user = resolveSystemdUserServiceAccount(process.env);
if (!user) {
return;
}
const status = await readSystemdUserLingerStatus({ env: process.env, user });
if (!status || status.linger === "yes") {
return;
}
warn(
`Systemd lingering is disabled for ${status.user}. The node service will stop when you log out. Run: sudo loginctl enable-linger ${status.user}`,
);
}
export async function runNodeDaemonInstall(opts: NodeDaemonInstallOptions) {
const { json, stdout, warnings, emit, fail } = createDaemonInstallActionContext(opts.json);
const installBlock = resolveDaemonInstallBlockMessage("node");
if (installBlock) {
fail(installBlock);
return;
}
const config = await loadNodeHostConfig();
let gatewayOptions;
try {
gatewayOptions = resolveNodeGatewayOptions(opts, config);
} catch (error) {
fail(error instanceof Error ? error.message : String(error));
return;
}
const { host, port, contextPath, tls, tlsFingerprint, cloudflareAccess } = gatewayOptions;
if (!Number.isFinite(port ?? Number.NaN) || (port ?? 0) <= 0 || (port ?? 0) > 65_535) {
fail(
opts.port !== undefined
? formatInvalidPortOption("--port")
: formatInvalidConfigPort("node.gateway.port"),
);
return;
}
if (opts.tls === false && opts.tlsFingerprint !== undefined) {
fail("--no-tls cannot be combined with --tls-fingerprint");
return;
}
if (cloudflareAccess && tls !== true) {
fail("Cloudflare Access credentials require --tls for the node Gateway connection");
return;
}
const runtimeRaw = opts.runtime ? opts.runtime : DEFAULT_GATEWAY_DAEMON_RUNTIME;
if (!isGatewayDaemonRuntime(runtimeRaw)) {
fail('Invalid --runtime (use "node" or "bun")');
return;
}
const service = resolveNodeService();
const warn = (message: string) => {
if (json) {
warnings.push(message);
} else {
defaultRuntime.log(message);
}
};
let loaded;
try {
loaded = await service.isLoaded({ env: process.env });
} catch (err) {
fail(`Node service check failed: ${formatErrorMessage(err)}`);
return;
}
if (loaded && !opts.force) {
await warnIfSystemdUserLingerDisabled(warn);
emit({
ok: true,
result: "already-installed",
message: `Node service already ${service.loadedText}.`,
service: buildDaemonServiceSnapshot(service, loaded),
warnings: warnings.length ? warnings : undefined,
});
if (!json) {
defaultRuntime.log(`Node service already ${service.loadedText}.`);
defaultRuntime.log(`Reinstall with: ${formatCliCommand("openclaw node install --force")}`);
}
return;
}
const { programArguments, workingDirectory, environment, environmentValueSources, description } =
await buildNodeInstallPlan({
env: process.env,
host,
port: port ?? 18789,
contextPath,
tls: Boolean(tls),
tlsFingerprint,
nodeId: opts.nodeId,
displayName: opts.displayName,
installedAppsSharing: opts.shareInstalledApps,
commands: opts.commands,
allCommands: opts.allCommands,
runtime: runtimeRaw,
warn: (message) => {
if (json) {
warnings.push(message);
} else {
defaultRuntime.log(message);
}
},
});
await installDaemonServiceAndEmit({
serviceNoun: "Node",
service,
warnings,
emit,
fail,
install: async () => {
await service.install({
env: process.env,
stdout,
warn,
programArguments,
workingDirectory,
environment,
environmentValueSources,
description,
});
},
// Run the linger diagnostic only on the verified-success path: placing it
// in `install` (before service-load verification) would let a linger
// warning accompany a failed install or verification, misdirecting the
// operator (see #107033 review). The already-installed short-circuit
// above warns separately.
onVerified: async () => {
await warnIfSystemdUserLingerDisabled(warn);
},
});
}
export async function runNodeDaemonUninstall(opts: NodeDaemonLifecycleOptions = {}) {
return await runServiceUninstall({
serviceNoun: "Node",
service: resolveNodeService(),
opts,
stopBeforeUninstall: false,
assertNotLoadedAfterUninstall: false,
});
}
export async function runNodeDaemonStart(opts: NodeDaemonLifecycleOptions = {}) {
return await runServiceStart({
serviceNoun: "Node",
service: resolveNodeService(),
renderStartHints: renderNodeServiceStartHints,
opts,
});
}
export async function runNodeDaemonRestart(opts: NodeDaemonLifecycleOptions = {}) {
await runServiceRestart({
serviceNoun: "Node",
service: resolveNodeService(),
renderStartHints: renderNodeServiceStartHints,
opts,
});
}
export async function runNodeDaemonStop(opts: NodeDaemonLifecycleOptions = {}) {
return await runServiceStop({
serviceNoun: "Node",
service: resolveNodeService(),
opts,
});
}
export async function runNodeDaemonStatus(opts: NodeDaemonStatusOptions = {}) {
const json = Boolean(opts.json);
const service = resolveNodeService();
let loaded: boolean;
try {
loaded = await service.isLoaded({ env: process.env });
} catch (error) {
const message = `Node service check failed: ${formatErrorMessage(error)}`;
if (json) {
throw new Error(message, { cause: error });
}
defaultRuntime.error(message);
defaultRuntime.exit(1);
return;
}
const [command, runtime] = await Promise.all([
service.readCommand(process.env).catch(() => null),
service.readRuntime(process.env).catch((err: unknown): GatewayServiceRuntime => ({
status: "unknown",
detail: formatErrorMessage(err),
})),
]);
const payload = {
service: {
...buildDaemonServiceSnapshot(service, loaded),
command,
runtime,
},
};
if (json) {
defaultRuntime.writeJson({
service: projectDaemonServiceForJson(payload.service, { includeDefinitionPaths: true }),
});
return;
}
const { rich, label, accent, infoText, okText, warnText, errorText } =
createCliStatusTextStyles();
const serviceStatus = loaded ? okText(service.loadedText) : warnText(service.notLoadedText);
defaultRuntime.log(`${label("Service:")} ${accent(service.label)} (${serviceStatus})`);
if (command?.programArguments?.length) {
defaultRuntime.log(`${label("Command:")} ${infoText(command.programArguments.join(" "))}`);
}
if (command?.sourcePath) {
defaultRuntime.log(`${label("Service file:")} ${infoText(command.sourcePath)}`);
}
if (command?.workingDirectory) {
defaultRuntime.log(`${label("Working dir:")} ${infoText(command.workingDirectory)}`);
}
const runtimeLine = formatRuntimeStatus(runtime);
if (runtimeLine) {
const runtimeColor = resolveRuntimeStatusColor(runtime?.status);
defaultRuntime.log(`${label("Runtime:")} ${colorize(rich, runtimeColor, runtimeLine)}`);
}
if (!loaded) {
defaultRuntime.log("");
for (const hint of renderNodeServiceStartHints()) {
defaultRuntime.log(`${warnText("Start with:")} ${infoText(hint)}`);
}
return;
}
const baseEnv = {
...(process.env as Record<string, string | undefined>),
...(command?.environment ?? undefined),
};
const hintEnv = {
...baseEnv,
OPENCLAW_LOG_PREFIX: baseEnv.OPENCLAW_LOG_PREFIX ?? "node",
} as NodeJS.ProcessEnv;
if (runtime?.missingUnit) {
defaultRuntime.error(errorText("Service unit not found."));
for (const hint of buildNodeRuntimeHints(hintEnv)) {
defaultRuntime.log(errorText(hint));
}
return;
}
if (runtime?.status === "stopped") {
defaultRuntime.error(errorText("Service is loaded but not running."));
for (const hint of buildNodeRuntimeHints(hintEnv)) {
defaultRuntime.log(errorText(hint));
}
}
}
|