From fe5661363b6e0dc85e5970ac75984553349a342d Mon Sep 17 00:00:00 2001 From: svvimming Date: Tue, 14 Mar 2023 15:42:10 -0400 Subject: [PATCH 1/2] feat: endpoint added to execute shell script --- .gitignore | 3 ++ packages/be/modules/exec/index.js | 10 ++++ packages/be/modules/exec/rest/exec-remote.js | 52 ++++++++++++++++++++ packages/fe/pages/index.vue | 7 +++ packages/fe/store/general.js | 15 ++++++ 5 files changed, 87 insertions(+) create mode 100644 packages/be/modules/exec/index.js create mode 100644 packages/be/modules/exec/rest/exec-remote.js diff --git a/.gitignore b/.gitignore index 0163acb..5fbde0b 100644 --- a/.gitignore +++ b/.gitignore @@ -107,3 +107,6 @@ sw.* # Storybook .nuxt-storybook storybook-static + +# test remote build script +remote-build.sh diff --git a/packages/be/modules/exec/index.js b/packages/be/modules/exec/index.js new file mode 100644 index 0000000..1b687b9 --- /dev/null +++ b/packages/be/modules/exec/index.js @@ -0,0 +1,10 @@ +console.log('📦 [module] exec') + +// ///////////////////////////////////////////////////////////////////// Imports +// ----------------------------------------------------------------------------- +const { RunStartupChecks } = require('@Module_Utilities') + +// ////////////////////////////////////////////////////////////// Startup Checks +// ----------------------------------------------------------------------------- +const checks = [] +RunStartupChecks(checks) diff --git a/packages/be/modules/exec/rest/exec-remote.js b/packages/be/modules/exec/rest/exec-remote.js new file mode 100644 index 0000000..28eb0c5 --- /dev/null +++ b/packages/be/modules/exec/rest/exec-remote.js @@ -0,0 +1,52 @@ +// ////////////////////////////////////////////////////////////////////// Import +// ----------------------------------------------------------------------------- +const Path = require('path') +const exec = require('child_process').exec + +const { SendData } = require('@Module_Utilities') +const MC = require('@Root/config') +require('dotenv').config({ path: Path.resolve(__dirname, '../.env') }) + +const REMOTE_EXEC_ENPOINT = process.env.REMOTE_EXEC_ENDPOINT +const REMOTE_EXEC_PATH = process.env.REMOTE_EXEC_PATH +const REMOTE_EXEC_FILENAME = process.env.REMOTE_EXEC_FILENAME + +// /////////////////////////////////////////////////////////////////// Functions +// ----------------------------------------------------------------------------- +const executeRemoteProcess = (command, path) => { + return new Promise((resolve, reject) => { + const executed = exec(REMOTE_EXEC_PATH + REMOTE_EXEC_FILENAME) + const results = [] + const errors = [] + executed.stdout.on('data', (msg) => { + results.push(msg.toString()) + }) + executed.stderr.on('data', msg => { + errors.push(msg.toString()) + }) + executed.on('exit', (code) => { + const err = errors.length > 0 && code !== 0 + if (err) { + reject(errors.join('\n')) + } + resolve(results.join('\n')) + }) + }) +} +// //////////////////////////////////////////////////////////////////// Endpoint +// ----------------------------------------------------------------------------- +MC.app.get(REMOTE_EXEC_ENPOINT, async (req, res) => { + const command = req.query.command + const path = req.query.path + const remoteProcess = executeRemoteProcess(command, path) + .then((result) => { + console.log(result) + SendData(res, 200, 'Successfully executed remote script') + }) + .catch((err) => { + console.log('================================ [Endpoint: /exec-remote]') + console.log(err) + SendData(res, 404, 'An error occured') + }) +}) + diff --git a/packages/fe/pages/index.vue b/packages/fe/pages/index.vue index 5156261..100a8e2 100644 --- a/packages/fe/pages/index.vue +++ b/packages/fe/pages/index.vue @@ -86,6 +86,13 @@ export default { }, async fetch ({ app, store, route, redirect }) { + await store.dispatch('general/execRemoteScript', { + endpoint: '/execute-remote-shell-script', + params: { + command: 'ls', + path: 'crons' + } + }) await store.dispatch('general/getBaseData', { key: 'index', data: IndexPageData }) const response = await store.dispatch('datasets/getDatasetList', { route }) if (response.fail) { diff --git a/packages/fe/store/general.js b/packages/fe/store/general.js index 046332d..e8f9f8f 100644 --- a/packages/fe/store/general.js +++ b/packages/fe/store/general.js @@ -94,6 +94,21 @@ const actions = { setClipboard ({ commit }, text) { this.$addTextToClipboard(text) commit('SET_CLIPBOARD', text) + }, + // ////////////////////////////////////////////////////////// execRemoteScript + async execRemoteScript ({ commit, getters }, payload) { + try { + const endpoint = payload.endpoint + const params = payload.params + const response = await this.$axiosAuth.get(endpoint, { params }) + const data = response.data.payload + console.log(response.data) + return data + } catch (e) { + console.log('================== [Store Action: general/execRemoteScript]') + console.log(e) + return false + } } } From 8a21af9f365cda96ea0d7211144fce8681206f18 Mon Sep 17 00:00:00 2001 From: svvimming Date: Wed, 15 Mar 2023 13:01:01 -0400 Subject: [PATCH 2/2] feat: arguments passed to shell script from query params --- packages/be/modules/exec/rest/exec-remote.js | 13 +++++++------ packages/fe/pages/index.vue | 4 ++-- packages/fe/store/general.js | 1 - 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/be/modules/exec/rest/exec-remote.js b/packages/be/modules/exec/rest/exec-remote.js index 28eb0c5..254bbb1 100644 --- a/packages/be/modules/exec/rest/exec-remote.js +++ b/packages/be/modules/exec/rest/exec-remote.js @@ -13,9 +13,13 @@ const REMOTE_EXEC_FILENAME = process.env.REMOTE_EXEC_FILENAME // /////////////////////////////////////////////////////////////////// Functions // ----------------------------------------------------------------------------- -const executeRemoteProcess = (command, path) => { +const executeRemoteProcess = (params = {}) => { return new Promise((resolve, reject) => { - const executed = exec(REMOTE_EXEC_PATH + REMOTE_EXEC_FILENAME) + let command = REMOTE_EXEC_PATH + REMOTE_EXEC_FILENAME + Object.keys(params).forEach((key) => { + command = `${command} --${key} ${params[key]}` + }) + const executed = exec(command) const results = [] const errors = [] executed.stdout.on('data', (msg) => { @@ -36,9 +40,7 @@ const executeRemoteProcess = (command, path) => { // //////////////////////////////////////////////////////////////////// Endpoint // ----------------------------------------------------------------------------- MC.app.get(REMOTE_EXEC_ENPOINT, async (req, res) => { - const command = req.query.command - const path = req.query.path - const remoteProcess = executeRemoteProcess(command, path) + executeRemoteProcess(req.query) .then((result) => { console.log(result) SendData(res, 200, 'Successfully executed remote script') @@ -49,4 +51,3 @@ MC.app.get(REMOTE_EXEC_ENPOINT, async (req, res) => { SendData(res, 404, 'An error occured') }) }) - diff --git a/packages/fe/pages/index.vue b/packages/fe/pages/index.vue index 100a8e2..bff95a3 100644 --- a/packages/fe/pages/index.vue +++ b/packages/fe/pages/index.vue @@ -89,8 +89,8 @@ export default { await store.dispatch('general/execRemoteScript', { endpoint: '/execute-remote-shell-script', params: { - command: 'ls', - path: 'crons' + foo: 'hello', + bar: 'world' } }) await store.dispatch('general/getBaseData', { key: 'index', data: IndexPageData }) diff --git a/packages/fe/store/general.js b/packages/fe/store/general.js index e8f9f8f..35a7584 100644 --- a/packages/fe/store/general.js +++ b/packages/fe/store/general.js @@ -102,7 +102,6 @@ const actions = { const params = payload.params const response = await this.$axiosAuth.get(endpoint, { params }) const data = response.data.payload - console.log(response.data) return data } catch (e) { console.log('================== [Store Action: general/execRemoteScript]')