From 40e7104cc22c5ad40bf311bea59c40e9a3de76d9 Mon Sep 17 00:00:00 2001 From: Jorge Miguel Lobo Escalona <47326048+jloboescalona2@users.noreply.github.com> Date: Thu, 14 Jan 2021 17:07:25 +0100 Subject: [PATCH] F #3951: fix create command provision (#644) Signed-off-by: Jorge Lobo --- src/fireedge/fireedge-server.conf | 3 +++ .../server/routes/api/provision/functions.js | 11 +++++++++ .../api/provision/provision-functions.js | 24 +++++++++++++------ .../routes/websockets/provision/index.js | 5 +--- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/fireedge/fireedge-server.conf b/src/fireedge/fireedge-server.conf index f168865cd2..5b7d61c7d8 100644 --- a/src/fireedge/fireedge-server.conf +++ b/src/fireedge/fireedge-server.conf @@ -25,6 +25,9 @@ limit_token: # Prepend for oneprovision command oneprovision_prepend_command: '' +# Opcional param for oneprovision command create +oneprovision_optional_create_command: '' + # Guacamole: use it if you have the Guacd in other server or port # guacd: # port: 4822 diff --git a/src/fireedge/src/server/routes/api/provision/functions.js b/src/fireedge/src/server/routes/api/provision/functions.js index 18d2f34e90..62f4638ea7 100644 --- a/src/fireedge/src/server/routes/api/provision/functions.js +++ b/src/fireedge/src/server/routes/api/provision/functions.js @@ -34,6 +34,7 @@ const { messageTerminal } = require('server/utils/general') const appConfig = getConfig() const prependCommand = appConfig.oneprovision_prepend_command || '' +const optionalCreateCommand = appConfig.oneprovision_optional_create_command || '' const eventsEmitter = new events.EventEmitter() const defaultError = (err = '', message = 'Error: %s') => ({ @@ -239,6 +240,15 @@ const addPrependCommand = (command = '', resource = '') => { } } +const addOptionalCreateCommand = () =>{ + let rtn = [] + if(optionalCreateCommand){ + rtn.push(optionalCreateCommand) + } + return rtn +} + + const executeCommandAsync = ( command = '', resource = '', @@ -335,6 +345,7 @@ const functionRoutes = { executeCommandAsync, findRecursiveFolder, publish, + addOptionalCreateCommand, subscriber } diff --git a/src/fireedge/src/server/routes/api/provision/provision-functions.js b/src/fireedge/src/server/routes/api/provision/provision-functions.js index f7246e2225..9bf631498b 100644 --- a/src/fireedge/src/server/routes/api/provision/provision-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provision-functions.js @@ -14,6 +14,7 @@ /* -------------------------------------------------------------------------- */ const { parse } = require('yaml') +const { v4 } = require('uuid') const { Validator } = require('jsonschema') const { createWriteStream } = require('fs-extra') const { lockSync, checkSync, unlockSync } = require('lockfile') @@ -39,7 +40,8 @@ const { publish, getFiles, getDirectories, - getEndpoint + getEndpoint, + addOptionalCreateCommand } = require('./functions') const { provision } = require('./schemas') @@ -239,15 +241,17 @@ const deleteProvision = (res = {}, next = () => undefined, params = {}, userData const { user, password } = userData const rtn = httpInternalError if (params && params.id && user && password) { + const command = 'delete' const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] - const paramsCommand = ['delete', params.id, '--batch', '--debug', ...authCommand, ...endpoint] + const paramsCommand = [command, params.id, '--batch', '--debug', '--json', ...authCommand, ...endpoint] let lastLine = '' + const uuid = v4() const emit = message => { message.toString().split(/\r|\n/).map(line => { if (line) { lastLine = line - publish(defaultCommandProvision, { id: params.id, message: lastLine }) + publish(defaultCommandProvision, { id: params.id, message: lastLine, command: command, commandId: uuid }) } }) } @@ -355,10 +359,12 @@ const createProvision = (res = {}, next = () => undefined, params = {}, userData const rtn = httpInternalError if (params && params.resource && user && password) { const authCommand = ['--user', user, '--password', password] + const optionalCommand = addOptionalCreateCommand() const endpoint = getEndpoint() const resource = parsePostData(params.resource) const content = createYMLContent(resource) if (content) { + const command = 'create'; const files = createFolderWithFiles(`${global.CPI}/provision/${id}/tmp`, [{ name: logFile.name, ext: logFile.ext }, { name: configFile.name, ext: configFile.ext, content }]) if (files && files.name && files.files) { const find = (val = '', ext = '', arr = files.files) => arr.find(e => e && e.path && e.ext && e.name && e.name === val && e.ext === ext) @@ -366,9 +372,11 @@ const createProvision = (res = {}, next = () => undefined, params = {}, userData const log = find(logFile.name, logFile.ext) if (config && log) { const create = (filedata = '') => { - const paramsCommand = ['create', config.path, '--batch', '--debug', ...authCommand, ...endpoint] + const paramsCommand = [command, config.path, '--batch', '--debug', '--json', ...optionalCommand, ...authCommand, ...endpoint] + console.log("-->", paramsCommand) let lastLine = '' var stream = createWriteStream(log.path, { flags: 'a' }) + const uuid = v4() const emit = message => { message.toString().split(/\r|\n/).map(line => { if (line) { @@ -385,7 +393,7 @@ const createProvision = (res = {}, next = () => undefined, params = {}, userData } lastLine = line stream.write(`${line}\n`) - publish(defaultCommandProvision, { id: files.name, message: line }) + publish(defaultCommandProvision, { id: files.name, message: line, command: command,commandId: uuid }) } }) } @@ -461,15 +469,17 @@ const configureProvision = (res = {}, next = () => undefined, params = {}, userD const { user, password } = userData const rtn = httpInternalError if (params && params.id && user && password) { + const command = 'configure' const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] - const paramsCommand = ['configure', params.id, '--debug', '--fail_cleanup', '--batch', ...authCommand, ...endpoint] + const paramsCommand = [command, params.id, '--debug', '--json', '--fail_cleanup', '--batch', ...authCommand, ...endpoint] let lastLine = '' + const uuid = v4(); const emit = message => { message.toString().split(/\r|\n/).map(line => { if (line) { lastLine = line - publish(defaultCommandProvision, { id: params.id, message: lastLine }) + publish(defaultCommandProvision, { id: params.id, message: lastLine, command: command, commandId: uuid }) } }) } diff --git a/src/fireedge/src/server/routes/websockets/provision/index.js b/src/fireedge/src/server/routes/websockets/provision/index.js index 5e64cd1a83..68b1c0ac2a 100644 --- a/src/fireedge/src/server/routes/websockets/provision/index.js +++ b/src/fireedge/src/server/routes/websockets/provision/index.js @@ -26,10 +26,7 @@ const main = (app = {}) => { subscriber( 'oneprovision', data => { - app.emit(type, { - id: data.id, - data: data.message - }) + app.emit(type, data) } ) })