Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
119f20915e | |||
ce1fa6640b | |||
8957e03445 | |||
3df86bee10 | |||
65326710ab | |||
2e174c9f55 | |||
9e42910456 | |||
ff85cee528 | |||
2a9fff0185 | |||
67689d79bd | |||
f8c851de97 | |||
09e9d8c190 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdaemon",
|
||||
"version": "1.0.12",
|
||||
"version": "1.0.18",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdaemon",
|
||||
"version": "1.0.12",
|
||||
"version": "1.0.18",
|
||||
"private": false,
|
||||
"description": "start scripts as long running daemons and manage them",
|
||||
"main": "dist/index.js",
|
||||
|
@ -23,6 +23,7 @@ export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions
|
||||
}
|
||||
|
||||
public options: ISmartDaemonServiceConstructorOptions;
|
||||
public alreadyExists = false;
|
||||
|
||||
public name: string;
|
||||
public version: string;
|
||||
@ -40,48 +41,44 @@ export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions
|
||||
* enables the service
|
||||
*/
|
||||
public async enable() {
|
||||
await this.save();
|
||||
await this.smartdaemonRef.systemdManager.enableService(this.name);
|
||||
await this.smartdaemonRef.systemdManager.enableService(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* disables the service
|
||||
*/
|
||||
public async disable() {
|
||||
await this.smartdaemonRef.systemdManager.disableService(this.name);
|
||||
await this.smartdaemonRef.systemdManager.disableService(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* starts a service
|
||||
*/
|
||||
public async start() {
|
||||
await this.smartdaemonRef.systemdManager.startService(this.name);
|
||||
await this.smartdaemonRef.systemdManager.startService(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* stops a service
|
||||
*/
|
||||
public async stop() {
|
||||
await this.smartdaemonRef.systemdManager.stopService(this.name);
|
||||
await this.smartdaemonRef.systemdManager.stopService(this);
|
||||
}
|
||||
|
||||
|
||||
// Save and Delete
|
||||
public async save() {
|
||||
const serviceTemplate = this.smartdaemonRef.templateManager.generateServiceTemplate({
|
||||
name: this.name,
|
||||
version: this.version,
|
||||
command: this.command,
|
||||
workkingDir: this.workingDir,
|
||||
description: this.description,
|
||||
});
|
||||
await this.smartdaemonRef.systemdManager.saveService(this.name, serviceTemplate);
|
||||
await this.smartdaemonRef.systemdManager.saveService(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes the service
|
||||
*/
|
||||
public async delete() {
|
||||
await this.smartdaemonRef.systemdManager.deleteService(this.name);
|
||||
await this.smartdaemonRef.systemdManager.deleteService(this);
|
||||
}
|
||||
|
||||
public async reload() {
|
||||
await this.smartdaemonRef.systemdManager.reload();
|
||||
}
|
||||
}
|
||||
|
@ -35,8 +35,4 @@ export class SmartDaemon {
|
||||
await serviceToAdd.save();
|
||||
return serviceToAdd;
|
||||
}
|
||||
|
||||
public async init() {
|
||||
await this.systemdManager.init();
|
||||
}
|
||||
}
|
||||
|
@ -6,16 +6,21 @@ import { ISmartDaemonServiceConstructorOptions, SmartDaemonService } from './sma
|
||||
export class SmartDaemonSystemdManager {
|
||||
// STATIC
|
||||
private static smartDaemonNamespace = 'smartdaemon';
|
||||
public static createFileNameFromServiceName = (serviceNameArg: string) => {
|
||||
return `${SmartDaemonSystemdManager.smartDaemonNamespace}_${serviceNameArg}.service`;
|
||||
|
||||
public static createServiceNameFromServiceName (serviceNameArg: string) {
|
||||
return `${SmartDaemonSystemdManager.smartDaemonNamespace}_${serviceNameArg}`;
|
||||
}
|
||||
|
||||
public static createFileNameFromServiceName (serviceNameArg: string) {
|
||||
return `${SmartDaemonSystemdManager.createServiceNameFromServiceName(serviceNameArg)}.service`;
|
||||
};
|
||||
|
||||
public static createFilePathFromServiceName = (serviceNameArg: string) => {
|
||||
public static createFilePathFromServiceName (serviceNameArg: string) {
|
||||
return plugins.path.join(
|
||||
paths.systemdDir,
|
||||
SmartDaemonSystemdManager.createFileNameFromServiceName(serviceNameArg)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public smartdaemonRef: SmartDaemon;
|
||||
@ -52,7 +57,7 @@ export class SmartDaemonSystemdManager {
|
||||
* gets all services that are already present
|
||||
*/
|
||||
public async getServices() {
|
||||
const existingServices = [];
|
||||
const existingServices: SmartDaemonService[] = [];
|
||||
if (await this.checkElegibility()) {
|
||||
const smartfmInstance = new plugins.smartfm.Smartfm({
|
||||
fmType: 'yaml'
|
||||
@ -64,59 +69,70 @@ export class SmartDaemonSystemdManager {
|
||||
for (const serviceFile of availableServices) {
|
||||
const data = smartfmInstance.parseFromComments('# ', serviceFile.contentBuffer.toString())
|
||||
.data as ISmartDaemonServiceConstructorOptions;
|
||||
const service = SmartDaemonService.createFromOptions(this.smartdaemonRef, data);
|
||||
const service = await SmartDaemonService.createFromOptions(this.smartdaemonRef, data);
|
||||
service.alreadyExists = true;
|
||||
existingServices.push(service);
|
||||
}
|
||||
}
|
||||
return existingServices;
|
||||
}
|
||||
|
||||
public async startService(serviceNameArg: string) {
|
||||
public async startService(serviceArg: SmartDaemonService) {
|
||||
if (await this.checkElegibility()) {
|
||||
await this.execute(
|
||||
`systemctl start ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)}`
|
||||
`systemctl start ${SmartDaemonSystemdManager.createServiceNameFromServiceName(serviceArg.name)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
public async stopService(serviceNameArg: string) {
|
||||
public async stopService(serviceArg: SmartDaemonService) {
|
||||
if (await this.checkElegibility()) {
|
||||
await this.execute(
|
||||
`systemctl stop ${SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)}`
|
||||
`systemctl stop ${SmartDaemonSystemdManager.createServiceNameFromServiceName(serviceArg.name)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public async saveService(serviceNameArg: string, serviceFileString: string) {
|
||||
public async saveService(serviceArg: SmartDaemonService) {
|
||||
if (await this.checkElegibility()) {
|
||||
await plugins.smartfile.memory.toFs(
|
||||
serviceFileString,
|
||||
SmartDaemonSystemdManager.createFilePathFromServiceName(serviceNameArg)
|
||||
this.smartdaemonRef.templateManager.generateUnitFileForService(serviceArg),
|
||||
SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteService(serviceName: string) {
|
||||
public async deleteService(serviceArg: SmartDaemonService) {
|
||||
if (await this.checkElegibility()) {
|
||||
await plugins.smartfile.fs.remove(
|
||||
SmartDaemonSystemdManager.createFilePathFromServiceName(serviceName)
|
||||
SmartDaemonSystemdManager.createServiceNameFromServiceName(serviceArg.name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public async enableService(serviceName: string) {
|
||||
public async enableService(serviceArg: SmartDaemonService) {
|
||||
if (await this.checkElegibility()) {
|
||||
await this.saveService(serviceArg);
|
||||
if (serviceArg.alreadyExists) {
|
||||
await this.execute(`systemctl daemon-reload`);
|
||||
}
|
||||
await this.execute(
|
||||
`systemctl enable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceName)}`
|
||||
`systemctl enable ${SmartDaemonSystemdManager.createServiceNameFromServiceName(serviceArg.name)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
public async disableService(serviceName: string) {
|
||||
public async disableService(serviceArg: SmartDaemonService) {
|
||||
if (await this.checkElegibility()) {
|
||||
await this.execute(
|
||||
`systemctl disable ${SmartDaemonSystemdManager.createFileNameFromServiceName(serviceName)}`
|
||||
`systemctl disable ${SmartDaemonSystemdManager.createServiceNameFromServiceName(serviceArg.name)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public async init() {}
|
||||
public async reload() {
|
||||
if (await this.checkElegibility()) {
|
||||
await this.execute(
|
||||
`systemctl daemon-reload`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import * as plugins from './smartdaemon.plugins';
|
||||
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
|
||||
import { SmartDaemonService } from './smartdaemon.classes.service';
|
||||
|
||||
export class SmartDaemonTemplateManager {
|
||||
public smartdaemonRef: SmartDaemon;
|
||||
@ -8,29 +9,23 @@ export class SmartDaemonTemplateManager {
|
||||
this.smartdaemonRef = smartdaemonRefArg;
|
||||
}
|
||||
|
||||
public generateServiceTemplate = (optionsArg: {
|
||||
name: string;
|
||||
description: string;
|
||||
version: string;
|
||||
command: string;
|
||||
workkingDir;
|
||||
}) => {
|
||||
public generateUnitFileForService = (serviceArg: SmartDaemonService) => {
|
||||
return `# ---
|
||||
# name: ${optionsArg.name}
|
||||
# version: ${optionsArg.version}
|
||||
# description: ${optionsArg.description}
|
||||
# command: ${optionsArg.command}
|
||||
# workingDir: ${optionsArg.workkingDir}
|
||||
# name: ${serviceArg.name}
|
||||
# version: ${serviceArg.version}
|
||||
# description: ${serviceArg.description}
|
||||
# command: ${serviceArg.command}
|
||||
# workingDir: ${serviceArg.workingDir}
|
||||
# ---
|
||||
[Unit]
|
||||
Description=${optionsArg.description}
|
||||
Description=${serviceArg.description}
|
||||
Requires=network.target
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/bin/bash -c "cd ${optionsArg.workkingDir} && ${optionsArg.command}"
|
||||
WorkingDirectory=${optionsArg.workkingDir}
|
||||
ExecStart=/bin/bash -c "cd ${serviceArg.workingDir} && ${serviceArg.command}"
|
||||
WorkingDirectory=${serviceArg.workingDir}
|
||||
Restart=on-failure
|
||||
LimitNOFILE=infinity
|
||||
LimitCORE=infinity
|
||||
|
Reference in New Issue
Block a user