mirror of
https://github.com/OpenNebula/one.git
synced 2025-02-28 17:57:22 +03:00
L #~: Refactor vcenter driver (#110)
Signed-off-by: Carlos Herrera <cherrera@opennebula.io>
This commit is contained in:
parent
a5041b4dee
commit
3f973e5a44
@ -123,7 +123,7 @@ def create_dpg(one_vnet, dc, cluster, vi_client)
|
||||
if dvs
|
||||
pnics_available = nil
|
||||
if pnics && !pnics.empty?
|
||||
pnics_available = esx_host.get_available_pnics
|
||||
pnics_available = esx_host.available_pnics
|
||||
end
|
||||
esx_host.assign_proxy_switch(dvs,
|
||||
sw_name,
|
||||
@ -164,7 +164,7 @@ def create_pg(one_vnet, esx_host)
|
||||
esx_host.lock # Exclusive lock for ESX host operation
|
||||
|
||||
pnics_available = nil
|
||||
pnics_available = esx_host.get_available_pnics if pnics
|
||||
pnics_available = esx_host.available_pnics if pnics
|
||||
|
||||
# Get port group if it exists
|
||||
pg = esx_host.pg_exists(pg_name)
|
||||
|
@ -88,6 +88,7 @@ AllCops:
|
||||
- src/im_mad/remotes/one.d/poll
|
||||
- src/im_mad/remotes/az.d/poll
|
||||
- src/im_mad/remotes/lib/vcenter_cluster.rb
|
||||
- src/im_mad/remotes/lib/vcenter_monitor.rb
|
||||
- src/vnm_mad/remotes/ovswitch/post
|
||||
- src/vnm_mad/remotes/ovswitch/clean
|
||||
- src/vnm_mad/remotes/ovswitch/pre
|
||||
@ -386,17 +387,8 @@ AllCops:
|
||||
- src/oca/ruby/deprecated/OpenNebula.rb
|
||||
- src/vmm_mad/dummy/one_vmm_dummy.rb
|
||||
- src/vmm_mad/remotes/one/opennebula_driver.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/datastore.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/vcenter_importer.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/vi_helper.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/network.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/vm_template.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/file_helper.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/vi_client.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/host.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/datacenter.rb
|
||||
- src/vmm_mad/remotes/lib/vcenter_driver/memoize.rb
|
||||
- src/vmm_mad/remotes/lib/lxd/mapper/qcow2.rb
|
||||
- src/vmm_mad/remotes/lib/lxd/mapper/mapper.rb
|
||||
- src/vmm_mad/remotes/lib/lxd/mapper/rbd.rb
|
||||
|
@ -86,7 +86,7 @@ target_path = "#{ds_image_dir}/#{id}"
|
||||
# If image is in a remote http location it has to be downloaded
|
||||
# or if is a zipped file it has to be unzipped in a temp folder
|
||||
|
||||
if VCenterDriver::FileHelper.is_remote_or_needs_unpack?(img_path)
|
||||
if VCenterDriver::FileHelper.remote_or_needs_unpack?(img_path)
|
||||
temp_folder = File.join(VAR_LOCATION, "vcenter/#{target_path}")
|
||||
temp_file = File.join(temp_folder, File.basename(img_path))
|
||||
# if the original file doesnt have the vmdk extension, add it
|
||||
@ -140,25 +140,25 @@ files_to_upload.each_with_index do |f, index|
|
||||
path = "#{target_path}/#{filename}"
|
||||
|
||||
# remove gz or bz2 if part of filename
|
||||
if path.end_with?('gz') && VCenterDriver::FileHelper.is_vmdk?(f)
|
||||
if path.end_with?('gz') && VCenterDriver::FileHelper.vmdk?(f)
|
||||
path.gsub!(/gz$/, '')
|
||||
end
|
||||
|
||||
if path.end_with?('bz2') && VCenterDriver::FileHelper.is_vmdk?(f)
|
||||
if path.end_with?('bz2') && VCenterDriver::FileHelper.vmdk?(f)
|
||||
path.gsub!(/bz2$/, '')
|
||||
end
|
||||
end
|
||||
|
||||
# Change path if vmdk is part of filename but it's not the extension
|
||||
# rubocop:disable Style/DoubleNegation
|
||||
if !!/[^.]+vmdk$/.match(path) && VCenterDriver::FileHelper.is_vmdk?(f)
|
||||
if !!/[^.]+vmdk$/.match(path) && VCenterDriver::FileHelper.vmdk?(f)
|
||||
path.gsub!(/vmdk$/, '')
|
||||
extension = '.vmdk'
|
||||
end
|
||||
# rubocop:enable Style/DoubleNegation
|
||||
|
||||
# Add iso extension if file is an ISO file
|
||||
if VCenterDriver::FileHelper.is_iso?(f)
|
||||
if VCenterDriver::FileHelper.iso?(f)
|
||||
path = "#{File.dirname(path)}/#{File.basename(path, '.*')}"
|
||||
extension = '.iso'
|
||||
end
|
||||
|
@ -76,7 +76,7 @@ begin
|
||||
|
||||
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
|
||||
|
||||
if vm.has_snapshots?
|
||||
if vm.snapshots?
|
||||
raise "'disk-saveas' not supported in VMs with system snapshots."
|
||||
end
|
||||
|
||||
|
@ -97,10 +97,10 @@ begin
|
||||
vm.destroy_disk(disk)
|
||||
else
|
||||
@error_message = "Error unregistering vm #{vmid} (#{vm_ref})."
|
||||
vm.poweroff_hard if vm.is_powered_on?
|
||||
vm.poweroff_hard if vm.powered_on?
|
||||
|
||||
if vm.instantiated_as_persistent?
|
||||
vm.remove_all_snapshots if vm.has_snapshots?
|
||||
vm.remove_all_snapshots if vm.snapshots?
|
||||
vm.convert_to_template
|
||||
else
|
||||
vm.destroy
|
||||
|
@ -71,7 +71,7 @@ begin
|
||||
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
|
||||
vmperst = vm.instantiated_as_persistent?
|
||||
|
||||
vm.remove_all_snapshots if vm.has_snapshots?
|
||||
vm.remove_all_snapshots if vm.snapshots?
|
||||
disk = vm.disk(disk_id)
|
||||
|
||||
# Don't detach persistent disks if the VM has snapshots
|
||||
|
@ -63,7 +63,7 @@ begin
|
||||
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
|
||||
|
||||
# Cannot resize if VM has snapshots
|
||||
if vm.has_snapshots?
|
||||
if vm.snapshots?
|
||||
raise "'disk-resize' operation not supported for VMs with snapshots."
|
||||
end
|
||||
|
||||
|
@ -126,7 +126,7 @@ module NSXDriver
|
||||
# Virtual Machine devices
|
||||
vm_devices = vm.item.config.hardware.device
|
||||
vm_devices.each do |device|
|
||||
next unless vm.is_nic?(device)
|
||||
next unless VCenterDriver::Network.nic?(device)
|
||||
|
||||
next if device.macAddress != network_mac
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -17,27 +17,39 @@
|
||||
require 'fileutils'
|
||||
require 'tempfile'
|
||||
|
||||
##############################################################################
|
||||
# Module VCenterDriver
|
||||
##############################################################################
|
||||
module VCenterDriver
|
||||
|
||||
class FileHelper
|
||||
##########################################################################
|
||||
# Class FileHelper
|
||||
##########################################################################
|
||||
class FileHelper
|
||||
|
||||
def self.get_img_name(disk, vm_id, vm_name, instantiate_as_persistent=false)
|
||||
if disk["PERSISTENT"] == "YES" || disk["TYPE"] == "CDROM"
|
||||
return disk["SOURCE"]
|
||||
def self.get_img_name(
|
||||
disk,
|
||||
vm_id,
|
||||
_vm_name,
|
||||
instantiate_as_persistent = false
|
||||
)
|
||||
if disk['PERSISTENT'] == 'YES' || disk['TYPE'] == 'CDROM'
|
||||
disk['SOURCE']
|
||||
else
|
||||
disk_id = disk["DISK_ID"]
|
||||
if disk["SOURCE"]
|
||||
disk_id = disk['DISK_ID']
|
||||
if disk['SOURCE']
|
||||
if instantiate_as_persistent &&
|
||||
disk["OPENNEBULA_MANAGED"] &&
|
||||
disk["OPENNEBULA_MANAGED"].upcase == "NO"
|
||||
return disk["SOURCE"] # Treat this disk as if was persistent
|
||||
disk['OPENNEBULA_MANAGED'] &&
|
||||
disk['OPENNEBULA_MANAGED'].upcase == 'NO'
|
||||
disk['SOURCE'] # Treat this disk as if was persistent
|
||||
else
|
||||
image_name = disk["SOURCE"].split(".").first
|
||||
return "#{image_name}-#{vm_id}-#{disk_id}.vmdk"
|
||||
image_name = disk['SOURCE'].split('.').first
|
||||
"#{image_name}-#{vm_id}-#{disk_id}.vmdk"
|
||||
end
|
||||
else
|
||||
ds_volatile_dir = disk["VCENTER_DS_VOLATILE_DIR"] || "one-volatile"
|
||||
return "#{ds_volatile_dir}/#{vm_id}/one-#{vm_id}-#{disk_id}.vmdk"
|
||||
ds_volatile_dir =
|
||||
disk['VCENTER_DS_VOLATILE_DIR'] || 'one-volatile'
|
||||
"#{ds_volatile_dir}/#{vm_id}/one-#{vm_id}-#{disk_id}.vmdk"
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -45,32 +57,32 @@ class FileHelper
|
||||
# REMOVE: no need to change...
|
||||
def self.get_img_name_from_path(path, vm_id, disk_id)
|
||||
# Note: This will probably fail if the basename contains '.'
|
||||
return "#{path.split(".").first}-#{vm_id}-#{disk_id}.vmdk"
|
||||
"#{path.split('.').first}-#{vm_id}-#{disk_id}.vmdk"
|
||||
end
|
||||
|
||||
def self.is_remote_or_needs_unpack?(file)
|
||||
return !is_remote?(file).nil? || needs_unpack?(file)
|
||||
def self.remote_or_needs_unpack?(file)
|
||||
!remote?(file).nil? || needs_unpack?(file)
|
||||
end
|
||||
|
||||
def self.is_remote?(file)
|
||||
def self.remote?(file)
|
||||
file.match(%r{^https?://})
|
||||
end
|
||||
|
||||
def self.is_vmdk?(file)
|
||||
type = %x{file #{file}}
|
||||
def self.vmdk?(file)
|
||||
type = `file #{file}`
|
||||
|
||||
type.include? "VMware"
|
||||
type.include? 'VMware'
|
||||
end
|
||||
|
||||
def self.is_iso?(file)
|
||||
type = %x{file #{file}}
|
||||
def self.iso?(file)
|
||||
type = `file #{file}`
|
||||
|
||||
type.include? "ISO"
|
||||
type.include? 'ISO'
|
||||
end
|
||||
|
||||
def self.get_type(file)
|
||||
type = %x{file -b --mime-type #{file}}
|
||||
if $?.exitstatus != 0
|
||||
type = `file -b --mime-type #{file}`
|
||||
if $?.exitstatus != 0 # rubocop:disable Style/SpecialGlobalVars
|
||||
STDERR.puts "Can not read file #{file}"
|
||||
exit(-1)
|
||||
end
|
||||
@ -80,7 +92,7 @@ class FileHelper
|
||||
def self.needs_unpack?(file_path)
|
||||
type = get_type(file_path)
|
||||
type.gsub!(%r{^application/(x-)?}, '')
|
||||
return %w{bzip2 gzip tar}.include?(type)
|
||||
%w[bzip2 gzip tar].include?(type)
|
||||
end
|
||||
|
||||
def self.vcenter_file_info(file_path)
|
||||
@ -91,7 +103,7 @@ class FileHelper
|
||||
last = nil
|
||||
|
||||
files.each do |f|
|
||||
if get_type(f).strip == "text/plain"
|
||||
if get_type(f).strip == 'text/plain'
|
||||
file_path = f
|
||||
found = true
|
||||
break
|
||||
@ -106,27 +118,27 @@ class FileHelper
|
||||
file_path = last
|
||||
found = true
|
||||
else
|
||||
STDERR.puts "Could not find vmdk"
|
||||
STDERR.puts 'Could not find vmdk'
|
||||
exit(-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
case get_type(file_path).strip
|
||||
when "application/octet-stream"
|
||||
return {
|
||||
when 'application/octet-stream'
|
||||
{
|
||||
:type => :standalone,
|
||||
:file => file_path,
|
||||
:dir => File.dirname(file_path)
|
||||
}
|
||||
when "application/x-iso9660-image"
|
||||
return {
|
||||
when 'application/x-iso9660-image'
|
||||
{
|
||||
:type => :standalone,
|
||||
:file => file_path,
|
||||
:dir => File.dirname(file_path),
|
||||
:extension => '.iso'
|
||||
}
|
||||
when "text/plain"
|
||||
when 'text/plain'
|
||||
info = {
|
||||
:type => :flat,
|
||||
:file => file_path,
|
||||
@ -135,117 +147,209 @@ class FileHelper
|
||||
|
||||
files_list = []
|
||||
descriptor = File.read(file_path).split("\n")
|
||||
flat_files = descriptor.select {|l| l.start_with?("RW")}
|
||||
flat_files = descriptor.select {|l| l.start_with?('RW') }
|
||||
|
||||
flat_files.each do |f|
|
||||
files_list << info[:dir] + "/" +
|
||||
f.split(" ")[3].chomp.chomp('"').reverse.chomp('"').reverse
|
||||
files_list <<
|
||||
info[:dir] +
|
||||
'/' +
|
||||
f
|
||||
.split(' ')[3]
|
||||
.chomp
|
||||
.chomp('"')
|
||||
.reverse
|
||||
.chomp('"')
|
||||
.reverse
|
||||
end
|
||||
|
||||
info[:flat_files] = files_list
|
||||
|
||||
return info
|
||||
info
|
||||
else
|
||||
STDERR.puts "Unrecognized file type"
|
||||
STDERR.puts 'Unrecognized file type'
|
||||
exit(-1)
|
||||
end
|
||||
end
|
||||
|
||||
def self.escape_path(path)
|
||||
return path.gsub(" ", "%20")
|
||||
path.gsub(' ', '%20')
|
||||
end
|
||||
|
||||
def self.unescape_path(path)
|
||||
return path.gsub("%20", " ")
|
||||
path.gsub('%20', ' ')
|
||||
end
|
||||
|
||||
# Recursively downloads vmdk related files and returns filenames
|
||||
# Recursively downloads vmdk related files and returns filenames
|
||||
def self.get_all_filenames_in_descriptor(descriptor_url, ds)
|
||||
descriptor_filename = File.basename descriptor_url.path
|
||||
# Build array of files to download
|
||||
files_to_download = [descriptor_filename]
|
||||
image_source = descriptor_url.host + descriptor_url.path
|
||||
descriptor_content = ds.get_text_file image_source
|
||||
flat_files = descriptor_content.select{|l| l.start_with?("RW")}
|
||||
flat_files = descriptor_content.select {|l| l.start_with?('RW') }
|
||||
flat_files.each do |file|
|
||||
# Get the filename from lines of type
|
||||
# RW 2048000 VMFS "filename-flat.vdmdk"
|
||||
file_to_download = file.split(" ")[3][1..-2]
|
||||
# Get the filename from lines of type
|
||||
# RW 2048000 VMFS "filename-flat.vdmdk"
|
||||
file_to_download = file.split(' ')[3][1..-2]
|
||||
files_to_download << file_to_download
|
||||
image_path = File.dirname(descriptor_url.host+descriptor_url.path)
|
||||
if ds.is_descriptor?(image_path + "/" + file_to_download)
|
||||
files_to_download << download_all_filenames_in_descriptor(image_path + "/" + file_to_download)
|
||||
end
|
||||
image_path =
|
||||
File
|
||||
.dirname(
|
||||
descriptor_url.host+descriptor_url.path
|
||||
)
|
||||
next unless ds.descriptor?(image_path + '/' + file_to_download)
|
||||
|
||||
files_to_download <<
|
||||
download_all_filenames_in_descriptor(
|
||||
image_path + '/' + file_to_download
|
||||
)
|
||||
end
|
||||
|
||||
return files_to_download
|
||||
files_to_download
|
||||
end
|
||||
|
||||
|
||||
def self.download_vmdks(files_to_download, url_prefix, temp_folder, ds)
|
||||
# Download files
|
||||
url_prefix = url_prefix + "/"
|
||||
url_prefix += '/'
|
||||
|
||||
VCenterDriver::VIClient.in_silence do
|
||||
files_to_download.each{|file|
|
||||
files_to_download.each do |file|
|
||||
ds.download_file(url_prefix + file, temp_folder + file)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Receives a VMDK descriptor or file, downloads all
|
||||
# related files, creates a tar.gz and dumps it in stdout
|
||||
# related files, creates a tar.gz and dumps it in stdout
|
||||
def self.dump_vmdk_tar_gz(vcenter_url, ds)
|
||||
image_source = vcenter_url.host + vcenter_url.path
|
||||
if ds.is_descriptor?(image_source)
|
||||
files_to_download = self.get_all_filenames_in_descriptor(vcenter_url, ds)
|
||||
if ds.descriptor?(image_source)
|
||||
files_to_download =
|
||||
get_all_filenames_in_descriptor(
|
||||
vcenter_url,
|
||||
ds
|
||||
)
|
||||
|
||||
descriptor_name = File.basename vcenter_url.path
|
||||
temp_folder = VAR_LOCATION + "/vcenter/" + descriptor_name + "/"
|
||||
FileUtils.mkdir_p(temp_folder) if !File.directory?(temp_folder)
|
||||
temp_folder = VAR_LOCATION + '/vcenter/' + descriptor_name + '/'
|
||||
unless File.directory?(temp_folder)
|
||||
FileUtils
|
||||
.mkdir_p(
|
||||
temp_folder
|
||||
)
|
||||
end
|
||||
|
||||
image_path = File.dirname(vcenter_url.host+vcenter_url.path)
|
||||
self.download_vmdks(files_to_download, image_path, temp_folder, ds)
|
||||
download_vmdks(files_to_download, image_path, temp_folder, ds)
|
||||
|
||||
# Create tar.gz
|
||||
rs = system("cd #{temp_folder} && tar czf #{descriptor_name}.tar.gz #{files_to_download.join(' ')} > /dev/null 2>&1")
|
||||
(FileUtils.rm_rf(temp_folder) ; raise "Error creating tar file for #{descriptor_name}") unless rs
|
||||
rs = system(
|
||||
"cd #{temp_folder} \&& tar czf #{descriptor_name}.tar.gz \
|
||||
#{files_to_download.join(' ')} > /dev/null 2>&1"
|
||||
)
|
||||
unless rs
|
||||
FileUtils.rm_rf temp_folder
|
||||
raise "Error creating tar file for #{descriptor_name}"
|
||||
end
|
||||
|
||||
# Cat file to stdout
|
||||
rs = system("cat #{temp_folder + descriptor_name}.tar.gz")
|
||||
(FileUtils.rm_rf(temp_folder) ; raise "Error reading tar for #{descriptor_name}") unless rs
|
||||
unless rs
|
||||
FileUtils.rm_rf temp_folder
|
||||
raise "Error reading tar for #{descriptor_name}"
|
||||
end
|
||||
|
||||
# Delete tar.gz
|
||||
rs = system("cd #{temp_folder} && rm #{descriptor_name}.tar.gz #{files_to_download.join(' ')}")
|
||||
(FileUtils.rm_rf(temp_folder) ; raise "Error removing tar for #{descriptor_name}") unless rs
|
||||
rs = system(
|
||||
"cd #{temp_folder} \
|
||||
&& rm #{descriptor_name}.tar.gz #{
|
||||
files_to_download
|
||||
.join(' ')}"
|
||||
)
|
||||
unless rs
|
||||
FileUtils.rm_rf temp_folder
|
||||
raise "Error removing tar for #{descriptor_name}"
|
||||
end
|
||||
else
|
||||
# Setting "." as the source will read from the stdin
|
||||
VCenterDriver::VIClient.in_stderr_silence do
|
||||
descriptor_name = File.basename vcenter_url.path
|
||||
file_to_download = [vcenter_url.path]
|
||||
temp_folder = VAR_LOCATION + "/vcenter/" + descriptor_name + "/"
|
||||
temp_folder =
|
||||
VAR_LOCATION + '/vcenter/' + descriptor_name + '/'
|
||||
|
||||
FileUtils.mkdir_p(temp_folder + File.dirname(vcenter_url.path) + "/") if !File.directory?(temp_folder + File.dirname(vcenter_url.path) + "/")
|
||||
unless File
|
||||
.directory?(
|
||||
temp_folder + File
|
||||
.dirname(
|
||||
vcenter_url
|
||||
.path
|
||||
) + '/'
|
||||
)
|
||||
FileUtils
|
||||
.mkdir_p(temp_folder + File
|
||||
.dirname(
|
||||
vcenter_url
|
||||
.path
|
||||
) + '/')
|
||||
end
|
||||
|
||||
self.download_vmdks(file_to_download, vcenter_url.host, temp_folder, ds)
|
||||
download_vmdks(
|
||||
file_to_download,
|
||||
vcenter_url.host,
|
||||
temp_folder,
|
||||
ds
|
||||
)
|
||||
|
||||
temp_folder = temp_folder + File.dirname(vcenter_url.path)
|
||||
temp_folder += File.dirname(vcenter_url.path)
|
||||
|
||||
# Create tar.gz
|
||||
rs = system("cd #{temp_folder} && tar czf #{descriptor_name}.tar.gz #{descriptor_name} > /dev/null 2>&1")
|
||||
(FileUtils.rm_rf(temp_folder) ; raise "Error creating tar file for #{descriptor_name}") unless rs
|
||||
rs = system(
|
||||
"cd #{temp_folder} && tar czf #{descriptor_name}.tar.gz\
|
||||
#{descriptor_name} > /dev/null 2>&1"
|
||||
)
|
||||
unless rs
|
||||
(
|
||||
FileUtils
|
||||
.rm_rf(
|
||||
temp_folder
|
||||
)
|
||||
raise "Error creating tar \
|
||||
file for #{descriptor_name}")
|
||||
end
|
||||
|
||||
# Cat file to stdout
|
||||
rs = system("cat #{temp_folder + "/" + descriptor_name}.tar.gz")
|
||||
(FileUtils.rm_rf(temp_folder) ; raise "Error reading tar for #{descriptor_name}") unless rs
|
||||
rs = system(
|
||||
"cat #{temp_folder + '/' + descriptor_name}.tar.gz"
|
||||
)
|
||||
unless rs
|
||||
(
|
||||
FileUtils
|
||||
.rm_rf(
|
||||
temp_folder
|
||||
)
|
||||
raise "Error reading tar for #{descriptor_name}")
|
||||
end # rubocop:disable Style/Semicolon
|
||||
|
||||
# Delete tar.gz
|
||||
rs = system("cd #{temp_folder} && rm #{descriptor_name}.tar.gz #{descriptor_name}")
|
||||
(FileUtils.rm_rf(temp_folder) ; raise "Error removing tar for #{descriptor_name}") unless rs
|
||||
rs = system(
|
||||
"cd #{temp_folder} \
|
||||
&& rm #{descriptor_name}.tar.gz #{descriptor_name}"
|
||||
)
|
||||
unless rs
|
||||
(
|
||||
FileUtils
|
||||
.rm_rf(
|
||||
temp_folder
|
||||
)
|
||||
raise "Error \ removing tar for #{descriptor_name}")
|
||||
end # rubocop:disable Style/Semicolon
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end # class FileHelper
|
||||
end
|
||||
# class FileHelper
|
||||
|
||||
end # module VCenterDriver
|
||||
end
|
||||
# module VCenterDriver
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -14,10 +14,13 @@
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
##############################################################################
|
||||
# Module Memoize
|
||||
##############################################################################
|
||||
module Memoize
|
||||
|
||||
def [](property)
|
||||
@memoize = {} if !defined?(@memoize)
|
||||
@memoize = {} unless defined?(@memoize)
|
||||
|
||||
if (value = @memoize[property])
|
||||
return value
|
||||
@ -25,13 +28,13 @@ module Memoize
|
||||
|
||||
current_item = @item
|
||||
|
||||
property_path = ""
|
||||
property_path = ''
|
||||
|
||||
property.split(".").each do |elem|
|
||||
property.split('.').each do |elem|
|
||||
if property_path.empty?
|
||||
property_path << elem
|
||||
else
|
||||
property_path << "." << elem
|
||||
property_path << '.' << elem
|
||||
end
|
||||
|
||||
if (val = @memoize[property_path])
|
||||
@ -39,7 +42,7 @@ module Memoize
|
||||
else
|
||||
begin
|
||||
current_item = current_item.send(elem)
|
||||
rescue Exception => e
|
||||
rescue StandardError
|
||||
current_item = nil
|
||||
end
|
||||
end
|
||||
@ -47,14 +50,13 @@ module Memoize
|
||||
break if current_item.nil?
|
||||
|
||||
@memoize[property_path] = current_item
|
||||
|
||||
end
|
||||
|
||||
@memoize[property] = current_item
|
||||
end
|
||||
|
||||
def clear(property)
|
||||
@memoize = {} if !defined?(@memoize)
|
||||
@memoize = {} unless defined?(@memoize)
|
||||
@memoize.clear[property] if @memoize[property]
|
||||
end
|
||||
|
||||
@ -63,8 +65,10 @@ module Memoize
|
||||
end
|
||||
|
||||
def []=(property, value)
|
||||
@memoize = {} if !defined?(@memoize)
|
||||
@memoize = {} unless defined?(@memoize)
|
||||
|
||||
@memoize[property] = value
|
||||
end
|
||||
end # module Memoize
|
||||
|
||||
end
|
||||
# module Memoize
|
||||
|
@ -16,9 +16,16 @@
|
||||
|
||||
require 'digest'
|
||||
|
||||
##############################################################################
|
||||
# Module VCenterDriver
|
||||
##############################################################################
|
||||
module VCenterDriver
|
||||
|
||||
class NetworkFolder
|
||||
##########################################################################
|
||||
# Class NetworkFolder
|
||||
##########################################################################
|
||||
class NetworkFolder
|
||||
|
||||
attr_accessor :item, :items
|
||||
|
||||
def initialize(item)
|
||||
@ -26,35 +33,41 @@ class NetworkFolder
|
||||
@items = {}
|
||||
end
|
||||
|
||||
########################################################################
|
||||
######################################################################
|
||||
# Builds a hash with Network-Ref / Network to be used as a cache
|
||||
# @return [Hash] in the form
|
||||
# { ds_ref [Symbol] => Network object }
|
||||
########################################################################
|
||||
######################################################################
|
||||
def fetch!
|
||||
VIClient.get_entities(@item, "Network").each do |item|
|
||||
VIClient.get_entities(@item, 'Network').each do |item|
|
||||
item_name = item._ref
|
||||
@items[item_name.to_sym] = PortGroup.new(item)
|
||||
end
|
||||
|
||||
VIClient.get_entities(@item, "DistributedVirtualPortgroup").each do |item|
|
||||
VIClient
|
||||
.get_entities(
|
||||
@item,
|
||||
'DistributedVirtualPortgroup'
|
||||
).each do |item|
|
||||
item_name = item._ref
|
||||
@items[item_name.to_sym] = DistributedPortGroup.new(item)
|
||||
end
|
||||
|
||||
VIClient.get_entities(@item, "VmwareDistributedVirtualSwitch").each do |item|
|
||||
VIClient
|
||||
.get_entities(
|
||||
@item,
|
||||
'VmwareDistributedVirtualSwitch'
|
||||
).each do |item|
|
||||
item_name = item._ref
|
||||
@items[item_name.to_sym] = DistributedVirtualSwitch.new(item)
|
||||
end
|
||||
|
||||
VIClient.get_entities(@item, "OpaqueNetwork").each do |item|
|
||||
VIClient.get_entities(@item, 'OpaqueNetwork').each do |item|
|
||||
item_name = item._ref
|
||||
@items[item_name.to_sym] = OpaqueNetwork.new(item)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
########################################################################
|
||||
# Returns a Network. Uses the cache if available.
|
||||
# @param ref [Symbol] the vcenter ref
|
||||
@ -68,23 +81,29 @@ class NetworkFolder
|
||||
|
||||
@items[ref.to_sym]
|
||||
end
|
||||
end # class NetworkFolder
|
||||
|
||||
class Network
|
||||
end
|
||||
# class NetworkFolder
|
||||
|
||||
##########################################################################
|
||||
# Class Network
|
||||
##########################################################################
|
||||
class Network
|
||||
|
||||
attr_accessor :item
|
||||
|
||||
include Memoize
|
||||
|
||||
NETWORK_TYPE_PG = "Port Group"
|
||||
NETWORK_TYPE_DPG = "Distributed Port Group"
|
||||
NETWORK_TYPE_NSXV = "NSX-V" #"Virtual Wire"
|
||||
NETWORK_TYPE_NSXT = "Opaque Network"
|
||||
NETWORK_TYPE_UNKNOWN = "Unknown Network"
|
||||
NETWORK_TYPE_PG = 'Port Group'
|
||||
NETWORK_TYPE_DPG = 'Distributed Port Group'
|
||||
NETWORK_TYPE_NSXV = 'NSX-V' # "Virtual Wire"
|
||||
NETWORK_TYPE_NSXT = 'Opaque Network'
|
||||
NETWORK_TYPE_UNKNOWN = 'Unknown Network'
|
||||
|
||||
def initialize(item, vi_client=nil)
|
||||
def initialize(item, vi_client = nil)
|
||||
begin
|
||||
check_item(item, RbVmomi::VIM::Network)
|
||||
rescue
|
||||
rescue StandardError
|
||||
check_item(item, RbVmomi::VIM::DistributedVirtualPortgroup)
|
||||
end
|
||||
|
||||
@ -93,36 +112,47 @@ class Network
|
||||
end
|
||||
|
||||
# Checks if a RbVmomi::VIM::VirtualDevice is a network interface
|
||||
def self.is_nic?(device)
|
||||
!device.class.ancestors.index(RbVmomi::VIM::VirtualEthernetCard).nil?
|
||||
def self.nic?(device)
|
||||
!device
|
||||
.class
|
||||
.ancestors
|
||||
.index(
|
||||
RbVmomi::VIM::VirtualEthernetCard
|
||||
).nil?
|
||||
end
|
||||
|
||||
def self.vlanid(vid)
|
||||
case vid
|
||||
when -1
|
||||
"error"
|
||||
'error'
|
||||
when 0
|
||||
"disabled"
|
||||
'disabled'
|
||||
when 4095
|
||||
"VGT"
|
||||
'VGT'
|
||||
else
|
||||
"#{vid}"
|
||||
vid.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def self.retrieve_vlanid(network)
|
||||
begin
|
||||
name = network.name
|
||||
id = network.host.first.configManager.networkSystem.networkConfig.portgroup.select{|p|
|
||||
id = network
|
||||
.host
|
||||
.first
|
||||
.configManager
|
||||
.networkSystem
|
||||
.networkConfig
|
||||
.portgroup
|
||||
.select do |p|
|
||||
p.spec.name == name
|
||||
}.first.spec.vlanId
|
||||
rescue
|
||||
end.first.spec.vlanId
|
||||
rescue StandardError
|
||||
id = -1
|
||||
end
|
||||
return id
|
||||
id
|
||||
end
|
||||
|
||||
|
||||
def self.generate_name(name, opts = {})
|
||||
vcenter_instance_name = opts[:vcenter_name] || nil
|
||||
dc_name = opts[:dc_name] || nil
|
||||
@ -130,11 +160,10 @@ class Network
|
||||
hash_name = "#{name} - [#{vcenter_instance_name} - #{dc_name}]"
|
||||
sha256 = Digest::SHA256.new
|
||||
network_hash = sha256.hexdigest(hash_name)[0..11]
|
||||
network_import_name = "#{name} - [#{vcenter_instance_name} - #{dc_name}]_#{network_hash}"
|
||||
"#{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]
|
||||
@ -142,8 +171,6 @@ class Network
|
||||
sw_name = opts[:sw_name]
|
||||
|
||||
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
|
||||
@ -158,10 +185,15 @@ class Network
|
||||
vlanid = opts[:vlanid] || nil
|
||||
|
||||
bridge_name = network_name
|
||||
network_name = network_name.gsub("/","_")
|
||||
network_name = network_name.gsub('/', '_')
|
||||
|
||||
|
||||
network_import_name = VCenterDriver::VIHelper.one_name(OpenNebula::VirtualNetworkPool, network_name, network_ref+vcenter_uuid)
|
||||
network_import_name =
|
||||
VCenterDriver::VIHelper
|
||||
.one_name(
|
||||
OpenNebula::VirtualNetworkPool,
|
||||
network_name,
|
||||
network_ref+vcenter_uuid
|
||||
)
|
||||
|
||||
one_tmp[:name] = bridge_name
|
||||
one_tmp[:import_name] = network_import_name
|
||||
@ -184,15 +216,14 @@ class Network
|
||||
:nsx_id => nsx_id,
|
||||
:nsx_vni => nsx_vni,
|
||||
:nsx_tz_id => nsx_tz_id,
|
||||
:vlanid => vlanid,
|
||||
:vlanid => vlanid
|
||||
}
|
||||
|
||||
one_tmp[:one] = to_one(opts)
|
||||
return one_tmp
|
||||
one_tmp
|
||||
end
|
||||
|
||||
def self.to_one(opts)
|
||||
|
||||
template = "NAME=\"#{opts[:network_import_name]}\"\n"\
|
||||
"BRIDGE=\"#{opts[:bridge_name]}\"\n"\
|
||||
"VN_MAD=\"vcenter\"\n"\
|
||||
@ -201,46 +232,74 @@ class Network
|
||||
"VCENTER_INSTANCE_ID=\"#{opts[:vcenter_uuid]}\"\n"\
|
||||
"VCENTER_IMPORTED=\"YES\"\n"
|
||||
|
||||
if opts[:unmanaged] == "wild"
|
||||
if opts[:unmanaged] == 'wild'
|
||||
template += "VCENTER_FROM_WILD=\"#{opts[:template_id]}\"\n"
|
||||
end
|
||||
|
||||
template += "VCENTER_TEMPLATE_REF=\"#{opts[:template_ref]}\"\n" if opts[:template_ref]
|
||||
if opts[:template_ref]
|
||||
template +=
|
||||
"VCENTER_TEMPLATE_REF=\"#{opts[:template_ref]}\"\n"
|
||||
end
|
||||
|
||||
template += "VCENTER_SWITCH_NAME=\"#{opts[:sw_name]}\"\n" if opts[:sw_name]
|
||||
if opts[:sw_name]
|
||||
template +=
|
||||
"VCENTER_SWITCH_NAME=\"#{opts[:sw_name]}\"\n"
|
||||
end
|
||||
|
||||
template += "NSX_ID=\"#{opts[:nsx_id]}\"\n" if opts[:nsx_id]
|
||||
template += "NSX_VNI=\"#{opts[:nsx_vni]}\"\n" if opts[:nsx_vni]
|
||||
template += "NSX_TZ_ID=\"#{opts[:nsx_tz_id]}\"\n" if opts[:nsx_tz_id]
|
||||
if opts[:nsx_id]
|
||||
template +=
|
||||
"NSX_ID=\"#{opts[:nsx_id]}\"\n"
|
||||
end
|
||||
if opts[:nsx_vni]
|
||||
template +=
|
||||
"NSX_VNI=\"#{opts[:nsx_vni]}\"\n"
|
||||
end
|
||||
if opts[:nsx_tz_id]
|
||||
template +=
|
||||
"NSX_TZ_ID=\"#{opts[:nsx_tz_id]}\"\n"
|
||||
end
|
||||
|
||||
template += "VCENTER_VLAN_ID=\"#{opts[:vlanid]}\"\n" if opts[:vlanid]
|
||||
if opts[:vlanid]
|
||||
template +=
|
||||
"VCENTER_VLAN_ID=\"#{opts[:vlanid]}\"\n"
|
||||
end
|
||||
|
||||
return template
|
||||
template
|
||||
end
|
||||
|
||||
REQUIRED_ATTRS = [:refs, :one_ids, :one_object]
|
||||
def self.create_one_network(net_config)
|
||||
|
||||
# mandatory parameters:
|
||||
REQUIRED_ATTRS.each do |attr|
|
||||
raise "#{attr} required for importing nics operation!" if net_config[attr].nil?
|
||||
if net_config[attr].nil?
|
||||
raise "#{attr} required for importing nics operation!"
|
||||
end
|
||||
end
|
||||
|
||||
one_vn = VCenterDriver::VIHelper.new_one_item(OpenNebula::VirtualNetwork)
|
||||
one_vn = VCenterDriver::VIHelper
|
||||
.new_one_item(
|
||||
OpenNebula::VirtualNetwork
|
||||
)
|
||||
|
||||
done = []
|
||||
for i in 0..net_config[:refs].size-1
|
||||
(0..net_config[:refs].size-1).each do |i|
|
||||
cl_id = net_config[:one_ids][i]
|
||||
next if cl_id == -1 || done.include?(cl_id)
|
||||
|
||||
if done.empty?
|
||||
rc = one_vn.allocate(net_config[:one_object],cl_id.to_i)
|
||||
VCenterDriver::VIHelper.check_error(rc, "create network")
|
||||
rc = one_vn.allocate(net_config[:one_object], cl_id.to_i)
|
||||
VCenterDriver::VIHelper.check_error(rc, 'create network')
|
||||
one_vn.info
|
||||
else
|
||||
one_cluster = VCenterDriver::VIHelper.one_item(OpenNebula::Cluster, cl_id, false)
|
||||
one_cluster = VCenterDriver::VIHelper
|
||||
.one_item(
|
||||
OpenNebula::Cluster,
|
||||
cl_id,
|
||||
false
|
||||
)
|
||||
rc = one_cluster.addvnet(one_vn['ID'].to_i)
|
||||
VCenterDriver::VIHelper.check_error(rc,"addvnet to cluster")
|
||||
VCenterDriver::VIHelper
|
||||
.check_error(rc, 'addvnet to cluster')
|
||||
end
|
||||
done << cl_id
|
||||
end
|
||||
@ -266,11 +325,37 @@ class Network
|
||||
end
|
||||
end
|
||||
|
||||
# Get vSwitch of Standard PortGroup
|
||||
# If there is differents vSwitches returns the first.
|
||||
def self.virtual_switch(vc_pg)
|
||||
vswitch = []
|
||||
vc_hosts = vc_pg.host
|
||||
vc_hosts.each do |vc_host|
|
||||
host_pgs = vc_host
|
||||
.configManager
|
||||
.networkSystem
|
||||
.networkInfo
|
||||
.portgroup
|
||||
host_pgs.each do |pg|
|
||||
if vc_pg.name == pg.spec.name
|
||||
vswitch << pg.spec.vswitchName
|
||||
end
|
||||
end
|
||||
end
|
||||
vswitch.uniq!
|
||||
vswitch << 'Invalid configuration' if vswitch.length > 1
|
||||
vswitch.join(' / ')
|
||||
end
|
||||
|
||||
def self.remove_net_ref(network_id)
|
||||
one_vnet = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualNetwork, network_id)
|
||||
one_vnet = VCenterDriver::VIHelper
|
||||
.one_item(
|
||||
OpenNebula::VirtualNetwork,
|
||||
network_id
|
||||
)
|
||||
one_vnet.info
|
||||
one_vnet.delete_element("TEMPLATE/VCENTER_NET_REF")
|
||||
one_vnet.delete_element("TEMPLATE/VCENTER_INSTANCE_ID")
|
||||
one_vnet.delete_element('TEMPLATE/VCENTER_NET_REF')
|
||||
one_vnet.delete_element('TEMPLATE/VCENTER_INSTANCE_ID')
|
||||
tmp_str = one_vnet.template_str
|
||||
one_vnet.update(tmp_str)
|
||||
one_vnet.info
|
||||
@ -278,15 +363,18 @@ class Network
|
||||
|
||||
# This is never cached
|
||||
def self.new_from_ref(ref, vi_client)
|
||||
self.new(RbVmomi::VIM::Network.new(vi_client.vim, ref), vi_client)
|
||||
new(RbVmomi::VIM::Network.new(vi_client.vim, ref), vi_client)
|
||||
end
|
||||
|
||||
end # class Network
|
||||
end
|
||||
# class Network
|
||||
|
||||
class PortGroup < Network
|
||||
|
||||
def initialize(item, vi_client=nil)
|
||||
##########################################################################
|
||||
# Class PortGroup
|
||||
##########################################################################
|
||||
class PortGroup < Network
|
||||
|
||||
def initialize(item, vi_client = nil)
|
||||
check_item(item, RbVmomi::VIM::Network)
|
||||
|
||||
@vi_client = vi_client
|
||||
@ -307,12 +395,16 @@ class PortGroup < Network
|
||||
def network_type
|
||||
VCenterDriver::Network::NETWORK_TYPE_PG
|
||||
end
|
||||
end # class PortGroup
|
||||
|
||||
class DistributedPortGroup < Network
|
||||
end
|
||||
# class PortGroup
|
||||
|
||||
def initialize(item, vi_client=nil)
|
||||
##########################################################################
|
||||
# Class DistributedPortGroup
|
||||
##########################################################################
|
||||
class DistributedPortGroup < Network
|
||||
|
||||
def initialize(item, vi_client = nil)
|
||||
check_item(item, RbVmomi::VIM::DistributedVirtualPortgroup)
|
||||
|
||||
@vi_client = vi_client
|
||||
@ -323,7 +415,8 @@ class DistributedPortGroup < Network
|
||||
net_clusters = {}
|
||||
# should have to work
|
||||
# host_members =@item['host']
|
||||
host_members = self['config.distributedVirtualSwitch.summary.hostMember']
|
||||
host_members =
|
||||
self['config.distributedVirtualSwitch.summary.hostMember']
|
||||
host_members.each do |h|
|
||||
if !net_clusters.key?(h.parent._ref.to_s)
|
||||
net_clusters[h.parent._ref.to_s] = h.parent.name.to_s
|
||||
@ -335,12 +428,16 @@ class DistributedPortGroup < Network
|
||||
def network_type
|
||||
VCenterDriver::Network::NETWORK_TYPE_DPG
|
||||
end
|
||||
end # class DistributedPortGroup
|
||||
|
||||
class OpaqueNetwork < Network
|
||||
end
|
||||
# class DistributedPortGroup
|
||||
|
||||
def initialize(item, vi_client=nil)
|
||||
##########################################################################
|
||||
# Class OpaqueNetwork
|
||||
##########################################################################
|
||||
class OpaqueNetwork < Network
|
||||
|
||||
def initialize(item, vi_client = nil)
|
||||
check_item(item, RbVmomi::VIM::OpaqueNetwork)
|
||||
|
||||
@vi_client = vi_client
|
||||
@ -361,56 +458,86 @@ class OpaqueNetwork < Network
|
||||
def network_type
|
||||
VCenterDriver::Network::NETWORK_TYPE_NSXT
|
||||
end
|
||||
end # class OpaqueNetwork
|
||||
|
||||
class DistributedVirtualSwitch < Network
|
||||
end
|
||||
# class OpaqueNetwork
|
||||
|
||||
def initialize(item, vi_client=nil)
|
||||
##########################################################################
|
||||
# Class DistributedVirtualSwitch
|
||||
##########################################################################
|
||||
class DistributedVirtualSwitch < Network
|
||||
|
||||
def initialize(item, vi_client = nil)
|
||||
check_item(item, RbVmomi::VIM::VmwareDistributedVirtualSwitch)
|
||||
|
||||
@vi_client = vi_client
|
||||
@item = item
|
||||
end
|
||||
end # class DistributedVirtualSwitch
|
||||
|
||||
class NetImporter < VCenterDriver::VcImporter
|
||||
end
|
||||
# class DistributedVirtualSwitch
|
||||
|
||||
##########################################################################
|
||||
# Class NetImporter
|
||||
##########################################################################
|
||||
class NetImporter < VCenterDriver::VcImporter
|
||||
|
||||
def initialize(one_client, vi_client)
|
||||
super(one_client, vi_client)
|
||||
@one_class = OpenNebula::VirtualNetwork
|
||||
@defaults = { size: "255", type: "ether" }
|
||||
@defaults = { :size => '255', :type => 'ether' }
|
||||
end
|
||||
|
||||
def get_list(args = {})
|
||||
dc_folder = VCenterDriver::DatacenterFolder.new(@vi_client)
|
||||
|
||||
# OpenNebula's VirtualNetworkPool
|
||||
npool = VCenterDriver::VIHelper.one_pool(OpenNebula::VirtualNetworkPool, false)
|
||||
npool = VCenterDriver::VIHelper
|
||||
.one_pool(
|
||||
OpenNebula::VirtualNetworkPool,
|
||||
false
|
||||
)
|
||||
if npool.respond_to?(:message)
|
||||
raise "Could not get OpenNebula VirtualNetworkPool: #{npool.message}"
|
||||
raise "Could not get \
|
||||
OpenNebula VirtualNetworkPool: #{npool.message}"
|
||||
end
|
||||
|
||||
# Get OpenNebula's host pool
|
||||
hpool = VCenterDriver::VIHelper.one_pool(OpenNebula::HostPool, false)
|
||||
hpool = VCenterDriver::VIHelper
|
||||
.one_pool(
|
||||
OpenNebula::HostPool,
|
||||
false
|
||||
)
|
||||
if hpool.respond_to?(:message)
|
||||
raise "Could not get OpenNebula HostPool: #{hpool.message}"
|
||||
end
|
||||
|
||||
rs = dc_folder.get_unimported_networks(npool, @vi_client.vc_name,hpool, args)
|
||||
rs = dc_folder
|
||||
.get_unimported_networks(
|
||||
npool,
|
||||
@vi_client.vc_name,
|
||||
hpool,
|
||||
args
|
||||
)
|
||||
@list = rs
|
||||
end
|
||||
|
||||
def add_cluster(cid, eid)
|
||||
one_cluster = @info[:clusters][cid]
|
||||
raise "no cluster defined" unless one_cluster
|
||||
raise 'no cluster defined' unless one_cluster
|
||||
|
||||
rc = one_cluster.addvnet(eid)
|
||||
one_cluster.addvnet(eid)
|
||||
end
|
||||
|
||||
def remove_default(id)
|
||||
cid = 0
|
||||
@info[:clusters][cid] ||= VCenterDriver::VIHelper.one_item(OpenNebula::Cluster, cid.to_s, false)
|
||||
@info[:clusters][cid] ||=
|
||||
VCenterDriver::VIHelper
|
||||
.one_item(
|
||||
OpenNebula::Cluster,
|
||||
cid.to_s,
|
||||
false
|
||||
)
|
||||
@info[:clusters][cid].delvnet(id.to_i)
|
||||
end
|
||||
|
||||
@ -419,33 +546,43 @@ class NetImporter < VCenterDriver::VcImporter
|
||||
type = opts[:type].downcase
|
||||
|
||||
case type
|
||||
when "4", "ip4", "ip"
|
||||
str << "IP4\""
|
||||
opts[:ip] = "192.168.1.1" if opts[:ip].empty?
|
||||
when '4', 'ip4', 'ip'
|
||||
str << 'IP4"'
|
||||
opts[:ip] = '192.168.1.1' if opts[:ip].empty?
|
||||
str << ",IP=\"#{opts[:ip]}\""
|
||||
when 'ip6'
|
||||
str << "IP6\""
|
||||
str << ",GLOBAL_PREFIX=\"#{opts[:global_prefix]}\"" if opts[:global_prefix]
|
||||
str << ",ULA_PREFIX=\"#{opts[:ula_prefix]}\"" if opts[:ula_prefix]
|
||||
str << 'IP6"'
|
||||
if opts[:global_prefix]
|
||||
str << ",GLOBAL_PREFIX=\"#{opts[:global_prefix]}\""
|
||||
end
|
||||
if opts[:ula_prefix]
|
||||
str << ",ULA_PREFIX=\"#{opts[:ula_prefix]}\""
|
||||
end
|
||||
when 'ether', 'e'
|
||||
str << "ETHER\""
|
||||
str << 'ETHER"'
|
||||
when 'ip6_static'
|
||||
str << "IP6_STATIC\""
|
||||
str << 'IP6_STATIC"'
|
||||
str << ",IP6=\"#{opts[:ip6]}\"" if opts[:ip6]
|
||||
str << ",PREFIX_LENGTH=\"#{opts[:prefix_length]}\"" if opts[:prefix_length]
|
||||
if opts[:prefix_length]
|
||||
str << ",PREFIX_LENGTH=\"#{opts[:prefix_length]}\""
|
||||
end
|
||||
end
|
||||
|
||||
str << ",MAC=\"#{opts[:mac]}\"" if opts[:mac]
|
||||
str << ",SIZE = \"#{opts[:size]}\"]"
|
||||
|
||||
return str
|
||||
str
|
||||
end
|
||||
|
||||
def import(selected)
|
||||
opts = @info[selected[:ref]][:opts]
|
||||
|
||||
net = VCenterDriver::Network.new_from_ref(selected[:ref], @vi_client)
|
||||
vid = VCenterDriver::Network.retrieve_vlanid(net.item) if net
|
||||
net = VCenterDriver::Network
|
||||
.new_from_ref(selected[:ref], @vi_client)
|
||||
if net
|
||||
vid = VCenterDriver::Network
|
||||
.retrieve_vlanid(net.item)
|
||||
end
|
||||
|
||||
# If type is NSX we need to update values
|
||||
if selected[:type] == VCenterDriver::Network::NETWORK_TYPE_NSXV
|
||||
@ -478,15 +615,23 @@ class NetImporter < VCenterDriver::VcImporter
|
||||
end
|
||||
|
||||
selected[:one] << build_ar(opts)
|
||||
selected[:clusters][:one_ids] = opts["selected_clusters"].each.map(&:to_i) if opts["selected_clusters"]
|
||||
if opts['selected_clusters']
|
||||
selected[:clusters][:one_ids] =
|
||||
opts['selected_clusters']
|
||||
.each
|
||||
.map(&:to_i)
|
||||
end
|
||||
|
||||
res = {id: [], name: selected[:name]}
|
||||
create(selected[:one]) do |one_object, id|
|
||||
res = { :id => [], :name => selected[:name] }
|
||||
create(selected[:one]) do |_one_object, id|
|
||||
res[:id] << id
|
||||
add_clusters(id, selected[:clusters][:one_ids])
|
||||
end
|
||||
|
||||
return res
|
||||
res
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end # module VCenterDriver
|
||||
# module VCenterDriver
|
||||
|
@ -14,7 +14,9 @@
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
##############################################################################
|
||||
# Module VCenterDriver
|
||||
##############################################################################
|
||||
module VCenterDriver
|
||||
|
||||
ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)
|
||||
@ -35,14 +37,16 @@ module VCenterDriver
|
||||
unless defined?(HOOK_LOCATION)
|
||||
end
|
||||
|
||||
# class VCImporter
|
||||
##########################################################################
|
||||
# Class VcImporter
|
||||
##########################################################################
|
||||
class VcImporter
|
||||
|
||||
attr_accessor :list
|
||||
|
||||
#######################################################################
|
||||
######################################################################
|
||||
# Constructors
|
||||
#######################################################################
|
||||
######################################################################
|
||||
#
|
||||
#
|
||||
|
||||
@ -120,7 +124,6 @@ module VCenterDriver
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Importer return value
|
||||
#
|
||||
# @ return [Hash{:sucess =>[[]] , :error => {}}
|
||||
|
@ -16,71 +16,87 @@
|
||||
require 'openssl'
|
||||
require 'yaml'
|
||||
|
||||
##############################################################################
|
||||
# Module VCenterDriver
|
||||
##############################################################################
|
||||
module VCenterDriver
|
||||
|
||||
class VIClient
|
||||
##########################################################################
|
||||
# Class VIClient
|
||||
##########################################################################
|
||||
class VIClient
|
||||
|
||||
attr_accessor :vim
|
||||
attr_accessor :rp
|
||||
attr_accessor :vc_name
|
||||
attr_accessor :ccr_ref
|
||||
|
||||
def initialize(opts, host_id = -1)
|
||||
opts = {:insecure => true}.merge(opts)
|
||||
opts = { :insecure => true }.merge(opts)
|
||||
@host_id = host_id
|
||||
@vim = RbVmomi::VIM.connect(opts)
|
||||
@vc_name = opts[:host] if opts[:host]
|
||||
|
||||
# Get ccr and get rp
|
||||
@ccr_ref = opts.delete(:ccr)
|
||||
if @ccr_ref
|
||||
|
||||
return unless @ccr_ref
|
||||
|
||||
ccr = RbVmomi::VIM::ClusterComputeResource.new(@vim, @ccr_ref)
|
||||
|
||||
#Get ref for rp
|
||||
if ccr
|
||||
# Get ref for rp
|
||||
|
||||
return unless ccr
|
||||
|
||||
rp = opts.delete(:rp)
|
||||
if rp
|
||||
|
||||
return unless rp
|
||||
|
||||
rp_list = get_resource_pools(ccr)
|
||||
rp_ref = rp_list.select { |r| r[:name] == rp }.first[:ref] rescue nil
|
||||
rp_ref =
|
||||
rp_list
|
||||
.select {|r| r[:name] == rp }.first[:ref] rescue nil
|
||||
@rp = RbVmomi::VIM::ResourcePool(@vim, rp_ref) if rp_ref
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def rp_confined?
|
||||
!!@rp
|
||||
end
|
||||
|
||||
def get_host_credentials()
|
||||
raise "no host id defined!" if @host_id == -1
|
||||
def host_credentials
|
||||
raise 'no host id defined!' if @host_id == -1
|
||||
|
||||
host = OpenNebula::Host.new_with_id(@host_id, OpenNebula::Client.new)
|
||||
host =
|
||||
OpenNebula::Host
|
||||
.new_with_id(
|
||||
@host_id,
|
||||
OpenNebula::Client.new
|
||||
)
|
||||
rc = host.info
|
||||
if OpenNebula.is_error?(rc)
|
||||
raise "Could not get host info for ID: #{host_id} - #{rc.message}"
|
||||
raise "Could not get host info \
|
||||
for ID: #{host_id} - #{rc.message}"
|
||||
end
|
||||
|
||||
{pass: host["TEMPLATE/VCENTER_PASSWORD"],
|
||||
user: host["TEMPLATE/VCENTER_USER"],
|
||||
host: @vc_name }
|
||||
|
||||
{ :pass => host['TEMPLATE/VCENTER_PASSWORD'],
|
||||
:user => host['TEMPLATE/VCENTER_USER'],
|
||||
:host => @vc_name }
|
||||
end
|
||||
|
||||
def get_resource_pools(ccr, rp = nil, parent_prefix = "", rp_array = [])
|
||||
|
||||
current_rp = ""
|
||||
def get_resource_pools(ccr, rp = nil, parent_prefix = '', rp_array = [])
|
||||
current_rp = ''
|
||||
|
||||
if !rp
|
||||
rp = ccr.resourcePool
|
||||
else
|
||||
if !parent_prefix.empty?
|
||||
current_rp << parent_prefix
|
||||
current_rp << "/"
|
||||
current_rp << '/'
|
||||
end
|
||||
current_rp << rp.name
|
||||
end
|
||||
|
||||
if rp.resourcePool.size == 0
|
||||
if rp.resourcePool.empty?
|
||||
rp_info = {}
|
||||
rp_info[:name] = current_rp
|
||||
rp_info[:ref] = rp._ref
|
||||
@ -92,7 +108,7 @@ class VIClient
|
||||
rp_info = {}
|
||||
rp_info[:name] = current_rp
|
||||
rp_info[:ref] = rp._ref
|
||||
rp_array << rp_info if !current_rp.empty?
|
||||
rp_array << rp_info unless current_rp.empty?
|
||||
end
|
||||
|
||||
rp_array
|
||||
@ -103,22 +119,22 @@ class VIClient
|
||||
end
|
||||
|
||||
# @return RbVmomi::VIM::<type> objects
|
||||
def self.get_entities(folder, type, entities=[])
|
||||
def self.get_entities(folder, type, entities = [])
|
||||
if folder == []
|
||||
return nil
|
||||
return
|
||||
end
|
||||
|
||||
folder.childEntity.each do |child|
|
||||
the_name, junk = child.to_s.split('(')
|
||||
the_name, _junk = child.to_s.split('(')
|
||||
case the_name
|
||||
when "Folder"
|
||||
when 'Folder'
|
||||
get_entities(child, type, entities)
|
||||
when type
|
||||
entities.push(child)
|
||||
end
|
||||
end
|
||||
|
||||
return entities
|
||||
entities
|
||||
end
|
||||
|
||||
def self.new_from_host(host_id)
|
||||
@ -127,22 +143,23 @@ class VIClient
|
||||
host = OpenNebula::Host.new_with_id(host_id, client)
|
||||
rc = host.info(true)
|
||||
if OpenNebula.is_error?(rc)
|
||||
raise "Could not get host info for ID: #{host_id} - #{rc.message}"
|
||||
raise "Could not get host info for \
|
||||
ID: #{host_id} - #{rc.message}"
|
||||
end
|
||||
|
||||
connection = {
|
||||
:host => host["TEMPLATE/VCENTER_HOST"],
|
||||
:user => host["TEMPLATE/VCENTER_USER"],
|
||||
:rp => host["TEMPLATE/VCENTER_RESOURCE_POOL"],
|
||||
:ccr => host["TEMPLATE/VCENTER_CCR_REF"],
|
||||
:password => host["TEMPLATE/VCENTER_PASSWORD"]
|
||||
:host => host['TEMPLATE/VCENTER_HOST'],
|
||||
:user => host['TEMPLATE/VCENTER_USER'],
|
||||
:rp => host['TEMPLATE/VCENTER_RESOURCE_POOL'],
|
||||
:ccr => host['TEMPLATE/VCENTER_CCR_REF'],
|
||||
:password => host['TEMPLATE/VCENTER_PASSWORD']
|
||||
}
|
||||
|
||||
connection[:port] = host["TEMPLATE/VCENTER_PORT"] unless host["TEMPLATE/VCENTER_PORT"].nil?
|
||||
vc_port = host['TEMPLATE/VCENTER_PORT']
|
||||
connection[:port] = vc_port unless vc_port.nil?
|
||||
|
||||
self.new(connection, host_id)
|
||||
|
||||
rescue Exception => e
|
||||
new(connection, host_id)
|
||||
rescue StandardError => e
|
||||
raise e
|
||||
end
|
||||
end
|
||||
@ -150,12 +167,19 @@ class VIClient
|
||||
def self.new_from_datastore(datastore_id)
|
||||
begin
|
||||
client = OpenNebula::Client.new
|
||||
datastore = OpenNebula::Datastore.new_with_id(datastore_id, client)
|
||||
datastore =
|
||||
OpenNebula::Datastore
|
||||
.new_with_id(
|
||||
datastore_id,
|
||||
client
|
||||
)
|
||||
rc = datastore.info
|
||||
if OpenNebula.is_error?(rc)
|
||||
raise "Could not get datastore info for ID: #{datastore_id} - #{rc.message}"
|
||||
raise "Could not get datastore info \
|
||||
for ID: #{datastore_id} - #{rc.message}"
|
||||
end
|
||||
vcenter_id = datastore["TEMPLATE/VCENTER_INSTANCE_ID"]
|
||||
|
||||
vcenter_id = datastore['TEMPLATE/VCENTER_INSTANCE_ID']
|
||||
|
||||
host_pool = OpenNebula::HostPool.new(client)
|
||||
rc = host_pool.info
|
||||
@ -163,40 +187,46 @@ class VIClient
|
||||
raise "Could not get hosts information - #{rc.message}"
|
||||
end
|
||||
|
||||
user = ""
|
||||
password = ""
|
||||
user = ''
|
||||
password = ''
|
||||
port = 0
|
||||
host_pool.each do |host|
|
||||
if host["TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_id
|
||||
host_decrypted = OpenNebula::Host.new_with_id(host["ID"], client)
|
||||
vc_instance_id = host['TEMPLATE/VCENTER_INSTANCE_ID']
|
||||
next unless vc_instance_id == vcenter_id
|
||||
|
||||
host_decrypted =
|
||||
OpenNebula::Host
|
||||
.new_with_id(
|
||||
host['ID'],
|
||||
client
|
||||
)
|
||||
host_decrypted.info(true)
|
||||
user = host_decrypted["TEMPLATE/VCENTER_USER"]
|
||||
password = host_decrypted["TEMPLATE/VCENTER_PASSWORD"]
|
||||
port = host_decrypted["TEMPLATE/VCENTER_PORT"]
|
||||
user = host_decrypted['TEMPLATE/VCENTER_USER']
|
||||
password = host_decrypted['TEMPLATE/VCENTER_PASSWORD']
|
||||
port = host_decrypted['TEMPLATE/VCENTER_PORT']
|
||||
end
|
||||
end
|
||||
if password.empty? or user.empty?
|
||||
raise "Error getting credentials for datastore #{datastore_id}"
|
||||
if password.empty? || user.empty?
|
||||
raise "Error getting \
|
||||
credentials for datastore #{datastore_id}"
|
||||
end
|
||||
|
||||
connection = {
|
||||
:host => datastore["TEMPLATE/VCENTER_HOST"],
|
||||
:host => datastore['TEMPLATE/VCENTER_HOST'],
|
||||
:user => user,
|
||||
:password => password
|
||||
}
|
||||
|
||||
connection[:port] = port unless port.nil?
|
||||
|
||||
self.new(connection)
|
||||
|
||||
rescue Exception => e
|
||||
new(connection)
|
||||
rescue StandardError => e
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
def self.decrypt(msg, token)
|
||||
begin
|
||||
cipher = OpenSSL::Cipher.new("aes-256-cbc")
|
||||
cipher = OpenSSL::Cipher.new('aes-256-cbc')
|
||||
|
||||
cipher.decrypt
|
||||
|
||||
@ -204,10 +234,10 @@ class VIClient
|
||||
# automatically truncated)
|
||||
cipher.key = token[0..31]
|
||||
|
||||
msg = cipher.update(Base64::decode64(msg))
|
||||
msg = cipher.update(Base64.decode64(msg))
|
||||
msg << cipher.final
|
||||
rescue
|
||||
raise "Error decrypting secret."
|
||||
rescue StandardError
|
||||
raise 'Error decrypting secret.'
|
||||
end
|
||||
end
|
||||
|
||||
@ -218,7 +248,7 @@ class VIClient
|
||||
$stderr.reopen File.new('/dev/null', 'w')
|
||||
$stdout.reopen File.new('/dev/null', 'w')
|
||||
retval = yield
|
||||
rescue Exception => e
|
||||
rescue StandardError => e
|
||||
$stdout.reopen orig_stdout
|
||||
$stderr.reopen orig_stderr
|
||||
raise e
|
||||
@ -234,7 +264,7 @@ class VIClient
|
||||
orig_stderr = $stderr.clone
|
||||
$stderr.reopen File.new('/dev/null', 'w')
|
||||
retval = yield
|
||||
rescue Exception => e
|
||||
rescue StandardError => e
|
||||
$stderr.reopen orig_stderr
|
||||
raise e
|
||||
ensure
|
||||
@ -242,6 +272,8 @@ class VIClient
|
||||
end
|
||||
retval
|
||||
end
|
||||
end
|
||||
|
||||
end # module VCenterDriver
|
||||
end
|
||||
|
||||
end
|
||||
# module VCenterDriver
|
||||
|
@ -14,21 +14,27 @@
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
########################################################################
|
||||
# Module VCenterDriver
|
||||
########################################################################
|
||||
module VCenterDriver
|
||||
|
||||
class VIHelper
|
||||
########################################################################
|
||||
# Class VIHelper
|
||||
########################################################################
|
||||
class VIHelper
|
||||
|
||||
ETC_LOCATION = "/etc/one/" if !defined?(ETC_LOCATION)
|
||||
ETC_LOCATION = '/etc/one/' unless defined?(ETC_LOCATION)
|
||||
VCENTER_DRIVER_DEFAULT = "#{ETC_LOCATION}/vcenter_driver.default"
|
||||
VM_PREFIX_DEFAULT = "one-$i-"
|
||||
VM_PREFIX_DEFAULT = 'one-$i-'
|
||||
|
||||
def self.client
|
||||
@@client ||= OpenNebula::Client.new
|
||||
@@client ||= OpenNebula::Client.new # rubocop:disable Style/ClassVars
|
||||
end
|
||||
|
||||
def self.return_if_error(rc, item, exit_if_fail)
|
||||
if OpenNebula::is_error?(rc)
|
||||
raise rc.message if !exit_if_fail
|
||||
if OpenNebula.is_error?(rc)
|
||||
raise rc.message unless exit_if_fail
|
||||
|
||||
STDERR.puts rc.message
|
||||
exit 1
|
||||
@ -38,28 +44,30 @@ class VIHelper
|
||||
end
|
||||
|
||||
require 'scripts_common'
|
||||
def self.check_error(rc, message, _exit=false)
|
||||
if OpenNebula::is_error?(rc)
|
||||
OpenNebula::error_message("\n Error #{message}: #{rc.message}\n")
|
||||
exit 1 if (_exit)
|
||||
def self.check_error(rc, message, exit_condition = false)
|
||||
return unless OpenNebula.is_error?(rc)
|
||||
|
||||
OpenNebula.error_message("\n Error #{message}: #{rc.message}\n")
|
||||
exit 1 if exit_condition
|
||||
|
||||
raise rc.message
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_cluster_id(clusters)
|
||||
clusters.each do |id|
|
||||
return id unless id == -1
|
||||
end
|
||||
return -1
|
||||
-1
|
||||
end
|
||||
|
||||
def self.one_managed?(object)
|
||||
if object.class.ancestors.include?(OpenNebula::XMLElement)
|
||||
managed = object["TEMPLATE/OPENNEBULA_MANAGED"] || object["USER_TEMPLATE/OPENNEBULA_MANAGED"]
|
||||
return managed != "NO"
|
||||
managed =
|
||||
object['TEMPLATE/OPENNEBULA_MANAGED'] ||
|
||||
object['USER_TEMPLATE/OPENNEBULA_MANAGED']
|
||||
return managed != 'NO'
|
||||
end
|
||||
return false
|
||||
false
|
||||
end
|
||||
|
||||
def self.check_opts(opts, att_list)
|
||||
@ -75,8 +83,7 @@ class VIHelper
|
||||
end
|
||||
|
||||
def self.new_one_item(the_class)
|
||||
item = the_class.new(the_class.build_xml, client)
|
||||
return item
|
||||
the_class.new(the_class.build_xml, client)
|
||||
end
|
||||
|
||||
def self.one_pool(the_class, exit_if_fail = true)
|
||||
@ -85,7 +92,7 @@ class VIHelper
|
||||
rc = nil
|
||||
begin
|
||||
rc = item.info_all
|
||||
rescue
|
||||
rescue StandardError
|
||||
rc = item.info
|
||||
end
|
||||
|
||||
@ -94,36 +101,34 @@ class VIHelper
|
||||
|
||||
def self.find_by_name(the_class, name, pool = nil, raise_if_fail = true)
|
||||
pool = one_pool(the_class, raise_if_fail) if pool.nil?
|
||||
element = pool.find{|e| e['NAME'] == "#{name}" }
|
||||
element = pool.find {|e| e['NAME'] == name.to_s }
|
||||
if element.nil? && raise_if_fail
|
||||
raise "Could not find element '#{name}' in pool '#{the_class}'"
|
||||
else
|
||||
element
|
||||
end
|
||||
|
||||
element
|
||||
end
|
||||
|
||||
def self.generate_name(opts, nbytes)
|
||||
|
||||
return opts[:name] if nbytes <= 0
|
||||
|
||||
@sha256 ||= Digest::SHA256.new
|
||||
chain = opts[:key]
|
||||
hash = @sha256.hexdigest(chain)[0..nbytes-1]
|
||||
|
||||
return "#{opts[:name]}-#{hash}"
|
||||
"#{opts[:name]}-#{hash}"
|
||||
end
|
||||
|
||||
def self.one_name(the_class, name, key, pool = nil, bytes = 0)
|
||||
|
||||
# Remove \u007F character that comes from vcenter
|
||||
name = name.tr("\u007F", "")
|
||||
name = name.tr("\u007F", '')
|
||||
pool = one_pool(the_class) if pool.nil?
|
||||
|
||||
import_name = generate_name({name: name, key: key}, bytes)
|
||||
import_name = generate_name({ :name => name, :key => key }, bytes)
|
||||
|
||||
begin
|
||||
find_by_name(the_class, import_name, pool)
|
||||
rescue RuntimeError => e
|
||||
rescue StandardError
|
||||
return import_name
|
||||
end
|
||||
|
||||
@ -133,10 +138,10 @@ class VIHelper
|
||||
def self.get_ref_key(element, attribute, vcenter_uuid = nil)
|
||||
key = element[attribute]
|
||||
|
||||
return nil if key.nil?
|
||||
return if key.nil?
|
||||
|
||||
tvid = element["TEMPLATE/VCENTER_INSTANCE_ID"]
|
||||
uvid = element["USER_TEMPLATE/VCENTER_INSTANCE_ID"]
|
||||
tvid = element['TEMPLATE/VCENTER_INSTANCE_ID']
|
||||
uvid = element['USER_TEMPLATE/VCENTER_INSTANCE_ID']
|
||||
|
||||
if tvid
|
||||
key += tvid
|
||||
@ -146,13 +151,13 @@ class VIHelper
|
||||
key += vcenter_uuid
|
||||
end
|
||||
|
||||
return key
|
||||
key
|
||||
end
|
||||
|
||||
def self.create_ref_hash(attribute, pool, vcenter_uuid = nil)
|
||||
hash = {}
|
||||
|
||||
pool.each_element(Proc.new do |e|
|
||||
pool.each_element(proc do |e|
|
||||
refkey = get_ref_key(e, attribute, vcenter_uuid)
|
||||
hash[refkey] = e
|
||||
end)
|
||||
@ -165,26 +170,32 @@ class VIHelper
|
||||
end
|
||||
|
||||
def self.add_ref_hash(attr, one_object)
|
||||
raise "cache is empty!" unless @ref_hash
|
||||
raise 'cache is empty!' unless @ref_hash
|
||||
|
||||
refkey = get_ref_key(one_object, attr)
|
||||
|
||||
if @ref_hash[attr]
|
||||
return unless @ref_hash[attr]
|
||||
|
||||
@ref_hash[attr][refkey] = one_object
|
||||
end
|
||||
end
|
||||
|
||||
def self.remove_ref_hash(attr, one_object)
|
||||
raise "cache is empty!" unless @ref_hash
|
||||
raise 'cache is empty!' unless @ref_hash
|
||||
|
||||
refkey = get_ref_key(one_object, attr)
|
||||
|
||||
if @ref_hash[attr]
|
||||
return unless @ref_hash[attr]
|
||||
|
||||
@ref_hash[attr].delete(refkey)
|
||||
end
|
||||
end
|
||||
|
||||
def self.find_by_ref(the_class, attribute, ref, vcenter_uuid, pool = nil)
|
||||
def self.find_by_ref(
|
||||
the_class,
|
||||
attribute,
|
||||
ref,
|
||||
vcenter_uuid,
|
||||
pool = nil
|
||||
)
|
||||
pool = one_pool(the_class, false) if pool.nil?
|
||||
@ref_hash ||= {}
|
||||
|
||||
@ -194,28 +205,26 @@ class VIHelper
|
||||
vcenter_uuid)
|
||||
end
|
||||
|
||||
refkey = ""
|
||||
refkey = ''
|
||||
refkey = ref if ref
|
||||
refkey += vcenter_uuid if vcenter_uuid
|
||||
|
||||
return @ref_hash[attribute][refkey]
|
||||
@ref_hash[attribute][refkey]
|
||||
end
|
||||
|
||||
def self.find_image_by(att, the_class, path, ds_id, pool = nil)
|
||||
pool = one_pool(the_class, false) if pool.nil?
|
||||
element = pool.find{|e|
|
||||
pool.find do |e|
|
||||
e[att] == path &&
|
||||
e["DATASTORE_ID"] == ds_id}
|
||||
return element
|
||||
e['DATASTORE_ID'] == ds_id
|
||||
end
|
||||
end
|
||||
|
||||
def self.find_persistent_image_by_source(source, pool)
|
||||
element = pool.find{|e|
|
||||
e["SOURCE"] == source &&
|
||||
e["PERSISTENT"] == "1"
|
||||
}
|
||||
|
||||
return element
|
||||
pool.find do |e|
|
||||
e['SOURCE'] == source &&
|
||||
e['PERSISTENT'] == '1'
|
||||
end
|
||||
end
|
||||
|
||||
def self.find_vcenter_vm_by_name(one_vm, host, vi_client)
|
||||
@ -223,49 +232,61 @@ class VIHelper
|
||||
# Let's build the VM name
|
||||
vm_prefix = host['TEMPLATE/VM_PREFIX']
|
||||
vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil? || vm_prefix.empty?
|
||||
vm_prefix.gsub!("$i", one_vm['ID'])
|
||||
vm_prefix.gsub!('$i', one_vm['ID'])
|
||||
vm_name = vm_prefix + one_vm['NAME']
|
||||
|
||||
# We have no DEPLOY_ID, the VM has never been deployed
|
||||
# let's use a view to try to find the VM from the root folder
|
||||
view = vi_client.vim.serviceContent.viewManager.CreateContainerView({
|
||||
container: vi_client.vim.rootFolder,
|
||||
type: ['VirtualMachine'],
|
||||
recursive: true
|
||||
})
|
||||
view = vi_client
|
||||
.vim
|
||||
.serviceContent
|
||||
.viewManager
|
||||
.CreateContainerView(
|
||||
{
|
||||
:container => vi_client.vim.rootFolder,
|
||||
:type => ['VirtualMachine'],
|
||||
:recursive => true
|
||||
}
|
||||
)
|
||||
|
||||
vcenter_vm = view.view.find{ |v| v.name == vm_name } if !!view.view && !view.view.empty?
|
||||
if !view.view.nil? && !view.view.empty?
|
||||
vcenter_vm = view
|
||||
.view
|
||||
.find {|v| v.name == vm_name }
|
||||
end
|
||||
|
||||
view.DestroyView # Destroy the view
|
||||
|
||||
return vcenter_vm
|
||||
vcenter_vm
|
||||
end
|
||||
|
||||
def self.get_default(xpath)
|
||||
begin
|
||||
xml = OpenNebula::XMLElement.new
|
||||
xml.initialize_xml(File.read(VCENTER_DRIVER_DEFAULT), 'VCENTER')
|
||||
return xml[xpath]
|
||||
rescue
|
||||
return nil
|
||||
xml[xpath]
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_location(item)
|
||||
folders = []
|
||||
while !item.instance_of? RbVmomi::VIM::Datacenter
|
||||
until item.instance_of? RbVmomi::VIM::Datacenter
|
||||
item = item.parent
|
||||
if !item.instance_of? RbVmomi::VIM::Datacenter
|
||||
folders << item.name if item.name != "host"
|
||||
folders << item.name if item.name != 'host'
|
||||
end
|
||||
raise "Could not find the location" if item.nil?
|
||||
raise 'Could not find the location' if item.nil?
|
||||
end
|
||||
location = folders.reverse.join("/")
|
||||
location = "/" if location.empty?
|
||||
location = folders.reverse.join('/')
|
||||
location = '/' if location.empty?
|
||||
|
||||
return location
|
||||
location
|
||||
end
|
||||
|
||||
end # class VIHelper
|
||||
end
|
||||
# class VIHelper
|
||||
|
||||
end # module VCenterDriver
|
||||
end
|
||||
# module VCenterDriver
|
||||
|
@ -198,7 +198,7 @@ module VCenterDriver
|
||||
############################################################################
|
||||
|
||||
# @return Boolean whether the VM exists in vCenter
|
||||
def is_new?
|
||||
def new?
|
||||
one_item["DEPLOY_ID"].empty?
|
||||
end
|
||||
|
||||
@ -237,7 +237,7 @@ module VCenterDriver
|
||||
@vi_client.vim.serviceContent.about.instanceUuid
|
||||
end
|
||||
|
||||
def get_disk_keys
|
||||
def disk_keys_get
|
||||
unmanaged_keys = {}
|
||||
@item.config.extraConfig.each do |val|
|
||||
u = val[:key].include?("opennebula.disk")
|
||||
@ -672,7 +672,7 @@ module VCenterDriver
|
||||
end
|
||||
|
||||
# Queries to OpenNebula the machine nics xml representation
|
||||
def get_one_nics
|
||||
def one_nics_get
|
||||
one_item.info if one_item.instance_of?(OpenNebula::VirtualMachine)
|
||||
one_item.retrieve_xmlelements("TEMPLATE/NIC")
|
||||
end
|
||||
@ -682,7 +682,7 @@ module VCenterDriver
|
||||
# @param one_disk [XMLelement] The OpenNebula object representation of the disk
|
||||
# @param keys [Hash (String => String)] Hashmap with the unmanaged keys
|
||||
# @param vc_disks [Array (vcenter_disks)] Array of the machine real disks
|
||||
# see get_vcenter_disks method
|
||||
# see vcenter_disks_get method
|
||||
#
|
||||
# @return [vCenter_disk] the proper disk
|
||||
def query_disk(one_disk, keys, vc_disks)
|
||||
@ -738,8 +738,8 @@ module VCenterDriver
|
||||
def info_nics
|
||||
@nics = {macs: {}}
|
||||
|
||||
vc_nics = get_vcenter_nics
|
||||
one_nics = get_one_nics
|
||||
vc_nics = vcenter_nics_get
|
||||
one_nics = one_nics_get
|
||||
|
||||
one_nics.each do |one_nic|
|
||||
index = one_nic["NIC_ID"]
|
||||
@ -785,8 +785,8 @@ module VCenterDriver
|
||||
def info_disks
|
||||
@disks = {}
|
||||
|
||||
keys = get_disk_keys
|
||||
vc_disks = get_vcenter_disks
|
||||
keys = disk_keys_get
|
||||
vc_disks = vcenter_disks_get
|
||||
one_disks = get_one_disks
|
||||
|
||||
one_disks.each do |one_disk|
|
||||
@ -834,7 +834,7 @@ module VCenterDriver
|
||||
|
||||
raise "nic #{index} not found" unless one_nic
|
||||
|
||||
vc_nics = get_vcenter_nics
|
||||
vc_nics = vcenter_nics_get
|
||||
vc_nic = query_nic(mac, vc_nics)
|
||||
|
||||
if vc_nic
|
||||
@ -858,8 +858,8 @@ module VCenterDriver
|
||||
|
||||
raise "disk #{index} not found" unless one_disk
|
||||
|
||||
keys = opts[:keys].nil? ? get_disk_keys : opts[:keys]
|
||||
vc_disks = opts[:disks].nil? ? get_vcenter_disks : opts[:disks]
|
||||
keys = opts[:keys].nil? ? disk_keys_get : opts[:keys]
|
||||
vc_disks = opts[:disks].nil? ? vcenter_disks_get : opts[:disks]
|
||||
vc_disk = query_disk(one_disk, keys, vc_disks)
|
||||
|
||||
if vc_disk
|
||||
@ -892,7 +892,7 @@ module VCenterDriver
|
||||
# for unmanaged nics
|
||||
begin
|
||||
if !unmanaged_nics.empty?
|
||||
nics = get_vcenter_nics
|
||||
nics = vcenter_nics_get
|
||||
|
||||
select_net =->(ref){
|
||||
device = nil
|
||||
@ -1042,16 +1042,16 @@ module VCenterDriver
|
||||
key_prefix = managed ? "opennebula.mdisk" : "opennebula.disk"
|
||||
|
||||
# Get vcenter VM disks to know real path of cloned disk
|
||||
vcenter_disks = get_vcenter_disks
|
||||
vcenter_disks = vcenter_disks_get
|
||||
|
||||
# Create an array with the paths of the disks in vcenter template
|
||||
if !template_ref.nil?
|
||||
template = VCenterDriver::Template.new_from_ref(template_ref, vi_client)
|
||||
template_disks = template.get_vcenter_disks
|
||||
template_disks = template.vcenter_disks_get
|
||||
else
|
||||
# If we are dealing with a Wild VM, we simply use
|
||||
# what is available in the vCenter VM
|
||||
template_disks = get_vcenter_disks
|
||||
template_disks = vcenter_disks_get
|
||||
end
|
||||
template_disks_vector = []
|
||||
template_disks.each do |d|
|
||||
@ -1386,7 +1386,7 @@ module VCenterDriver
|
||||
# start in one, we want the next avaliable id
|
||||
card_num = 1
|
||||
@item["config.hardware.device"].each do |dv|
|
||||
card_num += 1 if is_nic?(dv)
|
||||
card_num += 1 if VCenterDriver::Network.nic?(dv)
|
||||
end
|
||||
|
||||
nic_card = Nic.nic_model_class(model)
|
||||
@ -1413,7 +1413,7 @@ module VCenterDriver
|
||||
|
||||
# grab the last unitNumber to ensure the nic to be added at the end
|
||||
if !unumber
|
||||
@unic = @unic || get_vcenter_nics.map{|d| d.unitNumber}.max || 0
|
||||
@unic = @unic || vcenter_nics_get.map{|d| d.unitNumber}.max || 0
|
||||
unumber = @unic += 1
|
||||
else
|
||||
@unic = unumber
|
||||
@ -1487,7 +1487,7 @@ module VCenterDriver
|
||||
card_num = 1 # start in one, we want the next available id
|
||||
|
||||
@item["config.hardware.device"].each do |dv|
|
||||
card_num += 1 if is_nic?(dv)
|
||||
card_num += 1 if VCenterDriver::Network.nic?(dv)
|
||||
end
|
||||
|
||||
nic_card = Nic.nic_model_class(model)
|
||||
@ -1644,7 +1644,7 @@ module VCenterDriver
|
||||
def detach_disks_specs()
|
||||
detach_disk_array = []
|
||||
extra_config = []
|
||||
keys = get_disk_keys.invert
|
||||
keys = disk_keys_get.invert
|
||||
ipool = VCenterDriver::VIHelper.one_pool(OpenNebula::ImagePool)
|
||||
disks_each(:detached?) do |d|
|
||||
key = d.key.to_s
|
||||
@ -1675,7 +1675,7 @@ module VCenterDriver
|
||||
extraconfig_new = []
|
||||
# vCenter mob disks
|
||||
vc_disks = @item["config.hardware.device"].select do |vc_device|
|
||||
is_disk?(vc_device)
|
||||
disk?(vc_device)
|
||||
end
|
||||
return unless vc_disks
|
||||
# For each changed disk, compare with vcenter mob disk
|
||||
@ -1740,8 +1740,8 @@ module VCenterDriver
|
||||
device_change = []
|
||||
|
||||
# Extract unmanaged_keys
|
||||
unmanaged_keys = get_disk_keys
|
||||
vc_disks = get_vcenter_disks
|
||||
unmanaged_keys = disk_keys_get
|
||||
vc_disks = vcenter_disks_get
|
||||
|
||||
# Check if we're dealing with a StoragePod SYSTEM ds
|
||||
storpod = disk["VCENTER_DS_REF"].start_with?('group-')
|
||||
@ -1832,7 +1832,7 @@ module VCenterDriver
|
||||
end
|
||||
|
||||
vm.config.hardware.device.each do |disk|
|
||||
if is_disk_or_cdrom?(disk)
|
||||
if disk_or_cdrom?(disk)
|
||||
# Let's try to find if disks is persistent
|
||||
source_unescaped = disk.backing.fileName.sub(/^\[(.*?)\] /, "") rescue next
|
||||
source = VCenterDriver::FileHelper.escape_path(source_unescaped)
|
||||
@ -1922,10 +1922,10 @@ module VCenterDriver
|
||||
@used_keys = [] unless @used_keys
|
||||
|
||||
if type == "CDROM"
|
||||
bound = "is_cdrom?"
|
||||
bound = "cdrom?"
|
||||
key = 3000
|
||||
else
|
||||
bound = "is_disk?"
|
||||
bound = "disk?"
|
||||
key = 2000
|
||||
end
|
||||
|
||||
@ -2395,7 +2395,7 @@ module VCenterDriver
|
||||
############################################################################
|
||||
|
||||
def shutdown
|
||||
if !is_powered_off?
|
||||
if !powered_off?
|
||||
begin
|
||||
if vm_tools?
|
||||
@item.ShutdownGuest
|
||||
@ -2407,7 +2407,7 @@ module VCenterDriver
|
||||
raise e.message if error != 'InvalidPowerState'
|
||||
end
|
||||
timeout = CONFIG[:vm_poweron_wait_default]
|
||||
wait_timeout(:is_powered_off?, timeout)
|
||||
wait_timeout(:powered_off?, timeout)
|
||||
end
|
||||
end
|
||||
|
||||
@ -2442,14 +2442,14 @@ module VCenterDriver
|
||||
set_running(true, true) if set_running
|
||||
|
||||
timeout = CONFIG[:vm_poweron_wait_default]
|
||||
wait_timeout(:is_powered_on?, timeout)
|
||||
wait_timeout(:powered_on?, timeout)
|
||||
end
|
||||
|
||||
def is_powered_on?
|
||||
def powered_on?
|
||||
return @item.runtime.powerState == "poweredOn"
|
||||
end
|
||||
|
||||
def is_powered_off?
|
||||
def powered_off?
|
||||
return @item.runtime.powerState == "poweredOff"
|
||||
end
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -92,7 +92,7 @@ begin
|
||||
end
|
||||
end
|
||||
|
||||
if vm.is_powered_off?
|
||||
if vm.powered_off?
|
||||
vm.sync(deploy)
|
||||
# Only mark the VM as running if we are deploying it for the first time
|
||||
set_running = !deploy_id_valid?(deploy_id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user