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

F #1959: add way to select IP when importing (#1827)

(cherry picked from commit 058276c14833719c76bdab0f408f7285fe75dc06)
This commit is contained in:
Alejandro Huertas Herrero 2022-03-11 10:29:08 +01:00 committed by Tino Vazquez
parent 3c5a419c85
commit 1030286105
No known key found for this signature in database
GPG Key ID: 14201E424D02047E
4 changed files with 86 additions and 14 deletions

View File

@ -109,6 +109,20 @@ CommandParser::CmdParser.new(ARGV) do
:format => String
}
IPV4 = {
:name => 'ipv4',
:large => '--ipv4 ip1,ip2',
:description => 'Comma separated IPV4 to set',
:format => Array
}
IPV6 = {
:name => 'ipv6',
:large => '--ipv6 ip1,ip2',
:description => 'Comma separated IPV6 to set',
:format => Array
}
CREAT_OPTIONS = [IM, VMM, OneClusterHelper::CLUSTER, TYPE]
SYNC_OPTIONS = [OneClusterHelper::CLUSTER, FORCE, SSH]
@ -344,12 +358,16 @@ CommandParser::CmdParser.new(ARGV) do
Import VM to OpenNebula
EOT
command :importvm, importvm_desc, :hostid, :name do
command :importvm,
importvm_desc,
:hostid,
:name,
:options => [IPV4, IPV6] do
helper.perform_action(args[0], options, 'imported') do |o|
rc = o.info
next rc if OpenNebula.is_error?(rc)
o.import_wild(args[1])
o.import_wild(args[1], options[:ipv4], options[:ipv6])
end
end

View File

@ -14,7 +14,6 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'opennebula/pool_element'
require 'base64'
require 'yaml'
@ -217,10 +216,12 @@ module OpenNebula
# Imports a wild VM from the host and puts it in running state
#
# @param name [String] Name of the VM to import
# @param ipv4 [Array] Array with IP4s to set
# @param ipv6 [Array] Array with IP6s to set
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def import_wild(name)
def import_wild(name, ipv4 = nil, ipv6 = nil)
vms = importable_wilds.select {|vm| vm['VM_NAME'] == name }
if vms.length == 0
@ -247,8 +248,15 @@ module OpenNebula
vi_client = VCenterDriver::VIClient.new_from_host(self["ID"])
importer = VCenterDriver::VmmImporter.new(@client, vi_client)
return importer.import({wild: wild, template: template,
one_item: vm, host: self['ID']})
return importer.import(
{ :wild => wild,
:template => template,
:one_item => vm,
:host => self['ID'],
:ipv4 => ipv4,
:ipv6 => ipv6
}
)
else
rc = vm.allocate(template)

View File

@ -623,6 +623,23 @@ module VCenterDriver
network.info
if nic[:ipv4] || nic[:ipv6]
ar_array = network.to_hash['VNET']['AR_POOL']['AR']
ar_array = [ar_array] if ar_array.is_a?(Hash)
ipv4, _ipv6, _arid = find_ip_in_ar(
IPAddr.new(nic[:ipv4]),
ar_array
) if ar_array && nic[:ipv4]
_ipv4, ipv6, _arid = find_ip_in_ar(
IPAddr.new(nic[:ipv6]),
ar_array
) if ar_array && nic[:ipv6]
return [ipv4, ipv6]
end
# Iterate over Retrieve vCenter VM NICs
unless vm_object.item.guest.net.empty?
vm_object.item.guest.net.each do |net|
@ -755,9 +772,10 @@ module VCenterDriver
nic_tmp = "NIC=[\n"
nic_tmp << "NETWORK_ID=\"#{one_vn.id}\",\n"
nic_tmp << "NAME =\"VC_NIC#{nic_index}\",\n"
nic_tmp << "IP = \"#{nic[:ipv4]}\",\n" if nic[:ipv4]
if vm?
if nic[:mac]
if nic[:mac] && !nic[:ipv4]
nic_tmp << "MAC=\"#{nic[:mac]}\",\n"
end
if nic[:ipv4_additionals]
@ -813,6 +831,7 @@ module VCenterDriver
ar_tmp = create_ar(nic)
network_found.add_ar(ar_tmp)
end
ipv4, ipv6 = find_ips_in_network(network_found, vm_object,
nic, true)
network_found.info
@ -824,8 +843,10 @@ module VCenterDriver
if nic[:mac] && ipv4.empty? && ipv6.empty?
nic_tmp << "MAC=\"#{nic[:mac]}\",\n"
end
nic_tmp << "IP=\"#{ipv4}\"," unless ipv4.empty?
nic_tmp << "IP6=\"#{ipv6}\"," unless ipv6.empty?
if nic[:ipv4_additionals]
nic_tmp <<
'VCENTER_ADDITIONALS_IP4'\
@ -1053,7 +1074,13 @@ module VCenterDriver
ar_tmp << "]\n"
if vm?
ar_tmp << create_ar(nic, true)
ar_tmp << create_ar(nic, false, nic[:ipv4]) if nic[:ipv4]
if nic[:ipv6]
ar_tmp << create_ar(nic, false, nil, nic[:ipv6])
end
ar_tmp << create_ar(nic, true) if !nic[:ipv4] && !nic[:ipv6]
end
one_vnet[:one] << ar_tmp
@ -1108,6 +1135,23 @@ module VCenterDriver
nic_index = 0
vc_nics.each do |nic|
[:ipv4, :ipv6].each do |type|
if nic[type]
opts[type].shift if opts[type]
next
end
begin
ip = opts[type].shift if opts[type]
# Check if it is a valid IP
IPAddr.new(ip)
nic[type] = ip
rescue StandardError
end
end
# Check if the network already exists
network_found =
VCenterDriver::VIHelper

View File

@ -73,13 +73,15 @@ module VCenterDriver
template << template_disks
opts = {
:vi_client => @vi_client,
:vc_uuid => vc_uuid,
:npool => npool,
:hpool => hpool,
:vcenter => vc_name,
:vi_client => @vi_client,
:vc_uuid => vc_uuid,
:npool => npool,
:hpool => hpool,
:vcenter => vc_name,
:template_moref => vm_ref,
:vm_object => vc_vm
:vm_object => vc_vm,
:ipv4 => selected[:ipv4],
:ipv6 => selected[:ipv6]
}
# Create images or get nics information for template