diff --git a/src/cli/one_helper/oneprovision_helper.rb b/src/cli/one_helper/oneprovision_helper.rb index c11d360b12..d2621ff7d7 100644 --- a/src/cli/one_helper/oneprovision_helper.rb +++ b/src/cli/one_helper/oneprovision_helper.rb @@ -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 diff --git a/src/oneprovision/lib/provision/provision.rb b/src/oneprovision/lib/provision/provision.rb index d85941d051..ae6df92128 100644 --- a/src/oneprovision/lib/provision/provision.rb +++ b/src/oneprovision/lib/provision/provision.rb @@ -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 diff --git a/src/oneprovision/lib/provision/provision_config.rb b/src/oneprovision/lib/provision/provision_config.rb index 361bfcc063..5f9940d78c 100644 --- a/src/oneprovision/lib/provision/provision_config.rb +++ b/src/oneprovision/lib/provision/provision_config.rb @@ -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?