The service only runs under Linux systemd. Remove setupLaunchd, setupNohupFallback, buildLaunchdPlist, the launchd/nohup service checks in setup/verify-services.ts, ServiceDef.launchdLabel, launchd/ and the unused repo-tokens/ GitHub action. Without systemd, `setup --step service` now fails with systemd_unavailable instead of writing a nohup wrapper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
185 lines
4.7 KiB
TypeScript
185 lines
4.7 KiB
TypeScript
import { execSync } from 'child_process';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
import { logger } from '../src/logger.js';
|
|
import { getServiceManager, isRoot } from './platform.js';
|
|
import { STACK_RESTART_UNIT_NAME } from './restart-stack.js';
|
|
import type { ServiceDef } from './service-defs.js';
|
|
import {
|
|
buildStackRestartSystemdUnit,
|
|
buildSystemdUnit,
|
|
} from './service-renderers.js';
|
|
import { emitStatus } from './status.js';
|
|
|
|
export function setupLinux(
|
|
serviceDefs: ServiceDef[],
|
|
projectRoot: string,
|
|
nodePath: string,
|
|
homeDir: string,
|
|
): void {
|
|
const serviceManager = getServiceManager();
|
|
|
|
if (serviceManager === 'systemd') {
|
|
setupSystemdAll(serviceDefs, projectRoot, nodePath, homeDir);
|
|
} else {
|
|
failWithoutSystemd(projectRoot, nodePath);
|
|
}
|
|
}
|
|
|
|
function failWithoutSystemd(projectRoot: string, nodePath: string): never {
|
|
logger.error('systemd is required to install the EJClaw service');
|
|
emitStatus('SETUP_SERVICE', {
|
|
SERVICE_TYPE: 'none',
|
|
NODE_PATH: nodePath,
|
|
PROJECT_PATH: projectRoot,
|
|
STATUS: 'failed',
|
|
ERROR: 'systemd_unavailable',
|
|
LOG: 'logs/setup.log',
|
|
});
|
|
process.exit(1);
|
|
}
|
|
|
|
/**
|
|
* Kill any orphaned ejclaw node processes left from previous runs or debugging.
|
|
* Prevents connection conflicts when two instances connect to the same channel simultaneously.
|
|
*/
|
|
function killOrphanedProcesses(projectRoot: string): void {
|
|
try {
|
|
execSync(`pkill -f '${projectRoot}/dist/index\\.js' || true`, {
|
|
stdio: 'ignore',
|
|
});
|
|
logger.info('Stopped any orphaned ejclaw processes');
|
|
} catch {
|
|
// pkill not available or no orphans
|
|
}
|
|
}
|
|
|
|
function setupSystemdAll(
|
|
serviceDefs: ServiceDef[],
|
|
projectRoot: string,
|
|
nodePath: string,
|
|
homeDir: string,
|
|
): void {
|
|
const runningAsRoot = isRoot();
|
|
const systemctlPrefix = runningAsRoot ? 'systemctl' : 'systemctl --user';
|
|
|
|
if (!runningAsRoot) {
|
|
try {
|
|
execSync('systemctl --user daemon-reload', { stdio: 'pipe' });
|
|
} catch {
|
|
logger.warn('systemd user session not available');
|
|
failWithoutSystemd(projectRoot, nodePath);
|
|
}
|
|
}
|
|
|
|
killOrphanedProcesses(projectRoot);
|
|
|
|
for (const def of serviceDefs) {
|
|
setupSystemdUnit(def, projectRoot, nodePath, homeDir, runningAsRoot);
|
|
}
|
|
setupSystemdStackRestartUnit(projectRoot, nodePath, homeDir, runningAsRoot);
|
|
|
|
try {
|
|
execSync(`${systemctlPrefix} daemon-reload`, { stdio: 'ignore' });
|
|
} catch (err) {
|
|
logger.error({ err }, 'systemctl daemon-reload failed');
|
|
}
|
|
|
|
for (const def of serviceDefs) {
|
|
try {
|
|
execSync(`${systemctlPrefix} enable ${def.name}`, { stdio: 'ignore' });
|
|
} catch (err) {
|
|
logger.error({ err, service: def.name }, 'systemctl enable failed');
|
|
}
|
|
|
|
try {
|
|
execSync(`${systemctlPrefix} start ${def.name}`, { stdio: 'ignore' });
|
|
} catch (err) {
|
|
logger.error({ err, service: def.name }, 'systemctl start failed');
|
|
}
|
|
|
|
let serviceLoaded = false;
|
|
try {
|
|
execSync(`${systemctlPrefix} is-active ${def.name}`, {
|
|
stdio: 'ignore',
|
|
});
|
|
serviceLoaded = true;
|
|
} catch {
|
|
// Not active
|
|
}
|
|
|
|
emitStatus('SETUP_SERVICE', {
|
|
SERVICE_NAME: def.name,
|
|
SERVICE_TYPE: runningAsRoot ? 'systemd-system' : 'systemd-user',
|
|
NODE_PATH: nodePath,
|
|
PROJECT_PATH: projectRoot,
|
|
UNIT_PATH: getUnitPath(def.name, homeDir, runningAsRoot),
|
|
SERVICE_LOADED: serviceLoaded,
|
|
STATUS: 'success',
|
|
LOG: 'logs/setup.log',
|
|
});
|
|
}
|
|
}
|
|
|
|
function getUnitPath(
|
|
serviceName: string,
|
|
homeDir: string,
|
|
runningAsRoot: boolean,
|
|
): string {
|
|
if (runningAsRoot) {
|
|
return `/etc/systemd/system/${serviceName}.service`;
|
|
}
|
|
return path.join(
|
|
homeDir,
|
|
'.config',
|
|
'systemd',
|
|
'user',
|
|
`${serviceName}.service`,
|
|
);
|
|
}
|
|
|
|
function setupSystemdUnit(
|
|
def: ServiceDef,
|
|
projectRoot: string,
|
|
nodePath: string,
|
|
homeDir: string,
|
|
runningAsRoot: boolean,
|
|
): void {
|
|
const unitPath = getUnitPath(def.name, homeDir, runningAsRoot);
|
|
fs.mkdirSync(path.dirname(unitPath), { recursive: true });
|
|
|
|
const unit = buildSystemdUnit(
|
|
def,
|
|
projectRoot,
|
|
nodePath,
|
|
homeDir,
|
|
runningAsRoot,
|
|
);
|
|
|
|
fs.writeFileSync(unitPath, unit);
|
|
logger.info({ unitPath, service: def.name }, 'Wrote systemd unit');
|
|
}
|
|
|
|
function setupSystemdStackRestartUnit(
|
|
projectRoot: string,
|
|
nodePath: string,
|
|
homeDir: string,
|
|
runningAsRoot: boolean,
|
|
): void {
|
|
const unitPath = getUnitPath(
|
|
STACK_RESTART_UNIT_NAME.replace(/\.service$/, ''),
|
|
homeDir,
|
|
runningAsRoot,
|
|
);
|
|
fs.mkdirSync(path.dirname(unitPath), { recursive: true });
|
|
fs.writeFileSync(
|
|
unitPath,
|
|
buildStackRestartSystemdUnit(projectRoot, nodePath, homeDir),
|
|
);
|
|
logger.info(
|
|
{ unitPath, service: STACK_RESTART_UNIT_NAME },
|
|
'Wrote stack restart systemd unit',
|
|
);
|
|
}
|