From ed2fdf7b9a3c8702e1ab9cde925efcbf444e6220 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 4 Oct 2016 17:27:41 +0200 Subject: [PATCH] B #4385: use moref and name to search for templates (cherry picked from commit f191467515f01be74f4e19869a1f7f9f167196d3) --- src/vmm_mad/remotes/vcenter/deploy | 8 ++- src/vmm_mad/remotes/vcenter/vcenter_driver.rb | 69 ++++++++++++++----- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/vmm_mad/remotes/vcenter/deploy b/src/vmm_mad/remotes/vcenter/deploy index 31aaae24b2..12d0c2bff4 100755 --- a/src/vmm_mad/remotes/vcenter/deploy +++ b/src/vmm_mad/remotes/vcenter/deploy @@ -45,12 +45,18 @@ lcm_state_num = drv_action["/VM/LCM_STATE"].to_i datastore = drv_action["/VM/USER_TEMPLATE/VCENTER_DATASTORE"] lcm_state = OpenNebula::VirtualMachine::LCM_STATE[lcm_state_num] +ops = { + :ref => drv_action["/VM/USER_TEMPLATE/PUBLIC_CLOUD/VCENTER_REF"], + :name => drv_action["/VM/USER_TEMPLATE/PUBLIC_CLOUD/VCENTER_NAME"] +} + begin puts VCenterDriver::VCenterVm.deploy(File.read(dfile), lcm_state, deploy_id, host, - datastore) + datastore, + ops) rescue Exception => e STDERR.puts "Deploy of VM #{vm_id} on host #{host} with #{dfile} failed " + "due to \"#{e.message}\"" diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb index 0b3cf00ffa..bda8c40d6f 100644 --- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb +++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb @@ -144,6 +144,7 @@ class VIClient v.propSet.each{ |dynprop| obj[dynprop.name] = dynprop.val } + obj[:ref] = k._ref objects << OpenStruct.new(obj) end } @@ -301,6 +302,37 @@ class VIClient baseEntity end + ######################################################################## + # Searches the associated vmFolder of the DataCenter for the current + # connection. Returns a RbVmomi::VIM::VirtualMachine or nil if not found + # + # Searches by moref, name, uuid and then iterates over all VMs + # + # @param uuid [String] the UUID of the VM or VM Template + # @param ref [String] VMware moref + # @param name [String] VM name in vCenter + ######################################################################## + def find_vm_fast(uuid, ref = nil, name = nil) + if ref + # It can raise ManagedObjectNotFound + begin + vm = RbVmomi::VIM::VirtualMachine.new(@dc._connection, ref) + return vm if vm.config && vm.config.uuid == uuid + rescue => e + end + end + + if name + begin + vm = @dc.vmFolder.find(name) + return vm if vm.config && vm.config.uuid == uuid + rescue + end + end + + return find_vm_template(uuid) + end + ######################################################################## # Searches the associated vmFolder of the DataCenter for the current # connection. Returns a RbVmomi::VIM::VirtualMachine or nil if not found @@ -310,11 +342,7 @@ class VIClient version = @vim.serviceContent.about.version found_vm = nil - - if version.split(".").first.to_i >= 6 - found_vm = @dc.vmFolder.findByUuid(uuid, RbVmomi::VIM::VirtualMachine, @dc) - end - + found_vm = @dc.vmFolder.findByUuid(uuid, RbVmomi::VIM::VirtualMachine, @dc) return found_vm if found_vm vms = VIClient.get_entities(@dc.vmFolder, 'VirtualMachine') @@ -439,13 +467,15 @@ class VIClient ds = ds_cache[t.datastore[0].to_s] one_tmp << { - :name => "#{vi_tmp.vm.name} - #{host.cluster_name}", - :uuid => vi_tmp.vm.config.uuid, - :host => host.cluster_name, - :one => vi_tmp.to_one(host), - :ds => vi_tmp.to_one_ds(host, ds.name), - :default_ds => ds.name, - :rp => vi_tmp.to_one_rp(host) + :name => "#{vi_tmp.vm.name} - #{host.cluster_name}", + :uuid => vi_tmp.vm.config.uuid, + :host => host.cluster_name, + :one => vi_tmp.to_one(host), + :ds => vi_tmp.to_one_ds(host, ds.name), + :default_ds => ds.name, + :rp => vi_tmp.to_one_rp(host), + :vcenter_ref => vi_tmp.vm._ref, + :vcenter_name => vi_tmp.vm.name } end } @@ -1430,13 +1460,16 @@ class VCenterVm # Deploys a VM # @xml_text XML representation of the VM ############################################################################ - def self.deploy(xml_text, lcm_state, deploy_id, hostname, datastore = nil) + def self.deploy(xml_text, lcm_state, deploy_id, hostname, datastore = nil, + ops = {}) if lcm_state == "BOOT" || lcm_state == "BOOT_FAILURE" - return clone_vm(xml_text, hostname, datastore) + return clone_vm(xml_text, hostname, datastore, ops) else hid = VIClient::translate_hostname(hostname) connection = VIClient.new(hid) - vm = connection.find_vm_template(deploy_id) + vm = connection.find_vm_fast(deploy_id, + ops[:ref], + ops[:name]) xml = REXML::Document.new xml_text reconfigure_vm(vm, xml, false, hostname) @@ -1882,6 +1915,8 @@ class VCenterVm "PUBLIC_CLOUD = [\n"\ " TYPE =\"vcenter\",\n"\ " VM_TEMPLATE =\"#{@vm.config.uuid}\",\n"\ + " VCENTER_REF =\"#{@vm.ref}\",\n"\ + " VCENTER_NAME=\"#{@vm.name}\",\n"\ " HOST =\"#{cluster_name}\"\n"\ "]\n"\ "GRAPHICS = [\n"\ @@ -2145,7 +2180,7 @@ private ######################################################################## # Clone a vCenter VM Template and leaves it powered on ######################################################################## - def self.clone_vm(xml_text, hostname, datastore) + def self.clone_vm(xml_text, hostname, datastore, ops = {}) host_id = VCenterDriver::VIClient.translate_hostname(hostname) @@ -2207,7 +2242,7 @@ private raise "Cannot find host id in deployment file history." if hid.nil? connection = VIClient.new(hid) - vc_template = connection.find_vm_template(uuid) + vc_template = connection.find_vm_fast(uuid, ops[:ref], ops[:name]) # Find out requested and available resource pool