From 97d4a54a6075c951b506614a1a500fa605093ec6 Mon Sep 17 00:00:00 2001 From: Sergio Semedi Barranco Date: Fri, 3 Nov 2017 11:09:02 +0100 Subject: [PATCH] B #5531: vcenter images now are imported properly (#561) * B #5531: OpenNebula does not duplicate template/VM images * B #5531: onevcenter images works as expected * B #5531: vcenter imported images adquire proper names (cherry picked from commit a0a2a034d7920f2261d9bc5208f5db29a28af7de) --- src/sunstone/routes/vcenter.rb | 4 ++- .../remotes/lib/vcenter_driver/datastore.rb | 25 +++++++++---------- .../remotes/lib/vcenter_driver/importer.rb | 8 +++--- .../remotes/lib/vcenter_driver/vi_helper.rb | 4 +-- .../lib/vcenter_driver/virtual_machine.rb | 10 ++++++-- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/sunstone/routes/vcenter.rb b/src/sunstone/routes/vcenter.rb index e19adb305c..f8ca3bf29f 100644 --- a/src/sunstone/routes/vcenter.rb +++ b/src/sunstone/routes/vcenter.rb @@ -327,8 +327,10 @@ get '/vcenter/template/:vcenter_ref/:template_id' do end end + type = {:object => "template", :id => template_id} + # Create images or get disks information for template - error, template_disks = template.import_vcenter_disks(vc_uuid, dpool, ipool, true, template_id) + error, template_disks = template.import_vcenter_disks(vc_uuid, dpool, ipool, type, true) raise error if !error.empty? template_moref = template_copy_ref ? template_copy_ref : ref diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb b/src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb index 1cfc3a020e..9ae893ebbe 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb @@ -68,7 +68,7 @@ class Storage end end - def self.get_image_import_template(ds_name, image_path, image_type, image_prefix, ipool, template_id) + def self.get_image_import_template(ds_name, image_path, image_type, image_prefix, ipool, type, ds_id) one_image = {} one_image[:template] = "" @@ -77,18 +77,18 @@ class Storage # Get image name file_name = File.basename(image_path).gsub(/\.vmdk$/,"") - if template_id - image_name = "#{file_name} - #{ds_name} [Template #{template_id}]" - else - image_name = "#{file_name} - #{ds_name}" - end #Check if the image has already been imported - image = VCenterDriver::VIHelper.find_by_name(OpenNebula::ImagePool, - image_name, - ipool, - false) + image = VIHelper.find_image_by("SOURCE", OpenNebula::ImagePool, image_path, ds_id, ipool) + if image.nil? + # Generate a name with the reference + begin + image_name = "#{file_name} - #{ds_name} [#{type[:object]} #{type[:id]}]" + rescue + image_name = "#{file_name} - #{ds_name}" + end + #Set template one_image[:template] << "NAME=\"#{image_name}\"\n" one_image[:template] << "PATH=\"vcenter://#{image_path}\"\n" @@ -616,9 +616,8 @@ class Datastore < Storage one_image << "DEV_PREFIX=\"#{disk_prefix}\"\n" # Check image hasn't already been imported - vcenter_path = "vcenter://#{image_path}" - image_found = VCenterDriver::VIHelper.find_image_by_path(OpenNebula::ImagePool, - vcenter_path, + image_found = VCenterDriver::VIHelper.find_image_by("SOURCE", OpenNebula::ImagePool, + image_path, ds_id, ipool) diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/importer.rb b/src/vmm_mad/remotes/lib/vcenter_driver/importer.rb index 502ea32a9a..e4eb701864 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/importer.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/importer.rb @@ -34,7 +34,8 @@ def self.import_wild(host_id, vm_ref, one_vm, template) wild = true sunstone = false - error, template_disks = vcenter_vm.import_vcenter_disks(vc_uuid, dpool, ipool, sunstone) + type = {:object => "VM", :id => vm_name} + error, template_disks = vcenter_vm.import_vcenter_disks(vc_uuid, dpool, ipool, type, sunstone) return OpenNebula::Error.new(error) if !error.empty? template << template_disks @@ -427,11 +428,12 @@ def self.import_templates(con_ops, options) template = t[:template] if !template + type = {:object => "template", :id => one_t["ID"]} error, template_disks, allocated_images = template.import_vcenter_disks(vc_uuid, dpool, ipool, - false, - one_t["ID"]) + type, + false) if error.empty? t[:one] << template_disks diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/vi_helper.rb b/src/vmm_mad/remotes/lib/vcenter_driver/vi_helper.rb index 3504c5c9ca..5ff25a2c7f 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/vi_helper.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/vi_helper.rb @@ -107,10 +107,10 @@ class VIHelper return nil end - def self.find_image_by_path(the_class, path, ds_id, pool = nil) + def self.find_image_by(att, the_class, path, ds_id, pool = nil) pool = one_pool(the_class, false) if pool.nil? element = pool.find{|e| - e["PATH"] == path && + e[att] == path && e["DATASTORE_ID"] == ds_id} return element end diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb b/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb index d165a4754d..c5f7a013e7 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb @@ -225,7 +225,12 @@ class Template end end - def import_vcenter_disks(vc_uuid, dpool, ipool, sunstone=false, template_id=nil) + ######################################################################## + # Import vcenter disks + # @param type [object] contains the type of the object(:object) and identificator(:id) + # @return error, template_disks + ######################################################################## + def import_vcenter_disks(vc_uuid, dpool, ipool, type, sunstone=false) disk_info = "" error = "" sunstone_disk_info = [] @@ -264,7 +269,8 @@ class Template disk[:type], disk[:prefix], ipool, - template_id) + type, + datastore_found["ID"]) #Image is already in the datastore if image_import[:one] # This is the disk info