1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

Squashed commit of the following:

commit 0823e4c8025f5e2861bb6141170e94081a9a74a7
Author: semedi <sergio.semedi@gmail.com>
Date:   Mon Dec 18 12:01:57 2017 +0100

    F #1538: vcenter networks generate name method

commit a0071dafff8621fd017a6e4367f7147c77a29127
Author: semedi <sergio.semedi@gmail.com>
Date:   Wed Dec 13 12:35:30 2017 +0100

    F #1538: delete ccr_ref from net, changed template

commit 4f6632049970a1148768dcef90ab3cd710492555
Author: semedi <sergio.semedi@gmail.com>
Date:   Wed Dec 13 11:21:01 2017 +0100

    F #1538: standard port groups are UNIQUE

commit 5a8f04da2b6481e0ad52e54f7edb791f8e3811d8
Author: semedi <sergio.semedi@gmail.com>
Date:   Tue Dec 12 12:04:23 2017 +0100

    F #1538: to_one_template restructure

commit deb65599df584f3d59216b10c7ee91a98c4ccc3d
Author: semedi <sergio.semedi@gmail.com>
Date:   Mon Dec 11 14:22:52 2017 +0100

    F #1538: Importer now add the vnet to selected clusters

commit c61ceb170a63dcc124a44b240ae59ef630d74d5e
Author: semedi <sergio.semedi@gmail.com>
Date:   Mon Dec 11 11:58:30 2017 +0100

    F #1538: onevcenter import multicluster support

commit bb4f500b4942e184ec20db5befea368cf40da028
Author: semedi <sergio.semedi@gmail.com>
Date:   Mon Dec 11 09:06:22 2017 +0100

    F #1538: new method to convert to template

commit b1d16f9ae9ee5d493910ade6833b373b025e305b
Author: semedi <sergio.semedi@gmail.com>
Date:   Tue Dec 5 09:55:38 2017 +0100

    F #1538: get_location method && restructure

commit 7a9c753c2631775d1473401ee1de93309682fc01
Author: semedi <sergio.semedi@gmail.com>
Date:   Tue Dec 5 09:26:49 2017 +0100

    F #1538: storing clusters data

commit ccb0a7bccafd4094cb456b64485f0fbb2fb4c159
Author: semedi <sergio.semedi@gmail.com>
Date:   Mon Dec 4 12:45:49 2017 +0100

    F #1538: importing vcenter networks rework
This commit is contained in:
Tino Vazquez 2017-12-18 13:05:54 +01:00
parent 2a54b02f45
commit de86512791
4 changed files with 130 additions and 86 deletions

View File

