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