2022-10-19 19:27:42 +02:00
|
|
|
import { SmartDaemon } from './smartdaemon.classes.smartdaemon.js';
|
|
|
|
|
import { SmartDaemonService } from './smartdaemon.classes.service.js';
|
2019-09-03 11:29:14 +02:00
|
|
|
|
|
|
|
|
export class SmartDaemonTemplateManager {
|
2019-09-03 15:21:30 +02:00
|
|
|
public smartdaemonRef: SmartDaemon;
|
|
|
|
|
|
|
|
|
|
constructor(smartdaemonRefArg: SmartDaemon) {
|
|
|
|
|
this.smartdaemonRef = smartdaemonRefArg;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 08:08:01 +00:00
|
|
|
private generateEnvironmentLines(serviceArg: SmartDaemonService): string {
|
|
|
|
|
const environment = serviceArg.environment || {};
|
|
|
|
|
return Object.entries(environment)
|
|
|
|
|
.map(([key, value]) => {
|
|
|
|
|
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
|
|
|
|
|
throw new Error(`Invalid systemd environment key: ${key}`);
|
|
|
|
|
}
|
|
|
|
|
const escapedValue = value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
|
|
|
|
return `Environment="${key}=${escapedValue}"`;
|
|
|
|
|
})
|
|
|
|
|
.join('\n');
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 13:50:11 +02:00
|
|
|
public generateUnitFileForService = (serviceArg: SmartDaemonService) => {
|
2026-06-01 08:08:01 +00:00
|
|
|
const environmentLines = this.generateEnvironmentLines(serviceArg);
|
2019-09-05 11:15:17 +02:00
|
|
|
return `# ---
|
2019-09-05 13:50:11 +02:00
|
|
|
# name: ${serviceArg.name}
|
|
|
|
|
# version: ${serviceArg.version}
|
|
|
|
|
# description: ${serviceArg.description}
|
|
|
|
|
# command: ${serviceArg.command}
|
2025-09-03 11:16:45 +00:00
|
|
|
# workingDir: ${serviceArg.workingDir}${serviceArg.user ? `
|
|
|
|
|
# user: ${serviceArg.user}` : ''}${serviceArg.group ? `
|
|
|
|
|
# group: ${serviceArg.group}` : ''}
|
2019-09-05 11:15:17 +02:00
|
|
|
# ---
|
2019-09-03 11:29:14 +02:00
|
|
|
[Unit]
|
2019-09-05 13:50:11 +02:00
|
|
|
Description=${serviceArg.description}
|
2019-09-03 11:29:14 +02:00
|
|
|
Requires=network.target
|
|
|
|
|
After=network.target
|
|
|
|
|
|
|
|
|
|
[Service]
|
2025-09-03 11:16:45 +00:00
|
|
|
Type=simple${serviceArg.user ? `
|
|
|
|
|
User=${serviceArg.user}` : ''}${serviceArg.group ? `
|
2026-06-01 08:08:01 +00:00
|
|
|
Group=${serviceArg.group}` : ''}${environmentLines ? `
|
|
|
|
|
${environmentLines}` : ''}
|
2019-09-05 13:50:11 +02:00
|
|
|
ExecStart=/bin/bash -c "cd ${serviceArg.workingDir} && ${serviceArg.command}"
|
|
|
|
|
WorkingDirectory=${serviceArg.workingDir}
|
2019-09-03 11:29:14 +02:00
|
|
|
Restart=on-failure
|
|
|
|
|
LimitNOFILE=infinity
|
|
|
|
|
LimitCORE=infinity
|
|
|
|
|
StandardInput=null
|
|
|
|
|
StandardOutput=syslog
|
|
|
|
|
StandardError=syslog
|
|
|
|
|
Restart=always
|
|
|
|
|
[Install]
|
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
|
`;
|
2021-01-30 00:41:40 +00:00
|
|
|
};
|
2019-09-03 11:29:14 +02:00
|
|
|
}
|