1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-20 10:50:08 +03:00

F #3951: fix create command provision (#644)

Signed-off-by: Jorge Lobo <jlobo@opennebula.io>
This commit is contained in:
Jorge Miguel Lobo Escalona 2021-01-14 17:07:25 +01:00 committed by GitHub
parent b119d6a22b
commit 40e7104cc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 11 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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 })
}
})
}

View File

@ -26,10 +26,7 @@ const main = (app = {}) => {
subscriber(
'oneprovision',
data => {
app.emit(type, {
id: data.id,
data: data.message
})
app.emit(type, data)
}
)
})