fix(core): update

This commit is contained in:
Philipp Kunz 2019-09-05 13:50:11 +02:00
parent 80f5df3317
commit 09e9d8c190
3 changed files with 37 additions and 44 deletions

View File

@ -23,6 +23,7 @@ export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions
} }
public options: ISmartDaemonServiceConstructorOptions; public options: ISmartDaemonServiceConstructorOptions;
public alreadyExists = false;
public name: string; public name: string;
public version: string; public version: string;
@ -40,48 +41,40 @@ export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions
* enables the service * enables the service
*/ */
public async enable() { public async enable() {
await this.save(); await this.smartdaemonRef.systemdManager.enableService(this);
await this.smartdaemonRef.systemdManager.enableService(this.name);
} }
/** /**
* disables the service * disables the service
*/ */
public async disable() { public async disable() {
await this.smartdaemonRef.systemdManager.disableService(this.name); await this.smartdaemonRef.systemdManager.disableService(this);
} }
/** /**
* starts a service * starts a service
*/ */
public async start() { public async start() {
await this.smartdaemonRef.systemdManager.startService(this.name); await this.smartdaemonRef.systemdManager.startService(this);
} }
/** /**
* stops a service * stops a service
*/ */
public async stop() { public async stop() {
await this.smartdaemonRef.systemdManager.stopService(this.name); await this.smartdaemonRef.systemdManager.stopService(this);
} }
// Save and Delete // Save and Delete
public async save() { public async save() {
const serviceTemplate = this.smartdaemonRef.templateManager.generateServiceTemplate({ await this.smartdaemonRef.systemdManager.saveService(this);
name: this.name,
version: this.version,
command: this.command,
workkingDir: this.workingDir,
description: this.description,
});
await this.smartdaemonRef.systemdManager.saveService(this.name, serviceTemplate);
} }
/** /**
* deletes the service * deletes the service
*/ */
public async delete() { public async delete() {
await this.smartdaemonRef.systemdManager.deleteService(this.name); await this.smartdaemonRef.systemdManager.deleteService(this);
} }
} }

View File

@ -52,7 +52,7 @@ export class SmartDaemonSystemdManager {
* gets all services that are already present * gets all services that are already present
*/ */
public async getServices() { public async getServices() {
const existingServices = []; const existingServices: SmartDaemonService[] = [];
if (await this.checkElegibility()) { if (await this.checkElegibility()) {
const smartfmInstance = new plugins.smartfm.Smartfm({ const smartfmInstance = new plugins.smartfm.Smartfm({
fmType: 'yaml' fmType: 'yaml'
@ -64,56 +64,61 @@ export class SmartDaemonSystemdManager {
for (const serviceFile of availableServices) { for (const serviceFile of availableServices) {
const data = smartfmInstance.parseFromComments('# ', serviceFile.contentBuffer.toString()) const data = smartfmInstance.parseFromComments('# ', serviceFile.contentBuffer.toString())
.data as ISmartDaemonServiceConstructorOptions; .data as ISmartDaemonServiceConstructorOptions;
const service = SmartDaemonService.createFromOptions(this.smartdaemonRef, data); const service = await SmartDaemonService.createFromOptions(this.smartdaemonRef, data);
service.alreadyExists = true;
existingServices.push(service); existingServices.push(service);
} }
} }
return existingServices; return existingServices;
} }
public async startService(serviceNameArg: string) { public async startService(serviceArg: SmartDaemonService) {
if (await this.checkElegibility()) { if (await this.checkElegibility()) {
await this.execute( 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()) { if (await this.checkElegibility()) {
await this.execute( 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 (await this.checkElegibility()) {
if (serviceArg.alreadyExists) {
this.stopService(serviceArg);
}
await plugins.smartfile.memory.toFs( await plugins.smartfile.memory.toFs(
serviceFileString, this.smartdaemonRef.templateManager.generateUnitFileForService(serviceArg),
SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg) SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name)
); );
} }
} }
public async deleteService(serviceName: string) { public async deleteService(serviceArg: SmartDaemonService) {
if (await this.checkElegibility()) { if (await this.checkElegibility()) {
await plugins.smartfile.fs.remove( 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()) { if (await this.checkElegibility()) {
await this.saveService(serviceArg);
await this.execute( 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()) { if (await this.checkElegibility()) {
await this.execute( await this.execute(
`systemctl disable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceName)}` `systemctl disable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceArg.name)}`
); );
} }
} }

View File

@ -1,5 +1,6 @@
import * as plugins from './smartdaemon.plugins'; import * as plugins from './smartdaemon.plugins';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon'; import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
import { SmartDaemonService } from './smartdaemon.classes.service';
export class SmartDaemonTemplateManager { export class SmartDaemonTemplateManager {
public smartdaemonRef: SmartDaemon; public smartdaemonRef: SmartDaemon;
@ -8,29 +9,23 @@ export class SmartDaemonTemplateManager {
this.smartdaemonRef = smartdaemonRefArg; this.smartdaemonRef = smartdaemonRefArg;
} }
public generateServiceTemplate = (optionsArg: { public generateUnitFileForService = (serviceArg: SmartDaemonService) => {
name: string;
description: string;
version: string;
command: string;
workkingDir;
}) => {
return `# --- return `# ---
# name: ${optionsArg.name} # name: ${serviceArg.name}
# version: ${optionsArg.version} # version: ${serviceArg.version}
# description: ${optionsArg.description} # description: ${serviceArg.description}
# command: ${optionsArg.command} # command: ${serviceArg.command}
# workingDir: ${optionsArg.workkingDir} # workingDir: ${serviceArg.workingDir}
# --- # ---
[Unit] [Unit]
Description=${optionsArg.description} Description=${serviceArg.description}
Requires=network.target Requires=network.target
After=network.target After=network.target
[Service] [Service]
Type=simple Type=simple
ExecStart=/bin/bash -c "cd ${optionsArg.workkingDir} && ${optionsArg.command}" ExecStart=/bin/bash -c "cd ${serviceArg.workingDir} && ${serviceArg.command}"
WorkingDirectory=${optionsArg.workkingDir} WorkingDirectory=${serviceArg.workingDir}
Restart=on-failure Restart=on-failure
LimitNOFILE=infinity LimitNOFILE=infinity
LimitCORE=infinity LimitCORE=infinity