18 Commits

Author SHA1 Message Date
ceb30c7ac2 1.0.11 2019-09-04 16:51:58 +02:00
0df90eec5d fix(core): update 2019-09-04 16:51:58 +02:00
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
382b694027 1.0.7 2019-09-03 15:24:50 +02:00
de831b086f fix(core): update 2019-09-03 15:24:49 +02:00
01c7d2e482 1.0.6 2019-09-03 15:21:30 +02:00
49ebf991a2 fix(core): update 2019-09-03 15:21:30 +02:00
513337355f 1.0.5 2019-09-03 11:29:14 +02:00
5e5a679f99 fix(core): update 2019-09-03 11:29:14 +02:00
9ef366eee9 1.0.4 2019-08-28 11:55:14 +02:00
10562afea1 fix(core): update 2019-08-28 11:55:14 +02:00
c4e3f628cd 1.0.3 2019-08-28 08:27:12 +02:00
40e6a7abe4 fix(core): update 2019-08-28 08:27:11 +02:00
12 changed files with 512 additions and 307 deletions

View File

@ -1,4 +1,5 @@
Copyright (c) 2019 Lossless GmbH (hello@lossless.com)
Copyright (c) 2017-2019, braces lab
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

473
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartdaemon",
"version": "1.0.2",
"version": "1.0.11",
"private": false,
"description": "start scripts as long running daemons and manage them",
"main": "dist/index.js",
@ -13,14 +13,21 @@
"format": "(gitzone format)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.0.22",
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tstest": "^1.0.15",
"@pushrocks/tapbundle": "^3.0.7",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^10.11.7",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0"
},
"dependencies": {},
"dependencies": {
"@pushrocks/lik": "^3.0.11",
"@pushrocks/smartfile": "^7.0.4",
"@pushrocks/smartlog": "^2.0.19",
"@pushrocks/smartlog-destination-local": "^8.0.2",
"@pushrocks/smartshell": "^2.0.25",
"@pushrocks/smartsystem": "^2.0.8"
},
"files": [
"ts/*",
"ts_web/*",

View File

@ -1,8 +1,20 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartdaemon from '../ts/index';
tap.test('first test', async () => {
console.log(smartdaemon.standardExport);
let testSmartdaemon: smartdaemon.SmartDaemon;
tap.test('should create an instance of smartdaemon', async () => {
testSmartdaemon = new smartdaemon.SmartDaemon();
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

@ -1,3 +1 @@
import * as plugins from './smartdaemon.plugins';
export let standardExport = 'Hi there! :) This is an exported string';
export * from './smartdaemon.classes.smartdaemon';

View File

@ -0,0 +1,82 @@
import * as plugins from './smartdaemon.plugins';
import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export interface SmartDaemonServiceConstructorOptions {
name: string;
description: string;
command: string;
workingDir: string;
}
/**
* represents a service that is being spawned by SmartDaemon
*/
export class SmartDaemonService implements SmartDaemonServiceConstructorOptions {
public static async createFromOptions(smartdaemonRef: SmartDaemon, optionsArg: SmartDaemonServiceConstructorOptions) {
const service = new SmartDaemonService(smartdaemonRef);
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;
public smartdaemonRef: SmartDaemon;
constructor(smartdaemonRegfArg: SmartDaemon) {
this.smartdaemonRef = smartdaemonRegfArg;
}
/**
* enables the service
*/
public async enable() {
await this.save();
await this.smartdaemonRef.systemdManager.enableService(this.name);
}
/**
* disables the service
*/
public async disable() {
await this.smartdaemonRef.systemdManager.disableService(this.name);
}
/**
* starts a service
*/
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

@ -0,0 +1,44 @@
import * as plugins from './smartdaemon.plugins';
import { SmartDaemonTemplateManager } from './smartdaemon.classes.templatemanager';
import { SmartDaemonService, SmartDaemonServiceConstructorOptions } 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);
}
/**
* 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 === 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

@ -0,0 +1,97 @@
import * as plugins from './smartdaemon.plugins';
import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export class SmartDaemonSystemdManager {
// 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;
public shouldExecute: boolean = false;
constructor(smartdaemonRefArg: SmartDaemon) {
this.smartdaemonRef = smartdaemonRefArg;
this.smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash'
});
this.smartsystem = new plugins.smartsystem.Smartsystem();
}
public async checkElegibility() {
if (await this.smartsystem.env.isLinuxAsync()) {
this.shouldExecute = true;
} else {
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) {
if (await this.checkElegibility()) {
await this.smartshellInstance.exec(commandArg);
}
}
public async getServices() {
if (await this.checkElegibility()) {
const availableServices = plugins.smartfile.fs.listAllItems(
paths.systemdDir,
new RegExp(`${SmartDaemonSystemdManager.smartDaemonNamespace}`)
);
}
}
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

@ -0,0 +1,40 @@
import * as plugins from './smartdaemon.plugins';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export class SmartDaemonTemplateManager {
public smartdaemonRef: SmartDaemon;
constructor(smartdaemonRefArg: SmartDaemon) {
this.smartdaemonRef = smartdaemonRefArg;
}
public generateServiceTemplate = (optionsArg: {
serviceName: string;
description: string;
serviceVersion: string;
command: string;
pathWorkkingDir;
}) => {
return `
# servicVersion: ${optionsArg.serviceVersion}
[Unit]
Description=${optionsArg.description}
Requires=network.target
After=network.target
[Service]
Type=simple
ExecStart=/bin/bash -c "cd ${optionsArg.pathWorkkingDir} && ${optionsArg.command}"
WorkingDirectory=${optionsArg.pathWorkkingDir}
Restart=on-failure
LimitNOFILE=infinity
LimitCORE=infinity
StandardInput=null
StandardOutput=syslog
StandardError=syslog
Restart=always
[Install]
WantedBy=multi-user.target
`;
}
}

4
ts/smartdaemon.paths.ts Normal file
View File

@ -0,0 +1,4 @@
import * as plugins from './smartdaemon.plugins';
export const packageDir = plugins.path.join(__dirname, '../');
export const systemdDir = plugins.path.join('/lib/systemd/system/');

View File

@ -1,2 +1,26 @@
const removeme = {};
export { removeme };
// node native scope
import * as path from 'path';
export {
path
};
// @pushrocks scope
import * as lik from '@pushrocks/lik';
import * as smartfile from '@pushrocks/smartfile';
import * as smartlog from '@pushrocks/smartlog';
import * as smartlogDestinationLocal from '@pushrocks/smartlog-destination-local';
import * as smartshell from '@pushrocks/smartshell';
import * as smartsystem from '@pushrocks/smartsystem';
export {
lik,
smartfile,
smartlog,
smartlogDestinationLocal,
smartshell,
smartsystem
};
// third party

15
ts/smartdamon.logging.ts Normal file
View File

@ -0,0 +1,15 @@
import * as plugins from './smartdaemon.plugins';
export const logger = new plugins.smartlog.Smartlog({
logContext: {
company: 'Some Company',
companyunit: 'Some CompanyUnit',
containerName: 'Some Containername',
environment: 'local',
runtime: 'node',
zone: 'gitzone'
},
minimumLogLevel: 'silly'
});
logger.addLogDestination(new plugins.smartlogDestinationLocal.DestinationLocal());