updated documentation, made minor changes to the config

This commit is contained in:
Daniel Sommer 2022-03-18 14:51:01 +01:00
parent daeb9d004e
commit 7dc504171b
5 changed files with 69 additions and 15 deletions

View file

@ -1,3 +1,24 @@
# remex # remex
execute local commands remotely via http requests execute local commands remotely via http requests
## configuration
configuration is done entirely within the file `config.json`.
### server: [*object*]
- listen: [*string*] listen address
- port: [*number*] port to listen on
### log: [*object*]
- level: [*string*] verbosity of the log; either `debug`, `info`, `warning` or `error`
- timestamp: [*string*] format string for the timestamp; review [moment.js](https://momentjs.com/docs/#/displaying/format/) for further information
### api: [*object-array*]
- url: [*string*] endpoint url; example: `/example`
- method: [*string*] http request method
- command: [*string*] command to execute
- args: [*string-array*] arguments to pass to the executed command
- options: [*object*]
- detach [*boolean*] detach from the executed command / do not wait for the command to finish
- unique [*boolean* or *string*] if set to `true` the command can not be executed again until it has finished; if set to `restart` the command will be killed (if active) and started again; if set to `toggle` the command will either be killed if active or started if not active

View file

@ -18,8 +18,7 @@
], ],
"options": { "options": {
"detach": true, "detach": true,
"unique": true, "unique": "toggle"
"restart": true
} }
}, },
{ {
@ -27,6 +26,33 @@
"method": "get", "method": "get",
"command": "uptime", "command": "uptime",
"args": [] "args": []
},
{
"url": "/systemctl/docker",
"method": "get",
"command": "systemctl",
"args": [
"is-active",
"code-server@velvettear"
]
},
{
"url": "/systemctl/docker/start",
"method": "post",
"command": "systemctl",
"args": [
"start",
"code-server@velvettear"
]
},
{
"url": "/systemctl/docker/stop",
"method": "post",
"command": "systemctl",
"args": [
"stop",
"code-server@velvettear"
]
} }
] ]
} }

View file

@ -7,13 +7,21 @@ async function execute(endpoint) {
if (endpoint === undefined) { if (endpoint === undefined) {
return; return;
} }
if (endpoint.options?.unique && isCommandActive(endpoint)) { let unique = endpoint.options?.unique;
if (!endpoint.options?.restart) { if (unique !== undefined && isCommandActive(endpoint)) {
unique = unique.toLowerCase();
switch (unique.toLowerCase()) {
case 'true':
logger.info('not executing unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') because it is already active'); logger.info('not executing unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') because it is already active');
throw new Error('command is already active'); throw new Error('command is already active');
} case 'restart':
logger.info('killing and restarting unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')'); logger.info('killing and restarting unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')');
await killCommand(endpoint); await killCommand(endpoint);
break;
case 'toggle':
logger.info('stopping unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')');
return await killCommand(endpoint);
}
} }
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 + '\')...');
@ -91,8 +99,8 @@ async function killCommand(endpoint) {
return; return;
} }
process.kill(command.pid, 'SIGINT'); process.kill(command.pid, 'SIGINT');
while(isCommandActive(endpoint)) { while (isCommandActive(endpoint)) {
await sleep(100); await sleep(1);
} }
} }

View file

@ -3,8 +3,7 @@
"version": "0.0.1", "version": "0.0.1",
"description": "execute local commands remotely via http requests", "description": "execute local commands remotely via http requests",
"main": "remex.js", "main": "remex.js",
"scripts": { "scripts": {},
},
"keywords": [ "keywords": [
"scripts", "scripts",
"commands", "commands",

View file

@ -8,7 +8,7 @@ const INTERRUPTS = ['beforeExit', 'SIGINT', 'SIGTERM'];
main(); main();
async function main() { async function main() {
let configPath = path.resolve('./config.json'); let configPath = path.resolve(__dirname + '/config.json');
try { try {
global.config = require(configPath); global.config = require(configPath);
} catch (err) { } catch (err) {