From 542e5cf8ea4aa0f1a130066a61dca04adf5594d7 Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Fri, 2 Oct 2015 18:32:45 +0200 Subject: [PATCH] Feature #4021: Allow for multi cluster templates (cherry picked from commit 2fce703f8b5ae489f84eb78b3c31ade847b2fba1) --- src/vmm_mad/remotes/vcenter/vcenter_driver.rb | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb index c64f6a5351..4b0b88cd00 100644 --- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb +++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb @@ -789,7 +789,7 @@ class VCenterVm ############################################################################ def self.deploy(xml_text, lcm_state, deploy_id, hostname) if lcm_state == "BOOT" || lcm_state == "BOOT_FAILURE" - return clone_vm(xml_text) + return clone_vm(xml_text, hostname) else hid = VIClient::translate_hostname(hostname) connection = VIClient.new(hid) @@ -1341,18 +1341,36 @@ private ######################################################################## # Clone a vCenter VM Template and leaves it powered on ######################################################################## - def self.clone_vm(xml_text) + def self.clone_vm(xml_text, hostname) xml = REXML::Document.new xml_text pcs = xml.root.get_elements("//USER_TEMPLATE/PUBLIC_CLOUD") raise "Cannot find VCenter element in VM template." if pcs.nil? - template = pcs.find { |t| + template = pcs.select { |t| type = t.elements["TYPE"] !type.nil? && type.text.downcase == "vcenter" } + # If there are multiple vcenter templates, find the right one + if template.is_a? Array + all_vcenter_templates = template.clone + # If there is more than one coincidence, pick the first one + template = template.select {|t| + cluster_name = t.elements["CLUSTER"] + !cluster_name.nil? && cluster_name.text == hostname + }[0] + # The template may not reference any specific CLUSTER + # Therefore, here take the first one that does not + # specify a CLUSTER to see if we are lucky + if template.nil? + template = all_vcenter_templates.select {|t| + t.elements["CLUSTER"].nil? + }[0] + end + end + raise "Cannot find vCenter element in VM template." if template.nil? uuid = template.elements["VM_TEMPLATE"]