From ebb242a6ef7f0946b49eee0e340501a22f5196ed Mon Sep 17 00:00:00 2001 From: "Carlos J. Herrera" Date: Thu, 23 Jun 2022 05:11:43 -0500 Subject: [PATCH] F #5834: fix port asignation when import wilds (#2164) --- src/cli/one_helper/onevcenter_helper.rb | 55 ------------------- src/cli/onevcenter | 39 ++++++++++++- .../lib/vcenter_driver/virtual_machine.rb | 28 ++++++++++ .../lib/vcenter_driver/vmm_importer.rb | 33 +++++++++++ 4 files changed, 98 insertions(+), 57 deletions(-) diff --git a/src/cli/one_helper/onevcenter_helper.rb b/src/cli/one_helper/onevcenter_helper.rb index c2d15a6e77..232c94c035 100644 --- a/src/cli/one_helper/onevcenter_helper.rb +++ b/src/cli/one_helper/onevcenter_helper.rb @@ -520,59 +520,4 @@ class OneVcenterHelper < OpenNebulaHelper::OneHelper opts end - def clear_tags(vmid) - client = Client.new - - vm_pool = VirtualMachinePool.new(client, -1) - host_pool = HostPool.new(client) - deploy_id = -1 - host_id = -1 - hostname = '' - - rc = vm_pool.info - raise rc.message if OpenNebula.is_error?(rc) - - rc = host_pool.info - raise rc.message if OpenNebula.is_error?(rc) - - vm_pool.each do |vm| - next if vm.id.to_s != vmid - - deploy_id = vm.deploy_id - vm_history = vm.to_hash['VM']['HISTORY_RECORDS']['HISTORY'] - hostname = vm_history['HOSTNAME'] - break - end - - host_pool.each do |host| - if host.name == hostname - host_id = host.id - end - end - - vi_client = VCenterDriver::VIClient.new_from_host(host_id) - vm = VCenterDriver::VirtualMachine - .new(vi_client, deploy_id, vmid) - - keys_to_remove = [] - vm['config.extraConfig'].each do |extraconfig| - next unless extraconfig.key.include?('opennebula.disk') || - extraconfig.key.include?('opennebula.vm') || - extraconfig.key.downcase.include?('remotedisplay') - - keys_to_remove << extraconfig.key - end - - [vm, keys_to_remove] - end - - def remove_keys(vm, keys_to_remove) - spec_hash = keys_to_remove.map {|key| { :key => key, :value => '' } } - - spec = RbVmomi::VIM.VirtualMachineConfigSpec( - :extraConfig => spec_hash - ) - vm.item.ReconfigVM_Task(:spec => spec).wait_for_completion - end - end diff --git a/src/cli/onevcenter b/src/cli/onevcenter index 8cb5dbc8e4..83f149f459 100755 --- a/src/cli/onevcenter +++ b/src/cli/onevcenter @@ -409,7 +409,42 @@ CommandParser::CmdParser.new(ARGV) do begin print '.' - vm, keys = helper.clear_tags(vmid) + + client = Client.new + + vm_pool = VirtualMachinePool.new(client, -1) + host_pool = HostPool.new(client) + deploy_id = -1 + host_id = -1 + hostname = '' + + rc = vm_pool.info + raise rc.message if OpenNebula.is_error?(rc) + + rc = host_pool.info + raise rc.message if OpenNebula.is_error?(rc) + + vm_pool.each do |vm| + next if vm.id.to_s != vmid + + deploy_id = vm.deploy_id + vm_history = vm.to_hash['VM']['HISTORY_RECORDS']['HISTORY'] + hostname = vm_history['HOSTNAME'] + break + end + + host_pool.each do |host| + if host.name == hostname + host_id = host.id + end + end + + vi_client = VCenterDriver::VIClient.new_from_host(host_id) + vm = VCenterDriver::VirtualMachine + .new(vi_client, deploy_id, vmid) + + keys = vm.extra_config_keys + print '.' if keys.empty? @@ -422,7 +457,7 @@ CommandParser::CmdParser.new(ARGV) do puts 'The following keys will be removed:' keys.each {|key| puts "\t- #{key}" } - helper.remove_keys(vm, keys) + vm.clear_tags rescue StandardError => e STDERR.puts "Couldn't clear VM tags. Reason: #{e.message}" exit 1 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 440da790d7..a34e62bebc 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb @@ -1695,6 +1695,34 @@ end info_nics end + # Clear extraconfig tags from a vCenter VM + # + def clear_tags + keys_to_remove = extra_config_keys + + spec_hash = + keys_to_remove.map {|key| { :key => key, :value => '' } } + + spec = RbVmomi::VIM.VirtualMachineConfigSpec( + :extraConfig => spec_hash + ) + @item.ReconfigVM_Task(:spec => spec).wait_for_completion + end + + # Get extraconfig tags from a vCenter VM + # + def extra_config_keys + keys_to_remove = [] + @item['config.extraConfig'].each do |extraconfig| + next unless extraconfig.key.include?('opennebula.disk') || + extraconfig.key.include?('opennebula.vm') || + extraconfig.key.downcase.include?('remotedisplay') + + keys_to_remove << extraconfig.key + end + keys_to_remove + end + # Get required parameters to use VMware HTML Console SDK # To be used with the following SDK: # https://code.vmware.com/web/sdk/2.1.0/html-console diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/vmm_importer.rb b/src/vmm_mad/remotes/lib/vcenter_driver/vmm_importer.rb index 78f567f575..13c520e677 100644 --- a/src/vmm_mad/remotes/lib/vcenter_driver/vmm_importer.rb +++ b/src/vmm_mad/remotes/lib/vcenter_driver/vmm_importer.rb @@ -47,6 +47,36 @@ module VCenterDriver vm = selected[:one_item] || build template = selected[:template] || import_tmplt template = "DEPLOY_ID = #{vm_ref}\n" + template + + # Index where start GRAPHICS block + graphics_index = template.index('GRAPHICS = [') + unless graphics_index.nil? + # Index where finish GRAPHICS block + end_of_graphics = template[graphics_index..-1].index(']') + 1 + + # GRAPHICS block + graphics_sub_string = + template[graphics_index, end_of_graphics] + + # GRAPHICS block with PORT removed + # OpenNebula will asing a new not used PORT + graphics_sub_string = + graphics_sub_string.gsub(/PORT(.*?),[\r\n]/, '') + + # Index where graphics block finish + before_graphics = template[0, graphics_index] + + # Block after graphics block + after_graphics = + template[graphics_index..-1][end_of_graphics..-1] + + # Template with out PORT inside GRAPHICS + template = + before_graphics + + graphics_sub_string + + after_graphics + end + host_id = selected[:host] || @list.keys[0] vc_uuid = @vi_client.vim.serviceContent.about.instanceUuid @@ -56,6 +86,9 @@ module VCenterDriver vc_vm = VCenterDriver::VirtualMachine.new_without_id(@vi_client, vm_ref) + # clear OpenNebula attributes + vc_vm.clear_tags + # Importing Wild VMs with snapshots is not supported # https://github.com/OpenNebula/one/issues/1268 if vc_vm.snapshots? && vc_vm.disk_keys.empty?