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 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);
}
}

View File

@ -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)}`
);
}
}

View File

@ -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