refactor(daemon): share service arg types across backends

This commit is contained in:
Peter Steinberger
2026-02-19 10:03:09 +00:00
parent be7462af1e
commit 70900feaa7
5 changed files with 109 additions and 144 deletions

View File

@@ -10,6 +10,14 @@ import { formatLine, toPosixPath, writeFormattedLines } from "./output.js";
import { resolveHomeDir } from "./paths.js";
import { parseKeyValueOutput } from "./runtime-parse.js";
import type { GatewayServiceRuntime } from "./service-runtime.js";
import type {
GatewayServiceCommandConfig,
GatewayServiceControlArgs,
GatewayServiceEnv,
GatewayServiceEnvArgs,
GatewayServiceInstallArgs,
GatewayServiceManageArgs,
} from "./service-types.js";
import {
enableSystemdUserLinger,
readSystemdUserLingerStatus,
@@ -21,15 +29,12 @@ import {
parseSystemdExecStart,
} from "./systemd-unit.js";
function resolveSystemdUnitPathForName(
env: Record<string, string | undefined>,
name: string,
): string {
function resolveSystemdUnitPathForName(env: GatewayServiceEnv, name: string): string {
const home = toPosixPath(resolveHomeDir(env));
return path.posix.join(home, ".config", "systemd", "user", `${name}.service`);
}
function resolveSystemdServiceName(env: Record<string, string | undefined>): string {
function resolveSystemdServiceName(env: GatewayServiceEnv): string {
const override = env.OPENCLAW_SYSTEMD_UNIT?.trim();
if (override) {
return override.endsWith(".service") ? override.slice(0, -".service".length) : override;
@@ -37,11 +42,11 @@ function resolveSystemdServiceName(env: Record<string, string | undefined>): str
return resolveGatewaySystemdServiceName(env.OPENCLAW_PROFILE);
}
function resolveSystemdUnitPath(env: Record<string, string | undefined>): string {
function resolveSystemdUnitPath(env: GatewayServiceEnv): string {
return resolveSystemdUnitPathForName(env, resolveSystemdServiceName(env));
}
export function resolveSystemdUserUnitPath(env: Record<string, string | undefined>): string {
export function resolveSystemdUserUnitPath(env: GatewayServiceEnv): string {
return resolveSystemdUnitPath(env);
}
@@ -51,13 +56,8 @@ export type { SystemdUserLingerStatus };
// Unit file parsing/rendering: see systemd-unit.ts
export async function readSystemdServiceExecStart(
env: Record<string, string | undefined>,
): Promise<{
programArguments: string[];
workingDirectory?: string;
environment?: Record<string, string>;
sourcePath?: string;
} | null> {
env: GatewayServiceEnv,
): Promise<GatewayServiceCommandConfig | null> {
const unitPath = resolveSystemdUnitPath(env);
try {
const content = await fs.readFile(unitPath, "utf8");
@@ -188,14 +188,7 @@ export async function installSystemdService({
workingDirectory,
environment,
description,
}: {
env: Record<string, string | undefined>;
stdout: NodeJS.WritableStream;
programArguments: string[];
workingDirectory?: string;
environment?: Record<string, string | undefined>;
description?: string;
}): Promise<{ unitPath: string }> {
}: GatewayServiceInstallArgs): Promise<{ unitPath: string }> {
await assertSystemdAvailable();
const unitPath = resolveSystemdUnitPath(env);
@@ -243,10 +236,7 @@ export async function installSystemdService({
export async function uninstallSystemdService({
env,
stdout,
}: {
env: Record<string, string | undefined>;
stdout: NodeJS.WritableStream;
}): Promise<void> {
}: GatewayServiceManageArgs): Promise<void> {
await assertSystemdAvailable();
const serviceName = resolveGatewaySystemdServiceName(env.OPENCLAW_PROFILE);
const unitName = `${serviceName}.service`;
@@ -263,7 +253,7 @@ export async function uninstallSystemdService({
async function runSystemdServiceAction(params: {
stdout: NodeJS.WritableStream;
env?: Record<string, string | undefined>;
env?: GatewayServiceEnv;
action: "stop" | "restart";
label: string;
}) {
@@ -280,10 +270,7 @@ async function runSystemdServiceAction(params: {
export async function stopSystemdService({
stdout,
env,
}: {
stdout: NodeJS.WritableStream;
env?: Record<string, string | undefined>;
}): Promise<void> {
}: GatewayServiceControlArgs): Promise<void> {
await runSystemdServiceAction({
stdout,
env,
@@ -295,10 +282,7 @@ export async function stopSystemdService({
export async function restartSystemdService({
stdout,
env,
}: {
stdout: NodeJS.WritableStream;
env?: Record<string, string | undefined>;
}): Promise<void> {
}: GatewayServiceControlArgs): Promise<void> {
await runSystemdServiceAction({
stdout,
env,
@@ -307,9 +291,7 @@ export async function restartSystemdService({
});
}
export async function isSystemdServiceEnabled(args: {
env?: Record<string, string | undefined>;
}): Promise<boolean> {
export async function isSystemdServiceEnabled(args: GatewayServiceEnvArgs): Promise<boolean> {
await assertSystemdAvailable();
const serviceName = resolveSystemdServiceName(args.env ?? {});
const unitName = `${serviceName}.service`;
@@ -318,7 +300,7 @@ export async function isSystemdServiceEnabled(args: {
}
export async function readSystemdServiceRuntime(
env: Record<string, string | undefined> = process.env as Record<string, string | undefined>,
env: GatewayServiceEnv = process.env as GatewayServiceEnv,
): Promise<GatewayServiceRuntime> {
try {
await assertSystemdAvailable();
@@ -375,9 +357,7 @@ async function isSystemctlAvailable(): Promise<boolean> {
return !detail.includes("not found");
}
export async function findLegacySystemdUnits(
env: Record<string, string | undefined>,
): Promise<LegacySystemdUnit[]> {
export async function findLegacySystemdUnits(env: GatewayServiceEnv): Promise<LegacySystemdUnit[]> {
const results: LegacySystemdUnit[] = [];
const systemctlAvailable = await isSystemctlAvailable();
for (const name of LEGACY_GATEWAY_SYSTEMD_SERVICE_NAMES) {
@@ -404,10 +384,7 @@ export async function findLegacySystemdUnits(
export async function uninstallLegacySystemdUnits({
env,
stdout,
}: {
env: Record<string, string | undefined>;
stdout: NodeJS.WritableStream;
}): Promise<LegacySystemdUnit[]> {
}: GatewayServiceManageArgs): Promise<LegacySystemdUnit[]> {
const units = await findLegacySystemdUnits(env);
if (units.length === 0) {
return units;