diff --git a/README.md b/README.md index 1caed1e..880f86d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,24 @@ # remex -execute local commands remotely via http requests \ No newline at end of file +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 diff --git a/config.json b/config.json index c8d9b5c..41728f7 100644 --- a/config.json +++ b/config.json @@ -18,8 +18,7 @@ ], "options": { "detach": true, - "unique": true, - "restart": true + "unique": "toggle" } }, { @@ -27,6 +26,33 @@ "method": "get", "command": "uptime", "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" + ] } ] } \ No newline at end of file diff --git a/libs/commands.js b/libs/commands.js index bb5b19f..35def77 100644 --- a/libs/commands.js +++ b/libs/commands.js @@ -7,13 +7,21 @@ async function execute(endpoint) { if (endpoint === undefined) { return; } - if (endpoint.options?.unique && isCommandActive(endpoint)) { - if (!endpoint.options?.restart) { - logger.info('not executing unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\') because it is already active'); - throw new Error('command is already active'); + let unique = endpoint.options?.unique; + 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'); + throw new Error('command is already active'); + case 'restart': + logger.info('killing and restarting unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')'); + await killCommand(endpoint); + break; + case 'toggle': + logger.info('stopping unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')'); + return await killCommand(endpoint); } - logger.info('killing and restarting unique command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')'); - await killCommand(endpoint); } return new Promise((resolve, reject) => { logger.info('executing command \'' + endpoint.command + '\' (args: \'' + endpoint.args + '\')...'); @@ -91,8 +99,8 @@ async function killCommand(endpoint) { return; } process.kill(command.pid, 'SIGINT'); - while(isCommandActive(endpoint)) { - await sleep(100); + while (isCommandActive(endpoint)) { + await sleep(1); } } diff --git a/package.json b/package.json index 8128570..bab7558 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,7 @@ "version": "0.0.1", "description": "execute local commands remotely via http requests", "main": "remex.js", - "scripts": { - }, + "scripts": {}, "keywords": [ "scripts", "commands", @@ -20,4 +19,4 @@ "dependencies": { "moment": "^2.29.1" } -} \ No newline at end of file +} diff --git a/remex.js b/remex.js index 49ba37c..6cbdd04 100644 --- a/remex.js +++ b/remex.js @@ -8,7 +8,7 @@ const INTERRUPTS = ['beforeExit', 'SIGINT', 'SIGTERM']; main(); async function main() { - let configPath = path.resolve('./config.json'); + let configPath = path.resolve(__dirname + '/config.json'); try { global.config = require(configPath); } catch (err) {