(input: BuildSystemdUnitInput)
| 27 | |
| 28 | |
| 29 | export function buildSystemdUnit(input: BuildSystemdUnitInput): string { |
| 30 | const execStart = input.programArguments.map(systemdEscapeArg).join(' '); |
| 31 | const description = (input.description ?? 'Kimi Code local server').trim(); |
| 32 | assertNoLineBreaks(description, 'Systemd Description'); |
| 33 | |
| 34 | const workingDirLine = input.workingDirectory |
| 35 | ? `WorkingDirectory=${systemdEscapeArg(input.workingDirectory)}` |
| 36 | : null; |
| 37 | |
| 38 | const envLines = input.environment |
| 39 | ? Object.entries(input.environment).map(([k, v]) => { |
| 40 | assertNoLineBreaks(k, 'Systemd environment variable names'); |
| 41 | assertNoLineBreaks(v, 'Systemd environment variable values'); |
| 42 | return `Environment=${systemdEscapeArg(`${k}=${v}`)}`; |
| 43 | }) |
| 44 | : []; |
| 45 | |
| 46 | const lines = [ |
| 47 | '[Unit]', |
| 48 | `Description=${description}`, |
| 49 | 'After=network-online.target', |
| 50 | 'Wants=network-online.target', |
| 51 | 'StartLimitBurst=5', |
| 52 | 'StartLimitIntervalSec=60', |
| 53 | '', |
| 54 | '[Service]', |
| 55 | 'Type=simple', |
| 56 | `ExecStart=${execStart}`, |
| 57 | 'Restart=always', |
| 58 | 'RestartSec=5', |
| 59 | 'TimeoutStopSec=30', |
| 60 | 'TimeoutStartSec=30', |
| 61 | 'KillMode=control-group', |
| 62 | workingDirLine, |
| 63 | ...envLines, |
| 64 | '', |
| 65 | '[Install]', |
| 66 | 'WantedBy=default.target', |
| 67 | '', |
| 68 | ]; |
| 69 | return lines.filter((line): line is string => line !== null).join('\n'); |
| 70 | } |
| 71 | |
| 72 | export function parseSystemctlShow(output: string): Record<string, string> { |
| 73 | const result: Record<string, string> = {}; |
no test coverage detected