4 Commits

Author SHA1 Message Date
c9aa7fed48 1.0.9 2019-09-03 19:58:08 +02:00
44d30fc4d6 fix(core): update 2019-09-03 19:58:08 +02:00
dd5e1a978d 1.0.8 2019-09-03 16:50:25 +02:00
692602b463 fix(core): update 2019-09-03 16:50:24 +02:00
6 changed files with 110 additions and 20 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartdaemon", "name": "@pushrocks/smartdaemon",
"version": "1.0.7", "version": "1.0.9",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartdaemon", "name": "@pushrocks/smartdaemon",
"version": "1.0.7", "version": "1.0.9",
"private": false, "private": false,
"description": "start scripts as long running daemons and manage them", "description": "start scripts as long running daemons and manage them",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -4,6 +4,7 @@ import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export interface SmartDaemonServiceConstructorOptions { export interface SmartDaemonServiceConstructorOptions {
name: string; name: string;
description: string;
command: string; command: string;
workingDir: string; workingDir: string;
} }
@ -17,12 +18,13 @@ export class SmartDaemonService implements SmartDaemonServiceConstructorOptions
for (const key of Object.keys(optionsArg)) { for (const key of Object.keys(optionsArg)) {
service[key] = optionsArg[key]; service[key] = optionsArg[key];
} }
return service;
} }
public options: SmartDaemonServiceConstructorOptions; public options: SmartDaemonServiceConstructorOptions;
public name: string; public name: string;
public description: string;
public command: string; public command: string;
public workingDir: string; public workingDir: string;
@ -36,18 +38,45 @@ export class SmartDaemonService implements SmartDaemonServiceConstructorOptions
* enables the service * enables the service
*/ */
public async enable() { public async enable() {
this.smartdaemonRef await this.save();
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);
} }
/** /**
* pauses the service * starts a service
*/ */
public pause() {}; public async start() {
await this.smartdaemonRef.systemdManager.startService(this.name);
}
/**
* stops a service
*/
public async stop() {
await this.smartdaemonRef.systemdManager.stopService(this.name);
}
// Save and Delete
public async save() {
await this.smartdaemonRef.systemdManager.saveService(this.name, this.smartdaemonRef.templateManager.generateServiceTemplate({
command: this.command,
description: this.description,
pathWorkkingDir: this.workingDir,
serviceName: this.name,
serviceVersion: 'x.x.x'
}));
}
/** */
public async delete() {
await this.smartdaemonRef.systemdManager.deleteService(this.name);
}
} }

View File

@ -1,6 +1,6 @@
import * as plugins from './smartdaemon.plugins'; import * as plugins from './smartdaemon.plugins';
import { SmartDaemonTemplateManager } from './smartdaemon.classes.templatemanager'; import { SmartDaemonTemplateManager } from './smartdaemon.classes.templatemanager';
import { SmartDaemonService } from './smartdaemon.classes.service'; import { SmartDaemonService, SmartDaemonServiceConstructorOptions } from './smartdaemon.classes.service';
import { SmartDaemonSystemdManager } from './smartdaemon.classes.systemdmanager'; import { SmartDaemonSystemdManager } from './smartdaemon.classes.systemdmanager';
@ -17,15 +17,23 @@ export class SmartDaemon {
this.systemdManager = new SmartDaemonSystemdManager(this); this.systemdManager = new SmartDaemonSystemdManager(this);
} }
public async addService(serviceNameArg: string, commandArg: string, workingDirectory?: string): Promise<SmartDaemonService> { /**
* adds a service
* @param nameArg
* @param commandArg
* @param workingDirectoryArg
*/
public async addService(optionsArg: SmartDaemonServiceConstructorOptions): Promise<SmartDaemonService> {
let serviceToAdd: SmartDaemonService; let serviceToAdd: SmartDaemonService;
const existingService = this.serviceMap.find(serviceArg => { const existingService = this.serviceMap.find(serviceArg => {
return serviceArg.name === serviceNameArg; return serviceArg.name === optionsArg.name;
}); });
if (!existingService) { if (!existingService) {
serviceToAdd = await SmartDaemonService.createFromOptions(this, optionsArg);
} else { } else {
serviceToAdd = existingService;
Object.assign(serviceToAdd, optionsArg);
await serviceToAdd.save();
} }
return serviceToAdd; return serviceToAdd;
}; };

View File

@ -3,7 +3,20 @@ import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon'; import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export class SmartDaemonSystemdManager { export class SmartDaemonSystemdManager {
private smartDaemonNamespace = 'smartdaemon_'; // STATIC
private static smartDaemonNamespace = 'smartdaemon_';
public static createFileNameFromServiceName = (serviceNameArg: string) => {
return `${SmartDaemonSystemdManager.smartDaemonNamespace}_${serviceNameArg}.service`;
};
public static createFilePathFromServiceName = (serviceNameArg: string) => {
return plugins.path.join(
paths.systemdDir,
SmartDaemonSystemdManager.createFileNameFromServiceName(serviceNameArg)
);
};
// INSTANCE
public smartdaemonRef: SmartDaemon; public smartdaemonRef: SmartDaemon;
public smartshellInstance: plugins.smartshell.Smartshell; public smartshellInstance: plugins.smartshell.Smartshell;
public smartsystem: plugins.smartsystem.Smartsystem; public smartsystem: plugins.smartsystem.Smartsystem;
@ -29,15 +42,56 @@ export class SmartDaemonSystemdManager {
} }
public async execute(commandArg: string) { public async execute(commandArg: string) {
(await this.checkElegibility()) ? await this.smartshellInstance.exec(commandArg) : null; if (await this.checkElegibility()) {
await this.smartshellInstance.exec(commandArg);
}
} }
public async getServices () { public async getServices() {
const availableServices = plugins.smartfile.fs.listAllItems(paths.systemdDir, new RegExp(`${this.smartDaemonNamespace}`)); if (await this.checkElegibility()) {
const availableServices = plugins.smartfile.fs.listAllItems(
paths.systemdDir,
new RegExp(`${SmartDaemonSystemdManager.smartDaemonNamespace}`)
);
}
} }
public async init() { public async startService(serviceNameArg: string) {
if (await this.checkElegibility()) {
await this.execute(`systemctl start ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)}`);
}
}; };
public async stopService(serviceNameArg: string) {
if (await this.checkElegibility()) {
await this.execute(`systemctl stop ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)}`);
}
}
public async saveService(serviceNameArg: string, serviceFileString: string) {
if (await this.checkElegibility()) {
await plugins.smartfile.memory.toFs(
serviceFileString,
SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)
);
}
}
public async deleteService(serviceName: string) {
if (await this.checkElegibility()) {
await plugins.smartfile.fs.remove(SmartDaemonSystemdManager.createFilePathFromServiceName(serviceName));
}
}
public async enableService(serviceName: string) {
if (await this.checkElegibility()) {
await this.execute(`systemctl enable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceName)}`);
}
}
public async disableService(serviceName: string) {
if (await this.checkElegibility()) {
await this.execute(`systemctl disable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceName)}`);
}
}
public async init() {}
} }

View File

@ -14,7 +14,6 @@ export class SmartDaemonTemplateManager {
serviceVersion: string; serviceVersion: string;
command: string; command: string;
pathWorkkingDir; pathWorkkingDir;
pathJsFileToRun;
}) => { }) => {
return ` return `
# servicVersion: ${optionsArg.serviceVersion} # servicVersion: ${optionsArg.serviceVersion}