feat(systemd): support configurable service environment variables
This commit is contained in:
@@ -10,6 +10,7 @@ export interface ISmartDaemonServiceConstructorOptions {
|
||||
version: string;
|
||||
user?: string;
|
||||
group?: string;
|
||||
environment?: Record<string, string>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,6 +37,7 @@ export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions
|
||||
public description!: string;
|
||||
public user?: string;
|
||||
public group?: string;
|
||||
public environment?: Record<string, string>;
|
||||
|
||||
public smartdaemonRef: SmartDaemon;
|
||||
|
||||
|
||||
@@ -8,7 +8,21 @@ export class SmartDaemonTemplateManager {
|
||||
this.smartdaemonRef = smartdaemonRefArg;
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
public generateUnitFileForService = (serviceArg: SmartDaemonService) => {
|
||||
const environmentLines = this.generateEnvironmentLines(serviceArg);
|
||||
return `# ---
|
||||
# name: ${serviceArg.name}
|
||||
# version: ${serviceArg.version}
|
||||
@@ -26,8 +40,8 @@ After=network.target
|
||||
[Service]
|
||||
Type=simple${serviceArg.user ? `
|
||||
User=${serviceArg.user}` : ''}${serviceArg.group ? `
|
||||
Group=${serviceArg.group}` : ''}
|
||||
Environment=NODE_OPTIONS="--max_old_space_size=100"
|
||||
Group=${serviceArg.group}` : ''}${environmentLines ? `
|
||||
${environmentLines}` : ''}
|
||||
ExecStart=/bin/bash -c "cd ${serviceArg.workingDir} && ${serviceArg.command}"
|
||||
WorkingDirectory=${serviceArg.workingDir}
|
||||
Restart=on-failure
|
||||
|
||||
Reference in New Issue
Block a user