From 09e9d8c1903e420ecd51dde7e9671e5143554333 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Thu, 5 Sep 2019 13:50:11 +0200 Subject: [PATCH] fix(core): update --- ts/smartdaemon.classes.service.ts | 21 +++++--------- ts/smartdaemon.classes.systemdmanager.ts | 35 +++++++++++++---------- ts/smartdaemon.classes.templatemanager.ts | 25 +++++++--------- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/ts/smartdaemon.classes.service.ts b/ts/smartdaemon.classes.service.ts index 303bbd2..f7e163d 100644 --- a/ts/smartdaemon.classes.service.ts +++ b/ts/smartdaemon.classes.service.ts @@ -23,6 +23,7 @@ export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions } public options: ISmartDaemonServiceConstructorOptions; + public alreadyExists = false; public name: string; public version: string; @@ -40,48 +41,40 @@ export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions * enables the service */ public async enable() { - await this.save(); - await this.smartdaemonRef.systemdManager.enableService(this.name); + await this.smartdaemonRef.systemdManager.enableService(this); } /** * disables the service */ public async disable() { - await this.smartdaemonRef.systemdManager.disableService(this.name); + await this.smartdaemonRef.systemdManager.disableService(this); } /** * starts a service */ public async start() { - await this.smartdaemonRef.systemdManager.startService(this.name); + await this.smartdaemonRef.systemdManager.startService(this); } /** * stops a service */ public async stop() { - await this.smartdaemonRef.systemdManager.stopService(this.name); + await this.smartdaemonRef.systemdManager.stopService(this); } // Save and Delete public async save() { - const serviceTemplate = this.smartdaemonRef.templateManager.generateServiceTemplate({ - name: this.name, - version: this.version, - command: this.command, - workkingDir: this.workingDir, - description: this.description, - }); - await this.smartdaemonRef.systemdManager.saveService(this.name, serviceTemplate); + await this.smartdaemonRef.systemdManager.saveService(this); } /** * deletes the service */ public async delete() { - await this.smartdaemonRef.systemdManager.deleteService(this.name); + await this.smartdaemonRef.systemdManager.deleteService(this); } } diff --git a/ts/smartdaemon.classes.systemdmanager.ts b/ts/smartdaemon.classes.systemdmanager.ts index 2daab58..4a71e4d 100644 --- a/ts/smartdaemon.classes.systemdmanager.ts +++ b/ts/smartdaemon.classes.systemdmanager.ts @@ -52,7 +52,7 @@ export class SmartDaemonSystemdManager { * gets all services that are already present */ public async getServices() { - const existingServices = []; + const existingServices: SmartDaemonService[] = []; if (await this.checkElegibility()) { const smartfmInstance = new plugins.smartfm.Smartfm({ fmType: 'yaml' @@ -64,56 +64,61 @@ export class SmartDaemonSystemdManager { for (const serviceFile of availableServices) { const data = smartfmInstance.parseFromComments('# ', serviceFile.contentBuffer.toString()) .data as ISmartDaemonServiceConstructorOptions; - const service = SmartDaemonService.createFromOptions(this.smartdaemonRef, data); + const service = await SmartDaemonService.createFromOptions(this.smartdaemonRef, data); + service.alreadyExists = true; existingServices.push(service); } } return existingServices; } - public async startService(serviceNameArg: string) { + public async startService(serviceArg: SmartDaemonService) { if (await this.checkElegibility()) { await this.execute( - `systemctl start ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)}` + `systemctl start ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name)}` ); } } - public async stopService(serviceNameArg: string) { + public async stopService(serviceArg: SmartDaemonService) { if (await this.checkElegibility()) { await this.execute( - `systemctl stop ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)}` + `systemctl stop ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name)}` ); } } - public async saveService(serviceNameArg: string, serviceFileString: string) { + public async saveService(serviceArg: SmartDaemonService) { if (await this.checkElegibility()) { + if (serviceArg.alreadyExists) { + this.stopService(serviceArg); + } await plugins.smartfile.memory.toFs( - serviceFileString, - SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg) + this.smartdaemonRef.templateManager.generateUnitFileForService(serviceArg), + SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name) ); } } - public async deleteService(serviceName: string) { + public async deleteService(serviceArg: SmartDaemonService) { if (await this.checkElegibility()) { await plugins.smartfile.fs.remove( - SmartDaemonSystemdManager.createFilePathFromServiceName(serviceName) + SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name) ); } } - public async enableService(serviceName: string) { + public async enableService(serviceArg: SmartDaemonService) { if (await this.checkElegibility()) { + await this.saveService(serviceArg); await this.execute( - `systemctl enable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceName)}` + `systemctl enable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceArg.name)}` ); } } - public async disableService(serviceName: string) { + public async disableService(serviceArg: SmartDaemonService) { if (await this.checkElegibility()) { await this.execute( - `systemctl disable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceName)}` + `systemctl disable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceArg.name)}` ); } } diff --git a/ts/smartdaemon.classes.templatemanager.ts b/ts/smartdaemon.classes.templatemanager.ts index 185bfb4..4b77a85 100644 --- a/ts/smartdaemon.classes.templatemanager.ts +++ b/ts/smartdaemon.classes.templatemanager.ts @@ -1,5 +1,6 @@ import * as plugins from './smartdaemon.plugins'; import { SmartDaemon } from './smartdaemon.classes.smartdaemon'; +import { SmartDaemonService } from './smartdaemon.classes.service'; export class SmartDaemonTemplateManager { public smartdaemonRef: SmartDaemon; @@ -8,29 +9,23 @@ export class SmartDaemonTemplateManager { this.smartdaemonRef = smartdaemonRefArg; } - public generateServiceTemplate = (optionsArg: { - name: string; - description: string; - version: string; - command: string; - workkingDir; - }) => { + public generateUnitFileForService = (serviceArg: SmartDaemonService) => { return `# --- -# name: ${optionsArg.name} -# version: ${optionsArg.version} -# description: ${optionsArg.description} -# command: ${optionsArg.command} -# workingDir: ${optionsArg.workkingDir} +# name: ${serviceArg.name} +# version: ${serviceArg.version} +# description: ${serviceArg.description} +# command: ${serviceArg.command} +# workingDir: ${serviceArg.workingDir} # --- [Unit] -Description=${optionsArg.description} +Description=${serviceArg.description} Requires=network.target After=network.target [Service] Type=simple -ExecStart=/bin/bash -c "cd ${optionsArg.workkingDir} && ${optionsArg.command}" -WorkingDirectory=${optionsArg.workkingDir} +ExecStart=/bin/bash -c "cd ${serviceArg.workingDir} && ${serviceArg.command}" +WorkingDirectory=${serviceArg.workingDir} Restart=on-failure LimitNOFILE=infinity LimitCORE=infinity