fix(core): update

This commit is contained in:
2019-09-05 11:15:17 +02:00
parent ceb30c7ac2
commit 6cf3ff6e83
8 changed files with 121 additions and 33 deletions

View File

@ -2,18 +2,19 @@ import * as plugins from './smartdaemon.plugins';
import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export interface SmartDaemonServiceConstructorOptions {
export interface ISmartDaemonServiceConstructorOptions {
name: string;
description: string;
command: string;
workingDir: string;
version: string;
}
/**
* represents a service that is being spawned by SmartDaemon
*/
export class SmartDaemonService implements SmartDaemonServiceConstructorOptions {
public static async createFromOptions(smartdaemonRef: SmartDaemon, optionsArg: SmartDaemonServiceConstructorOptions) {
export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions {
public static async createFromOptions(smartdaemonRef: SmartDaemon, optionsArg: ISmartDaemonServiceConstructorOptions) {
const service = new SmartDaemonService(smartdaemonRef);
for (const key of Object.keys(optionsArg)) {
service[key] = optionsArg[key];
@ -21,12 +22,13 @@ export class SmartDaemonService implements SmartDaemonServiceConstructorOptions
return service;
}
public options: SmartDaemonServiceConstructorOptions;
public options: ISmartDaemonServiceConstructorOptions;
public name: string;
public description: string;
public version: string;
public command: string;
public workingDir: string;
public description: string;
public smartdaemonRef: SmartDaemon;
@ -66,16 +68,19 @@ export class SmartDaemonService implements SmartDaemonServiceConstructorOptions
// Save and Delete
public async save() {
await this.smartdaemonRef.systemdManager.saveService(this.name, this.smartdaemonRef.templateManager.generateServiceTemplate({
const serviceTemplate = this.smartdaemonRef.templateManager.generateServiceTemplate({
name: this.name,
version: this.version,
command: this.command,
workkingDir: this.workingDir,
description: this.description,
pathWorkkingDir: this.workingDir,
serviceName: this.name,
serviceVersion: 'x.x.x'
}));
});
await this.smartdaemonRef.systemdManager.saveService(this.name, serviceTemplate);
}
/** */
/**
* deletes the service
*/
public async delete() {
await this.smartdaemonRef.systemdManager.deleteService(this.name);
}

View File

@ -1,18 +1,15 @@
import * as plugins from './smartdaemon.plugins';
import { SmartDaemonTemplateManager } from './smartdaemon.classes.templatemanager';
import { SmartDaemonService, SmartDaemonServiceConstructorOptions } from './smartdaemon.classes.service';
import { SmartDaemonService, ISmartDaemonServiceConstructorOptions } from './smartdaemon.classes.service';
import { SmartDaemonSystemdManager } from './smartdaemon.classes.systemdmanager';
export class SmartDaemon {
public serviceMap: plugins.lik.Objectmap<SmartDaemonService>;
public templateManager: SmartDaemonTemplateManager;
public systemdManager: SmartDaemonSystemdManager;
constructor() {
this.serviceMap = new plugins.lik.Objectmap<SmartDaemonService>();
this.templateManager = new SmartDaemonTemplateManager(this);
this.systemdManager = new SmartDaemonSystemdManager(this);
}
@ -23,9 +20,10 @@ export class SmartDaemon {
* @param commandArg
* @param workingDirectoryArg
*/
public async addService(optionsArg: SmartDaemonServiceConstructorOptions): Promise<SmartDaemonService> {
public async addService(optionsArg: ISmartDaemonServiceConstructorOptions): Promise<SmartDaemonService> {
let serviceToAdd: SmartDaemonService;
const existingService = this.serviceMap.find(serviceArg => {
const existingServices = await this.systemdManager.getServices();
const existingService = existingServices.find(serviceArg => {
return serviceArg.name === optionsArg.name;
});
if (!existingService) {

View File

@ -1,6 +1,7 @@
import * as plugins from './smartdaemon.plugins';
import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
import { ISmartDaemonServiceConstructorOptions, SmartDaemonService } from './smartdaemon.classes.service';
export class SmartDaemonSystemdManager {
// STATIC
@ -47,23 +48,41 @@ export class SmartDaemonSystemdManager {
}
}
/**
* gets all services that are already present
*/
public async getServices() {
const existingServices = [];
if (await this.checkElegibility()) {
const availableServices = plugins.smartfile.fs.listAllItems(
const smartfmInstance = new plugins.smartfm.Smartfm({
fmType: 'yaml'
});
const availableServices = await plugins.smartfile.fs.fileTreeToObject(
paths.systemdDir,
new RegExp(`${SmartDaemonSystemdManager.smartDaemonNamespace}`)
'smartdaemon_*.service'
);
for (const serviceFile of availableServices) {
const data = smartfmInstance.parseFromComments('# ', serviceFile.contentBuffer.toString())
.data as ISmartDaemonServiceConstructorOptions;
const service = SmartDaemonService.createFromOptions(this.smartdaemonRef, data);
existingServices.push(service);
}
}
return existingServices;
}
public async startService(serviceNameArg: string) {
if (await this.checkElegibility()) {
await this.execute(`systemctl start ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)}`);
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)}`);
await this.execute(
`systemctl stop ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)}`
);
}
}
@ -78,18 +97,24 @@ export class SmartDaemonSystemdManager {
public async deleteService(serviceName: string) {
if (await this.checkElegibility()) {
await plugins.smartfile.fs.remove(SmartDaemonSystemdManager.createFilePathFromServiceName(serviceName));
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)}`);
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)}`);
await this.execute(
`systemctl disable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceName)}`
);
}
}

View File

@ -9,14 +9,19 @@ export class SmartDaemonTemplateManager {
}
public generateServiceTemplate = (optionsArg: {
serviceName: string;
name: string;
description: string;
serviceVersion: string;
version: string;
command: string;
pathWorkkingDir;
workkingDir;
}) => {
return `
# servicVersion: ${optionsArg.serviceVersion}
return `# ---
# name: ${optionsArg.name}
# version: ${optionsArg.version}
# description: ${optionsArg.description}
# command: ${optionsArg.command}
# workingDir: ${optionsArg.workkingDir}
# ---
[Unit]
Description=${optionsArg.description}
Requires=network.target
@ -24,8 +29,8 @@ After=network.target
[Service]
Type=simple
ExecStart=/bin/bash -c "cd ${optionsArg.pathWorkkingDir} && ${optionsArg.command}"
WorkingDirectory=${optionsArg.pathWorkkingDir}
ExecStart=/bin/bash -c "cd ${optionsArg.workkingDir} && ${optionsArg.command}"
WorkingDirectory=${optionsArg.workkingDir}
Restart=on-failure
LimitNOFILE=infinity
LimitCORE=infinity

View File

@ -9,6 +9,7 @@ export {
// @pushrocks scope
import * as lik from '@pushrocks/lik';
import * as smartfile from '@pushrocks/smartfile';
import * as smartfm from '@pushrocks/smartfm';
import * as smartlog from '@pushrocks/smartlog';
import * as smartlogDestinationLocal from '@pushrocks/smartlog-destination-local';
import * as smartshell from '@pushrocks/smartshell';
@ -17,6 +18,7 @@ import * as smartsystem from '@pushrocks/smartsystem';
export {
lik,
smartfile,
smartfm,
smartlog,
smartlogDestinationLocal,
smartshell,