Files
openclaw/src/daemon/systemd-unit.ts

109 lines
2.8 KiB
TypeScript
Raw Normal View History

import { splitArgsPreservingQuotes } from "./arg-split.js";
2026-01-14 01:08:15 +00:00
function systemdEscapeArg(value: string): string {
if (!/[\\s"\\\\]/.test(value)) {
return value;
}
2026-01-14 01:08:15 +00:00
return `"${value.replace(/\\\\/g, "\\\\\\\\").replace(/"/g, '\\\\"')}"`;
}
function renderEnvLines(env: Record<string, string | undefined> | undefined): string[] {
if (!env) {
return [];
}
2026-01-14 01:08:15 +00:00
const entries = Object.entries(env).filter(
([, value]) => typeof value === "string" && value.trim(),
);
if (entries.length === 0) {
return [];
}
2026-01-14 01:08:15 +00:00
return entries.map(
([key, value]) => `Environment=${systemdEscapeArg(`${key}=${value?.trim() ?? ""}`)}`,
2026-01-14 01:08:15 +00:00
);
}
export function buildSystemdUnit({
description,
programArguments,
workingDirectory,
environment,
}: {
description?: string;
programArguments: string[];
workingDirectory?: string;
environment?: Record<string, string | undefined>;
}): string {
const execStart = programArguments.map(systemdEscapeArg).join(" ");
2026-01-30 03:15:10 +01:00
const descriptionLine = `Description=${description?.trim() || "OpenClaw Gateway"}`;
2026-01-14 01:08:15 +00:00
const workingDirLine = workingDirectory
? `WorkingDirectory=${systemdEscapeArg(workingDirectory)}`
: null;
const envLines = renderEnvLines(environment);
return [
"[Unit]",
descriptionLine,
"After=network-online.target",
"Wants=network-online.target",
"",
"[Service]",
`ExecStart=${execStart}`,
"Restart=always",
"RestartSec=5",
// KillMode=process ensures systemd only waits for the main process to exit.
// Without this, podman's conmon (container monitor) processes block shutdown
// since they run as children of the gateway and stay in the same cgroup.
"KillMode=process",
workingDirLine,
...envLines,
"",
"[Install]",
"WantedBy=default.target",
"",
]
.filter((line) => line !== null)
.join("\n");
}
export function parseSystemdExecStart(value: string): string[] {
return splitArgsPreservingQuotes(value, { escapeMode: "backslash" });
2026-01-14 01:08:15 +00:00
}
export function parseSystemdEnvAssignment(raw: string): { key: string; value: string } | null {
2026-01-14 01:08:15 +00:00
const trimmed = raw.trim();
if (!trimmed) {
return null;
}
2026-01-14 01:08:15 +00:00
const unquoted = (() => {
if (!(trimmed.startsWith('"') && trimmed.endsWith('"'))) {
return trimmed;
}
2026-01-14 01:08:15 +00:00
let out = "";
let escapeNext = false;
for (const ch of trimmed.slice(1, -1)) {
if (escapeNext) {
out += ch;
escapeNext = false;
continue;
}
if (ch === "\\\\") {
escapeNext = true;
continue;
}
out += ch;
}
return out;
})();
const eq = unquoted.indexOf("=");
if (eq <= 0) {
return null;
}
2026-01-14 01:08:15 +00:00
const key = unquoted.slice(0, eq).trim();
if (!key) {
return null;
}
2026-01-14 01:08:15 +00:00
const value = unquoted.slice(eq + 1);
return { key, value };
}