From 396cd704cd50339945c1ec91beead35c837955ad Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Wed, 15 Feb 2017 00:38:38 +0100 Subject: [PATCH] F #4913: Refactor deploy --- .../lib/vcenter_driver/virtual_machine.rb | 20 ++++---- src/vmm_mad/remotes/vcenter/deploy | 47 +++++++++---------- 2 files changed, 34 insertions(+), 33 deletions(-) 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 156854e3d1..c7f97c0dc8 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb @@ -54,7 +54,10 @@ class VirtualMachine ############################################################################ # Attributes that must be defined when the VM does not exist in vCenter - attr_writer :vi_client + attr_accessor :vi_client + + # these have their own getter (if they aren't set, we can set them + # dynamically) attr_writer :one_item attr_writer :host attr_writer :target_ds_ref @@ -258,14 +261,15 @@ class VirtualMachine # Crate and reconfigure VM related methods ############################################################################ - # When creating a new VM these instance variables must be set beforehand - # @vi_client - # @one_item - # @host - # # This function creates a new VM from the @one_item XML and returns the # VMware ref - def clone_vm + # @param one_item OpenNebula::VirtualMachine + # @param vi_client VCenterDriver::VIClient + # @return String vmware ref + def clone_vm(one_item, vi_client) + @one_item = one_item + @vi_client = vi_client + vcenter_name = get_vcenter_name vc_template_ref = one_item['USER_TEMPLATE/VCENTER_TEMPLATE_REF'] @@ -302,8 +306,8 @@ class VirtualMachine end # @item is populated - @item = vm + return self['_ref'] end diff --git a/src/vmm_mad/remotes/vcenter/deploy b/src/vmm_mad/remotes/vcenter/deploy index f691d5bb11..cdee076d1e 100755 --- a/src/vmm_mad/remotes/vcenter/deploy +++ b/src/vmm_mad/remotes/vcenter/deploy @@ -29,42 +29,39 @@ $: << File.dirname(__FILE__) require 'vcenter_driver2' -dfile = ARGV[0] -cluster_name = ARGV[1] -vm_id = ARGV[2] +dfile = ARGV[0] +cluster_name = ARGV[1] +vm_id = ARGV[2] drv_action = OpenNebula::XMLElement.new drv_action.initialize_xml(Base64.decode64(STDIN.read), 'VM') -host_id = drv_action["/VM/HISTORY_RECORDS/HISTORY/HID"] - -# lcm_state_num = drv_action["/VM/LCM_STATE"].to_i -# lcm_state = OpenNebula::VirtualMachine::LCM_STATE[lcm_state_num] - -host = OpenNebula::Host.new_with_id(host_id, OpenNebula::Client.new) -rc = host.info -if OpenNebula::is_error?(rc) - STDERR.puts rc.message - exit -1 -end +deploy_id = drv_action["DEPLOY_ID"] +host_id = drv_action["HISTORY_RECORDS/HISTORY/HID"] vi_client = VCenterDriver::VIClient.new_from_host(host_id) -target_ds_id = drv_action['HISTORY_RECORDS/HISTORY[last()]/DS_ID'] -target_ds_one = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, target_ds_id) -target_ds_ref = target_ds_one['TEMPLATE/VCENTER_DS_REF'] - begin - # Clone from template - vm = VCenterDriver::VirtualMachine.new + if deploy_id && !deploy_id.empty? + # VM is not new, we just need to reconfigure it and to power it on + vm = VCenterDriver::VirtualMachine.new_from_ref(deploy_id, vi_client) - vm.vi_client = vi_client - vm.one_item = drv_action - vm.host = host - vm.target_ds_ref = target_ds_ref + # Setting one_item is optional, but it saves a couple of API calls if + # we already have it + vm.one_item = drv_action + else + # VM is new + vm = VCenterDriver::VirtualMachine.new - puts vm.clone_vm + # Clone from template + vm.clone_vm(drv_action, vi_client) + end + vm.reconfigure + vm.poweron + vm.set_running(true) + + puts vm['_ref'] rescue Exception => e STDERR.puts "Deploy of VM #{vm_id} on vCenter cluster #{cluster_name} " + "with #{dfile} failed due to \"#{e.message}\""