6 Commits

Author SHA1 Message Date
261031a49c 1.0.10 2019-09-03 22:09:31 +02:00
615cc6aa7c fix(core): update 2019-09-03 22:09:30 +02:00
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
7 changed files with 121 additions and 22 deletions

2
package-lock.json generated
View File

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

View File

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

View File

@ -8,4 +8,13 @@ tap.test('should create an instance of smartdaemon', async () => {
expect(testSmartdaemon).to.be.instanceOf(smartdaemon.SmartDaemon);
});
tap.test('should create a service', async () => {
testSmartdaemon.addService({
command: 'npm -v',
description: 'displays the npm version',
name: 'npmversion',
workingDir: __dirname
});
});
tap.start();

View File

@ -4,6 +4,7 @@ import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export interface SmartDaemonServiceConstructorOptions {
name: string;
description: string;
command: string;
workingDir: string;
}
@ -17,12 +18,13 @@ export class SmartDaemonService implements SmartDaemonServiceConstructorOptions
for (const key of Object.keys(optionsArg)) {
service[key] = optionsArg[key];
}
return service;
}
public options: SmartDaemonServiceConstructorOptions;
public name: string;
public description: string;
public command: string;
public workingDir: string;
@ -36,18 +38,45 @@ export class SmartDaemonService implements SmartDaemonServiceConstructorOptions
* enables the service
*/
public async enable() {
this.smartdaemonRef
await this.save();
await this.smartdaemonRef.systemdManager.enableService(this.name);
}
/**
* disables the service
*/
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 { SmartDaemonTemplateManager } from './smartdaemon.classes.templatemanager';
import { SmartDaemonService } from './smartdaemon.classes.service';
import { SmartDaemonService, SmartDaemonServiceConstructorOptions } from './smartdaemon.classes.service';
import { SmartDaemonSystemdManager } from './smartdaemon.classes.systemdmanager';
@ -17,18 +17,26 @@ export class SmartDaemon {
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;
const existingService = this.serviceMap.find(serviceArg => {
return serviceArg.name === serviceNameArg;
return serviceArg.name === optionsArg.name;
});
if (!existingService) {
serviceToAdd = await SmartDaemonService.createFromOptions(this, optionsArg);
} else {
serviceToAdd = existingService;
Object.assign(serviceToAdd, optionsArg);
}
await serviceToAdd.save();
return serviceToAdd;
};
}
public async init() {
await this.systemdManager.init();

View File

@ -3,7 +3,20 @@ import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
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 smartshellInstance: plugins.smartshell.Smartshell;
public smartsystem: plugins.smartsystem.Smartsystem;
@ -22,22 +35,63 @@ export class SmartDaemonSystemdManager {
if (await this.smartsystem.env.isLinuxAsync()) {
this.shouldExecute = true;
} else {
console.log('Smartdaemon can only be used on Linuc systems! Refusing to set up a service.');
console.log('Smartdaemon can only be used on Linux systems! Refusing to set up a service.');
this.shouldExecute = false;
}
return this.shouldExecute;
}
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 () {
const availableServices = plugins.smartfile.fs.listAllItems(paths.systemdDir, new RegExp(`${this.smartDaemonNamespace}`));
public async getServices() {
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;
command: string;
pathWorkkingDir;
pathJsFileToRun;
}) => {
return `
# servicVersion: ${optionsArg.serviceVersion}