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

M #-: implement objects cache in provision (#583)

This commit is contained in:
Alejandro Huertas Herrero 2021-01-08 12:10:01 +01:00 committed by GitHub
parent d6d299e612
commit 00c546c72b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 40 deletions

View File

@ -332,9 +332,8 @@ class OneProvisionHelper < OpenNebulaHelper::OneHelper
# @return [Array] [rc, information to show]
def show_resource(_, options)
# Add body paremter to get resource in hash format
options[:body] = true
options[:decrypt] = true
info = super
options[:body] = true
info = super
return info if info[0] != 0

View File

@ -56,6 +56,16 @@ module OneProvision
FULL_CLUSTER = INFRASTRUCTURE_RESOURCES + %w[hosts clusters]
# Class constructor
#
# @param xml [XML] XML object representation
# @param client [OpenNebula::Client] Client to execute calls
def initialize(xml, client)
super
@mutex = Mutex.new
end
# Allocates a new document
#
# @param template [Hash] Document information
@ -126,7 +136,8 @@ module OneProvision
return { 'ID' => -1, 'NAME' => 'dummy' }
end
Provider.by_name(@client, @body['provider'])
@provider ||= Provider.by_name(@client, @body['provider'])
@provider
end
# Returns infrastructure + resource objects
@ -153,35 +164,48 @@ module OneProvision
# Get OpenNebula information for specific objects
#
# @param object [String] Object to check
# @param object [String] Object to check
#
# @return [Array] OpenNebula objects
def info_objects(object)
rc = info
def info_objects(object, update = false)
ret = nil
if OpenNebula.is_error?(rc)
raise OneProvisionLoopException, rc.message
end
@mutex.synchronize do
if FULL_CLUSTER.include?(object)
path = 'infrastructure'
else
path = 'resource'
end
return [] unless @body['provision'][path][object]
resource = Resource.object(object)
ret = []
@body['provision'][path][object].each do |o|
rc = resource.info(o['id'])
if OpenNebula.is_error?(rc)
raise OneProvisionLoopException, rc.message
if @cache
ret = @cache[object]
end
ret << resource.one
if update || !ret || ret.empty?
rc = info
if OpenNebula.is_error?(rc)
raise OneProvisionLoopException, rc.message
end
@cache = {} unless @cache
@cache[object] = []
if FULL_CLUSTER.include?(object)
path = 'infrastructure'
else
path = 'resource'
end
resource = Resource.object(object)
@body['provision'][path][object].each do |o|
rc = resource.info(o['id'])
if OpenNebula.is_error?(rc)
raise OneProvisionLoopException, rc.message
end
@cache[object] << resource.one
end
ret = @cache[object]
end
end
ret
@ -291,7 +315,7 @@ module OneProvision
if skip == :none
configure
else
info_objects('hosts') {|h| h.enable }
info_objects('hosts', true) {|h| h.enable }
end
create_virtual_resources(cfg)
@ -355,10 +379,6 @@ module OneProvision
)
end
rc = info
return rc if OpenNebula.is_error?(rc)
if running_vms? && !cleanup
Utils.fail('Provision with running VMs can\'t be deleted')
end
@ -403,9 +423,6 @@ module OneProvision
return rc if OpenNebula.is_error?(rc)
0
ensure
# If provision does not exist, skip unlock
unlock unless OpenNebula.is_error?(info)
end
# Updates provision objects

View File

@ -240,7 +240,9 @@ module OneProvision
match = match.split('.')
if match.size == 1
index = @config['provision']['index'] if @config['provision']
if @config['provision']
index = @config['provision']['index']
end
value.gsub!('${provision}', provision.name.to_s)
value.gsub!('${provision_id}', provision.id.to_s)
@ -248,7 +250,6 @@ module OneProvision
value.gsub!('${index}', index.to_s) if index
else
objects = provision.info_objects("#{match[0]}s")
obj_int = Integer(match[1]) rescue false
if obj_int
@ -264,8 +265,8 @@ module OneProvision
object = object[object.keys[0]]
replace = object[key]
replace = object['TEMPLATE'][key] unless replace
replace = object['TEMPLATE']['PROVISION'][key] unless replace
replace ||= object['TEMPLATE'][key]
replace ||= object['TEMPLATE']['PROVISION'][key]
value.gsub!("${#{match.join('.')}}", replace)
end
@ -577,7 +578,9 @@ module OneProvision
if elem_int
elem = elements[elem_int]
else
elem = elements.find {|o| o['NAME'] == match[1] }
elem = elements.find do |o|
o['NAME'] == match[1] || o['name'] == match[1]
end
end
return [false, "#{match[0]} #{match[1]} not found"] if elem.nil?