From 1c61d158a037a6f88a8a03a03f4e76f652f1fd12 Mon Sep 17 00:00:00 2001 From: mcabrerizo Date: Tue, 8 Nov 2016 11:47:05 +0100 Subject: [PATCH 1/3] Test fix --- src/vmm_mad/remotes/vcenter/vcenter_driver.rb | 69 +++++++++++++++++-- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb index d21d3d9a7e..857ea881bb 100644 --- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb +++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb @@ -1787,12 +1787,38 @@ class VCenterVm vm = connection.find_vm_template(deploy_id) - spec_hash = calculate_addnic_spec(vm, mac, bridge, model) + spec_nics = calculate_addnic_spec(vm, mac, bridge, model) - spec = RbVmomi::VIM.VirtualMachineConfigSpec({:deviceChange => - [spec_hash]}) + spec_hash = {:deviceChange => [spec_nics]} + + + #B4897 track hot plugged nics + hotplugged_nics = vm.config.extraConfig.select do |val| + val[:key] == "opennebula.hotplugged_nics" + end + + if hotplugged_nics + hotplugged_nics << mac + else + hotplugged_nics = [] + hotplugged_nics << mac + end + + config_array = [{:key=>"opennebula.hotplugged_nics", + :value=>hotplugged_nics}] + extra_config_spec = {:extraConfig =>config_array} + + spec_hash.merge!(extra_config_spec) + + spec = RbVmomi::VIM.VirtualMachineConfigSpec(spec_hash) vm.ReconfigVM_Task(:spec => spec).wait_for_completion + + config_array << { + :key => 'opennebula.hotplugged_nics', + :value => hotplugged_nics + } + end ############################################################################ @@ -1810,11 +1836,24 @@ class VCenterVm raise "Could not find NIC with mac address #{mac}" if nic.nil? + #B4897 track hot plugged nics + hotplugged_nics = vm.config.extraConfig.select do |val| + val[:key] == "opennebula.hotplugged_nics" + end + + config_array = [] + if hotplugged_nics + hotplugged_nics.delete(mac) + config_array = [{:key=>"opennebula.hotplugged_nics", + :value=>hotplugged_nics}] + end + spec = { :deviceChange => [ :operation => :remove, :device => nic - ] + ], + :extraConfig => config_array } vm.ReconfigVM_Task(:spec => spec).wait_for_completion @@ -2582,10 +2621,22 @@ private # NIC section, build the reconfig hash nics = xml.root.get_elements("/VM/TEMPLATE/NIC") - nic_spec = {} # If the VM is not new, avoid readding NiCs if !newvm + nic_array = [] + + # Get MACs from NICs inside VM template + one_mac_addresses = Array.new + nics.each{|nic| + one_mac_addresses << nic.elements["MAC"].text + } + + # Get hotplugged_nics that may haven't been removed from vCenter + hotplugged_nics = vm.config.extraConfig.select do |val| + val[:key] == "opennebula.hotplugged_nics" + end + vm.config.hardware.device.each{ |dv| if is_nic?(dv) nics.each{|nic| @@ -2594,8 +2645,16 @@ private nics.delete(nic) end } + + # B4897 - Remove detached NICs from vCenter that were unplugged in POWEROFF + if !one_mac_addresses.include?(dv.macAddress) and + hotplugged_nics.include?(dv.macAddress) + nic_array << { :operation => :remove, :device => dv} + end end } + + device_change += nic_array end if !nics.nil? From bf58b09e4b07ca7998d3e88d822abb58ea99bf06 Mon Sep 17 00:00:00 2001 From: mcabrerizo Date: Wed, 9 Nov 2016 20:12:08 +0100 Subject: [PATCH 2/3] Fix B4897 --- src/vmm_mad/remotes/vcenter/vcenter_driver.rb | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb index 857ea881bb..a9dd90a43e 100644 --- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb +++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb @@ -1797,11 +1797,12 @@ class VCenterVm val[:key] == "opennebula.hotplugged_nics" end - if hotplugged_nics - hotplugged_nics << mac + if hotplugged_nics && !hotplugged_nics.empty? + hotplugged_nics = hotplugged_nics[0][:value].to_s + hotplugged_nics << mac.to_s << ";" if !hotplugged_nics.include?(mac) else - hotplugged_nics = [] - hotplugged_nics << mac + hotplugged_nics = "" + hotplugged_nics << mac.to_s << ";" end config_array = [{:key=>"opennebula.hotplugged_nics", @@ -1842,8 +1843,9 @@ class VCenterVm end config_array = [] - if hotplugged_nics - hotplugged_nics.delete(mac) + if hotplugged_nics && !hotplugged_nics.empty? + hotplugged_nics = hotplugged_nics[0][:value].to_s + hotplugged_nics.slice!(mac + ";") # remove hotplugged nic config_array = [{:key=>"opennebula.hotplugged_nics", :value=>hotplugged_nics}] end @@ -2612,10 +2614,6 @@ private :value=>context_text}] end - if config_array != [] - context_vnc_spec = {:extraConfig =>config_array} - end - device_change = [] # NIC section, build the reconfig hash @@ -2632,24 +2630,32 @@ private one_mac_addresses << nic.elements["MAC"].text } - # Get hotplugged_nics that may haven't been removed from vCenter - hotplugged_nics = vm.config.extraConfig.select do |val| + # B4897 - Get mac of NICs that were hot-plugged from vCenter extraConfig + hotplugged_nics = [] + extraconfig_nics = vm.config.extraConfig.select do |val| val[:key] == "opennebula.hotplugged_nics" end + if extraconfig_nics && !extraconfig_nics.empty? + hotplugged_nics = extraconfig_nics[0][:value].to_s.split(";") + end + vm.config.hardware.device.each{ |dv| if is_nic?(dv) nics.each{|nic| - if nic.elements["MAC"].text == dv.macAddress and - nic.elements["BRIDGE"].text == dv.deviceInfo.summary + if nic.elements["MAC"].text == dv.macAddress nics.delete(nic) end } # B4897 - Remove detached NICs from vCenter that were unplugged in POWEROFF - if !one_mac_addresses.include?(dv.macAddress) and - hotplugged_nics.include?(dv.macAddress) + if !one_mac_addresses.include?(dv.macAddress) && hotplugged_nics.include?(dv.macAddress) nic_array << { :operation => :remove, :device => dv} + hotplugged_nics.delete(dv.macAddress) + config_array << { + :key => 'opennebula.hotplugged_nics', + :value => hotplugged_nics.join(";") + } end end } @@ -2728,6 +2734,10 @@ private :memoryMB => memory } # Perform the VM reconfiguration + if config_array != [] + context_vnc_spec = {:extraConfig =>config_array} + end + spec_hash = context_vnc_spec.merge(capacity_spec) if device_change.length > 0 spec_hash.merge!({ :deviceChange => device_change }) From 3cbd3e796403768b199e4a15655bba49ed786054 Mon Sep 17 00:00:00 2001 From: mcabrerizo Date: Wed, 9 Nov 2016 20:17:52 +0100 Subject: [PATCH 3/3] Fix B4897. Clean up --- src/vmm_mad/remotes/vcenter/vcenter_driver.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb index a9dd90a43e..c2f766cd69 100644 --- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb +++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb @@ -1815,11 +1815,6 @@ class VCenterVm vm.ReconfigVM_Task(:spec => spec).wait_for_completion - config_array << { - :key => 'opennebula.hotplugged_nics', - :value => hotplugged_nics - } - end ############################################################################