F #4913: Fix wild vm import. Add template moref and opennebula_managed to networks created

This commit is contained in:
mcabrerizo committed 2017-05-12 09:28:18 +02:00
1 parent e7369f72db
commit 08cf1957d8
5 files changed
+89 -47

No files matched your search

+6 -1
View File
@@ -219,8 +219,13 @@ get '/vcenter/template/:vcenter_ref' do
t[:one] << template_disks
template_moref = template_copy_ref ? template_copy_ref : ref
# Create images or get nics information for template
error, template_nics = template.import_vcenter_nics(vc_uuid, npool, vcenter_client.vim.host)
error, template_nics = template.import_vcenter_nics(vc_uuid,
npool,
vcenter_client.vim.host,
template_moref)
if !error.empty?
append = false
@@ -7,7 +7,7 @@ def self.import_wild(host_id, vm_ref, one_vm, template)
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vc_uuid = vi_client.vim.serviceContent.about.instanceUuid
dc_name = vi_client.vim.host
vc_name = vi_client.vim.host
dpool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool)
ipool = VCenterDriver::VIHelper.one_pool(OpenNebula::ImagePool)
@@ -16,36 +16,48 @@ def self.import_wild(host_id, vm_ref, one_vm, template)
vcenter_vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
error, template_disks = vcenter_vm.import_vcenter_disks(vc_uuid, dpool, ipool)
return OpenNebula::Error.new(error) if !error.empty?
template << template_disks
# Create images or get nics information for template
error, template_nics = vcenter_vm.import_vcenter_nics(vc_uuid, npool, dc_name)
error, template_nics = vcenter_vm.import_vcenter_nics(vc_uuid,
npool,
vm_ref,
vc_name)
return OpenNebula::Error.new(error) if !error.empty?
template << template_nics
rc = one_vm.allocate(template)
return rc if OpenNebula.is_error?(rc)
one_vm.deploy(host_id, false)
# Set reference to template disks and nics in VM template
vcenter_vm.one_item = one_vm
vcenter_vm.reference_imported_disks(vm_ref)
vcenter_vm.reference_imported_nics
vcenter_vm.reference_unmanaged_devices(vm_ref)
# Set vnc configuration F#5074
one_vm.info # Let's update the info to gather VNC info
extraconfig = []
extraconfig += vcenter_vm.extraconfig_vnc
spec_hash = { :extraConfig => extraconfig }
spec = RbVmomi::VIM.VirtualMachineConfigSpec(spec_hash)
vcenter_vm.item.ReconfigVM_Task(:spec => spec).wait_for_completion
vnc_port = one_vm["TEMPLATE/GRAPHICS/PORT"]
elapsed_seconds = 0
# Let's update the info to gather VNC port
until vnc_port || elapsed_seconds > 30
sleep(2)
one_vm.info
vnc_port = one_vm["TEMPLATE/GRAPHICS/PORT"]
elapsed_seconds += 2
end
if vnc_port
vcenter_vm.one_item = one_vm
extraconfig = []
extraconfig += vcenter_vm.extraconfig_vnc
spec_hash = { :extraConfig => extraconfig }
spec = RbVmomi::VIM.VirtualMachineConfigSpec(spec_hash)
vcenter_vm.item.ReconfigVM_Task(:spec => spec).wait_for_completion
end
return one_vm.id
@@ -322,10 +334,13 @@ def self.import_templates(con_ops, options)
next
end
template_moref = template_copy_ref ? template_copy_ref : t[:vcenter_ref]
error, template_nics = template.import_vcenter_nics(vc_uuid,
npool,
options[:vcenter],
dc)
npool,
options[:vcenter],
template_moref,
dc)
if error.empty?
t[:one] << template_nics
else
@@ -70,22 +70,28 @@ class Network
def self.to_one_template(network_name, network_ref, network_type,
ccr_ref, ccr_name, vcenter_uuid,
vcenter_instance_name, dc_name)
vcenter_instance_name, dc_name,
unmanaged=false, template_ref=nil)
one_tmp = {}
network_import_name = "[#{vcenter_instance_name} - #{dc_name}] #{network_name} - #{ccr_name.tr(" ", "_")}"
if unmanaged
network_import_name = "#{network_name} [#{network_ref} - #{template_ref} - #{vcenter_uuid}]"
else
network_import_name = "[#{vcenter_instance_name} - #{dc_name}] #{network_name} - #{ccr_name.tr(" ", "_")}"
end
one_tmp[:name] = network_import_name
one_tmp[:bridge] = network_name
one_tmp[:type] = network_type
one_tmp[:cluster] = ccr_name
one_tmp[:vcenter_ccr_ref] = ccr_ref
one_tmp[:one] = to_one(network_import_name, network_name, network_ref, network_type,
ccr_ref, vcenter_uuid)
ccr_ref, vcenter_uuid, unmanaged, template_ref)
return one_tmp
end
def self.to_one(network_import_name, network_name, network_ref, network_type,
ccr_ref, vcenter_uuid)
ccr_ref, vcenter_uuid, unmanaged, template_ref)
template = "NAME=\"#{network_import_name}\"\n"\
"BRIDGE=\"#{network_name}\"\n"\
"VN_MAD=\"dummy\"\n"\
@@ -94,6 +100,10 @@ class Network
"VCENTER_CCR_REF=\"#{ccr_ref}\"\n"\
"VCENTER_INSTANCE_ID=\"#{vcenter_uuid}\"\n"
template += "OPENNEBULA_MANAGED=\"NO\"\n" if unmanaged
template += "VCENTER_TEMPLATE_REF=\"#{template_ref}\"\n" if template_ref
return template
end
@@ -105,12 +115,14 @@ class Network
end
end
def self.get_one_vnet_ds_by_ref_and_ccr(ref, ccr_ref, vcenter_uuid, pool = nil)
def self.get_unmanaged_vnet_by_ref(ref, ccr_ref, template_ref, vcenter_uuid, pool = nil)
pool = VCenterDriver::VIHelper.one_pool(OpenNebula::VirtualNetworkPool, false) if pool.nil?
element = pool.select do |e|
e["TEMPLATE/VCENTER_NET_REF"] == ref &&
e["TEMPLATE/VCENTER_CCR_REF"] == ccr_ref &&
e["TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_uuid
e["TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_uuid &&
e["TEMPLATE/VCENTER_TEMPLATE_REF"] == template_ref &&
e["TEMPLATE/OPENNEBULA_MANAGED"] == "NO"
end.first rescue nil
return element
@@ -51,6 +51,7 @@ class VIHelper
pool = one_pool(the_class, false) if pool.nil?
element = pool.select{|e|
e["#{attribute}"] == ref &&
(!e["TEMPLATE/OPENNEBULA_MANAGED"] || e["TEMPLATE/OPENNEBULA_MANAGED"] != "NO") &&
(e["TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_uuid ||
e["USER_TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_uuid)}.first rescue nil
@@ -296,7 +296,8 @@ class Template
return error, disk_info
end
def import_vcenter_nics(vc_uuid, npool, vcenter_instance_name, dc_name=nil)
def import_vcenter_nics(vc_uuid, npool, vcenter_instance_name,
template_ref, dc_name=nil)
nic_info = ""
error = ""
@@ -318,11 +319,11 @@ class Template
allocated_networks = []
vc_nics.each do |nic|
network_found = VCenterDriver::Network.get_one_vnet_ds_by_ref_and_ccr(nic[:net_ref],
ccr_ref,
vc_uuid,
npool)
network_found = VCenterDriver::Network.get_unmanaged_vnet_by_ref(nic[:net_ref],
ccr_ref,
template_ref,
vc_uuid,
npool)
#Network is already in the datastore
if network_found
# This is the existing nic info
@@ -336,14 +337,18 @@ class Template
allocated_networks << one_vn
unmanaged = true
one_vnet = VCenterDriver::Network.to_one_template(nic[:net_name],
nic[:net_ref],
nic[:pg_type],
ccr_ref,
ccr_name,
vc_uuid,
vcenter_instance_name,
dc_name)
nic[:net_ref],
nic[:pg_type],
ccr_ref,
ccr_name,
vc_uuid,
vcenter_instance_name,
dc_name,
unmanaged,
template_ref)
# By default add an ethernet range to network size 255
ar_str = ""
@@ -520,7 +525,8 @@ class Template
" NETWORK = \"YES\",\n"\
" SSH_PUBLIC_KEY = \"$USER[SSH_PUBLIC_KEY]\"\n"\
"]\n"\
"VCENTER_INSTANCE_ID =\"#{@vm_info[:vc_uuid]}\"\n"
"VCENTER_INSTANCE_ID =\"#{@vm_info[:vc_uuid]}\"\n"\
"VCENTER_CCR_REF =\"#{@vm_info[:cluster_ref]}\"\n"
str << "IMPORT_VM_ID =\"#{self["_ref"]}\"\n"
str << "IMPORT_STATE =\"#{@state}\"\n"
@@ -1448,7 +1454,6 @@ class VirtualMachine < Template
end
def extraconfig_context
# TODO: migrator to 5.4 (create token.sh)
context_text = "# Context variables generated by OpenNebula\n"
one_item.each('TEMPLATE/CONTEXT/*') do |context_element|
# next if !context_element.text
@@ -1472,18 +1477,22 @@ class VirtualMachine < Template
end
def extraconfig_vnc
vnc_port = one_item["TEMPLATE/GRAPHICS/PORT"]
vnc_listen = one_item["TEMPLATE/GRAPHICS/LISTEN"] || "0.0.0.0"
vnc_keymap = one_item["TEMPLATE/GRAPHICS/KEYMAP"]
if one_item["TEMPLATE/GRAPHICS"]
vnc_port = one_item["TEMPLATE/GRAPHICS/PORT"]
vnc_listen = one_item["TEMPLATE/GRAPHICS/LISTEN"] || "0.0.0.0"
vnc_keymap = one_item["TEMPLATE/GRAPHICS/KEYMAP"]
conf = [ {:key => "remotedisplay.vnc.enabled",:value => "TRUE"},
{:key => "remotedisplay.vnc.port", :value => vnc_port},
{:key => "remotedisplay.vnc.ip", :value => vnc_listen}]
conf = [ {:key => "remotedisplay.vnc.enabled",:value => "TRUE"},
{:key => "remotedisplay.vnc.port", :value => vnc_port},
{:key => "remotedisplay.vnc.ip", :value => vnc_listen}]
conf += [{:key => "remotedisplay.vnc.keymap",
:value => vnc_keymap}] if vnc_keymap
conf += [{:key => "remotedisplay.vnc.keymap",
:value => vnc_keymap}] if vnc_keymap
conf
conf
else
conf = []
end
end
def device_change_nics