@ -122,7 +122,6 @@ class DatacenterFolder
def get_unimported_datastores(dpool, vcenter_instance_name, hpool)
ds_objects = {}
vcenter_uuid = get_vcenter_instance_uuid
fetch! if @items.empty? #Get datacenters
@ -319,7 +318,6 @@ class DatacenterFolder
network_objects = {}
vcenter_uuid = get_vcenter_instance_uuid
pc = @vi_client.vim.serviceContent.propertyCollector
#Get all port groups and distributed port groups in vcenter instance
@ -347,21 +345,26 @@ class DatacenterFolder
{ :type => 'DistributedVirtualPortgroup', :pathSet => ['name'] }
]
)
result = pc.RetrieveProperties(:specSet => [filterSpec])
networks = {}
result.each do |r|
networks[r.obj._ref] = r.to_hash if r.obj.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) || r.obj.is_a?(RbVmomi::VIM::Network)
networks[r.obj._ref][:network_type] = r.obj.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) ? "Distributed Port Group" : "Port Group"
end
#Multicluster nets support
networks[r.obj._ref][:clusters] = {}
networks[r.obj._ref][:clusters][:refs] = []
networks[r.obj._ref][:clusters][:one_ids] = []
networks[r.obj._ref][:clusters][:names] = []
end
view.DestroyView # Destroy the view
fetch! if @items.empty? #Get datacenters
#Get datacenters
fetch! if @items.empty?
#Iterate over datacenters
@items.values.each do |dc|
dc_name = dc.item.name
network_objects[dc_name] = []
@ -392,19 +395,19 @@ class DatacenterFolder
result = pc.RetrieveProperties(:specSet => [filterSpec])
clusters = {}
result.each do |r|
result.each do |r|
clusters[r.obj._ref] = r.to_hash if r.obj.is_a?(RbVmomi::VIM::ClusterComputeResource)
end
view.DestroyView # Destroy the view
clusters.each do |ref, info|
one_host = VCenterDriver::VIHelper.find_by_ref(OpenNebula::HostPool,
"TEMPLATE/VCENTER_CCR_REF",
ref,
vcenter_uuid,
hpool)
if !one_host || !one_host['CLUSTER_ID']
cluster_id = -1
else
@ -412,59 +415,51 @@ class DatacenterFolder
end
one_cluster = VCenterDriver::ClusterComputeResource.new_from_ref(ref, @vi_client)
# Determine a host location
item = one_cluster.item
folders = []
while !item.instance_of? RbVmomi::VIM::Datacenter
item = item.parent
if !item.instance_of? RbVmomi::VIM::Datacenter
folders << item.name if item.name != "host"
end
if item.nil?
raise "Could not find the host's location"
end
end
location = folders.reverse.join("/")
location = "/" if location.empty?
location = VCenterDriver::VIHelper.get_location(one_cluster.item)
network_obj = info['network']
cname = info['name']
network_obj.each do |n|
network_ref = n._ref
network_name = networks[network_ref]['name']
network_type = networks[network_ref][:network_type]
one_network = VCenterDriver::VIHelper.find_by_ref(OpenNebula::VirtualNetworkPool,
# network can belong to more than 1 cluster
networks[network_ref][:clusters][:refs] << ref
networks[network_ref][:clusters][:one_ids] << cluster_id
networks[network_ref][:clusters][:names] << cname
next if networks[network_ref][:clusters][:refs].size > 1
# Is the first and first time net:
networks[network_ref][:one_net] = VCenterDriver::VIHelper.find_by_ref(OpenNebula::VirtualNetworkPool,
"TEMPLATE/VCENTER_NET_REF",
network_ref,
vcenter_uuid,
npool,
false)
next if one_network #If the network has been already imported
end #networks loop
end #clusters loop
one_vnet = VCenterDriver::Network.to_one_template(network_name,
network_ref,
network_type,
ref,
info['name'],
vcenter_uuid,
vcenter_instance_name,
dc_name,
cluster_id,
location)
#general net_info related to datacenter
opts = {}
opts[:vcenter_uuid] = vcenter_uuid
opts[:vcenter_instance_name] = vcenter_instance_name
opts[:dc_name] = dc_name
networks.each do |nref, net_info|
next if net_info[:one_net] || net_info[:clusters][:refs].size < 1
network_objects[dc_name] << one_vnet
end
opts[:clusters] = net_info[:clusters]
opts[:network_name] = net_info['name']
opts[:network_ref] = nref
opts[:network_type] = net_info[:network_type]
end # network loop
end #datacenters loop
network_objects[dc_name] << VCenterDriver::Network.to_one_template(opts)
end
end # datacenters loop
return network_objects
end
end # class DatatacenterFolder

View File

@ -1,5 +1,4 @@
module VCenterDriver
class Importer
VNC_ESX_HOST_FOLDER = "/tmp"
@ -640,22 +639,34 @@ def self.import_networks(con_ops, options)
tmps.each do |n|
if !use_defaults
message = false
print_str = "\n * Network found:\n"\
" - Name : \e[92m#{n[:name]}\e[39m\n"\
" - Type : #{n[:type]}\n"\
" - Cluster : \e[96m#{n[:cluster]}\e[39m\n"\
" - Cluster location : #{n[:cluster_location]}\n"\
" - OpenNebula Cluster ID : #{n[:one_cluster_id]}\n"
" - Name : \e[92m#{n[:name]}\e[39m\n"\
" - Type : #{n[:type]}\n"\
" - Vcenter Clusters(host id): "
if n[:one_cluster_id] == -1
print_str << "You need to import the associated vcenter cluster as one host first!"
unimported = ""
import = false
for i in 0..(n[:clusters][:refs].size-1)
if n[:clusters][:one_ids][i] != -1
import = true
print_str << "\e[96m#{n[:clusters][:names][i]}(#{n[:clusters][:one_ids][i]})\e[39m "
else
message = true
print_str << "\e[91m#{n[:clusters][:names][i]}\e[39m "
unimported << "#{n[:clusters][:names][i]} "
end
end
if !import
print_str << "\n You need to at least 1 vcenter cluster as one host first!"
else
print_str << " Import this Network (y/[n])? "
print_str << "\n Import this Network (y/[n])? "
end
STDOUT.print print_str
next if STDIN.gets.strip.downcase != 'y' || n[:one_cluster_id] == -1
next if STDIN.gets.strip.downcase != 'y' || !import
end
# we try to retrieve once we know the desired net
@ -683,6 +694,11 @@ def self.import_networks(con_ops, options)
ip6_address = nil
prefix_length = nil
if !use_defaults && message
STDOUT.print "\n\e[93mWarning: you have unimported clusters for this network: #{unimported}\n"\
"cancel this process(ctrl-C) if you want to import them first\e[39m\n\n"
end
# Size
if !use_defaults
STDOUT.print " How many VMs are you planning"\
@ -787,8 +803,22 @@ def self.import_networks(con_ops, options)
n[:one] << ar_str
one_vn = VCenterDriver::VIHelper.new_one_item(OpenNebula::VirtualNetwork)
if n[:clusters] #Distributed Port Group
done = []
for i in 0..n[:clusters][:refs].size-1
cl_id = n[:clusters][:one_ids][i]
next if cl_id == -1 || done.include?(cl_id)
rc = one_vn.allocate(n[:one],n[:one_cluster_id].to_i)
if done.empty?
rc = one_vn.allocate(n[:one],cl_id.to_i)
one_vn.info
else
one_cluster = VCenterDriver::VIHelper.one_item(OpenNebula::Cluster, cl_id, false)
rc = one_cluster.addvnet(one_vn['ID'].to_i)
end
done << cl_id
end
end
if ::OpenNebula.is_error?(rc)
STDOUT.puts "\n Error creating virtual network: " +

View File

@ -91,37 +91,52 @@ class Network
return id
end
def self.to_one_template(network_name, network_ref, network_type,
ccr_ref, ccr_name, vcenter_uuid,
vcenter_instance_name, dc_name, cluster_id,
cluster_location,
unmanaged=nil, template_ref=nil, dc_ref=nil,
vm_or_template_name=nil, template_id=nil)
one_tmp = {}
def self.generate_name(name, opts = {})
vcenter_instance_name = opts[:vcenter_name] || nil
dc_name = opts[:dc_name] || nil
hash_name = "#{network_name} - #{ccr_name.tr(" ", "_")} [#{vcenter_instance_name} - #{dc_name}]_#{cluster_location}"
hash_name = "#{name} - [#{vcenter_instance_name} - #{dc_name}]"
sha256 = Digest::SHA256.new
network_hash = sha256.hexdigest(hash_name)[0..11]
network_import_name = "#{network_name} - #{ccr_name.tr(" ", "_")} [#{vcenter_instance_name} - #{dc_name}]_#{network_hash}"
network_import_name = "#{name} - [#{vcenter_instance_name} - #{dc_name}]_#{network_hash}"
end
def self.to_one_template(opts = {})
one_tmp = {}
network_name = opts[:network_name]
network_ref = opts[:network_ref]
network_type = opts[:network_type]
vcenter_uuid = opts[:vcenter_uuid]
vcenter_instance_name = opts[:vcenter_instance_name]
dc_name = opts[:dc_name]
cluster_id = opts[:cluster_id]
unmanaged = opts[:unmanaged] || nil
template_ref = opts[:template_ref] || nil
dc_ref = opts[:dc_ref] || nil
vm_or_template_name = opts[:vm_or_template_name] || nil
template_id = opts[:template_id] || nil
network_import_name = generate_name(network_name, {:vcenter_name=>vcenter_instance_name, :dc_name=>dc_name})
one_tmp[:name] = network_name
one_tmp[:import_name] = network_import_name
one_tmp[:bridge] = network_name
one_tmp[:type] = network_type
one_tmp[:cluster] = ccr_name
one_tmp[:cluster_location] = cluster_location
one_tmp[:vcenter_ccr_ref] = ccr_ref
one_tmp[:one_cluster_id] = cluster_id
one_tmp[:vcenter_net_ref] = network_ref
one_tmp[:clusters] = opts[:clusters] || nil
one_tmp[:one] = to_one(network_import_name, network_name, network_ref, network_type,
ccr_ref, vcenter_uuid, unmanaged, template_ref, dc_ref, template_id)
vcenter_uuid, unmanaged, template_ref, dc_ref, template_id)
return one_tmp
end
def self.to_one(network_import_name, network_name, network_ref, network_type,
ccr_ref, vcenter_uuid, unmanaged, template_ref, dc_ref, template_id)
vcenter_uuid, unmanaged, template_ref, dc_ref, template_id)
template = "NAME=\"#{network_import_name}\"\n"\
"BRIDGE=\"#{network_name}\"\n"\
@ -135,7 +150,7 @@ class Network
template += "VCENTER_FROM_WILD=\"#{template_id}\"\n"
end
template += "VCENTER_CCR_REF=\"#{ccr_ref}\"\n" if !unmanaged
#template += "VCENTER_CCR_REF=\"#{ccr_ref}\"\n" if !unmanaged
template += "VCENTER_TEMPLATE_REF=\"#{template_ref}\"\n" if template_ref

View File

@ -446,22 +446,26 @@ class Template
net_host = VCenterDriver::ClusterComputeResource.new_from_ref(ccr_ref, @vi_client)
location = VCenterDriver::VIHelper.get_location(net_host.item)
import_opts = {
:network_name=> nic[:net_name],
:network_ref=> nic[:net_ref],
:network_type=> nic[:pg_type],
:ccr_ref=> ccr_ref,
:ccr_name=> ccr_name,
:vcenter_uuid=> vc_uuid,
:vcenter_instance_name=> vcenter_instance_name,
:dc_name=> dc_name,
:cluster_id=> cluster_id,
:location=> location,
:unmanaged=> unmanaged,
:template_ref=> template_ref,
:dc_ref=> dc_ref,
:vm_or_template_name=> vm_name,
:template_id=> vm_id
}
# Prepare the Virtual Network template
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,
cluster_id,
location,
unmanaged,
template_ref,
dc_ref,
vm_name,
vm_id)
one_vnet = VCenterDriver::Network.to_one_template(import_opts)
# By default add an ethernet range to network size 255
ar_str = ""