From 5b69575993da277b090f2f4cfb906710a8e4be34 Mon Sep 17 00:00:00 2001 From: Frederick Ernesto Borges Noronha Date: Tue, 22 Dec 2020 22:52:19 +0100 Subject: [PATCH 01/12] F #3951: Fix checks for HOT_RESIZE attribute Signed-off-by: Frederick Ernesto Borges Noronha (cherry picked from commit d9177be1e84014aef4478eced8acad38a4f02344) --- src/sunstone/public/app/tabs/vms-tab/panels/capacity.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sunstone/public/app/tabs/vms-tab/panels/capacity.js b/src/sunstone/public/app/tabs/vms-tab/panels/capacity.js index e456ad01e2..45a02b78b1 100644 --- a/src/sunstone/public/app/tabs/vms-tab/panels/capacity.js +++ b/src/sunstone/public/app/tabs/vms-tab/panels/capacity.js @@ -122,6 +122,10 @@ define(function(require) { var that = this; if (that.element.STATE === "3" && + that.element.USER_TEMPLATE && + that.element.USER_TEMPLATE.HOT_RESIZE && + that.element.USER_TEMPLATE.HOT_RESIZE.MEMORY_HOT_ADD_ENABLED && + that.element.USER_TEMPLATE.HOT_RESIZE.CPU_HOT_ADD_ENABLED && that.element.USER_TEMPLATE.HOT_RESIZE.MEMORY_HOT_ADD_ENABLED === "NO" && that.element.USER_TEMPLATE.HOT_RESIZE.CPU_HOT_ADD_ENABLED === "NO"){ $('#resize_capacity',context).attr("disabled", "disabled"); From b40a282a1b010b8e423a90eca730bfe09705df75 Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Mon, 4 Jan 2021 11:58:57 +0100 Subject: [PATCH 02/12] F #3951: Fix xmlrpc variable (cherry picked from commit b44911e5fa5b29547600f92a8b1deea22eb0dfbf) --- src/fireedge/package.json | 2 +- src/fireedge/src/server/index.js | 4 ++-- src/fireedge/src/server/routes/entrypoints/Api.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fireedge/package.json b/src/fireedge/package.json index 49046ab49b..1f5fc6e0a3 100644 --- a/src/fireedge/package.json +++ b/src/fireedge/package.json @@ -127,4 +127,4 @@ "yup": "^0.29.3", "zeromq": "^5.2.0" } -} \ No newline at end of file +} diff --git a/src/fireedge/src/server/index.js b/src/fireedge/src/server/index.js index 84f8089838..ebd8fce02e 100644 --- a/src/fireedge/src/server/index.js +++ b/src/fireedge/src/server/index.js @@ -15,7 +15,7 @@ import { } from 'fs-extra' import http from 'http' import https from 'https' -import { +import { defaultAppName, defaultTypeLog, defaultPort, @@ -50,7 +50,7 @@ const unsecureServer = http.createServer const secureServer = https.createServer const app = express() -const basename = defaultAppName? `/${defaultAppName}`: '' +const basename = defaultAppName ? `/${defaultAppName}` : '' let frontPath = 'client' diff --git a/src/fireedge/src/server/routes/entrypoints/Api.js b/src/fireedge/src/server/routes/entrypoints/Api.js index ece9ee2b66..b2a9953586 100644 --- a/src/fireedge/src/server/routes/entrypoints/Api.js +++ b/src/fireedge/src/server/routes/entrypoints/Api.js @@ -58,7 +58,7 @@ if ( defaultOpennebulaZones[0] && defaultOpennebulaZones[0].rpc ) { - defaultOpennebulaZones[0].rpc = appConfig.xmlrpc + defaultOpennebulaZones[0].rpc = appConfig.one_xmlrpc } const router = express.Router() From 1628216d9c8584753194a85823f5c50c264aee18 Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Mon, 4 Jan 2021 12:20:51 +0100 Subject: [PATCH 03/12] F #3951: Fix aws provider template --- share/oneprovision/providers/aws.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/oneprovision/providers/aws.yml b/share/oneprovision/providers/aws.yml index 8a1bb3d753..e22f119da7 100644 --- a/share/oneprovision/providers/aws.yml +++ b/share/oneprovision/providers/aws.yml @@ -4,10 +4,10 @@ plain: image: 'AWS.webp' connection: - acces_key: "Encrypted aws access key" + access_key: "Encrypted aws access key" secret_key: "Encrypted aws secret key" -location_key: 'aws_region' +location_key: 'region' locations: us-east-1: From 6ee6d5a725188ea6a0f81189e9759a3d96cfdc6a Mon Sep 17 00:00:00 2001 From: Jorge Lobo Date: Tue, 5 Jan 2021 13:22:10 +0100 Subject: [PATCH 04/12] F #3951: add prepend one_provision Signed-off-by: Jorge Lobo --- src/fireedge/fireedge-server.conf | 3 ++ .../server/routes/api/provision/functions.js | 31 ++++++++++++++++--- .../api/provision/provider-functions.js | 13 ++++---- .../api/provision/provision-functions.js | 31 +++++++++---------- .../provision/provision_template-functions.js | 14 ++++----- .../src/server/utils/constants/defaults.js | 3 ++ 6 files changed, 59 insertions(+), 36 deletions(-) diff --git a/src/fireedge/fireedge-server.conf b/src/fireedge/fireedge-server.conf index b1c8d1fc19..089c00e345 100644 --- a/src/fireedge/fireedge-server.conf +++ b/src/fireedge/fireedge-server.conf @@ -25,6 +25,9 @@ limit_token: # VMRC: use it for connect to vmrc. Its is a URL for the ESX server vmrc: 'http://opennebula.io' +# Prepend for one_provision command +oneprovision_prepend_command: 'ssh localhost' + # 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 6f1ab6223f..d2d023e745 100644 --- a/src/fireedge/src/server/routes/api/provision/functions.js +++ b/src/fireedge/src/server/routes/api/provision/functions.js @@ -32,6 +32,9 @@ const { getConfig } = require('server/utils/yml') const { spawnSync, spawn } = require('child_process') const { messageTerminal } = require('server/utils/general') +const appConfig = getConfig() +const prependCommand = appConfig.oneprovision_prepend_command || '' + const eventsEmitter = new events.EventEmitter() const defaultError = (err = '', message = 'Error: %s') => ({ color: 'red', @@ -197,6 +200,25 @@ const moveToFolder = (path = '', relative = '/../') => { return rtn } +const addPrependCommand = (command="", resource='') => { + const rsc = Array.isArray(resource) ? resource : [resource] + + let newCommand = command + let newRsc = rsc + + if(prependCommand){ + let splitPrepend = prependCommand.split(" ") + newCommand = splitPrepend[0] + splitPrepend = splitPrepend.splice(1); + newRsc = [...splitPrepend, command, ...rsc].filter(el => el !== "") + } + + return { + cmd: newCommand, + rsc: newRsc + } +} + const executeCommandAsync = ( command = '', resource = '', @@ -210,9 +232,9 @@ const executeCommandAsync = ( const out = callbacks && callbacks.out && typeof callbacks.out === 'function' ? callbacks.out : () => undefined const close = callbacks && callbacks.close && typeof callbacks.close === 'function' ? callbacks.close : () => undefined - const rsc = Array.isArray(resource) ? resource : [resource] + const {cmd, rsc} = addPrependCommand(command, resource) - const execute = spawn(command, [...rsc]) + const execute = spawn(cmd, rsc) if (execute) { execute.stderr.on('data', (data) => { err(data) @@ -236,9 +258,9 @@ const executeCommandAsync = ( } const executeCommand = (command = '', resource = '', options = {}) => { - const rsc = Array.isArray(resource) ? resource : [resource] let rtn = { success: false, data: null } - const execute = spawnSync(command, [...rsc], options) + const {cmd, rsc} = addPrependCommand(command, resource) + const execute = spawnSync(cmd, rsc, options) if (execute) { if (execute.stdout) { rtn = { success: true, data: execute.stdout.toString() } @@ -269,7 +291,6 @@ const findRecursiveFolder = (path = '', finder = '', rtn = false) => { } const getEndpoint = () => { - const appConfig = getConfig() let rtn = [] if (appConfig && appConfig.one_xmlrpc) { const parseUrl = parse(appConfig.one_xmlrpc) diff --git a/src/fireedge/src/server/routes/api/provision/provider-functions.js b/src/fireedge/src/server/routes/api/provision/provider-functions.js index 2722c0ee0f..6c48b00b81 100644 --- a/src/fireedge/src/server/routes/api/provision/provider-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provider-functions.js @@ -15,7 +15,7 @@ const { parse } = require('yaml') const { Validator } = require('jsonschema') -const { tmpPath } = require('server/utils/constants/defaults') +const { tmpPath, defaultCommandProvider } = require('server/utils/constants/defaults') const { ok, @@ -28,8 +28,6 @@ const { provider, providerUpdate } = require('./schemas') const httpInternalError = httpResponse(internalServerError, '', '') -const command = 'oneprovider' - const getListProviders = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -40,7 +38,7 @@ const getListProviders = (res = {}, next = () => undefined, params = {}, userDat if (params && params.id) { paramsCommand = ['show', `${params.id}`.toLowerCase(), ...authCommand, '--json'] } - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvider, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError res.locals.httpCode = httpResponse(response, JSON.parse(executedCommand.data)) @@ -108,7 +106,8 @@ const createProviders = (res = {}, next = () => undefined, params = {}, userData const file = createTemporalFile(tmpPath, 'yaml', content) if (file && file.name && file.path) { const paramsCommand = ['create', file.path, ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvider, paramsCommand) + console.log("JORGE: ", executeCommand) res.locals.httpCode = httpResponse(internalServerError) if (executedCommand && executedCommand.data) { if (executedCommand.success) { @@ -151,7 +150,7 @@ const updateProviders = (res = {}, next = () => undefined, params = {}, userData const file = createTemporalFile(tmpPath, 'json', JSON.stringify(resource)) if (file && file.name && file.path) { const paramsCommand = ['update', params.id, file.path, ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvider, paramsCommand) res.locals.httpCode = httpResponse(internalServerError) if (executedCommand && executedCommand.success) { res.locals.httpCode = httpResponse(ok) @@ -181,7 +180,7 @@ const deleteProvider = (res = {}, next = () => undefined, params = {}, userData const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] const paramsCommand = ['delete', `${params.id}`.toLowerCase(), ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvider, paramsCommand) const data = executedCommand.data || '' try { if (executedCommand) { 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 12c324a4a5..8725d1ba42 100644 --- a/src/fireedge/src/server/routes/api/provision/provision-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provision-functions.js @@ -24,7 +24,7 @@ const { internalServerError } = require('server/utils/constants/http-codes') const { httpResponse, parsePostData, existsFile, createFile } = require('server/utils/server') -const { tmpPath } = require('server/utils/constants/defaults') +const { tmpPath, defaultCommandProvision } = require('server/utils/constants/defaults') const { executeCommand, executeCommandAsync, @@ -43,7 +43,6 @@ const { provision } = require('./schemas') const httpInternalError = httpResponse(internalServerError, '', '') -const command = 'oneprovision' const logFile = { name: 'stdouterr', ext: 'log' @@ -70,7 +69,7 @@ const getProvisionDefaults = (res = {}, next = () => undefined, params = {}, use const fillData = (content = '', filePath = '') => { try { const paramsCommand = ['validate', '--dump', filePath, ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand, { cwd: path }) + const executedCommand = executeCommand(defaultCommandProvision, paramsCommand, { cwd: path }) if (executedCommand && executedCommand.success) { files.push(parse(executedCommand.data)) } @@ -109,7 +108,7 @@ const getList = (res = {}, next = () => undefined, params = {}, userData = {}) = if (params && params.resource && user && password) { const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] - const executedCommand = executeCommand(command, [`${params.resource}`.toLowerCase(), 'list', ...authCommand, ...endpoint, '--json']) + const executedCommand = executeCommand(defaultCommandProvision, [`${params.resource}`.toLowerCase(), 'list', ...authCommand, ...endpoint, '--json']) try { const response = executedCommand.success ? ok : internalServerError res.locals.httpCode = httpResponse(response, JSON.parse(executedCommand.data)) @@ -133,7 +132,7 @@ const getListProvisions = (res = {}, next = () => undefined, params = {}, userDa if (params && params.id) { paramsCommand = ['show', `${params.id}`.toLowerCase(), ...authCommand, ...endpoint, '--json'] } - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvision, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError let data = JSON.parse(executedCommand.data) @@ -163,7 +162,7 @@ const deleteResource = (res = {}, next = () => undefined, params = {}, userData const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] const paramsCommand = [`${params.resource}`.toLowerCase(), 'delete', `${params.id}`.toLowerCase(), ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvision, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError rtn = httpResponse(response, executedCommand.data ? JSON.parse(executedCommand.data) : params.id) @@ -191,12 +190,12 @@ const deleteProvision = (res = {}, next = () => undefined, params = {}, userData message.toString().split(/\r|\n/).map(line => { if (line) { lastLine = line - publish(command, { id: params.id, message: lastLine }) + publish(defaultCommandProvision, { id: params.id, message: lastLine }) } }) } executeCommandAsync( - command, + defaultCommandProvision, paramsCommand, { err: emit, @@ -255,7 +254,7 @@ const hostCommand = (res = {}, next = () => undefined, params = {}, userData = { const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] const paramsCommand = ['host', `${params.action}`.toLowerCase(), `${params.id}`.toLowerCase(), ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvision, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError res.locals.httpCode = httpResponse(response, executedCommand.data ? JSON.parse(executedCommand.data) : params.id) @@ -276,7 +275,7 @@ const hostCommandSSH = (res = {}, next = () => undefined, params = {}, userData const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] const paramsCommand = ['host', `${params.action}`.toLowerCase(), `${params.id}`.toLowerCase(), `${params.command}`.toLowerCase(), ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvision, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError res.locals.httpCode = httpResponse(response, executedCommand.data ? JSON.parse(executedCommand.data) : params.id) @@ -329,12 +328,12 @@ const createProvision = (res = {}, next = () => undefined, params = {}, userData } lastLine = line stream.write(`${line}\n`) - publish(command, { id: files.name, message: line }) + publish(defaultCommandProvision, { id: files.name, message: line }) } }) } executeCommandAsync( - command, + defaultCommandProvision, paramsCommand, { err: emit, @@ -413,12 +412,12 @@ const configureProvision = (res = {}, next = () => undefined, params = {}, userD message.toString().split(/\r|\n/).map(line => { if (line) { lastLine = line - publish(command, { id: params.id, message: lastLine }) + publish(defaultCommandProvision, { id: params.id, message: lastLine }) } }) } executeCommandAsync( - command, + defaultCommandProvision, paramsCommand, { err: emit, @@ -440,7 +439,7 @@ const configureHost = (res = {}, next = () => undefined, params = {}, userData = const endpoint = getEndpoint() const authCommand = ['--user', user, '--password', password] const paramsCommand = ['host', 'configure', `${params.id}`.toLowerCase(), '--debug', '--fail_cleanup', '--batch', ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvision, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError res.locals.httpCode = httpResponse(response, JSON.parse(executedCommand.data)) @@ -469,7 +468,7 @@ const validate = (res = {}, next = () => undefined, params = {}, userData = {}) const file = createTemporalFile(tmpPath, 'yaml', content) if (file && file.name && file.path) { const paramsCommand = ['validate', '--dump', file.path, ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvision, paramsCommand) let response = internalServerError if (executedCommand && executedCommand.success) { response = ok diff --git a/src/fireedge/src/server/routes/api/provision/provision_template-functions.js b/src/fireedge/src/server/routes/api/provision/provision_template-functions.js index 4a20a7faf3..a6314e0509 100644 --- a/src/fireedge/src/server/routes/api/provision/provision_template-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provision_template-functions.js @@ -14,7 +14,7 @@ /* -------------------------------------------------------------------------- */ const { Validator } = require('jsonschema') -const { tmpPath } = require('server/utils/constants/defaults') +const { tmpPath, defaultCommandProvisionTemplate } = require('server/utils/constants/defaults') const { ok, @@ -26,8 +26,6 @@ const { provider } = require('./schemas') const httpInternalError = httpResponse(internalServerError, '', '') -const command = 'oneprovision-template' - const getListProvisionTemplates = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -38,7 +36,7 @@ const getListProvisionTemplates = (res = {}, next = () => undefined, params = {} if (params && params.id) { paramsCommand = ['show', `${params.id}`.toLowerCase(), ...authCommand, ...endpoint, '--json'] } - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvisionTemplate, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError res.locals.httpCode = httpResponse(response, JSON.parse(executedCommand.data)) @@ -67,7 +65,7 @@ const createProvisionTemplate = (res = {}, next = () => undefined, params = {}, const file = createTemporalFile(tmpPath, 'yaml', content) if (file && file.name && file.path) { const paramsCommand = ['create', file.path, ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvisionTemplate, paramsCommand) res.locals.httpCode = httpResponse(internalServerError) if (executedCommand && executedCommand.success && executedCommand.data) { res.locals.httpCode = httpResponse(ok, executedCommand.data) @@ -98,7 +96,7 @@ const instantiateProvisionTemplate = (res = {}, next = () => undefined, params = const authCommand = ['--user', user, '--password', password] const endpoint = getEndpoint() const paramsCommand = ['instantiate', `${params.id}`.toLowerCase(), ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvisionTemplate, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError res.locals.httpCode = httpResponse(response, JSON.parse(executedCommand.data)) @@ -127,7 +125,7 @@ const updateProvisionTemplate = (res = {}, next = () => undefined, params = {}, const file = createTemporalFile(tmpPath, 'yaml', content) if (file && file.name && file.path) { const paramsCommand = ['update', params.id, file.path, ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvisionTemplate, paramsCommand) res.locals.httpCode = httpResponse(internalServerError) if (executedCommand && executedCommand.success && executedCommand.data) { res.locals.httpCode = httpResponse(ok, executedCommand.data) @@ -158,7 +156,7 @@ const deleteProvisionTemplate = (res = {}, next = () => undefined, params = {}, const authCommand = ['--user', user, '--password', password] const endpoint = getEndpoint() const paramsCommand = ['delete', `${params.id}`.toLowerCase(), ...authCommand, ...endpoint] - const executedCommand = executeCommand(command, paramsCommand) + const executedCommand = executeCommand(defaultCommandProvisionTemplate, paramsCommand) try { const response = executedCommand.success ? ok : internalServerError res.locals.httpCode = httpResponse(response, JSON.parse(executedCommand.data)) diff --git a/src/fireedge/src/server/utils/constants/defaults.js b/src/fireedge/src/server/utils/constants/defaults.js index db65d1bd7f..c87d4e70d1 100644 --- a/src/fireedge/src/server/utils/constants/defaults.js +++ b/src/fireedge/src/server/utils/constants/defaults.js @@ -69,6 +69,9 @@ const defaults = { zeromq: `tcp://${defaultIp}:2101` } ], + defaultCommandProvision: 'oneprovision', + defaultCommandProvisionTemplate: 'oneprovision-template', + defaultCommandProvider: 'oneprovider', defaultOneFlowServer: `${protocol}://${defaultIp}:2474`, defaultEndpointWebsocket: `${appName ? '/' + appName : ''}/websocket`, defaultConfigFile: `${appName}-server.conf`, From fe8c62912246ccf3b7dd85fd1ba81e8c9453b12f Mon Sep 17 00:00:00 2001 From: Jorge Lobo Date: Tue, 5 Jan 2021 14:09:05 +0100 Subject: [PATCH 05/12] F #3951: fix conf Signed-off-by: Jorge Lobo --- src/fireedge/fireedge-server.conf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fireedge/fireedge-server.conf b/src/fireedge/fireedge-server.conf index 089c00e345..09e1c0f15d 100644 --- a/src/fireedge/fireedge-server.conf +++ b/src/fireedge/fireedge-server.conf @@ -25,8 +25,8 @@ limit_token: # VMRC: use it for connect to vmrc. Its is a URL for the ESX server vmrc: 'http://opennebula.io' -# Prepend for one_provision command -oneprovision_prepend_command: 'ssh localhost' +# Prepend for oneprovision command +oneprovision_prepend_command: '' # Guacamole: use it if you have the Guacd in other server or port # guacd: From 5851d3d067345ed64fd356023e976c9c989d7e1e Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sun, 3 Jan 2021 20:46:00 +0100 Subject: [PATCH 06/12] M #-: Fix fedora/CentOS manual compilation --- share/scons/get_xmlrpc_config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/scons/get_xmlrpc_config b/share/scons/get_xmlrpc_config index a8e4255f0b..4473a4c3dd 100755 --- a/share/scons/get_xmlrpc_config +++ b/share/scons/get_xmlrpc_config @@ -143,7 +143,7 @@ Configs=[ # Configuration for fedora { :description => "pkg-config", - :client => exec_command("pkg-config xmlrpc_client++ xmlrpc++ --libs"), + :client => exec_command("pkg-config xmlrpc_util xmlrpc_util++ xmlrpc_client++ xmlrpc++ --libs"), :server => exec_command("pkg-config xmlrpc_server_abyss++ --static --libs") }, # Debian lenny From bfa711b6174c37a997cd69f06ba693af45e47472 Mon Sep 17 00:00:00 2001 From: Carlos Herrera Date: Sun, 3 Jan 2021 03:23:25 -0500 Subject: [PATCH 07/12] F #4386: import nic alias in vCenter Signed-off-by: Carlos Herrera --- .../remotes/lib/vcenter_driver/vm_template.rb | 85 +++++++++++-------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb b/src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb index 76dbb95110..bfc9fc7f0a 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb @@ -344,13 +344,12 @@ module VCenterDriver ar_tmp << "AR=[\n" ar_tmp << "TYPE=\"IP4\",\n" ar_tmp << "IP=\"#{ipv4}\",\n" - ar_tmp << "PREFIX_LENGTH=\"64\",\n" ar_tmp << "SIZE=\"1\"\n" ar_tmp << "]\n" elsif ipv6 ar_tmp << "AR=[\n" - ar_tmp << "TYPE=\"IP6\",\n" - ar_tmp << "IP6=\"#{nic[:ipv6]}\",\n" + ar_tmp << "TYPE=\"IP6_STATIC\",\n" + ar_tmp << "IP6=\"#{ipv6}\",\n" ar_tmp << "PREFIX_LENGTH=\"64\",\n" ar_tmp << "SIZE=\"1\"\n" ar_tmp << "]\n" @@ -437,7 +436,7 @@ module VCenterDriver [ipv4, ipv6] end - def find_ips_in_network(network, vm_object, nic, force = false) + def find_ips_in_network(network, vm_object, nic, force = false, first_ip = false) ipv4 = ipv6 = '' ar_id = -1 return unless vm_object.is_a?(VCenterDriver::VirtualMachine) @@ -464,6 +463,11 @@ module VCenterDriver ar_array = network.to_hash['VNET']['AR_POOL']['AR'] ar_array = [ar_array] if ar_array.is_a?(Hash) ipv4, ipv6, ar_id = find_ip_in_ar(ip, ar_array) if ar_array + + if first_ip + return [ipv4, ipv6, ar_id] + end + break if (ipv4 !='') || (ipv6 != '') end break @@ -476,10 +480,13 @@ module VCenterDriver ipv4 = ipv6 = '' ar_id = -1 ar_array.each do |ar| - next unless ar.key?('IP') && ar.key?('IP_END') + next unless ((ar.key?('IP') && ar.key?('IP_END')) || (ar.key?('IP6') && ar.key?('IP6_END'))) + + start_ip = IPAddr.new(ar['IP']) if !ar['IP'].nil? + end_ip = IPAddr.new(ar['IP_END']) if !ar['IP_END'].nil? + start_ip = IPAddr.new(ar['IP6']) if !ar['IP6'].nil? + end_ip = IPAddr.new(ar['IP6_END']) if !ar['IP6_END'].nil? - start_ip = IPAddr.new(ar['IP']) - end_ip = IPAddr.new(ar['IP_END']) next unless ip.family == start_ip.family && ip.family == end_ip.family @@ -514,6 +521,24 @@ module VCenterDriver nic_alias_index += 1 end end + if nic[:ipv6_additionals] + nic[:ipv6_additionals].split(",").each do |ipv6_additional| + ipv4, ipv6 = find_alias_ips_in_network(network_found, vm_object, alias_ipv6 = ipv6_additional) + if ipv4.empty? && ipv6.empty? + ar_tmp = create_ar(nic, with_id = false, ipv4 = nil, ipv6 = ipv6_additional) + network_found.add_ar(ar_tmp) + end + network_found.info + + nic_tmp << "NIC_ALIAS=[\n" + nic_tmp << "NETWORK_ID=\"#{id}\",\n" + nic_tmp << "IP6=\"#{ipv6_additional}\",\n" + nic_tmp << "NAME=\"NIC#{nic_index}_ALIAS#{nic_alias_index}\",\n" + nic_tmp << "PARENT=\"NIC#{nic_index}\"\n" + nic_tmp << "]\n" + nic_alias_index += 1 + end + end nic_tmp end @@ -524,8 +549,6 @@ module VCenterDriver nic_tmp << "NAME =\"NIC#{nic_index}\",\n" if vm? - last_id = save_ar_ids(one_vn, nic, ar_ids) - nic_tmp << "AR_ID=\"#{last_id}\",\n" if nic[:mac] nic_tmp << "MAC=\"#{nic[:mac]}\",\n" end @@ -554,7 +577,9 @@ module VCenterDriver nic_tmp << "OPENNEBULA_MANAGED=\"NO\"\n" nic_tmp << "]\n" - nic_tmp << nic_alias_from_nic(one_vn.id, nic, nic_index, one_vn, vm_object) + if vm? + nic_tmp << nic_alias_from_nic(one_vn.id, nic, nic_index, one_vn, vm_object) + end nic_tmp end @@ -565,22 +590,20 @@ module VCenterDriver nic_tmp << "NAME =\"NIC#{nic_index}\",\n" if vm? - ipv4, ipv6 = find_ips_in_network(network_found, vm_object, nic) + ipv4, ipv6 = find_ips_in_network(network_found, vm_object, nic, false, true) if ipv4.empty? && ipv6.empty? ar_tmp = create_ar(nic) network_found.add_ar(ar_tmp) end ipv4, ipv6 = find_ips_in_network(network_found, vm_object, nic, true) network_found.info - last_id = save_ar_ids(network_found, nic, ar_ids) # This is the existing nic info - nic_tmp << "AR_ID=\"#{last_id}\",\n" if nic[:mac] && ipv4.empty? && ipv6.empty? nic_tmp << "MAC=\"#{nic[:mac]}\",\n" end nic_tmp << "IP=\"#{ipv4}\"," unless ipv4.empty? - nic_tmp << "IP=\"#{ipv6}\"," unless ipv6.empty? + nic_tmp << "IP6=\"#{ipv6}\"," unless ipv6.empty? if nic[:ipv4_additionals] nic_tmp << "VCENTER_ADDITIONALS_IP4=\"#{nic[:ipv4_additionals]}\",\n" @@ -606,7 +629,9 @@ module VCenterDriver nic_tmp << "OPENNEBULA_MANAGED=\"NO\"\n" nic_tmp << "]\n" - nic_tmp << nic_alias_from_nic(network_found['ID'], nic, nic_index, network_found, vm_object) + if vm? + nic_tmp << nic_alias_from_nic(network_found['ID'], nic, nic_index, network_found, vm_object) + end nic_tmp end @@ -1141,27 +1166,19 @@ module VCenterDriver nic[:ipv4] = ip end elsif ip_addresses[i].ipAddress =~ Resolv::IPv6::Regex - if get_ipv6_prefix(ip, 3) == '2000' - if nic[:ipv6_global] - if nic[:ipv6_additionals] - nic[:ipv6_additionals] += ',' + ip - else - nic[:ipv6_additionals] = ip - end - else - nic[:ipv6_global] = ip - end - elsif get_ipv6_prefix(ip, 10) == 'fe80' - nic[:ipv6] = ip + if get_ipv6_prefix(ip, 10) == 'fe80' + # we not process this address elsif get_ipv6_prefix(ip, 7) == 'fc00' - if nic[:ipv6_ula] - if nic[:ipv6_additionals] - nic[:ipv6_additionals] += ',' + ip - else - nic[:ipv6_additionals] = ip - end + nic[:ipv6_ula] = ip + else + if nic[:ipv6] + if nic[:ipv6_additionals] + nic[:ipv6_additionals] += ',' + ip + else + nic[:ipv6_additionals] = ip + end else - nic[:ipv6_ula] = ip + nic[:ipv6] = ip end end end From e02a9f9424cd12351611b75967607ec57f7903e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Czern=C3=BD?= Date: Mon, 4 Jan 2021 17:14:46 +0100 Subject: [PATCH 08/12] F #4090: Inherit vector values (#560) Vector attributes are merged with DISK/NIC attribute --- src/datastore/Datastore.cc | 30 ++++++++++++++++++++++++------ src/image/Image.cc | 29 ++++++++++++++++++++++++----- src/vnm/VirtualNetwork.cc | 31 +++++++++++++++++++++++++------ 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/src/datastore/Datastore.cc b/src/datastore/Datastore.cc index 9ac150c2cd..8f54dabc74 100644 --- a/src/datastore/Datastore.cc +++ b/src/datastore/Datastore.cc @@ -154,13 +154,31 @@ void Datastore::disk_attribute( for (const auto& inherit : inherit_attrs) { - current_val = disk->vector_value(inherit); - - get_template_attribute(inherit, inherit_val); - - if ( current_val.empty() && !inherit_val.empty() ) + if (auto va = PoolObjectSQL::get_template_attribute(inherit)) { - disk->replace(inherit, inherit_val); + // Vector attribute, inherit all its values + const auto& values = va->value(); + + for (const auto& val : values) + { + string current_val = disk->vector_value(val.first); + + if (current_val.empty() && !val.second.empty()) + { + disk->replace(val.first, val.second); + } + } + } + else + { + // Simple attribute, inherit value + string current_val = disk->vector_value(inherit); + PoolObjectSQL::get_template_attribute(inherit, inherit_val); + + if (current_val.empty() && !inherit_val.empty()) + { + disk->replace(inherit, inherit_val); + } } } diff --git a/src/image/Image.cc b/src/image/Image.cc index 531b51b147..ff936bd710 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -678,12 +678,31 @@ void Image::disk_attribute(VirtualMachineDisk * disk, for (const auto& inherit : inherit_attrs) { - string current_val = disk->vector_value(inherit); - get_template_attribute(inherit, inherit_val); - - if (current_val.empty() && !inherit_val.empty()) + if (auto va = PoolObjectSQL::get_template_attribute(inherit)) { - disk->replace(inherit, inherit_val); + // Vector attribute, inherit all its values + const auto& values = va->value(); + + for (const auto& val : values) + { + string current_val = disk->vector_value(val.first); + + if (current_val.empty() && !val.second.empty()) + { + disk->replace(val.first, val.second); + } + } + } + else + { + // Simple attribute, inherit value + string current_val = disk->vector_value(inherit); + PoolObjectSQL::get_template_attribute(inherit, inherit_val); + + if (current_val.empty() && !inherit_val.empty()) + { + disk->replace(inherit, inherit_val); + } } } } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index caad97e6f3..3a7c0fcf97 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -882,14 +882,33 @@ int VirtualNetwork::nic_attribute( inherit_attrs); } - for (const auto& inherited : inherit_attrs) + for (const auto& inherit : inherit_attrs) { - string current_val = nic->vector_value(inherited); - PoolObjectSQL::get_template_attribute(inherited, inherit_val); - - if (current_val.empty() && !inherit_val.empty()) + if (auto va = PoolObjectSQL::get_template_attribute(inherit)) { - nic->replace(inherited, inherit_val); + // Vector attribute, inherit all its values + const auto& values = va->value(); + + for (const auto& val : values) + { + string current_val = nic->vector_value(val.first); + + if (current_val.empty() && !val.second.empty()) + { + nic->replace(val.first, val.second); + } + } + } + else + { + // Simple attribute, inherit value + string current_val = nic->vector_value(inherit); + PoolObjectSQL::get_template_attribute(inherit, inherit_val); + + if (current_val.empty() && !inherit_val.empty()) + { + nic->replace(inherit, inherit_val); + } } } From 0d71df71ebf55ac87f5ffc4ca3cae442c4225149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Czern=C3=BD?= Date: Mon, 4 Jan 2021 17:23:16 +0100 Subject: [PATCH 09/12] F #678: Scheduler reads http_proxy from config (#584) --- src/scheduler/etc/sched.conf | 3 +++ src/scheduler/src/sched/Scheduler.cc | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/src/scheduler/etc/sched.conf b/src/scheduler/etc/sched.conf index 7b91494606..b01d3670e3 100644 --- a/src/scheduler/etc/sched.conf +++ b/src/scheduler/etc/sched.conf @@ -11,6 +11,8 @@ # # ONE_XMLRPC: URL to connect to the OpenNebula daemon (oned) # +# HTTP_PROXY: Proxy for ONE_XMLRPC +# # SCHED_INTERVAL: Seconds between two scheduling actions # # MAX_VM: Maximum number of Virtual Machines scheduled in each scheduling @@ -95,6 +97,7 @@ MESSAGE_SIZE = 1073741824 TIMEOUT = 60 ONE_XMLRPC = "http://localhost:2633/RPC2" +#HTTP_PROXY = "" SCHED_INTERVAL = 15 diff --git a/src/scheduler/src/sched/Scheduler.cc b/src/scheduler/src/sched/Scheduler.cc index ea76511332..044f4bda97 100644 --- a/src/scheduler/src/sched/Scheduler.cc +++ b/src/scheduler/src/sched/Scheduler.cc @@ -203,11 +203,16 @@ void Scheduler::start() { long long message_size; unsigned int timeout; + string proxy; conf.get("MESSAGE_SIZE", message_size); conf.get("TIMEOUT", timeout); + conf.get("HTTP_PROXY", proxy); + + setenv("http_proxy", proxy.c_str(), 1); + Client::initialize("", one_xmlrpc, message_size, timeout); oss.str(""); From 836f6b633567191e676442ec2528545142e391d2 Mon Sep 17 00:00:00 2001 From: Jorge Miguel Lobo Escalona <47326048+jloboescalona2@users.noreply.github.com> Date: Mon, 4 Jan 2021 18:06:29 +0100 Subject: [PATCH 10/12] F #4967: Delete tmps and images (#590) Co-authored-by: Jorge Lobo --- src/sunstone/public/app/opennebula/action.js | 17 +- .../public/app/opennebula/servicetemplate.js | 15 +- .../app/tabs/oneflow-templates-tab/actions.js | 70 ++- .../app/tabs/oneflow-templates-tab/buttons.js | 12 +- .../app/tabs/templates-tab/actions-common.js | 510 +++++++++--------- .../public/app/tabs/templates-tab/actions.js | 30 +- .../public/app/utils/common-actions.js | 62 +-- src/sunstone/routes/oneflow.rb | 2 +- 8 files changed, 361 insertions(+), 357 deletions(-) diff --git a/src/sunstone/public/app/opennebula/action.js b/src/sunstone/public/app/opennebula/action.js index 06d1b79359..ff1405fd6d 100644 --- a/src/sunstone/public/app/opennebula/action.js +++ b/src/sunstone/public/app/opennebula/action.js @@ -92,15 +92,14 @@ define(function(require) { }); }, - "del": function(params, resource, path) { + "del": function(params, resource, path, extra_param) { var callback = params.success; var callbackError = params.error; var id = params.data.id; var request = OpenNebulaHelper.request(resource, "delete", id); var reqPath = path ? path : resource.toLowerCase(); var cache_name = params.cache_name ? params.cache_name : resource; - - $.ajax({ + var ajaxData = { url: reqPath + "/" + id, type: "DELETE", success: function() { @@ -112,7 +111,13 @@ define(function(require) { return callbackError ? callbackError(request, OpenNebulaError(response)) : null; } - }); + }; + if(extra_param){ + ajaxData.data = JSON.stringify(extra_param); + ajaxData.dataType = "json"; + ajaxData.contentType = "application/json; charset=utf-8"; + } + $.ajax(ajaxData); }, "cache": function(resource) { @@ -342,8 +347,8 @@ define(function(require) { var callback = params && params.success; var callbackError = params && params.error; var id = params && params.data && params.data.id; - var roleName = params && params.data && params.data.roleName - var action = params && params.data && params.data.action + var roleName = params && params.data && params.data.roleName; + var action = params && params.data && params.data.action; var cacheName = params.cacheName ? params.cacheName : resource; if(id!==undefined && action && resource){ $.ajax({ diff --git a/src/sunstone/public/app/opennebula/servicetemplate.js b/src/sunstone/public/app/opennebula/servicetemplate.js index ae967e2ce3..08492388c9 100644 --- a/src/sunstone/public/app/opennebula/servicetemplate.js +++ b/src/sunstone/public/app/opennebula/servicetemplate.js @@ -15,10 +15,10 @@ /* -------------------------------------------------------------------------- */ define(function(require) { - var OpenNebulaAction = require('./action'); + var OpenNebulaAction = require("./action"); var RESOURCE = "DOCUMENT"; - var PATH = 'service_template'; + var PATH = "service_template"; var CACHE_NAME = "SERVICE_TEMPLATE"; var ServiceTemplate = { @@ -72,15 +72,16 @@ define(function(require) { }, "del": function(params) { params.cache_name = CACHE_NAME; - OpenNebulaAction.del(params, RESOURCE, PATH); + var action_obj = params.data.extra_param; + OpenNebulaAction.del(params, RESOURCE, PATH, action_obj); }, "list" : function(params) { params.cache_name = CACHE_NAME; - OpenNebulaAction.list(params, RESOURCE, PATH) + OpenNebulaAction.list(params, RESOURCE, PATH); }, "show" : function(params) { params.cache_name = CACHE_NAME; - OpenNebulaAction.show(params, RESOURCE, false, PATH) + OpenNebulaAction.show(params, RESOURCE, false, PATH); }, "chown" : function(params) { params.cache_name = CACHE_NAME; @@ -108,7 +109,7 @@ define(function(require) { "getName": function(id){ return OpenNebulaAction.getName(id, CACHE_NAME); } - } + }; return ServiceTemplate; -}) +}); diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab/actions.js b/src/sunstone/public/app/tabs/oneflow-templates-tab/actions.js index 11fe42d8eb..2d01cf5712 100644 --- a/src/sunstone/public/app/tabs/oneflow-templates-tab/actions.js +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab/actions.js @@ -22,21 +22,18 @@ define(function(require) { var CommonActions = require("utils/common-actions"); var OpenNebulaAction = require("opennebula/action"); var Navigation = require("utils/navigation"); - var OpenNebula = require('opennebula'); - - var CREATE_APP_DIALOG_ID = require('tabs/marketplaceapps-tab/form-panels/create/formPanelId'); - var MARKETPLACEAPPS_TAB_ID = require('tabs/marketplaceapps-tab/tabId'); - + var OpenNebula = require("opennebula"); + var CREATE_APP_DIALOG_ID = require("tabs/marketplaceapps-tab/form-panels/create/formPanelId"); + var CONFIRM_DIALOG_ID = require("utils/dialogs/generic-confirm/dialogId"); + var MARKETPLACEAPPS_TAB_ID = require("tabs/marketplaceapps-tab/tabId"); var TAB_ID = require("./tabId"); var CREATE_DIALOG_ID = require("./form-panels/create/formPanelId"); var INSTANTIATE_DIALOG_ID = require("./form-panels/instantiate/formPanelId"); var CLONE_DIALOG_ID = require("./dialogs/clone/dialogId"); var XML_ROOT = "DOCUMENT"; var RESOURCE = "ServiceTemplate"; - var _commonActions = new CommonActions(OpenNebulaResource, RESOURCE, TAB_ID, XML_ROOT, Locale.tr("Service Template created")); - var _actions = { "ServiceTemplate.create" : _commonActions.create(CREATE_DIALOG_ID), "ServiceTemplate.show" : _commonActions.show(), @@ -48,8 +45,45 @@ define(function(require) { "ServiceTemplate.rename": _commonActions.singleAction("rename"), "ServiceTemplate.update" : _commonActions.update(), "ServiceTemplate.update_dialog" : _commonActions.checkAndShowUpdate(), - - "ServiceTemplate.create_dialog" : { + "ServiceTemplate.delete_dialog" : { + type: "custom", + call: function() { + Sunstone.getDialog(CONFIRM_DIALOG_ID).setParams({ + //header : + headerTabId: TAB_ID, + body : Locale.tr("This will delete the Service.
You can also delete the template or the template and image referenced inside this Service."), + //question : + buttons : [ + Locale.tr("Delete Images and VM Templates"), + Locale.tr("Delete VM Templates"), + Locale.tr("Delete"), + ], + submit : [ + function(){ + Sunstone.runAction(RESOURCE+".delete", Sunstone.getDataTable(TAB_ID).elements(), {"delete_type": "all"}); + return false; + }, + function(){ + Sunstone.runAction(RESOURCE+".delete", Sunstone.getDataTable(TAB_ID).elements(), {"delete_type": "templates"}); + return false; + }, + function(){ + Sunstone.runAction(RESOURCE+".delete", Sunstone.getDataTable(TAB_ID).elements(), {"delete_type": "none"}); + return false; + } + ] + }); + Sunstone.getDialog(CONFIRM_DIALOG_ID).reset(); + Sunstone.getDialog(CONFIRM_DIALOG_ID).show(); + }, + callback : function(request, response) { + var tab = $("#" + that.tabId); + if (Sunstone.getTab() == that.tabId) { + Sunstone.showTab(that.tabId); + } + }, + }, + "ServiceTemplate.create_dialog" : { type: "custom", call: function() { Sunstone.runAction("Network.list"); @@ -57,7 +91,7 @@ define(function(require) { Sunstone.showFormPanel(TAB_ID, CREATE_DIALOG_ID, "create"); } }, - "ServiceTemplate.show_to_update" : { + "ServiceTemplate.show_to_update" : { type: "single", call: function(params) { Sunstone.runAction("Network.list"); @@ -73,7 +107,6 @@ define(function(require) { }, error: Notifier.onError }, - "ServiceTemplate.list" : { type: "list", call: OpenNebulaResource.list, @@ -85,8 +118,6 @@ define(function(require) { Notifier.onError(request, error_json, $(".oneflow_templates_error_message")); } }, - - "ServiceTemplate.instantiate" : { type: "single", call: OpenNebulaResource.instantiate, @@ -107,7 +138,6 @@ define(function(require) { }, notify: false }, - "ServiceTemplate.instantiate_dialog" : { type: "custom", call: function() { @@ -116,9 +146,7 @@ define(function(require) { Notifier.notifyMessage("Please select one (and just one) template to instantiate."); return false; } - var templateId = "" + selected_nodes[0]; - Sunstone.resetFormPanel(TAB_ID, INSTANTIATE_DIALOG_ID); Sunstone.showFormPanel(TAB_ID, INSTANTIATE_DIALOG_ID, "instantiate", function(formPanelInstance, context) { @@ -126,21 +154,18 @@ define(function(require) { }); } }, - "ServiceTemplate.clone_dialog" : { type: "custom", call: function(){ Sunstone.getDialog(CLONE_DIALOG_ID).show(); } }, - "ServiceTemplate.clone" : { type: "single", call: OpenNebulaResource.clone, error: Notifier.onError, notify: true }, - "ServiceTemplate.upload_marketplace_dialog" : { type: "custom", call: function(params) { @@ -150,9 +175,7 @@ define(function(require) { Notifier.notifyMessage(Locale.tr("Please select one (and just one) Service to export.")); return false; } - - var resourceId = '' + selectedNodes[0]; - + var resourceId = "" + selectedNodes[0]; OpenNebulaResource.show({ data : { id: resourceId @@ -165,7 +188,7 @@ define(function(require) { "export_service", function(formPanelInstance, context) { formPanelInstance.setServiceId(resourceId); - $('#marketplaceapps-tab-wizardForms #TYPE').val('service_template').change(); + $("#marketplaceapps-tab-wizardForms #TYPE").val("service_template").change(); } ); }, @@ -176,6 +199,5 @@ define(function(require) { } } }; - return _actions; }); diff --git a/src/sunstone/public/app/tabs/oneflow-templates-tab/buttons.js b/src/sunstone/public/app/tabs/oneflow-templates-tab/buttons.js index 151a872ced..7d76c57ebf 100644 --- a/src/sunstone/public/app/tabs/oneflow-templates-tab/buttons.js +++ b/src/sunstone/public/app/tabs/oneflow-templates-tab/buttons.js @@ -15,7 +15,7 @@ /* -------------------------------------------------------------------------- */ define(function(require) { - var Locale = require('utils/locale'); + var Locale = require("utils/locale"); var Buttons = { "ServiceTemplate.refresh" : { @@ -32,7 +32,7 @@ define(function(require) { "ServiceTemplate.upload_marketplace_dialog" : { type: "action", layout: "main", - text: ' '+ Locale.tr("MarketPlace") + text: " "+ Locale.tr("MarketPlace") }, "ServiceTemplate.update_dialog" : { type: "action", @@ -63,11 +63,17 @@ define(function(require) { layout: "main", text: Locale.tr("Clone") }, - "ServiceTemplate.delete" : { + /*"ServiceTemplate.delete" : { type: "confirm", text: Locale.tr("Delete"), layout: "del", tip: Locale.tr("This will delete the selected templates") + },*/ + "ServiceTemplate.delete_dialog" : { + type: "action", + text: Locale.tr("Delete"), + layout: "del", + tip: Locale.tr("This will delete the selected templates") }, "ServiceTemplate.edit_labels" : { layout: "labels", diff --git a/src/sunstone/public/app/tabs/templates-tab/actions-common.js b/src/sunstone/public/app/tabs/templates-tab/actions-common.js index 66e2350e1e..109dd6995c 100644 --- a/src/sunstone/public/app/tabs/templates-tab/actions-common.js +++ b/src/sunstone/public/app/tabs/templates-tab/actions-common.js @@ -15,289 +15,259 @@ /* -------------------------------------------------------------------------- */ define(function(require) { - var Sunstone = require('sunstone'); - var Notifier = require('utils/notifier'); - var Locale = require('utils/locale'); - var OpenNebulaResource = require('opennebula/template'); - var CommonActions = require('utils/common-actions'); - var OpenNebulaAction = require('opennebula/action'); - var Navigation = require('utils/navigation'); - var OpenNebula = require('opennebula'); - var CREATE_APP_DIALOG_ID = require('tabs/marketplaceapps-tab/form-panels/create/formPanelId'); - var MARKETPLACEAPPS_TAB_ID = require('tabs/marketplaceapps-tab/tabId'); - - var XML_ROOT = "VMTEMPLATE" - + var Sunstone = require("sunstone"); + var Notifier = require("utils/notifier"); + var Locale = require("utils/locale"); + var OpenNebulaResource = require("opennebula/template"); + var CommonActions = require("utils/common-actions"); + var OpenNebulaAction = require("opennebula/action"); + var Navigation = require("utils/navigation"); + var OpenNebula = require("opennebula"); + var CREATE_APP_DIALOG_ID = require("tabs/marketplaceapps-tab/form-panels/create/formPanelId"); + var MARKETPLACEAPPS_TAB_ID = require("tabs/marketplaceapps-tab/tabId"); + var XML_ROOT = "VMTEMPLATE"; function Actions(tabId, resource, ids){ - var RESOURCE = resource; - var TAB_ID = ids.TAB_ID; var CREATE_DIALOG_ID = ids.CREATE_DIALOG_ID; var CLONE_DIALOG_ID = ids.CLONE_DIALOG_ID; var INSTANTIATE_DIALOG_ID = ids.INSTANTIATE_DIALOG_ID; var IMPORT_DIALOG_ID = ids.IMPORT_DIALOG_ID; var CONFIRM_DIALOG_ID = ids.CONFIRM_DIALOG_ID; - var _commonActions = new CommonActions(OpenNebulaResource, RESOURCE, TAB_ID, XML_ROOT, Locale.tr("VM Template created")); - var _actions = {}; - - _actions[resource+".list"] = _commonActions.list(), - _actions[resource+".show"] = _commonActions.show(), - _actions[resource+".refresh"] = _commonActions.refresh(), - _actions[resource+".delete"] = _commonActions.del(), - - _actions[resource+".delete_dialog"] = - { - type: "custom", - call: function() { - Sunstone.getDialog(CONFIRM_DIALOG_ID).setParams({ - //header : - headerTabId: TAB_ID, - body : Locale.tr("This will delete the Template.
You can also delete any Image referenced inside this Template"), - //question : - buttons : [ - Locale.tr("Delete all images"), - Locale.tr("Delete"), - ], - submit : [ - function(){ - Sunstone.runAction(RESOURCE+'.delete_recursive', Sunstone.getDataTable(TAB_ID).elements()); - return false; - }, - function(){ - Sunstone.runAction(RESOURCE+'.delete', Sunstone.getDataTable(TAB_ID).elements()); - return false; - } - ] - }); - - Sunstone.getDialog(CONFIRM_DIALOG_ID).reset(); - Sunstone.getDialog(CONFIRM_DIALOG_ID).show(); - } - }, - - _actions[resource+".delete_recursive"] = - { - type: "multiple", - call: OpenNebulaResource.delete_recursive, - callback : function(request, response) { - if (Sunstone.getTab() == TAB_ID) { - Sunstone.showTab(TAB_ID); + _actions[resource+".list"] = _commonActions.list(), + _actions[resource+".show"] = _commonActions.show(), + _actions[resource+".refresh"] = _commonActions.refresh(), + _actions[resource+".delete"] = _commonActions.del(), + _actions[resource+".delete_dialog"] = { + type: "custom", + call: function() { + Sunstone.getDialog(CONFIRM_DIALOG_ID).setParams({ + //header : + headerTabId: TAB_ID, + body : Locale.tr("This will delete the Template.
You can also delete any Image referenced inside this Template"), + //question : + buttons : [ + Locale.tr("Delete all images"), + Locale.tr("Delete"), + ], + submit : [ + function(){ + Sunstone.runAction(RESOURCE+".delete_recursive", Sunstone.getDataTable(TAB_ID).elements()); + return false; + }, + function(){ + Sunstone.runAction(RESOURCE+".delete", Sunstone.getDataTable(TAB_ID).elements()); + return false; } - }, - elements: function(opts) { - return Sunstone.getDataTable(TAB_ID).elements(opts); - }, - error: Notifier.onError, - notify: true - }, - - _actions[resource+".chown"] = _commonActions.multipleAction('chown'), - _actions[resource+".chgrp"] = _commonActions.multipleAction('chgrp'), - _actions[resource+".chmod"] = _commonActions.singleAction('chmod'), - _actions[resource+".share"] = - { - type: "multiple", - call: function(params){ - var permissions = { - 'group_u' : 1, - 'recursive' : true - }; - - Sunstone.runAction(RESOURCE+".chmod", params.data.id, permissions); - }, - callback : function(request, response) { - Sunstone.runAction(RESOURCE+".show", req.request.data[0]); - }, - elements: function(opts) { - return Sunstone.getDataTable(TAB_ID).elements(opts); - }, - error: Notifier.onError, - notify: false - }, - - _actions[resource+".unshare"] = - { - type: "multiple", - call: function(params){ - var permissions = { - 'group_u' : 0, - 'recursive' : true - }; - - Sunstone.runAction(RESOURCE+".chmod", params.data.id, permissions); - }, - callback : function(request, response) { - Sunstone.runAction(RESOURCE+".show", req.request.data[0]); - }, - elements: function(opts) { - return Sunstone.getDataTable(TAB_ID).elements(opts); - }, - error: Notifier.onError, - notify: false - }, - - _actions[resource+".rename"] = _commonActions.singleAction('rename'), - _actions[resource+".create"] = _commonActions.create(CREATE_DIALOG_ID), - _actions[resource+".create_dialog"] = _commonActions.showCreate(CREATE_DIALOG_ID), - _actions[resource+".import_dialog"] = { - type: "custom", - call: function() { - Sunstone.showFormPanel(TAB_ID, IMPORT_DIALOG_ID, "import"); - } - }, - _actions[resource+".append_template"] = _commonActions.appendTemplate(), - _actions[resource+".update"] = _commonActions.update(), - _actions[resource+".update_dialog"] = _commonActions.checkAndShowUpdate(), - _actions[resource+".update_template"] = _commonActions.updateTemplate(), - _actions[resource+".show_to_update"] = _commonActions.showUpdate(CREATE_DIALOG_ID), - _actions[resource+".instantiate"] = { - type: "multiple", - call: OpenNebulaResource.instantiate, - callback: function(request, response) { - Sunstone.hideFormPanel(); - OpenNebulaAction.clear_cache("VM"); - - Notifier.notifyCustom( - Locale.tr("VM created"), - Navigation.link(" ID: " + response, "vms-tab", response), - false - ); - }, - elements: function(opts) { - return Sunstone.getDataTable(TAB_ID).elements(opts); - }, - error: function(request, response){ - // without tab id param to work for both templates and vms tab - Sunstone.hideFormPanelLoading(); - Notifier.onError(request, response); - }, - notify: false - }, - _actions[resource+".instantiate_quiet"] = { - type: "single", - call: OpenNebulaResource.instantiate, - callback: function(request, response) { - Sunstone.hideFormPanel(); - OpenNebulaAction.clear_cache("VM"); - }, - error: function(request, response){ - // without tab id param to work for both templates and vms tab - Sunstone.hideFormPanelLoading(); - Notifier.onError(request, response); - } - }, - _actions[resource+".instantiate_vms"] = { - type: "custom", - call: function(){ - //Sunstone.resetFormPanel(TAB_ID, INSTANTIATE_DIALOG_ID); - var selected_nodes = Sunstone.getDataTable(TAB_ID).elements(); - - Sunstone.showFormPanel(TAB_ID, INSTANTIATE_DIALOG_ID, "instantiate", - function(formPanelInstance, context) { - formPanelInstance.setTemplateIds(context, selected_nodes); - }); - } - }, - _actions[resource+".instantiate_persistent"] = - { - type: "single", - call: OpenNebulaResource.instantiate_persistent, - callback: function(request, response) { - Sunstone.hideFormPanel(); - OpenNebulaAction.clear_cache("VM"); - - Notifier.notifyCustom(Locale.tr("VM created"), - Navigation.link(" ID: " + response, "vms-tab", response), - false); - }, - error: function(request, response){ - // without tab id param to work for both templates and vms tab - Sunstone.hideFormPanelLoading(); - Notifier.onError(request, response); - } - }, - _actions[resource+".clone_dialog"] = { - type: "custom", - call: function(){ - Sunstone.getDialog(CLONE_DIALOG_ID).setParams( - { tabId : TAB_ID, - resource : resource - }); - Sunstone.getDialog(CLONE_DIALOG_ID).reset(); - Sunstone.getDialog(CLONE_DIALOG_ID).show(); - } - }, - _actions[resource+".clone"] = { - type: "single", - call: OpenNebulaResource.clone, - callback: function(request, response) { - OpenNebulaAction.clear_cache("VMTEMPLATE"); - Notifier.notifyCustom(Locale.tr("VM Template created"), - Navigation.link(" ID: " + response.VMTEMPLATE.ID, "templates-tab", response.VMTEMPLATE.ID), - false); - }, - error: Notifier.onError, - notify: true - }, - _actions[resource+".clone_recursive"] = - { - type: "single", - call: OpenNebulaResource.clone_recursive, - callback : function(request, response) { - OpenNebulaAction.clear_cache("VMTEMPLATE"); - Notifier.notifyCustom(Locale.tr("VM Template created"), - Navigation.link(" ID: " + response.VMTEMPLATE.ID, "templates-tab", response.VMTEMPLATE.ID), - false); - }, - error: Notifier.onError, - notify: true - }, - _actions[resource+".lockA"] = _commonActions.multipleAction("lock"), - _actions[resource+".lockM"] = _commonActions.multipleAction("lock"), - _actions[resource+".lockU"] = _commonActions.multipleAction("lock"), - _actions[resource+".unlock"] = _commonActions.multipleAction("unlock"), - _actions[resource+".upload_marketplace_dialog"] = { - type: "custom", - call: function() { - var selectedNodes = Sunstone.getDataTable(TAB_ID).elements(); - - if (selectedNodes.length !== 1) { - Notifier.notifyMessage(Locale.tr("Please select one (and just one) Image to export.")); - return false; - } - - var resourceId = '' + selectedNodes[0]; - - OpenNebulaResource.show({ - data : { - id: resourceId - }, - success: function(){ - Sunstone.showTab(MARKETPLACEAPPS_TAB_ID); - Sunstone.showFormPanel( - MARKETPLACEAPPS_TAB_ID, - CREATE_APP_DIALOG_ID, - 'export_template', - function(formPanelInstance, context) { - formPanelInstance.setTemplateId(resourceId); - $('#marketplaceapps-tab-wizardForms #TYPE').val('vmtemplate').change(); - } - ); - }, - error: function(error){ - Notifier.onError(error); - } - }); - } + ] + }); + Sunstone.getDialog(CONFIRM_DIALOG_ID).reset(); + Sunstone.getDialog(CONFIRM_DIALOG_ID).show(); } - - + }, + _actions[resource+".delete_recursive"] = { + type: "multiple", + call: OpenNebulaResource.delete_recursive, + callback : function(request, response) { + if (Sunstone.getTab() == TAB_ID) { + Sunstone.showTab(TAB_ID); + } + }, + elements: function(opts) { + return Sunstone.getDataTable(TAB_ID).elements(opts); + }, + error: Notifier.onError, + notify: true + }, + _actions[resource+".chown"] = _commonActions.multipleAction("chown"), + _actions[resource+".chgrp"] = _commonActions.multipleAction("chgrp"), + _actions[resource+".chmod"] = _commonActions.singleAction("chmod"), + _actions[resource+".share"] = { + type: "multiple", + call: function(params){ + var permissions = { + "group_u" : 1, + "recursive" : true + }; + Sunstone.runAction(RESOURCE+".chmod", params.data.id, permissions); + }, + callback : function(request, response) { + Sunstone.runAction(RESOURCE+".show", req.request.data[0]); + }, + elements: function(opts) { + return Sunstone.getDataTable(TAB_ID).elements(opts); + }, + error: Notifier.onError, + notify: false + }, + _actions[resource+".unshare"] = { + type: "multiple", + call: function(params){ + var permissions = { + "group_u" : 0, + "recursive" : true + }; + Sunstone.runAction(RESOURCE+".chmod", params.data.id, permissions); + }, + callback : function(request, response) { + Sunstone.runAction(RESOURCE+".show", req.request.data[0]); + }, + elements: function(opts) { + return Sunstone.getDataTable(TAB_ID).elements(opts); + }, + error: Notifier.onError, + notify: false + }, + _actions[resource+".rename"] = _commonActions.singleAction("rename"), + _actions[resource+".create"] = _commonActions.create(CREATE_DIALOG_ID), + _actions[resource+".create_dialog"] = _commonActions.showCreate(CREATE_DIALOG_ID), + _actions[resource+".import_dialog"] = { + type: "custom", + call: function() { + Sunstone.showFormPanel(TAB_ID, IMPORT_DIALOG_ID, "import"); + } + }, + _actions[resource+".append_template"] = _commonActions.appendTemplate(), + _actions[resource+".update"] = _commonActions.update(), + _actions[resource+".update_dialog"] = _commonActions.checkAndShowUpdate(), + _actions[resource+".update_template"] = _commonActions.updateTemplate(), + _actions[resource+".show_to_update"] = _commonActions.showUpdate(CREATE_DIALOG_ID), + _actions[resource+".instantiate"] = { + type: "multiple", + call: OpenNebulaResource.instantiate, + callback: function(request, response) { + Sunstone.hideFormPanel(); + OpenNebulaAction.clear_cache("VM"); + Notifier.notifyCustom( + Locale.tr("VM created"), + Navigation.link(" ID: " + response, "vms-tab", response), + false + ); + }, + elements: function(opts) { + return Sunstone.getDataTable(TAB_ID).elements(opts); + }, + error: function(request, response){ + // without tab id param to work for both templates and vms tab + Sunstone.hideFormPanelLoading(); + Notifier.onError(request, response); + }, + notify: false + }, + _actions[resource+".instantiate_quiet"] = { + type: "single", + call: OpenNebulaResource.instantiate, + callback: function(request, response) { + Sunstone.hideFormPanel(); + OpenNebulaAction.clear_cache("VM"); + }, + error: function(request, response){ + // without tab id param to work for both templates and vms tab + Sunstone.hideFormPanelLoading(); + Notifier.onError(request, response); + } + }, + _actions[resource+".instantiate_vms"] = { + type: "custom", + call: function(){ + //Sunstone.resetFormPanel(TAB_ID, INSTANTIATE_DIALOG_ID); + var selected_nodes = Sunstone.getDataTable(TAB_ID).elements(); + Sunstone.showFormPanel(TAB_ID, INSTANTIATE_DIALOG_ID, "instantiate", + function(formPanelInstance, context) { + formPanelInstance.setTemplateIds(context, selected_nodes); + }); + } + }, + _actions[resource+".instantiate_persistent"] = { + type: "single", + call: OpenNebulaResource.instantiate_persistent, + callback: function(request, response) { + Sunstone.hideFormPanel(); + OpenNebulaAction.clear_cache("VM"); + Notifier.notifyCustom(Locale.tr("VM created"), + Navigation.link(" ID: " + response, "vms-tab", response), + false); + }, + error: function(request, response){ + // without tab id param to work for both templates and vms tab + Sunstone.hideFormPanelLoading(); + Notifier.onError(request, response); + } + }, + _actions[resource+".clone_dialog"] = { + type: "custom", + call: function(){ + Sunstone.getDialog(CLONE_DIALOG_ID).setParams( + { tabId : TAB_ID, + resource : resource + }); + Sunstone.getDialog(CLONE_DIALOG_ID).reset(); + Sunstone.getDialog(CLONE_DIALOG_ID).show(); + } + }, + _actions[resource+".clone"] = { + type: "single", + call: OpenNebulaResource.clone, + callback: function(request, response) { + OpenNebulaAction.clear_cache("VMTEMPLATE"); + Notifier.notifyCustom(Locale.tr("VM Template created"), + Navigation.link(" ID: " + response.VMTEMPLATE.ID, "templates-tab", response.VMTEMPLATE.ID), + false); + }, + error: Notifier.onError, + notify: true + }, + _actions[resource+".clone_recursive"] = { + type: "single", + call: OpenNebulaResource.clone_recursive, + callback : function(request, response) { + OpenNebulaAction.clear_cache("VMTEMPLATE"); + Notifier.notifyCustom(Locale.tr("VM Template created"), + Navigation.link(" ID: " + response.VMTEMPLATE.ID, "templates-tab", response.VMTEMPLATE.ID), + false); + }, + error: Notifier.onError, + notify: true + }, + _actions[resource+".lockA"] = _commonActions.multipleAction("lock"), + _actions[resource+".lockM"] = _commonActions.multipleAction("lock"), + _actions[resource+".lockU"] = _commonActions.multipleAction("lock"), + _actions[resource+".unlock"] = _commonActions.multipleAction("unlock"), + _actions[resource+".upload_marketplace_dialog"] = { + type: "custom", + call: function() { + var selectedNodes = Sunstone.getDataTable(TAB_ID).elements(); + if (selectedNodes.length !== 1) { + Notifier.notifyMessage(Locale.tr("Please select one (and just one) Image to export.")); + return false; + } + var resourceId = "" + selectedNodes[0]; + OpenNebulaResource.show({ + data : { + id: resourceId + }, + success: function(){ + Sunstone.showTab(MARKETPLACEAPPS_TAB_ID); + Sunstone.showFormPanel( + MARKETPLACEAPPS_TAB_ID, + CREATE_APP_DIALOG_ID, + "export_template", + function(formPanelInstance, context) { + formPanelInstance.setTemplateId(resourceId); + $("#marketplaceapps-tab-wizardForms #TYPE").val("vmtemplate").change(); + } + ); + }, + error: function(error){ + Notifier.onError(error); + } + }); + } + }; return _actions; } - return Actions; -}) +}); diff --git a/src/sunstone/public/app/tabs/templates-tab/actions.js b/src/sunstone/public/app/tabs/templates-tab/actions.js index 9c5c0b6b71..16bb7f28df 100644 --- a/src/sunstone/public/app/tabs/templates-tab/actions.js +++ b/src/sunstone/public/app/tabs/templates-tab/actions.js @@ -15,24 +15,24 @@ /* -------------------------------------------------------------------------- */ define(function(require) { - var Actions = require('tabs/templates-tab/actions-common'); + var Actions = require("tabs/templates-tab/actions-common"); - var TAB_ID = require('tabs/templates-tab/tabId'); - var CREATE_DIALOG_ID = require('tabs/templates-tab/form-panels/create/formPanelId'); - var CLONE_DIALOG_ID = require('tabs/templates-tab/dialogs/clone/dialogId'); - var INSTANTIATE_DIALOG_ID = require('tabs/templates-tab/form-panels/instantiate/formPanelId'); - var IMPORT_DIALOG_ID = require('tabs/templates-tab/form-panels/import/formPanelId'); - var CONFIRM_DIALOG_ID = require('utils/dialogs/generic-confirm/dialogId'); + var TAB_ID = require("tabs/templates-tab/tabId"); + var CREATE_DIALOG_ID = require("tabs/templates-tab/form-panels/create/formPanelId"); + var CLONE_DIALOG_ID = require("tabs/templates-tab/dialogs/clone/dialogId"); + var INSTANTIATE_DIALOG_ID = require("tabs/templates-tab/form-panels/instantiate/formPanelId"); + var IMPORT_DIALOG_ID = require("tabs/templates-tab/form-panels/import/formPanelId"); + var CONFIRM_DIALOG_ID = require("utils/dialogs/generic-confirm/dialogId"); - var RESOURCE = "Template" + var RESOURCE = "Template"; - return Actions(TAB_ID, RESOURCE, + return Actions(TAB_ID, RESOURCE, { - 'TAB_ID' : TAB_ID, - 'CREATE_DIALOG_ID' : CREATE_DIALOG_ID, - 'CLONE_DIALOG_ID' : CLONE_DIALOG_ID, - 'INSTANTIATE_DIALOG_ID' : INSTANTIATE_DIALOG_ID, - 'IMPORT_DIALOG_ID' : IMPORT_DIALOG_ID, - 'CONFIRM_DIALOG_ID' : CONFIRM_DIALOG_ID, + "TAB_ID" : TAB_ID, + "CREATE_DIALOG_ID" : CREATE_DIALOG_ID, + "CLONE_DIALOG_ID" : CLONE_DIALOG_ID, + "INSTANTIATE_DIALOG_ID" : INSTANTIATE_DIALOG_ID, + "IMPORT_DIALOG_ID" : IMPORT_DIALOG_ID, + "CONFIRM_DIALOG_ID" : CONFIRM_DIALOG_ID, }); }); diff --git a/src/sunstone/public/app/utils/common-actions.js b/src/sunstone/public/app/utils/common-actions.js index 311ee84c19..19b4f59e07 100644 --- a/src/sunstone/public/app/utils/common-actions.js +++ b/src/sunstone/public/app/utils/common-actions.js @@ -15,10 +15,10 @@ /* -------------------------------------------------------------------------- */ define(function(require) { - var Sunstone = require('sunstone'); - var Notifier = require('utils/notifier'); - var Locale = require('utils/locale'); - var Navigation = require('utils/navigation'); + var Sunstone = require("sunstone"); + var Notifier = require("utils/notifier"); + var Locale = require("utils/locale"); + var Navigation = require("utils/navigation"); /* CONSTRUCTOR @@ -61,7 +61,7 @@ define(function(require) { } }, error: Notifier.onError - } + }; } function _show() { @@ -72,13 +72,13 @@ define(function(require) { callback: function(request, response) { if(that && that.tabId && Sunstone && Sunstone.getDataTable && Sunstone.getDataTable(that.tabId)){ Sunstone.getDataTable(that.tabId).updateElement(request, response); - if (Sunstone.rightInfoVisible($('#' + that.tabId))) { + if (Sunstone.rightInfoVisible($("#" + that.tabId))) { Sunstone.insertPanels(that.tabId, response); } } }, error: Notifier.onError - } + }; } function _refresh() { @@ -86,7 +86,7 @@ define(function(require) { return { type: "custom", call: function() { - var tab = $('#' + that.tabId); + var tab = $("#" + that.tabId); if (Sunstone.rightInfoVisible(tab)) { Sunstone.runAction(that.resourceStr + ".show", Sunstone.rightInfoResourceId(tab)); } else { @@ -95,7 +95,7 @@ define(function(require) { } }, error: Notifier.onError - } + }; } function _del() { @@ -104,7 +104,7 @@ define(function(require) { type: "multiple", call : that.openNebulaResource.del, callback : function(request, response) { - var tab = $('#' + that.tabId); + var tab = $("#" + that.tabId); if (Sunstone.getTab() == that.tabId) { Sunstone.showTab(that.tabId); @@ -115,7 +115,7 @@ define(function(require) { }, error: Notifier.onError, notify: true - } + }; } function _multipleAction(actionStr, notify) { @@ -135,7 +135,7 @@ define(function(require) { }, error: Notifier.onError, notify: notify_bool - } + }; } function _singleAction(actionStr) { @@ -151,7 +151,7 @@ define(function(require) { }, error: Notifier.onError, notify: true - } + }; } function _create(formPanelId) { @@ -177,7 +177,7 @@ define(function(require) { Notifier.onError(request, response); }, notify: false - } + }; } function _showCreate(formPanelId) { @@ -187,7 +187,7 @@ define(function(require) { call: function() { Sunstone.showFormPanel(that.tabId, formPanelId, "create"); } - } + }; } function _showUpdate(formPanelId) { @@ -206,42 +206,42 @@ define(function(require) { }); }, error: Notifier.onError - } + }; } function _checkAndShowUpdate() { var that = this; return { - type: 'single', + type: "single", call: function() { var selectedNodes = Sunstone.getDataTable(that.tabId).elements(); if (selectedNodes.length != 1) { - Notifier.notifyMessage('Please select one (and just one) resource to update.'); + Notifier.notifyMessage("Please select one (and just one) resource to update."); return false; } - var resourceId = '' + selectedNodes[0]; + var resourceId = "" + selectedNodes[0]; window.ServiceId = resourceId; - Sunstone.runAction(that.resourceStr + '.show_to_update', resourceId); + Sunstone.runAction(that.resourceStr + ".show_to_update", resourceId); } - } + }; } function _checkAndShow(action) { var that = this; return { - type: 'single', + type: "single", call: function() { var selectedNodes = Sunstone.getDataTable(that.tabId).elements(); if (selectedNodes.length != 1) { - Notifier.notifyMessage('Please select one (and just one) resource to update.'); + Notifier.notifyMessage("Please select one (and just one) resource to update."); return false; } - var resourceId = '' + selectedNodes[0]; - Sunstone.runAction(that.resourceStr + '.' + action, resourceId); + var resourceId = "" + selectedNodes[0]; + Sunstone.runAction(that.resourceStr + "." + action, resourceId); } - } + }; } function _update() { @@ -256,7 +256,7 @@ define(function(require) { Sunstone.hideFormPanelLoading(that.tabId); Notifier.onError(request, response); } - } + }; } function _updateTemplate() { @@ -265,10 +265,10 @@ define(function(require) { type: "single", call: that.openNebulaResource.update, callback: function(request) { - Sunstone.runAction(that.resourceStr + '.show', request.request.data[0]); + Sunstone.runAction(that.resourceStr + ".show", request.request.data[0]); }, error: Notifier.onError - } + }; } function _appendTemplate() { @@ -277,9 +277,9 @@ define(function(require) { type: "single", call: that.openNebulaResource.append, callback: function(request) { - Sunstone.runAction(that.resourceStr + '.show', request.request.data[0]); + Sunstone.runAction(that.resourceStr + ".show", request.request.data[0]); }, error: Notifier.onError - } + }; } }); diff --git a/src/sunstone/routes/oneflow.rb b/src/sunstone/routes/oneflow.rb index dd93aebbb5..b47afe25b3 100644 --- a/src/sunstone/routes/oneflow.rb +++ b/src/sunstone/routes/oneflow.rb @@ -136,7 +136,7 @@ end delete '/service_template/:id' do client = af_build_client - resp = client.delete('/service_template/' + params[:id]) + resp = client.delete('/service_template/' + params[:id], @request_body) af_format_response(resp) end From 10cc0c57ca2b43ef33b6d37ee01e5da3a97fb17b Mon Sep 17 00:00:00 2001 From: Vlastimil Holer Date: Tue, 5 Jan 2021 10:38:14 +0100 Subject: [PATCH 11/12] F #5175: onecfg patch support with simple diff (#581) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Christian González --- .gitignore | 7 + install.sh | 17 +- share/install_gems/Gemfile | 19 +- src/onecfg/bin/onecfg | 138 +++++++- src/onecfg/lib/common.rb | 1 + src/onecfg/lib/common/backup.rb | 8 +- src/onecfg/lib/common/logger/cli_logger.rb | 3 +- src/onecfg/lib/common/parser.rb | 155 +++++++++ src/onecfg/lib/config.rb | 1 + src/onecfg/lib/config/exception.rb | 11 + src/onecfg/lib/config/files.rb | 379 +++++++++++++++++++++ src/onecfg/lib/config/type/augeas/one.rb | 24 +- src/onecfg/lib/config/type/augeas/shell.rb | 31 +- src/onecfg/lib/config/type/base.rb | 157 ++++++++- src/onecfg/lib/config/type/yaml.rb | 30 ++ src/onecfg/lib/exception.rb | 11 + src/onecfg/lib/onecfg.rb | 21 +- src/onecfg/lib/patch.rb | 31 ++ src/onecfg/lib/patch/apply.rb | 221 ++++++++++++ src/onecfg/lib/transaction.rb | 194 +++++++++++ src/onecfg/share/augeas/oned.aug | 78 ----- src/onecfg/share/augeas/test_oned.aug | 184 ---------- 22 files changed, 1387 insertions(+), 334 deletions(-) create mode 100644 src/onecfg/lib/common/parser.rb create mode 100644 src/onecfg/lib/config/files.rb create mode 100644 src/onecfg/lib/patch.rb create mode 100644 src/onecfg/lib/patch/apply.rb create mode 100644 src/onecfg/lib/transaction.rb delete mode 100644 src/onecfg/share/augeas/oned.aug delete mode 100644 src/onecfg/share/augeas/test_oned.aug diff --git a/.gitignore b/.gitignore index 739c281c6a..ffc6610034 100644 --- a/.gitignore +++ b/.gitignore @@ -79,3 +79,10 @@ share/esx-fw-vnc/.vagrant* share/context/* !share/context/download_context.sh !share/context/SConstruct + +# editors temporary/swap files +*.swp +*.swo + +# backups +*.*- diff --git a/install.sh b/install.sh index 15c20aac6f..5bfcd1a43f 100755 --- a/install.sh +++ b/install.sh @@ -262,7 +262,6 @@ SHARE_DIRS="$SHARE_LOCATION/examples \ $SHARE_LOCATION/conf \ $SHARE_LOCATION/context \ $SHARE_LOCATION/onecfg - $SHARE_LOCATION/onecfg/augeas \ $SHARE_LOCATION/onecfg/etc" ETC_DIRS="$ETC_LOCATION/vmm_exec \ @@ -314,7 +313,8 @@ LIB_DIRS="$LIB_LOCATION/ruby \ $LIB_LOCATION/onecfg/lib/config \ $LIB_LOCATION/onecfg/lib/config/type \ $LIB_LOCATION/onecfg/lib/config/type/augeas \ - $LIB_LOCATION/onecfg/lib/config/type/yaml" + $LIB_LOCATION/onecfg/lib/config/type/yaml \ + $LIB_LOCATION/onecfg/lib/patch" VAR_DIRS="$VAR_LOCATION/remotes \ $VAR_LOCATION/remotes/etc \ @@ -760,7 +760,7 @@ INSTALL_ONECFG_FILES=( ONECFG_LIB_CONFIG_TYPE_FILES:$LIB_LOCATION/onecfg/lib/config/type ONECFG_LIB_CONFIG_TYPE_AUGEAS_FILES:$LIB_LOCATION/onecfg/lib/config/type/augeas ONECFG_LIB_CONFIG_TYPE_YAML_FILES:$LIB_LOCATION/onecfg/lib/config/type/yaml - ONECFG_SHARE_AUGEAS_FILES:$SHARE_LOCATION/onecfg/augeas + ONECFG_LIB_PATCH_FILES:$LIB_LOCATION/onecfg/lib/patch ONECFG_SHARE_ETC_FILES:$SHARE_LOCATION/onecfg/etc ) @@ -2706,11 +2706,15 @@ ONECFG_LIB_FILES="src/onecfg/lib/onecfg.rb src/onecfg/lib/config.rb \ src/onecfg/lib/exception.rb \ src/onecfg/lib/settings.rb \ + src/onecfg/lib/transaction.rb \ + src/onecfg/lib/patch.rb \ src/onecfg/lib/version.rb" -ONECFG_LIB_COMMON_FILES="src/onecfg/lib/common/backup.rb" +ONECFG_LIB_COMMON_FILES="src/onecfg/lib/common/backup.rb \ + src/onecfg/lib/common/parser.rb" ONECFG_LIB_COMMON_HELPERS_FILES="src/onecfg/lib/common/helpers/onecfg_helper.rb" ONECFG_LIB_COMMON_LOGGER_FILES="src/onecfg/lib/common/logger/cli_logger.rb" ONECFG_LIB_CONFIG_FILES="src/onecfg/lib/config/exception.rb \ + src/onecfg/lib/config/files.rb \ src/onecfg/lib/config/fsops.rb \ src/onecfg/lib/config/type.rb \ src/onecfg/lib/config/utils.rb" @@ -2719,11 +2723,10 @@ ONECFG_LIB_CONFIG_TYPE_FILES="src/onecfg/lib/config/type/augeas.rb \ src/onecfg/lib/config/type/simple.rb \ src/onecfg/lib/config/type/yaml.rb" ONECFG_LIB_CONFIG_TYPE_AUGEAS_FILES="src/onecfg/lib/config/type/augeas/one.rb \ - src/onecfg/lib/config/type/augeas/shell.rb" + src/onecfg/lib/config/type/augeas/shell.rb" ONECFG_LIB_CONFIG_TYPE_YAML_FILES="src/onecfg/lib/config/type/yaml/strict.rb" +ONECFG_LIB_PATCH_FILES="src/onecfg/lib/patch/apply.rb" -ONECFG_SHARE_AUGEAS_FILES="src/onecfg/share/augeas/oned.aug \ - src/onecfg/share/augeas/test_oned.aug" ONECFG_SHARE_ETC_FILES="src/onecfg/share/etc/files.yaml" diff --git a/share/install_gems/Gemfile b/share/install_gems/Gemfile index e1dc53c4c8..a0c763d16e 100644 --- a/share/install_gems/Gemfile +++ b/share/install_gems/Gemfile @@ -63,7 +63,6 @@ gem 'nokogiri', nokogiri gem 'public_suffix', ps group :cli do - gem 'augeas', '~> 0.6' gem 'gnuplot' gem 'highline', '~> 1.7' gem 'mysql2' @@ -72,6 +71,18 @@ group :cli do gem 'sequel' end +group :onecfg, :cli do + gem 'augeas', '~> 0.6' +end + +group :onecfg, :cloud, :oneflow, :sunstone do + gem 'json', '>= 2.0' +end + +group :onecfg do + gem 'git', '~> 1.5' +end + group :hybrid do gem 'aws-sdk-ec2', '>=1.151' gem 'aws-sdk-s3' @@ -94,7 +105,6 @@ end group :cloud, :oneflow, :sunstone do gem 'rack', rack - gem 'json' gem 'sinatra' end @@ -142,8 +152,3 @@ group :vmware do gem 'rbvmomi', '~> 2.2.0' end end - -group :onecfg do - gem 'git', '~> 1.5' - gem 'augeas', '~> 0.6' -end diff --git a/src/onecfg/bin/onecfg b/src/onecfg/bin/onecfg index 3d88ae5fb2..f3aa1875a2 100755 --- a/src/onecfg/bin/onecfg +++ b/src/onecfg/bin/onecfg @@ -41,6 +41,10 @@ $LOAD_PATH << LIB_LOCATION + '/onecfg/lib' # Supported patch modes SUPPORTED_PATCH_MODES = [:skip, :force, :replace] +# Early load of newer JSON gem, which supports serialization of scalars, e.g. +# > JSON.generate('string') +gem 'json', '>= 2.0' + require 'git' require 'tmpdir' require 'yaml' @@ -56,11 +60,143 @@ CommandParser::CmdParser.new(ARGV) do # onecfg helper helper = OneCfgHelper.new - # public command should go here + ######################################################################## + # Global Options + ######################################################################## + + UNPRIVILEGED = { + :name => 'unprivileged', + :large => '--unprivileged', + :description => 'Skip privileged operations (e.g., chown)' + } + + NO_OPERATION = { + :name => 'noop', + :short => '-n', + :large => '--noop', + :description => 'Runs update without changing system state' + } + + PREFIX = { + :name => 'prefix', + :large => '--prefix prefix', + :description => 'Root location prefix (default: /)', + :format => String + } + + ######################################################################## + # Logging modes + ######################################################################## + + VERBOSE = { + :name => 'verbose', + :short => '-d', + :large => '--verbose', + :description => 'Set verbose logging mode' + } + + DEBUG = { + :name => 'debug', + :large => '--debug', + :description => 'Set debug logging mode' + } + + DDEBUG = { + :name => 'ddebug', + :large => '--ddebug', + :description => 'Set extra debug logging mode' + } + + DDDEBUG = { + :name => 'dddebug', + :large => '--dddebug', + :description => 'Set extra debug logging mode' + } + + # logging modes + LOG_MODES = [VERBOSE, DEBUG, DDEBUG, DDDEBUG] + + ######################################################################## + # Command Specific Parameters + ######################################################################## + + PATCH_ALL = { + :name => 'all', + :short => '-a', + :large => '--all', + :description => 'All changes must be applied or patch fails' + } + + PATCH_FORMAT = { + :name => 'format', + :large => '--format format', + :description => 'Specify the patch input format. ' \ + 'Supported values are:' \ + ' "line" (single line format),' \ + ' "yaml" (YAML format).', + :format => String + } + + PATCH_FORMATS = %w[line yaml] begin require 'ee' OneCfg::EE::Commands.load_commands(self, ARGV, helper) rescue LoadError + # If we can't load EE, we are running CE only + end + + ########################################################################### + # Community Edition Commands + ########################################################################### + + patch_desc = <<-EOT.unindent + Apply changes to configuration files + EOT + + command :patch, patch_desc, + [:file, nil], + :options => [UNPRIVILEGED, + NO_OPERATION, + PREFIX, + PATCH_FORMAT, + PATCH_ALL] + + CommandParser::OPTIONS + LOG_MODES \ + do + OneCfg::LOG.get_logger(options) + rc = -1 + + begin + format = options[:format] unless options[:format].nil? + + if !format.nil? && !PATCH_FORMATS.include?(format) + STDERR.puts "Unsupported format '#{format}'. "\ + "Available formats - #{PATCH_FORMATS.join(', ')}" + exit(-1) + end + + patcher = OneCfg::Patch::Apply.new(options) + + if ARGV[0] + patcher.read_from_file(ARGV[0], format) + elsif !STDIN.tty? + Tempfile.open('stdin_patch_content') do |temp_file| + temp_file.write($stdin.read) + temp_file.close + + patcher.read_from_file(temp_file.path, format) + end + else + STDERR.puts 'No patch file or data on standard input found.' + exit(-1) + end + + rc = patcher.apply(options.key?(:all)) + rescue StandardError => e + OneCfg::LOG.fatal("FAILED - #{e}") + rc = -1 + end + + exit(rc) end end diff --git a/src/onecfg/lib/common.rb b/src/onecfg/lib/common.rb index e23dce5c71..0995f40dca 100644 --- a/src/onecfg/lib/common.rb +++ b/src/onecfg/lib/common.rb @@ -25,3 +25,4 @@ end require 'common/backup' require 'common/helpers/onecfg_helper' require 'common/logger/cli_logger' +require 'common/parser' diff --git a/src/onecfg/lib/common/backup.rb b/src/onecfg/lib/common/backup.rb index 13d506da71..c73157b85d 100644 --- a/src/onecfg/lib/common/backup.rb +++ b/src/onecfg/lib/common/backup.rb @@ -92,9 +92,11 @@ module OneCfg::Common backup(src, dst) end - # TODO: hmmm - versions = OneCfg::EE::Config::Versions.new - cfg_version = versions.cfg_version + begin + versions = OneCfg::EE::Config::Versions.new + cfg_version = versions.cfg_version + rescue NameError + end if cfg_version File.open("#{backup}/version", 'w') do |file| diff --git a/src/onecfg/lib/common/logger/cli_logger.rb b/src/onecfg/lib/common/logger/cli_logger.rb index 8fa0bae946..20bb31dd1c 100644 --- a/src/onecfg/lib/common/logger/cli_logger.rb +++ b/src/onecfg/lib/common/logger/cli_logger.rb @@ -75,7 +75,8 @@ module OneCfg # # @param msg [String] Message to show def self.ddebug(msg) - return unless instance.custom_level == :ddebug + return unless \ + [:ddebug, :dddebug].include?(instance.custom_level) instance.logger.debug(msg) # TODO end diff --git a/src/onecfg/lib/common/parser.rb b/src/onecfg/lib/common/parser.rb new file mode 100644 index 0000000000..584a6285b2 --- /dev/null +++ b/src/onecfg/lib/common/parser.rb @@ -0,0 +1,155 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'strscan' + +# rubocop:disable Style/ClassAndModuleChildren +module OneCfg::Common + + # HintingParser class + class HintingParser + + def initialize(string) + @scanner = StringScanner.new(string) + end + + # Parse string passed to constructor as single hinting in a format + # [] + # and return hash with :command, :path, :value keys. + # + # @param normalize [Bool] Check and process parsed values + # + # @return [Hash] Hash with parsed data + def parse(normalize = true) + # reset pointer to the begining + @scanner.pointer = 0 + + ret = { + :command => get_word, + :path => get_word, + :value => get_until_end + } + + normalize!(ret) if normalize + + ret + end + + # Parse string passed to constructor as single hinting in a format + # [] + # and return hash with :filename, :command, :path, :value keys. + # + # @param normalize [Bool] Check and process parsed values + # + # @return [Hash] Hash with parsed data + def parse_with_filename(normalize = true) + # reset pointer to the beginning + @scanner.pointer = 0 + + ret = { + :filename => get_word, + :command => get_word, + :path => get_word, + :value => get_until_end + } + + normalize!(ret) if normalize + + ret + end + + private + + QUOTES_STRING = ['"'] + + # Validate and normalize parser data. Works on input data. + # + # @param parsed [Hash] parsed data + # + # @return [Hash] normalized parsed data + def normalize!(parsed) + # command + parsed[:command].downcase! + + if !%w[rm ins set].include?(parsed[:command]) + raise OneCfg::Exception::FileParseError, + "Invalid patch action '#{parsed[:command]}'" + end + + # path + parsed[:path] = parsed[:path].split('/').reject(&:empty?) + + # value + unless parsed[:value].nil? + parsed[:value] = JSON.parse(parsed[:value]) + end + + parsed + end + + # rubocop:disable Naming/AccessorMethodName + # TODO, uncovered case: + # - When get word it's called for strings like: path/\"kvm \"/path, + # it won't parse the scaped quotes in the middle of the word. + # Workaround: use single quotes 'path/path/"kvm "/path' + def get_word + return if @scanner.eos? + + # advance until first char difference from space + if @scanner.match?(/\s/) + @scanner.scan_until(/\S/) + @scanner.pointer -= 1 + end + + if QUOTES_STRING.include? @scanner.peek(1) + # read until next quote + match = @scanner.scan(/(?:"(?[^"\\]*(?:\\.[^"\\]*)*"))/) + + # remove last quote and unscape them + match = match.strip[1..-2].gsub('\"', '"') unless match.nil? + else + match = @scanner.scan_until(/\s/) + if match.nil? + match = @scanner.scan_until(/$/) + end + + match.strip! unless match.nil? + end + + # Advanced until next word + @scanner.scan(/\s+/) + + match + end + + def get_until_end + return if @scanner.eos? + + match = @scanner.scan_until(/$/) + + if !match.nil? + # unscape elements + match = match.strip + end + + match + end + # rubocop:enable Naming/AccessorMethodName + + end + +end +# rubocop:enable Style/ClassAndModuleChildren diff --git a/src/onecfg/lib/config.rb b/src/onecfg/lib/config.rb index 17f5d53304..d62cd35900 100644 --- a/src/onecfg/lib/config.rb +++ b/src/onecfg/lib/config.rb @@ -26,6 +26,7 @@ require 'config/type/yaml/strict' require 'config/utils' require 'config/exception' require 'config/fsops' +require 'config/files' begin require 'ee/config' diff --git a/src/onecfg/lib/config/exception.rb b/src/onecfg/lib/config/exception.rb index 4f04d678c8..84401d1c9e 100644 --- a/src/onecfg/lib/config/exception.rb +++ b/src/onecfg/lib/config/exception.rb @@ -159,6 +159,17 @@ module OneCfg::Config::Exception end + # Exception to indicate that the current patch + # operation should be restarted by searching + # the place in a tree and reapplication. + class PatchRetryOperation < PatchException + + def initialize + super('Retrying patch operation') + end + + end + end # rubocop:enable Style/ClassAndModuleChildren # rubocop:enable Lint/UselessMethodDefinition diff --git a/src/onecfg/lib/config/files.rb b/src/onecfg/lib/config/files.rb new file mode 100644 index 0000000000..066bb6323d --- /dev/null +++ b/src/onecfg/lib/config/files.rb @@ -0,0 +1,379 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +# rubocop:disable Style/ClassAndModuleChildren +module OneCfg::Config + + # Class to scan and validate configuration files + class Files < OneCfg::Settings + + # Base name prefix for configuration classes + TYPE_CLASS_NAME = 'OneCfg::Config::Type' + + # Class constructor + def initialize + super(OneCfg::FILES_CFG, [:auto_load, :read_only]) + + # we really need the classification data + # rubocop:disable Style/GuardClause + if !@content.is_a?(Array) || @content.empty? + raise OneCfg::Config::Exception::FatalError, + 'Missing classification of configuration files' + end + # rubocop:enable Style/GuardClause + end + + def scan(prefix = '/', strict = true) + ret = {} + + # fileops provides sandboxed operations + fops = OneCfg::Config::FileOperation.new(prefix) + + @content.each do |file| + fops.glob(file['name'], false).each do |match| + next if fops.directory?(match) + + if file.key?('class') + # file already scanned matched once again + if ret.key?(match) + raise OneCfg::Config::Exception::FatalError, + "File '#{match}' already scanned. " \ + 'Duplicate expressions matching same file.' + else + ret[match] = file.dup + + # replace stripped class with real Ruby class + begin + ret[match]['ruby_class'] = \ + self.class.type_class(file['class']) + rescue NameError + raise OneCfg::Config::Exception::FatalError, + "File '#{match}' has invalid class " \ + "'#{file['class']}'" + end + end + + # fail in strict mode if we found some files + # matched by catch-all expressions + elsif strict && !ret.key?(match) + raise OneCfg::Config::Exception::FatalError, + "File '#{match}' doesn't have classification." + end + end + end + + ret + end + + # Validates all known files from specified prefix can be read and + # and written into temporary location. Checks that files and + # OneCfg code is OK. + # + # @param prefix [String] Root prefix + # + # @return [Boolean] True if all valid + def validate(prefix = '/') + ret = true + + scan(prefix, false).each do |name, data| + begin + # create type object and load + pre_name = OneCfg::Config::Utils.prefixed(name, prefix) + + OneCfg::LOG.debug("Load '#{pre_name}' " \ + "with #{data['ruby_class']}") + + file = data['ruby_class'].new(pre_name) + file.load + + # valid file and OneCfg code must evaluate file + # as same, similar and without diff on self + unless file.same?(file) && + file.similar?(file) && + file.diff(file).nil? + raise OneCfg::Config::Exception::StructureError, + 'Error when comparing file content with self' + end + + # test save into temporary file + Tempfile.open('validate') do |temp| + OneCfg::LOG.debug("Save '#{pre_name}' " \ + "into '#{temp.path}'") + + temp.close + file.save(temp.path) + end + + OneCfg::LOG.info("File '#{pre_name}' - OK") + rescue StandardError => e + ret = false + + # if any error, just print it and continue with the rest + OneCfg::LOG.error('Unable to process file ' \ + "'#{name}' - #{e.message}") + end + end + + ret + end + + # Show hintings based on diffs + # + # @param prefix [String] Path to OpenNebula installation + # + # @return [String] Returns string with diff + def diff(prefix, diff_format) + versions = OneCfg::EE::Config::Versions.new + migrators = versions.migrators + migrators = migrators.get_migrators_range(migrators.all_from, + versions.cfg_version) + migrators.select! {|m| m.yaml? } + + migrator = migrators.last + + # load YAML descriptor + if migrator.nil? + raise OneCfg::Config::Exception::UnsupportedVersion, + 'Could not find suitable migrator with files' + end + + migrator.load_yaml + + if !migrator.yaml || !migrator.yaml['patches'] + # this should not happen + raise OneCfg::Config::Exception::FatalError, + "Migrator for #{migrator.label} doesn't have valid YAML" + end + + ### + + OneCfg::LOG.debug('Comparing against state from ' \ + "#{migrator.label} migrator") + + patch_gen = OneCfg::EE::Patch::Generate.new(prefix) + + # process each file from descriptor + migrator.yaml['patches'].each do |name, info| + patch_gen.add(name, info) + end + + case diff_format + when 'yaml' + patch_gen.generate_yaml + when 'line' + patch_gen.generate_line + else + patch_gen.generate_hintings + end + end + + # Get configuration type class from stripped string. + # + # @param [String] Stripped class name + # + # @return [Class] Ruby class + def self.type_class(name) + Kernel.const_get("#{TYPE_CLASS_NAME}::#{name}") + end + + # Filters single file metadata entry returned by scan function + # for suitable keys, which can be used for generated YAML migrator. + # + # @param [Hash] File data + # + # @return [Hash] Filtered file data + def file4desc(data) + data.select do |k, _v| + %w[class owner group mode].include?(k) + end + end + + end + +end +# rubocop:enable Style/ClassAndModuleChildren + +__END__ + + def cfg_version + ret = nil + + @settings.load + + if @settings.content && @settings.content['version'] + ret = Gem::Version.new(@settings.content['version']) + end + + ret + rescue StandardError + nil + end + + def cfg_version=(version) + unless version + raise OneCfg::Config::Exception::FatalError, + 'Missing configuration version to save' + end + + # TODO: can fail? + @settings.load + @settings.content['version'] = version.to_s + @settings.save + end + + # Return installed OpenNebula version + # + # @param prefix [String] Location prefix + # + # @return [Gem::Version, Nil] ) Gem::Version or Nil for unknown. + def one_version(prefix = nil) + ret = nil + + if prefix + cmd = "#{prefix}/bin/oned --version" + else + cmd = 'oned --version' + end + + o, _e, s = Open3.capture3(cmd) + + if s.success? + matches = o.match(/^OpenNebula (\d+\.\d+.\d+) /) + + if matches + ret = Gem::Version.new(matches[1]) + end + end + + # TODO: add alternative methods (e.g., query packager) + + ret + rescue StandardError + nil + end + + # Return first defined version. + def defaults(ver1, ver2) + # take first defined version + ret = ver1 + ret ||= ver2 + ret = Gem::Version.new(ret) if ret && !ret.is_a?(Gem::Version) + + unless ret + raise OneCfg::Config::Exception::UnsupportedVersion, + 'Unknown OpenNebula or config. version. Check status.' + end + + # check version is supported by migrators + unless @migrators.supported?(ret) + raise OneCfg::Config::Exception::UnsupportedVersion, + "Unsupported version #{ret}" + end + + ret + end + + # Dumps versions status for poor humans beings + # + # @return[[String, Integer]] Returns twin with String and Integer. + # String is a text to show to the user. + # Integer is the exit status. + def dump_status + ret = '' + + ret << "--- Versions -----------------\n" + + ret << format("%-11s %-8s\n", + 'OpenNebula:', + one_version ? one_version : 'unknown') + + ret << format("%-11s %-8s\n", + 'Config:', + cfg_version ? cfg_version : 'unknown') + + unless one_version + ret << "ERROR: Unknown OpenNebula version\n" + return([ret, -1]) + end + + if !supported?(one_version) + ret << "ERROR: Unsupported OpenNebula version #{one_version}\n" + return([ret, -1]) + end + + unless cfg_version + ret << "ERROR: Unknown config version\n" + return([ret, -1]) + end + + if !supported?(cfg_version) + ret << "ERROR: Unsupported config version #{cfg_version}\n" + return([ret, -1]) + end + + ret << "\n--- Available updates --------\n" + + if cfg_version && latest && latest > cfg_version + ret << format("%-11s %-8s\n", 'New config:', latest) + end + + # TODO: test for downgrade + if upgrades? + get_migrators.each do |u| + ret << "- from #{u.from} to #{u.to} (" + ret << 'YAML' if u.yaml? + ret << ',' if u.yaml? && u.ruby? + ret << 'Ruby' if u.ruby? + ret << ")\n" + end + + return([ret, 1]) + elsif cfg_version && latest && latest > cfg_version + # TODO: how this can happen? + ret << "No updates available, but config. is not latest!?!\n" + else + ret << "No updates available.\n" + end + + [ret, 0] + end + + ### Proxy methods into migrators ### + + # Gets latest (highest) configuration version matching + # current OpenNebula version. + def latest(v = one_version) + @migrators.latest(v) + end + + # Check if there is any update. + def upgrades?(v1 = cfg_version, v2 = one_version) + @migrators.upgrades?(v1, v2) + end + + # Check if version is supported + def supported?(version) + @migrators.supported?(version) + end + + def get_migrators(v1 = cfg_version, v2 = one_version) + @migrators.get_migrators_range(v1, v2) + end + + end + +end +# rubocop:enable Style/ClassAndModuleChildren diff --git a/src/onecfg/lib/config/type/augeas/one.rb b/src/onecfg/lib/config/type/augeas/one.rb index bd723f9b43..6a70589521 100644 --- a/src/onecfg/lib/config/type/augeas/one.rb +++ b/src/onecfg/lib/config/type/augeas/one.rb @@ -63,7 +63,7 @@ module OneCfg::Config::Type # # @param name [String] File name # @param load_path [String] directories for modules - def initialize(name = nil, load_path = OneCfg::LENS_DIR) + def initialize(name = nil, load_path = nil) super(name, 'Oned.lns', load_path) end @@ -721,7 +721,7 @@ module OneCfg::Config::Type if found.empty? ret[:status] = true - content.set(path, data['value']) + content.set(path, data['value'].to_s) else is_m = multiple?([data['path'], data['key']]) @@ -733,13 +733,13 @@ module OneCfg::Config::Type if is_m unless found.include?(data['value']) - content.set("#{path}[0]", data['value']) + content.set("#{path}[0]", data['value'].to_s) ret[:status] = true end elsif found.length == 1 if found[0] != data['value'] && mode.include?(:replace) - content.set(path, data['value']) + content.set(path, data['value'].to_s) ret[:status] = true ret[:mode] = :replace @@ -755,7 +755,7 @@ module OneCfg::Config::Type content.rm("#{path}[last()]") end - content.set(path, data['value']) + content.set(path, data['value'].to_s) ret[:status] = true ret[:mode] = :replace @@ -773,7 +773,7 @@ module OneCfg::Config::Type ret end - # Appply single diff/patch "set" operation. + # Apply single diff/patch "set" operation. # # @param path [String] Augeas path # @param data [Hash] Single diff operation data @@ -792,9 +792,9 @@ module OneCfg::Config::Type # can be multiple params created wrongly by user. So we # try to test and set only first one. Nothing else # matters anyway... - content.set("#{path}[1]", data['value']) + content.set("#{path}[1]", data['value'].to_s) else - content.set(path, data['value']) + content.set(path, data['value'].to_s) end ret[:status] = true @@ -806,7 +806,7 @@ module OneCfg::Config::Type end end - content.set(path, data['value']) + content.set(path, data['value'].to_s) ret[:status] = true ret[:mode] = :replace @@ -825,6 +825,11 @@ module OneCfg::Config::Type # # @return [String] Formatted key def hinting_key(data) + return super(data) + + ### This function is disabled for now ### + # rubocop:disable Lint/UnreachableCode + full_path = [data['path'], data['key']].flatten.compact if full_path.empty? @@ -843,6 +848,7 @@ module OneCfg::Config::Type full_path.join('/') end + # rubocop:enable Lint/UnreachableCode end # Upcase node names in the Augeas content object to avoid diff --git a/src/onecfg/lib/config/type/augeas/shell.rb b/src/onecfg/lib/config/type/augeas/shell.rb index 6f4fab781f..23be3519b3 100644 --- a/src/onecfg/lib/config/type/augeas/shell.rb +++ b/src/onecfg/lib/config/type/augeas/shell.rb @@ -311,7 +311,7 @@ module OneCfg::Config::Type # New variables are easily set with required export status. if found.empty? - content.set(key, val) + content.set(key, val.to_s) export_key(key, exp) ret[:status] = true @@ -324,7 +324,7 @@ module OneCfg::Config::Type content.rm("#{key}[1]") end - content.set(key, val) + content.set(key, val.to_s) export_key(key, exp) ret[:status] = true @@ -335,7 +335,7 @@ module OneCfg::Config::Type ret end - # Appply single diff/patch "set" operation. + # Apply single diff/patch "set" operation. # # @param data [Hash] Single diff operation data # @param mode [Array] Patch modes (see patch method) @@ -358,7 +358,7 @@ module OneCfg::Config::Type content.rm("#{key}[1]") end - content.set(key, val) + content.set(key, val.to_s) ret[:status] = true ret[:mode] = :replace if found[-1] != old @@ -381,29 +381,17 @@ module OneCfg::Config::Type ret end - # Get value for hintings - # - # @param data [Hash] Element with diff information - # - # @return [String] Formatted value - def hinting_value(data) - ret = nil - - if data.key?('value') - ret = data['value'].inspect - elsif data.key?('old') - ret = data['old'].inspect - end - - ret - end - # Get extra metadata for hintings # # @param data [Hash] Element with diff information # # @return [String] Formatted value def hinting_extra(data) + return super(data) + + ### This function is disabled for now ### + # rubocop:disable Lint/UnreachableCode + return unless data.key?('extra') ret = '' @@ -417,6 +405,7 @@ module OneCfg::Config::Type end ret.strip + # rubocop:enable Lint/UnreachableCode end # Returns all details from single diff operation metadata diff --git a/src/onecfg/lib/config/type/base.rb b/src/onecfg/lib/config/type/base.rb index dc32bb9773..8180158db8 100644 --- a/src/onecfg/lib/config/type/base.rb +++ b/src/onecfg/lib/config/type/base.rb @@ -17,6 +17,11 @@ require 'fileutils' require 'open3' +# Early load of newer JSON gem, which supports serialization of scalars, e.g. +# > JSON.generate('string') +gem 'json', '>= 2.0' +require 'json' + # rubocop:disable Style/ClassAndModuleChildren module OneCfg::Config::Type @@ -165,7 +170,7 @@ module OneCfg::Config::Type # # @param cfg[OneCfg::Config::Base] Configuration to diff with # - # @return [String, nil] String with diff if files are not + # @return [Array, nil] Array with diff if files are not # identical. nil if files are identical. Exception on error. def diff(cfg) data = file_diff(cfg) @@ -184,6 +189,71 @@ module OneCfg::Config::Type end end + # Create a diff metadata based based on hintings. + # + # @param diff [Array] Array of strings with hintings + # @param symbols [Bool] If symbol-like strings should be symbolized + # + # @return [Array] Array with diff. + def diff_from_hintings(hintings, symbols = false) + # hinting formats: + # - [] + parsed = OneCfg::Common::HintingParser.new(hintings).parse + + # path/key value + state = parsed[:command] + path = parsed[:path] + key = path.pop + value = parsed[:value] + + # symbolize path/key/value back + if symbols + path = symbolize(path) + key = symbolize(key) + value = symbolize(value) + end + + # TODO, check how to manage extras + extra = { 'hintings' => true } + + ret = { + 'state' => state, + 'path' => path, + 'key' => key, + 'value' => nil, + 'old' => nil, + 'extra' => extra + } + + case state + when 'rm' + ret['old'] = value + + # Problematic case: on removal of array element, the path + # contains whole path to the array, but there is no key, + # since it's not a key/valued-hash. We identify array + # element removal by presence of value in rm command. + # + # > rm path/key "value" + # + # In this case, we put key back into path structure. + unless value.nil? || key.nil? + ret['path'] << key + ret['key'] = nil + end + when 'set' + # 'set' changes existing value from old to value. This + # workarounds the fact that we don't know old value and + # forces the change to happen if target is not on value. + ret['value'] = value + ret['old'] = value + else + ret['value'] = value + end + + ret + end + # Patches object based on provided diff data. # # @param data [Array] Diff data @@ -236,6 +306,8 @@ module OneCfg::Config::Type ret ||= patch_status[:status] rep << patch_status + rescue OneCfg::Config::Exception::PatchRetryOperation + retry # TODO: rescue on any exception rescue StandardError => e @@ -286,18 +358,18 @@ module OneCfg::Config::Type case d['state'] when 'ins' - ret << "#{r_status}ins #{key} = #{value} #{extra}" + ret << "#{r_status}ins #{key} #{value} #{extra}" when 'set' if value.nil? ret << "#{r_status}set #{key} #{extra}" else - ret << "#{r_status}set #{key} = #{value} #{extra}" + ret << "#{r_status}set #{key} #{value} #{extra}" end when 'rm' if value.nil? ret << "#{r_status}rm #{key} #{extra}" else - ret << "#{r_status}rm #{key} = #{value} #{extra}" + ret << "#{r_status}rm #{key} #{value} #{extra}" end else ret << "unknown operation #{state}" @@ -398,36 +470,91 @@ module OneCfg::Config::Type ret end + # Looks into value and all symbols convers to strings starting with ':'. + # Recursively goes deep through the arrays and hashes, where + # stringifies both suspicious keys and values. + # + # @param value [Any] Value to stringify + # + # @return [Any] Value with symbols converted to strings starting with : + def unsymbolize(value) + # rubocop:disable Style/CaseLikeIf + if value.is_a?(Symbol) + ":#{value}" + elsif value.is_a?(Array) + value.collect {|v| unsymbolize(v) } + elsif value.is_a?(Hash) + Hash[value.collect {|k, v| [unsymbolize(k), unsymbolize(v)] }] + else + value + end + # rubocop:enable Style/CaseLikeIf + end + + # Looks into value and texts which begin with ':' converts to symbols. + # Recursively goes deep through the arrays and hashes, where symbolizes + # back both suspicious keys and values. + # + # @param value [Any] Value to symbolize + # + # @return [Any] Value with symbolized values where begins with : + def symbolize(value) + # TODO: don't symbolize value if it contains spaces? + if value.is_a?(String) && value.start_with?(':') && value.length > 1 + value[1..-1].to_sym + elsif value.is_a?(Array) + value.collect {|v| symbolize(v) } + elsif value.is_a?(Hash) + Hash[value.collect {|k, v| [symbolize(k), symbolize(v)] }] + else + value + end + end + # Get key for hintings # - # @param diff [Hash] Element with diff information + # @param diff [Hash] Element with diff information + # @param symbols [Bool] If symbols in paths should be preserved # # @return [String] Formatted key - def hinting_key(data) + def hinting_key(data, symbols = false) full_path = [data['path'], data['key']].flatten.compact if full_path.empty? - '(top)' + ret = '/' + elsif symbols + ret = unsymbolize(full_path).join('/') else - full_path.join('/') + ret = full_path.join('/') end + + # if there are special characters, better quote + # TODO: parser probably doesn't handle correctly \'?? + ret.index(/[" ]/) ? ret.inspect : ret end # Get value for hintings # - # @param data [Hash] Element with diff information + # @param data [Hash] Element with diff information + # @param symbols [Bool] If symbols in values should be preserved # # @return [String] Formatted value - def hinting_value(data) - ret = '??? UNKNOWN ???' + def hinting_value(data, symbols = false) + ret = nil if data.key?('value') - ret = data['value'].inspect - elsif data.key?('old') - ret = data['old'].inspect + ret = data['value'] + elsif data.key?('old') && !data['key'] + ret = data['old'] + else + # Quiet here, just in case we are going to JSONify nil, + # as it can be value if set by above cases!! + return '' end - ret + ret = unsymbolize(ret) if symbols + + JSON.generate(ret) end # Get extra metadata for hintings diff --git a/src/onecfg/lib/config/type/yaml.rb b/src/onecfg/lib/config/type/yaml.rb index 92f582dcef..ca38aed593 100644 --- a/src/onecfg/lib/config/type/yaml.rb +++ b/src/onecfg/lib/config/type/yaml.rb @@ -85,12 +85,24 @@ module OneCfg::Config::Type ret.empty? ? nil : ret end + def diff_from_hintings(hintings) + super(hintings, true) + end + ################################################################## # Private Methods ################################################################## private + def hinting_key(data) + super(data, true) + end + + def hinting_value(data) + super(data, true) + end + # Walks through the tree1 and tree2 data structures # starting the provided path, compares them and returns # array of differences. @@ -476,6 +488,24 @@ module OneCfg::Config::Type # insert new key into a Hash if data['key'] + + ### WORKAROUND + # Insert action created from hintings can't properly + # distinguish between insert into Array and Hash and + # set path/key properly. We double check if we don't + # try to insert into actual Array and if yes, we + # update the diff structure and retry operation. + # rubocop:disable Style/SoleNestedConditional + if data['extra'] && data['extra']['hintings'] + if tree.is_a?(Hash) && tree[data['key']].is_a?(Array) + data['path'] << data['key'] + data['key'] = nil + + raise OneCfg::Config::Exception::PatchRetryOperation + end + end + # rubocop:enable Style/SoleNestedConditional + if tree.is_a? Hash if tree.key?(data['key']) dc = OneCfg::Config::Utils.deep_compare( diff --git a/src/onecfg/lib/exception.rb b/src/onecfg/lib/exception.rb index 1c0fb29258..b0c646024e 100644 --- a/src/onecfg/lib/exception.rb +++ b/src/onecfg/lib/exception.rb @@ -59,6 +59,17 @@ module OneCfg end # rubocop:enable Lint/UselessMethodDefinition + # OneCfg parser exception on file + # rubocop:disable Lint/UselessMethodDefinition + class FileParseError < Generic + + def initialize(text) + super(text) + end + + end + # rubocop:enable Lint/UselessMethodDefinition + end end diff --git a/src/onecfg/lib/onecfg.rb b/src/onecfg/lib/onecfg.rb index 46c582fe91..fa87cdad6e 100644 --- a/src/onecfg/lib/onecfg.rb +++ b/src/onecfg/lib/onecfg.rb @@ -19,24 +19,31 @@ require 'common' require 'settings' require 'version' require 'config' +require 'transaction' +require 'patch' begin require 'ee' rescue LoadError + # If we can't load EE, we are running CE only end # OneCfg main module module OneCfg + LOG = OneCfg::Common::CliLogger + ONE_LOCATION = ENV['ONE_LOCATION'] + ETC_DIR = ENV['ONECFG_ETC_DIR'] if ENV.key?('ONECFG_ETC_DIR') + BACKUP_DIR = ENV['ONECFG_BACKUP_DIR'] if ENV.key?('ONECFG_BACKUP_DIR') # Global directories # TODO: improve if ONE_LOCATION BIN_DIR = File.join(ONE_LOCATION, 'bin') - ETC_DIR = '/tmp/onescape/etc' - BACKUP_DIR = '/tmp/onescape/backups' SHARE_DIR = ONE_LOCATION + '/share/onecfg' + ETC_DIR ||= '/tmp/onescape/etc' + BACKUP_DIR ||= '/tmp/onescape/backups' [ETC_DIR, BACKUP_DIR].each do |d| OneCfg::LOG.warn("Using local state in #{d}") @@ -44,13 +51,11 @@ module OneCfg end else BIN_DIR = '/usr/bin' - ETC_DIR = '/etc/onescape' - BACKUP_DIR = '/var/lib/one/backups/config' - SHARE_DIR = '/usr/share/one/onecfg' + SHARE_DIR = '/usr/share/one/onecfg' + ETC_DIR ||= '/etc' + BACKUP_DIR ||= '/var/lib/one/backups/config' end - LOG = OneCfg::Common::CliLogger - # Project local directories CONF_DIR = File.join(SHARE_DIR, 'etc') MIGR_DIR = File.join(SHARE_DIR, 'migrators') @@ -58,7 +63,7 @@ module OneCfg # Individual files FILES_CFG = File.join(CONF_DIR, 'files.yaml') - CONFIG_CFG = File.join(ETC_DIR, 'config.yaml') + CONFIG_CFG = File.join(ETC_DIR, 'onecfg.conf') # Configuration management releated constants CONFIG_BACKUP_DIRS = ['/etc/one', '/var/lib/one/remotes'] diff --git a/src/onecfg/lib/patch.rb b/src/onecfg/lib/patch.rb new file mode 100644 index 0000000000..b09f4cbe53 --- /dev/null +++ b/src/onecfg/lib/patch.rb @@ -0,0 +1,31 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require_relative 'patch/apply' + +begin + require 'ee/patch' +rescue LoadError +end + +module OneCfg + + # OneCfg::Patch module + module Patch + + end + +end diff --git a/src/onecfg/lib/patch/apply.rb b/src/onecfg/lib/patch/apply.rb new file mode 100644 index 0000000000..840166b009 --- /dev/null +++ b/src/onecfg/lib/patch/apply.rb @@ -0,0 +1,221 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +# rubocop:disable Style/ClassAndModuleChildren +module OneCfg::Patch + + # Class for generating patches + class Apply + + # Class constructor + def initialize(args = {}) + @patches = { 'patches' => {} } + + # TODO: move common defaults on a single place + @prefix = '/' + @unprivileged = false + @no_operation = false + + # set based on args + @prefix = args[:prefix] unless args[:prefix].nil? + @unprivileged = args[:unprivileged] unless args[:unprivileged].nil? + @no_operation = args[:noop] unless args[:noop].nil? + end + + def read_from_file(filename, format) + if format.nil? + begin + if File.open(filename, &:readline).strip == '---' + format = 'yaml' + end + rescue StandardError + # we do silently ignore any errors in format detection + # as they might be much better handled below + end + + format ||= 'line' + end + + begin + case format + when 'yaml' + parse_yaml(filename) + else + parse_hintings(filename) + end + rescue OneCfg::Exception::FileParseError => e + raise e + rescue StandardError => e + raise "Error reading #{format} patch (#{e})" + end + end + + def apply(all = true) + if @patches['patches'].empty? + OneCfg::LOG.error('No changes to apply') + return(-1) + end + + # all changes are running in transaction, + # which is "rollbacked" in case of error + tr = OneCfg::Transaction.new + + tr.prefix = @prefix + tr.unprivileged = @unprivileged + tr.no_operation = @no_operation + + OneCfg::LOG.info('Applying patch to ' \ + "#{@patches['patches'].size} files") + + ret = 0 + + # total number of changes + total_count = 0 + total_success = 0 + + tr.execute do |tr_prefix, _fops| + @patches['patches'].each do |filename, patch| + # Get prefixed name (do not modified original file) + pre_name = OneCfg::Config::Utils.prefixed(filename, + tr_prefix) + + # Get file object based on the type + OneCfg::LOG.ddebug("Reading file '#{filename}'") + file = OneCfg::Config::Files.type_class(patch['class']) + .new(pre_name) + file.load + + # Apply changes + begin + OneCfg::LOG.debug("Patching file '#{filename}'") + rc, rep = file.patch(patch['change'], [:replace]) + rescue StandardError => e + OneCfg::LOG.error('Failed to patch file ' \ + "'#{filename}' - #{e}") + return(-1) + end + + # Count non/applied patches + patch_count = rep.size + patch_success = 0 + + rep.each.each do |r| + if r[:status] + patch_success += 1 + else + ret = 1 + end + end + + # Add counts to totals + total_count += patch_count + total_success += patch_success + + OneCfg::LOG.info("Patched '#{filename}' with " \ + "#{patch_success}/#{patch_count} " \ + 'changes') + + # show report + OneCfg::LOG.debug("--- PATCH REPORT '#{filename}' --- ") + file.hintings(patch['change'], rep).each do |l| + OneCfg::LOG.debug("Patch #{l}") + end + + file.save if rc + end + + # there were changes, but nothing could be applied + if total_success == 0 + OneCfg::LOG.error('No changes applied') + return(-1) + end + + # use requested all changes to apply, not even less + if all && total_success != total_count + OneCfg::LOG.error('Modifications not saved due to ' \ + "#{total_count - total_success} " \ + 'unapplied changes!') + return(-1) + end + + # statistics at the end before finishing transaction + OneCfg::LOG.info("Applied #{total_success}/#{total_count} " \ + 'changes') + end + + ret + end + + private + + # Add a file diff to be processed + # + # @param filename [String] path to patch in YAML format + def parse_yaml(filename) + @patches = YAML.load_file(filename) + + return if @patches.is_a?(Hash) + + raise OneCfg::Exception::FileParseError, + 'The patch does not contain expected YAML document' + end + + # Add a file diff to be processed in hinting format + # + # @param filename [String] path to patch in hintings format + def parse_hintings(filename) + files = OneCfg::Config::Files.new.scan(@prefix, false) + + changes = {} + + File.open(filename).each_with_index do |line, idx| + # skip empty lines and comments + line.strip! + next if line.empty? || line.start_with?('#') + + # line format: + if line.split.size < 2 + raise OneCfg::Exception::FileParseError, + "Invalid format at line: #{idx + 1}." + end + + # TODO: should be managed by parser + h_filename, hintings = line.split(' ', 2) + + unless files.key?(h_filename) + raise OneCfg::Exception::FileParseError, + "Unknown or missing file to patch '#{h_filename}'" + end + + # get suitable file manipulation object and create a diff + file_obj = files[h_filename]['ruby_class'].new(h_filename) + changes[h_filename] ||= [] + changes[h_filename] << file_obj.diff_from_hintings(hintings) + end + + # create internal structure similar to what we get from YAML + changes.each do |f, c| + @patches['patches'][f] = { + 'class' => files[f]['class'], + 'change' => c + } + end + end + + end + +end +# rubocop:enable Style/ClassAndModuleChildren diff --git a/src/onecfg/lib/transaction.rb b/src/onecfg/lib/transaction.rb new file mode 100644 index 0000000000..caddc03d96 --- /dev/null +++ b/src/onecfg/lib/transaction.rb @@ -0,0 +1,194 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2020, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module OneCfg + + # Transactional operations with configuration files + class Transaction + + attr_accessor :prefix + attr_accessor :read_from + attr_accessor :unprivileged + attr_accessor :no_operation + attr_accessor :hook_post_copy + + def initialize + # TODO: move common defaults on a single place + @prefix = '/' + @read_from = nil + @unprivileged = false + @no_operation = false + @hook_post_copy = nil + end + + # Runs a passed code block on a copy of configuration files in + # a transaction-like way. If block finishes successfully, + # the changed configuration files are copied back to their + # right place. Code gets transactino prefix directory and + # file FileOperation object. + # + # @yield [tr_prefix, fops] Execute custom code with + # + # @return [Boolean,Nil] True on successful migration. + def execute + OneCfg::LOG.ddebug("Preparing transaction for prefix '#{@prefix}'") + + check_symlinks(@prefix) + check_symlinks(@read_from) if @read_from + + ### emergency backup ### <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + backup = backup_dirs + ### emergency backup ### <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + tr_prefix = Dir.mktmpdir + + # copy data from @read_from/@prefix into transaction_prefix + # rubocop:disable Style/RedundantCondition + OneCfg::Common::Backup.restore_dirs( + @read_from ? @read_from : @prefix, + OneCfg::CONFIG_BACKUP_DIRS, + tr_prefix + ) + # rubocop:enable Style/RedundantCondition + + # file operations will be locked to transaction prefix + fops = OneCfg::Config::FileOperation.new(tr_prefix, @unprivileged) + + # run custom code + OneCfg::LOG.ddebug("Running transaction in '#{tr_prefix}'") + ret = yield(tr_prefix, fops) + + # in no-operation mode, we finish before copying + # changed configuration back + if @no_operation + OneCfg::LOG.ddebug('Transaction code successful, but ' \ + 'executed in no-op mode. Changes will ' \ + 'NOT BE SAVED!') + + OneCfg::LOG.info('Changes ARE NOT saved in no-op mode!') + + return(ret) + end + + OneCfg::LOG.ddebug('Transaction code successful') + + # Copy updated configuration back from transaction_prefix + # to original @prefix location. Restore on any failure. + begin + # We copy back from transaction_prefix only CONFIG_UPDATE_DIRS, + # which should be a subset of directories we have + # backuped. This enables to work with whole remotes/, + # but copy back only remotes/etc. + OneCfg::Common::Backup.restore_dirs( + tr_prefix, + OneCfg::CONFIG_UPDATE_DIRS, # <-- !!! + @prefix + ) + + if @hook_post_copy + OneCfg::LOG.ddebug('Running transaction post-copy hook') + @hook_post_copy.call(ret) + end + rescue StandardError + restore_dirs(backup) + + raise + end + + OneCfg::LOG.ddebug('Transaction done') + + ret + ensure + # cleanup temporary transaction directory + if defined?(tr_prefix) && !tr_prefix.nil? && File.exist?(tr_prefix) + FileUtils.rm_r(tr_prefix) + end + end + + private + + # Checks there are no symlinks in backup directories. + # Raise exception in case of error. + # + # @param custom_prefix [String] Custom prefix to check + def check_symlinks(custom_prefix = @prefix) + OneCfg::CONFIG_BACKUP_DIRS.each do |dir| + pre_dir = OneCfg::Config::Utils.prefixed(dir, custom_prefix) + OneCfg::LOG.dddebug("Checking symbolic links in '#{pre_dir}'") + + Dir["#{pre_dir}/**/**"].each do |f| + if File.symlink?(f) + raise OneCfg::Config::Exception::FatalError, + "Found symbolic links in '#{f}'" + end + end + end + end + + # Backup all dirs inside prefix. This is intended as + # backup for emergency cases, when upgrade fails and + # we need to revert changes back. + # + # @return [String] Backup path + def backup_dirs + backup = OneCfg::Common::Backup.backup_dirs( + OneCfg::CONFIG_BACKUP_DIRS, + nil, # backup name autogenerated + @prefix + ) + + OneCfg::LOG.unknown("Backup stored in '#{backup}'") + + backup + rescue StandardError => e + raise OneCfg::Config::Exception::FatalError, + "Error making backup due to #{e.message}" + end + + # Restore all dirs inside prefix. This is intended as + # restore after upgrade failure of production directories. + # + # @param backup [String] Backup path + def restore_dirs(backup) + OneCfg::LOG.unknown('Restoring from backups') + + OneCfg::Common::Backup.restore_dirs( + backup, + OneCfg::CONFIG_BACKUP_DIRS, + @prefix + ) + + OneCfg::LOG.debug('Restore successful') + rescue StandardError + msg = 'Fatal error on restore, we are very sorry! ' \ + 'You have to restore following directories ' \ + 'manually:' + + OneCfg::CONFIG_BACKUP_DIRS.each do |dir| + src = File.join(backup, dir) + dst = prefixed(dir) + + msg << "\n\t- copy #{src} into #{dst}" + end + + OneCfg::LOG.fatal(msg) + + raise + end + + end + +end diff --git a/src/onecfg/share/augeas/oned.aug b/src/onecfg/share/augeas/oned.aug deleted file mode 100644 index 1501f74806..0000000000 --- a/src/onecfg/share/augeas/oned.aug +++ /dev/null @@ -1,78 +0,0 @@ -module Oned = - autoload xfm - -(* Version: 1.4 *) - -(* Change log: *) -(* 1.4: Allow space after section *) -(* 1.3: Allow escaped quotes in values *) -(* 1.2: Include /etc/one/monitord.conf *) - -(* primitives *) -let sep = del /[ \t]*=[ \t]*/ " = " -let eol = del /\n/ "\n" -let opt_space = del /[ \t]*/ "" -let opt_space_nl = del /[ \t\n]*/ "\n" -let opt_nl_indent = del /[ \t\n]*/ "\n " -let comma = del /,/ "," -let left_br = del /\[/ "[" -let right_br = del /\]/ "]" - -(* Regexes *) -(* Match everyhting within quotes, allow escape quote *) -let re_quoted_str = /"(\\\\[\\\\"]|[^\\\\"])*"/ - -(* Match everything except spaces, quote("), l-bracket([) and num-sign(#) *) -let re_value_str = /[^ \t\n"\[#]+/ - -(* Match everything except spaces, quote("), num-sign(#) and comma(,) *) -let re_section_value_str = /[^ \t\n"#,]+/ - -(* Store either after-value comment or full-line comment *) -let comment = [ label "#comment" . store /#[^\n]*/ ] -let comment_eol = comment . eol - - -(* Simple words *) -let name = key /[A-Za-z_0-9]+/ -let re_simple_value = re_quoted_str | re_value_str - - -(* Top level entry like `PORT = 2633` *) -let top_level_entry = name . sep . store re_simple_value -let top_level_line = opt_space - . [ top_level_entry . opt_space . (comment)? ] - . eol - - -(* Section lens for section like `LOG = [ ... ]` *) -let section_value = re_quoted_str | re_section_value_str -let section_entry = [ name . sep . store section_value ] -let section_entry_list = - ( section_entry . opt_space . comma . opt_nl_indent - | comment_eol . opt_space )* - . section_entry . opt_space_nl - . ( comment_eol )* - -let section = opt_space - . [ name . sep - . left_br - . opt_nl_indent - . section_entry_list - . right_br ] - . opt_space - . eol - -let empty_line = [ del /[ \t]*\n/ "\n" ] - -(* Main lens *) -let lns = ( top_level_line | comment_eol | section | empty_line )* - - -(* Variable: filter *) -let filter = incl "/etc/one/oned.conf" - . incl "/etc/one/sched.conf" - . incl "/etc/one/monitord.conf" - . incl "/etc/one/vmm_exec/vmm_exec_kvm.conf" - -let xfm = transform lns filter diff --git a/src/onecfg/share/augeas/test_oned.aug b/src/onecfg/share/augeas/test_oned.aug deleted file mode 100644 index d635e91f39..0000000000 --- a/src/onecfg/share/augeas/test_oned.aug +++ /dev/null @@ -1,184 +0,0 @@ -module Test_oned = - - test Oned.lns get -"ENTRY = 123 -" =? - - test Oned.lns get -"ENTRY = \"MANAGE ABC\" -" =? - - test Oned.lns get -"TM_MAD_CONF = [NAME=123] -" =? - - test Oned.lns get " -A = [ NAME=123 ] -" =? - - test Oned.lns get -"A = [ -NAME=123 -] -" = ? - - test Oned.lns get -"A = [ -NAME=123, NAME2=2 -] -" = ? - - test Oned.lns get - -"#abc -LOG = [ - SYSTEM = \"file\", - DEBUG_LEVEL = 3 -] -" =? - - test Oned.lns get -"A=1 -A=1 -B=2 # comment -# abc -# - - C=[ - A=\"B\", - A=\"B\",#abc - # abc - X=\"Y\", - A=123 -] -" =? - - test Oned.lns get -"C=[ - A=123, #abc - B=223# abc -] -" -=? - test Oned.lns get -"TM_MAD = [ - EXECUTABLE = \"one_tm\", - ARGUMENTS = \"-t 15 -d dummy,lvm,shared,fs_lvm,qcow2,ssh,ceph,dev,vcenter,iscsi_libvirt\" -] -INHERIT_DATASTORE_ATTR = \"CEPH_HOST\" -" -=? - -test Oned.lns get -"LOG = [ - SYSTEM = \"file\", - DEBUG_LEVEL = 3 -] - -MONITORING_INTERVAL_HOST = 180 -MONITORING_INTERVAL_VM = 180 -MONITORING_INTERVAL_DATASTORE = 300 -MONITORING_INTERVAL_MARKET = 600 -MONITORING_THREADS = 50 - -SCRIPTS_REMOTE_DIR=/var/tmp/one -PORT = 2633 -LISTEN_ADDRESS = \"0.0.0.0\" -DB = [ BACKEND = \"sqlite\" ] - -VNC_PORTS = [ - START = 5900 -] - -FEDERATION = [ - MODE = \"STANDALONE\", - ZONE_ID = 0, - SERVER_ID = -1, - MASTER_ONED = \"\" -] - -RAFT = [ - LIMIT_PURGE = 100000, - LOG_RETENTION = 500000, - LOG_PURGE_TIMEOUT = 600, - ELECTION_TIMEOUT_MS = 2500, - BROADCAST_TIMEOUT_MS = 500, - XMLRPC_TIMEOUT_MS = 450 -] - -DEFAULT_COST = [ - CPU_COST = 0, - MEMORY_COST = 0, - DISK_COST = 0 -] - -NETWORK_SIZE = 254 - -MAC_PREFIX = \"02:00\" - -VLAN_IDS = [ - START = \"2\", - RESERVED = \"0, 1, 4095\" -] - -VXLAN_IDS = [ - START = \"2\" -] - -DATASTORE_CAPACITY_CHECK = \"yes\" - -DEFAULT_DEVICE_PREFIX = \"hd\" -DEFAULT_CDROM_DEVICE_PREFIX = \"hd\" - -DEFAULT_IMAGE_TYPE = \"OS\" -IM_MAD = [ - NAME = \"collectd\", - EXECUTABLE = \"collectd\", - ARGUMENTS = \"-p 4124 -f 5 -t 50 -i 60\" ] - -IM_MAD = [ - NAME = \"kvm\", - SUNSTONE_NAME = \"KVM\", - EXECUTABLE = \"one_im_ssh\", - ARGUMENTS = \"-r 3 -t 15 -w 90 kvm\" ] - -IM_MAD = [ - NAME = \"vcenter\", - SUNSTONE_NAME = \"VMWare vCenter\", - EXECUTABLE = \"one_im_sh\", - ARGUMENTS = \"-c -t 15 -r 0 vcenter\" ] - -IM_MAD = [ - NAME = \"ec2\", - SUNSTONE_NAME = \"Amazon EC2\", - EXECUTABLE = \"one_im_sh\", - ARGUMENTS = \"-c -t 1 -r 0 -w 600 ec2\" ] - -VM_MAD = [ - NAME = \"kvm\", - SUNSTONE_NAME = \"KVM\", - EXECUTABLE = \"one_vmm_exec\", - ARGUMENTS = \"-t 15 -r 0 kvm\", - DEFAULT = \"vmm_exec/vmm_exec_kvm.conf\", - TYPE = \"kvm\", - KEEP_SNAPSHOTS = \"no\", - IMPORTED_VMS_ACTIONS = \"terminate, terminate-hard, hold, release, suspend, - resume, delete, reboot, reboot-hard, resched, unresched, disk-attach, - disk-detach, nic-attach, nic-detach, snapshot-create, snapshot-delete\" -] -" = ? - - - test Oned.lns get -"PASSWORD = \"open\\\"nebula\" -" =? - - test Oned.lns get -"DB = [ - PASSWORD = \"open\\\"nebula\" -] -" =? - - test Oned.lns get -" NIC = [ model=\"virtio\" ] -" =? From f1d7811ac26160b956b653bd07dfcab00378343d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ospal=C3=BD?= Date: Tue, 5 Jan 2021 12:57:28 +0100 Subject: [PATCH 12/12] F OpenNebula/one#5178: Ignore logrotate's failure (#585) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Petr Ospalý --- .../services/supervisor/centos8/scripts/opennebula-fireedge.sh | 2 +- .../pkgs/services/supervisor/centos8/scripts/opennebula-flow.sh | 2 +- .../pkgs/services/supervisor/centos8/scripts/opennebula-gate.sh | 2 +- .../pkgs/services/supervisor/centos8/scripts/opennebula-hem.sh | 2 +- .../services/supervisor/centos8/scripts/opennebula-novnc.sh | 2 +- .../services/supervisor/centos8/scripts/opennebula-scheduler.sh | 2 +- .../services/supervisor/centos8/scripts/opennebula-sunstone.sh | 2 +- share/pkgs/services/supervisor/centos8/scripts/opennebula.sh | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/share/pkgs/services/supervisor/centos8/scripts/opennebula-fireedge.sh b/share/pkgs/services/supervisor/centos8/scripts/opennebula-fireedge.sh index 83adff7791..7549b14a23 100755 --- a/share/pkgs/services/supervisor/centos8/scripts/opennebula-fireedge.sh +++ b/share/pkgs/services/supervisor/centos8/scripts/opennebula-fireedge.sh @@ -37,7 +37,7 @@ fi msg "Rotate log to start with an empty one" /usr/sbin/logrotate -s /var/lib/one/.logrotate.status \ - -f /etc/logrotate.d/opennebula-fireedge + -f /etc/logrotate.d/opennebula-fireedge || true msg "Service started!" exec /usr/bin/node /usr/lib/one/fireedge/dist/index.js diff --git a/share/pkgs/services/supervisor/centos8/scripts/opennebula-flow.sh b/share/pkgs/services/supervisor/centos8/scripts/opennebula-flow.sh index dfb296832b..434dd233ee 100755 --- a/share/pkgs/services/supervisor/centos8/scripts/opennebula-flow.sh +++ b/share/pkgs/services/supervisor/centos8/scripts/opennebula-flow.sh @@ -37,7 +37,7 @@ fi msg "Rotate log to start with an empty one" /usr/sbin/logrotate -s /var/lib/one/.logrotate.status \ - -f /etc/logrotate.d/opennebula-flow + -f /etc/logrotate.d/opennebula-flow || true msg "Service started!" exec /usr/bin/ruby /usr/lib/one/oneflow/oneflow-server.rb diff --git a/share/pkgs/services/supervisor/centos8/scripts/opennebula-gate.sh b/share/pkgs/services/supervisor/centos8/scripts/opennebula-gate.sh index fe92565a17..b21f31367a 100755 --- a/share/pkgs/services/supervisor/centos8/scripts/opennebula-gate.sh +++ b/share/pkgs/services/supervisor/centos8/scripts/opennebula-gate.sh @@ -37,7 +37,7 @@ fi msg "Rotate log to start with an empty one" /usr/sbin/logrotate -s /var/lib/one/.logrotate.status \ - -f /etc/logrotate.d/opennebula-gate + -f /etc/logrotate.d/opennebula-gate || true msg "Service started!" exec /usr/bin/ruby /usr/lib/one/onegate/onegate-server.rb diff --git a/share/pkgs/services/supervisor/centos8/scripts/opennebula-hem.sh b/share/pkgs/services/supervisor/centos8/scripts/opennebula-hem.sh index 1f1de0196f..5d88c4dbf2 100755 --- a/share/pkgs/services/supervisor/centos8/scripts/opennebula-hem.sh +++ b/share/pkgs/services/supervisor/centos8/scripts/opennebula-hem.sh @@ -40,7 +40,7 @@ export SSH_AUTH_SOCK msg "Rotate log to start with an empty one" /usr/sbin/logrotate -s /var/lib/one/.logrotate.status \ - -f /etc/logrotate.d/opennebula-hem + -f /etc/logrotate.d/opennebula-hem || true msg "Service started!" exec /usr/bin/ruby /usr/lib/one/onehem/onehem-server.rb diff --git a/share/pkgs/services/supervisor/centos8/scripts/opennebula-novnc.sh b/share/pkgs/services/supervisor/centos8/scripts/opennebula-novnc.sh index 5b6c3d8e2c..057cbe8d69 100755 --- a/share/pkgs/services/supervisor/centos8/scripts/opennebula-novnc.sh +++ b/share/pkgs/services/supervisor/centos8/scripts/opennebula-novnc.sh @@ -64,7 +64,7 @@ fi msg "Rotate log to start with an empty one" /usr/sbin/logrotate -s /var/lib/one/.logrotate.status \ - -f /etc/logrotate.d/opennebula-novnc + -f /etc/logrotate.d/opennebula-novnc || true # now we will stay in a loop monitoring and faking the foreground process... while sleep 1 ; do diff --git a/share/pkgs/services/supervisor/centos8/scripts/opennebula-scheduler.sh b/share/pkgs/services/supervisor/centos8/scripts/opennebula-scheduler.sh index f362a630b1..ee2c79fee1 100755 --- a/share/pkgs/services/supervisor/centos8/scripts/opennebula-scheduler.sh +++ b/share/pkgs/services/supervisor/centos8/scripts/opennebula-scheduler.sh @@ -37,7 +37,7 @@ fi msg "Rotate log to start with an empty one" /usr/sbin/logrotate -s /var/lib/one/.logrotate.status \ - -f /etc/logrotate.d/opennebula-scheduler + -f /etc/logrotate.d/opennebula-scheduler || true msg "Service started!" exec /usr/bin/mm_sched diff --git a/share/pkgs/services/supervisor/centos8/scripts/opennebula-sunstone.sh b/share/pkgs/services/supervisor/centos8/scripts/opennebula-sunstone.sh index f2b7abc72d..0c24c1b4a7 100755 --- a/share/pkgs/services/supervisor/centos8/scripts/opennebula-sunstone.sh +++ b/share/pkgs/services/supervisor/centos8/scripts/opennebula-sunstone.sh @@ -37,7 +37,7 @@ fi msg "Rotate log to start with an empty one" /usr/sbin/logrotate -s /var/lib/one/.logrotate.status \ - -f /etc/logrotate.d/opennebula-sunstone + -f /etc/logrotate.d/opennebula-sunstone || true msg "Service started!" exec /usr/bin/ruby /usr/lib/one/sunstone/sunstone-server.rb diff --git a/share/pkgs/services/supervisor/centos8/scripts/opennebula.sh b/share/pkgs/services/supervisor/centos8/scripts/opennebula.sh index bc9403c586..e15e14c433 100755 --- a/share/pkgs/services/supervisor/centos8/scripts/opennebula.sh +++ b/share/pkgs/services/supervisor/centos8/scripts/opennebula.sh @@ -65,7 +65,7 @@ fi msg "Rotate log to start with an empty one" /usr/sbin/logrotate -s /var/lib/one/.logrotate.status \ - -f /etc/logrotate.d/opennebula + -f /etc/logrotate.d/opennebula || true msg "Service started!" exec /usr/bin/oned -f