updated and fixed some stuff
This commit is contained in:
parent
fe1a21f222
commit
797c32d5c6
6 changed files with 41 additions and 27 deletions
6
.vscode/launch.json
vendored
6
.vscode/launch.json
vendored
|
@ -1,13 +1,11 @@
|
||||||
{
|
{
|
||||||
// Use IntelliSense to learn about possible attributes.
|
|
||||||
// Hover to view descriptions of existing attributes.
|
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"type": "pwa-node",
|
"type": "pwa-node",
|
||||||
|
"runtimeVersion": "16",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "Launch Program",
|
"name": "remex",
|
||||||
"skipFiles": [
|
"skipFiles": [
|
||||||
"<node_internals>/**"
|
"<node_internals>/**"
|
||||||
],
|
],
|
||||||
|
|
15
config.json
15
config.json
|
@ -8,14 +8,21 @@
|
||||||
"timestamp": "DD.MM.YYYY HH:mm:ss:SS"
|
"timestamp": "DD.MM.YYYY HH:mm:ss:SS"
|
||||||
},
|
},
|
||||||
"api": [
|
"api": [
|
||||||
|
{
|
||||||
|
"url": "/tail",
|
||||||
|
"type": "get",
|
||||||
|
"command": "tail",
|
||||||
|
"args": [
|
||||||
|
"-f", "/tmp/test"
|
||||||
|
],
|
||||||
|
"detach": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"url": "/uptime",
|
"url": "/uptime",
|
||||||
"type": "get",
|
"type": "get",
|
||||||
"command": "uptime1",
|
"command": "uptime",
|
||||||
"args": [
|
"args": [
|
||||||
],
|
]
|
||||||
"passargs": true,
|
|
||||||
"detach": false
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -7,6 +7,10 @@ async function execute(endpoint) {
|
||||||
if (endpoint === undefined) {
|
if (endpoint === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (isCommandActive(endpoint)) {
|
||||||
|
logger.info('not executing command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') because it is already active');
|
||||||
|
throw new Error('command is already active');
|
||||||
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
logger.info('executing command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...');
|
logger.info('executing command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...');
|
||||||
var cmd = spawn(endpoint.command, endpoint.args);
|
var cmd = spawn(endpoint.command, endpoint.args);
|
||||||
|
@ -22,14 +26,14 @@ async function execute(endpoint) {
|
||||||
cmd.on('spawn', () => {
|
cmd.on('spawn', () => {
|
||||||
logger.info('spawned command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')');
|
logger.info('spawned command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')');
|
||||||
addCommand(cmd, endpoint);
|
addCommand(cmd, endpoint);
|
||||||
if (endpoint.detach !== false) {
|
if (endpoint.detach === true) {
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
cmd.on('error', (err) => {
|
cmd.on('error', (err) => {
|
||||||
error += err;
|
error += err;
|
||||||
removeCommand(endpoint);
|
removeCommand(endpoint);
|
||||||
if (endpoint.detach !== false) {
|
if (endpoint.detach === true) {
|
||||||
reject();
|
reject();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -38,12 +42,14 @@ async function execute(endpoint) {
|
||||||
let fn = logger.info;
|
let fn = logger.info;
|
||||||
let msg = 'command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') finished with exit code ' + code + ' after ' + (new Date().getTime() - cmd.timestamp) + 'ms';
|
let msg = 'command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') finished with exit code ' + code + ' after ' + (new Date().getTime() - cmd.timestamp) + 'ms';
|
||||||
if (error !== undefined && error.length > 0) {
|
if (error !== undefined && error.length > 0) {
|
||||||
msg += ' >>> ' + error;
|
error = error.trim();
|
||||||
|
msg += ' > error: ' + error;
|
||||||
fn = logger.error;
|
fn = logger.error;
|
||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
if (result !== undefined && result.length > 0) {
|
if (result !== undefined && result.length > 0) {
|
||||||
msg += ' > ' + result;
|
result = result.trim();
|
||||||
|
msg += ' > data: ' + result;
|
||||||
}
|
}
|
||||||
fn(msg);
|
fn(msg);
|
||||||
resolve(result);
|
resolve(result);
|
||||||
|
|
|
@ -113,11 +113,7 @@ function error(message) {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (message.message) {
|
trace(message.toString(), 'error');
|
||||||
trace(message.message, 'error');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
trace(message, 'error');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// default logging function
|
// default logging function
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
const logger = require('../libs/logger.js');
|
const logger = require('./logger.js');
|
||||||
const commands = require('../libs/commands.js');
|
const commands = require('./commands.js');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
const { time } = require('console');
|
|
||||||
|
|
||||||
let server;
|
let server;
|
||||||
let api;
|
let api;
|
||||||
|
@ -9,7 +8,7 @@ let api;
|
||||||
async function start() {
|
async function start() {
|
||||||
const listen = global.config?.server?.listen || '0.0.0.0';
|
const listen = global.config?.server?.listen || '0.0.0.0';
|
||||||
const port = global.config?.server?.port;
|
const port = global.config?.server?.port;
|
||||||
buildAPI();
|
setupAPI();
|
||||||
if (server === undefined) {
|
if (server === undefined) {
|
||||||
server = http.createServer();
|
server = http.createServer();
|
||||||
}
|
}
|
||||||
|
@ -35,8 +34,13 @@ async function respond(request, response, endpoint) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let data = {
|
let data = {
|
||||||
status: 'ok'
|
status: 'ok',
|
||||||
|
command: endpoint.command,
|
||||||
|
args: endpoint.args
|
||||||
};
|
};
|
||||||
|
if (endpoint.detached === true) {
|
||||||
|
data.detached = true;
|
||||||
|
}
|
||||||
let code = 200;
|
let code = 200;
|
||||||
if (endpoint === undefined) {
|
if (endpoint === undefined) {
|
||||||
code = 501;
|
code = 501;
|
||||||
|
@ -48,20 +52,24 @@ async function respond(request, response, endpoint) {
|
||||||
data.msg = 'endpoint does not support ' + request.method + ' requests';
|
data.msg = 'endpoint does not support ' + request.method + ' requests';
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
data.result = await commands.execute(endpoint);
|
const result = await commands.execute(endpoint);
|
||||||
|
if (result !== undefined) {
|
||||||
|
data.result = result;
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
code = 501;
|
code = 501;
|
||||||
data.status = 'error';
|
data.status = 'error';
|
||||||
data.msg = err;
|
data.msg = err.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
data.time = new Date().getTime() - request.timestamp + 'ms';
|
||||||
const json = JSON.stringify(data);
|
const json = JSON.stringify(data);
|
||||||
response.writeHead(code);
|
response.writeHead(code);
|
||||||
response.end(json);
|
response.end(json);
|
||||||
logger.http({ request: request, code: code, data: data });
|
logger.http({ request: request, code: code, data: json });
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildAPI() {
|
function setupAPI() {
|
||||||
const apiConfig = global.config?.api;
|
const apiConfig = global.config?.api;
|
||||||
if (apiConfig === undefined || apiConfig.length === 0) {
|
if (apiConfig === undefined || apiConfig.length === 0) {
|
||||||
throw new Error('no api endpoints configured - aborting');
|
throw new Error('no api endpoints configured - aborting');
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
"description": "execute local commands remotely via http requests",
|
"description": "execute local commands remotely via http requests",
|
||||||
"main": "remex.js",
|
"main": "remex.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"scripts",
|
"scripts",
|
||||||
|
|
Loading…
Reference in a new issue