8 Commits

Author SHA1 Message Date
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 412 additions and 307 deletions

View File

@ -1,4 +1,5 @@
Copyright (c) 2019 Lossless GmbH (hello@lossless.com) 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 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal 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", "name": "@pushrocks/smartdaemon",
"version": "1.0.2", "version": "1.0.6",
"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",
@ -13,14 +13,21 @@
"format": "(gitzone format)" "format": "(gitzone format)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.0.22", "@gitzone/tsbuild": "^2.1.17",
"@gitzone/tstest": "^1.0.15", "@gitzone/tstest": "^1.0.15",
"@pushrocks/tapbundle": "^3.0.7", "@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^10.11.7", "@types/node": "^10.11.7",
"tslint": "^5.11.0", "tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.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": [ "files": [
"ts/*", "ts/*",
"ts_web/*", "ts_web/*",

View File

@ -1,8 +1,11 @@
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import * as smartdaemon from '../ts/index'; import * as smartdaemon from '../ts/index';
tap.test('first test', async () => { let testSmartdaemon: smartdaemon.SmartDaemon;
console.log(smartdaemon.standardExport);
tap.test('should create an instance of smartdaemon', async () => {
testSmartdaemon = new smartdaemon.SmartDaemon();
expect(testSmartdaemon).to.be.instanceOf(smartdaemon.SmartDaemon);
}); });
tap.start(); tap.start();

View File

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

View File

@ -0,0 +1,53 @@
import * as plugins from './smartdaemon.plugins';
import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export interface SmartDaemonServiceConstructorOptions {
name: 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];
}
}
public options: SmartDaemonServiceConstructorOptions;
public name: string;
public command: string;
public workingDir: string;
public smartdaemonRef: SmartDaemon;
constructor(smartdaemonRegfArg: SmartDaemon) {
this.smartdaemonRef = smartdaemonRegfArg;
}
/**
* enables the service
*/
public async enable() {
this.smartdaemonRef
}
/**
* disables the service
*/
public async disable() {
}
/**
* pauses the service
*/
public pause() {};
}

View File

@ -0,0 +1,29 @@
import * as plugins from './smartdaemon.plugins';
import { SmartDaemonTemplateManager } from './smartdaemon.classes.templatemanager';
import { SmartDaemonService } 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);
}
public async addService(serviceName: string, workingDirectory): Promise<SmartDaemonService> {
const existingService = this.serviceMap.find(serviceArg => {
return serviceArg
})
};
public async init() {
await this.systemdManager.init();
}
}

View File

@ -0,0 +1,43 @@
import * as plugins from './smartdaemon.plugins';
import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
export class SmartDaemonSystemdManager {
private smartDaemonNamespace = 'smartdaemon_';
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 Linuc 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;
}
public async getServices () {
const availableServices = plugins.smartfile.fs.listAllItems(paths.systemdDir, new RegExp(`${this.smartDaemonNamespace}`));
}
public async init() {
};
}

View File

@ -0,0 +1,41 @@
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;
pathJsFileToRun;
}) => {
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,32 @@
const removeme = {}; // node native scope
export { removeme }; 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
import * as fs from 'fs-extra';
export {
fs
};

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