smartdaemon/ts/smartdaemon.classes.systemdmanager.ts

148 lines
4.6 KiB
TypeScript
Raw Normal View History

2022-10-19 17:27:42 +00:00
import * as plugins from './smartdaemon.plugins.js';
import * as paths from './smartdaemon.paths.js';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon.js';
2021-01-30 00:41:40 +00:00
import {
ISmartDaemonServiceConstructorOptions,
SmartDaemonService,
2022-10-19 17:27:42 +00:00
} from './smartdaemon.classes.service.js';
2019-09-03 13:21:30 +00:00
export class SmartDaemonSystemdManager {
2019-09-03 17:58:08 +00:00
// STATIC
2019-09-04 14:51:58 +00:00
private static smartDaemonNamespace = 'smartdaemon';
2019-09-05 13:48:00 +00:00
2021-01-30 00:41:40 +00:00
public static createServiceNameFromServiceName(serviceNameArg: string) {
2019-09-05 13:48:00 +00:00
return `${SmartDaemonSystemdManager.smartDaemonNamespace}_${serviceNameArg}`;
}
2021-01-30 00:41:40 +00:00
public static createFileNameFromServiceName(serviceNameArg: string) {
2019-09-05 13:48:00 +00:00
return `${SmartDaemonSystemdManager.createServiceNameFromServiceName(serviceNameArg)}.service`;
2021-01-30 00:41:40 +00:00
}
2019-09-03 17:58:08 +00:00
2021-01-30 00:41:40 +00:00
public static createFilePathFromServiceName(serviceNameArg: string) {
2019-09-03 17:58:08 +00:00
return plugins.path.join(
paths.systemdDir,
SmartDaemonSystemdManager.createFileNameFromServiceName(serviceNameArg)
);
2019-09-05 13:48:00 +00:00
}
2019-09-03 17:58:08 +00:00
// INSTANCE
2019-09-03 13:21:30 +00:00
public smartdaemonRef: SmartDaemon;
public smartshellInstance: plugins.smartshell.Smartshell;
public smartsystem: plugins.smartsystem.Smartsystem;
public shouldExecute: boolean = false;
constructor(smartdaemonRefArg: SmartDaemon) {
this.smartdaemonRef = smartdaemonRefArg;
this.smartshellInstance = new plugins.smartshell.Smartshell({
2021-01-30 00:41:40 +00:00
executor: 'bash',
2019-09-03 13:21:30 +00:00
});
this.smartsystem = new plugins.smartsystem.Smartsystem();
}
public async checkElegibility() {
if (await this.smartsystem.env.isLinuxAsync()) {
this.shouldExecute = true;
} else {
2019-09-03 20:09:30 +00:00
console.log('Smartdaemon can only be used on Linux systems! Refusing to set up a service.');
2019-09-03 13:21:30 +00:00
this.shouldExecute = false;
}
return this.shouldExecute;
}
public async execute(commandArg: string) {
2019-09-03 17:58:08 +00:00
if (await this.checkElegibility()) {
await this.smartshellInstance.exec(commandArg);
}
2019-09-03 13:21:30 +00:00
}
2019-09-05 09:15:17 +00:00
/**
* gets all services that are already present
*/
2019-09-03 17:58:08 +00:00
public async getServices() {
2019-09-05 11:50:11 +00:00
const existingServices: SmartDaemonService[] = [];
2019-09-03 17:58:08 +00:00
if (await this.checkElegibility()) {
2019-09-05 09:15:17 +00:00
const smartfmInstance = new plugins.smartfm.Smartfm({
2021-01-30 00:41:40 +00:00
fmType: 'yaml',
2019-09-05 09:15:17 +00:00
});
const availableServices = await plugins.smartfile.fs.fileTreeToObject(
2019-09-03 17:58:08 +00:00
paths.systemdDir,
2019-09-05 09:15:17 +00:00
'smartdaemon_*.service'
2019-09-03 17:58:08 +00:00
);
2019-09-05 09:15:17 +00:00
for (const serviceFile of availableServices) {
const data = smartfmInstance.parseFromComments('# ', serviceFile.contentBuffer.toString())
.data as ISmartDaemonServiceConstructorOptions;
2019-09-05 11:50:11 +00:00
const service = await SmartDaemonService.createFromOptions(this.smartdaemonRef, data);
service.alreadyExists = true;
2019-09-05 09:15:17 +00:00
existingServices.push(service);
}
2019-09-03 17:58:08 +00:00
}
2019-09-05 09:15:17 +00:00
return existingServices;
2019-09-03 13:21:30 +00:00
}
2019-09-05 11:50:11 +00:00
public async startService(serviceArg: SmartDaemonService) {
2019-09-03 17:58:08 +00:00
if (await this.checkElegibility()) {
2019-09-05 09:15:17 +00:00
await this.execute(
2021-01-30 00:41:40 +00:00
`systemctl start ${SmartDaemonSystemdManager.createServiceNameFromServiceName(
serviceArg.name
)}`
2019-09-05 09:15:17 +00:00
);
2019-09-03 17:58:08 +00:00
}
2019-09-05 09:15:17 +00:00
}
2019-09-05 11:50:11 +00:00
public async stopService(serviceArg: SmartDaemonService) {
2019-09-03 17:58:08 +00:00
if (await this.checkElegibility()) {
2019-09-05 09:15:17 +00:00
await this.execute(
2021-01-30 00:41:40 +00:00
`systemctl stop ${SmartDaemonSystemdManager.createServiceNameFromServiceName(
serviceArg.name
)}`
2019-09-05 09:15:17 +00:00
);
2019-09-03 17:58:08 +00:00
}
}
2019-09-05 11:50:11 +00:00
public async saveService(serviceArg: SmartDaemonService) {
2019-09-03 17:58:08 +00:00
if (await this.checkElegibility()) {
await plugins.smartfile.memory.toFs(
2019-09-05 11:50:11 +00:00
this.smartdaemonRef.templateManager.generateUnitFileForService(serviceArg),
SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name)
2019-09-03 17:58:08 +00:00
);
}
}
2019-09-05 11:50:11 +00:00
public async deleteService(serviceArg: SmartDaemonService) {
2019-09-03 17:58:08 +00:00
if (await this.checkElegibility()) {
2019-09-05 09:15:17 +00:00
await plugins.smartfile.fs.remove(
2019-09-05 13:48:00 +00:00
SmartDaemonSystemdManager.createServiceNameFromServiceName(serviceArg.name)
2019-09-05 09:15:17 +00:00
);
2019-09-03 17:58:08 +00:00
}
}
2019-09-05 11:50:11 +00:00
public async enableService(serviceArg: SmartDaemonService) {
2019-09-03 17:58:08 +00:00
if (await this.checkElegibility()) {
2019-09-05 11:50:11 +00:00
await this.saveService(serviceArg);
2019-09-05 14:04:38 +00:00
if (serviceArg.alreadyExists) {
await this.execute(`systemctl daemon-reload`);
}
2019-09-05 09:15:17 +00:00
await this.execute(
2021-01-30 00:41:40 +00:00
`systemctl enable ${SmartDaemonSystemdManager.createServiceNameFromServiceName(
serviceArg.name
)}`
2019-09-05 09:15:17 +00:00
);
2019-09-03 17:58:08 +00:00
}
}
2019-09-05 11:50:11 +00:00
public async disableService(serviceArg: SmartDaemonService) {
2019-09-03 17:58:08 +00:00
if (await this.checkElegibility()) {
2019-09-05 09:15:17 +00:00
await this.execute(
2021-01-30 00:41:40 +00:00
`systemctl disable ${SmartDaemonSystemdManager.createServiceNameFromServiceName(
serviceArg.name
)}`
2019-09-05 09:15:17 +00:00
);
2019-09-03 17:58:08 +00:00
}
}
2019-09-03 13:21:30 +00:00
2019-09-06 15:33:01 +00:00
public async reload() {
if (await this.checkElegibility()) {
2021-01-30 00:41:40 +00:00
await this.execute(`systemctl daemon-reload`);
2019-09-06 15:33:01 +00:00
}
}
2019-09-03 17:58:08 +00:00
}