1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-28 17:57:22 +03:00

F #4913: Initial support for importing disks from vcenter templates

This commit is contained in:
mcabrerizo 2017-03-03 13:38:10 +01:00
parent 862d54fb16
commit 918bce7733
4 changed files with 167 additions and 10 deletions

View File

@ -281,10 +281,7 @@ class DatacenterFolder
end
rp = rp_cache[template_ccr.name.to_s]
object = template.to_one_template(template_name,
template_ref,
template_ccr._ref,
cluster_name,
object = template.to_one_template(template,
ds,
ds_list,
default_ds,

View File

@ -68,6 +68,44 @@ class Storage
end
end
def self.get_image_import_template(ds_name, image_path, image_type, ipool)
one_image = ""
# Remove ds info from path
image_path.sub!(/^\[#{ds_name}\] /, "")
# Get image name
file_name = File.basename(image_path).reverse.sub("kdmv.","").reverse
image_name = "#{file_name} - #{ds_name}"
#Chek if the image has already been imported
if VCenterDriver::VIHelper.find_by_name(OpenNebula::ImagePool,
image_name,
ipool,
false).nil?
#Set template
one_image << "NAME=\"#{image_name}\"\n"
one_image << "PATH=\"vcenter://#{image_path}\"\n"
one_image << "TYPE=\"#{image_type}\"\n"
one_image << "PERSISTENT=\"NO\"\n"
one_image << "OPENNEBULA_MANAGED=\"YES\"\n"
end
return one_image
end
def self.get_one_image_ds_by_ref_and_ccr(ref, ccr_ref, vcenter_uuid, pool = nil)
pool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool, false) if pool.nil?
element = pool.select{|e|
e["TEMPLATE/TYPE"] == "IMAGE_DS" &&
e["TEMPLATE/VCENTER_DS_REF"] == ref &&
e["TEMPLATE/VCENTER_CCR_REF"] == ccr_ref &&
e["TEMPLATE/VCENTER_INSTANCE_ID"] == vcenter_uuid}.first rescue nil
return element
end
def monitor
summary = self['summary']

View File

@ -74,6 +74,13 @@ def self.import_templates(con_ops, options)
STDOUT.print "done!\n"
# Create OpenNebula pools
dpool = VCenterDriver::VIHelper.one_pool(OpenNebula::DatastorePool)
ipool = VCenterDriver::VIHelper.one_pool(OpenNebula::ImagePool)
# Get vcenter intance uuid as moref is unique for each vcenter
vc_uuid = vi_client.vim.serviceContent.about.instanceUuid
rs.each {|dc, tmps|
STDOUT.print "\nDo you want to process datacenter #{dc}"\
" (y/[n])? "
@ -95,7 +102,23 @@ def self.import_templates(con_ops, options)
next if STDIN.gets.strip.downcase != 'y'
=begin
## Add existing disks to template (OPENNEBULA_MANAGED)
template = t[:template]
error, template_disks = template.import_vcenter_disks(vc_uuid,
dpool,
ipool)
if error.empty?
t[:one] << template_disks
else
STDOUT.puts error
next
end
=end
# Datastore placement
ds_input = ""
STDOUT.print "\n This template is currently set to be "\
@ -246,7 +269,7 @@ def self.import_templates(con_ops, options)
end
list_str = "\n [Index] Resource pool :"\
"\n #{dashes}\n"
"\n #{dashes}\n"
STDOUT.print list_str

View File

@ -978,13 +978,104 @@ class VirtualMachine
end
end
# Checks if a RbVmomi::VIM::VirtualDevice is a disk
def get_vcenter_disks
disks = []
self["config.hardware.device"].each do |device|
disk = {}
if is_disk_or_iso?(device)
disk[:datastore] = device.backing.datastore
disk[:path] = device.backing.fileName
disk[:type] = is_disk?(device) ? "OS" : "CDROM"
disks << disk
end
end
return disks
end
def import_vcenter_disks(vc_uuid, dpool, ipool)
disk_info = ""
error = ""
ccr_ref = self["runtime.host.parent._ref"]
#Get disks and info required
vc_disks = get_vcenter_disks
# Track allocated images
allocated_images = []
vc_disks.each do |disk|
datastore_found = VCenterDriver::Storage.get_one_image_ds_by_ref_and_ccr(disk[:datastore]._ref,
ccr_ref,
vc_uuid,
dpool)
if datastore_found.nil?
error = " Error datastore #{disk[:datastore].name}: has to be imported first as an image datastore!\n"
#Rollback delete disk images
allocated_images.each do |i|
i.delete
end
break
end
image_template = VCenterDriver::Datastore.get_image_import_template(disk[:datastore].name,
disk[:path],
disk[:type], ipool)
if !image_template.empty?
# Then the image is created
one_i = VCenterDriver::VIHelper.new_one_item(OpenNebula::Image)
allocated_images << one_i
rc = one_i.allocate(image_template, datastore_found['ID'].to_i)
if ::OpenNebula.is_error?(rc)
error = " Error creating disk from template: #{rc.message}. Cannot import the template\n"
#Rollback delete disk images
allocated_images.each do |i|
i.delete
end
break
end
#Add info for One template
one_i.info
disk_info << "DISK=[\n"
disk_info << "IMAGE=\"#{one_i["NAME"]}\",\n"
disk_info << "IMAGE_UNAME=\"#{one_i["UNAME"]}\"\n"
disk_info << "]\n"
end
end
return error, disk_info
end
# Checks if a RbVmomi::VIM::VirtualDevice is a disk or a cdrom
def is_disk_or_cdrom?(device)
is_disk = !(device.class.ancestors.index(RbVmomi::VIM::VirtualDisk)).nil?
is_cdrom = !(device.class.ancestors.index(RbVmomi::VIM::VirtualCdrom)).nil?
is_disk || is_cdrom
end
# Checks if a RbVmomi::VIM::VirtualDevice is a disk or an iso file
def is_disk_or_iso?(device)
is_disk = !(device.class.ancestors.index(RbVmomi::VIM::VirtualDisk)).nil?
is_iso = device.backing.is_a? RbVmomi::VIM::VirtualCdromIsoBackingInfo
is_disk || is_iso
end
# Checks if a RbVmomi::VIM::VirtualDevice is a disk
def is_disk?(device)
!(device.class.ancestors.index(RbVmomi::VIM::VirtualDisk)).nil?
end
def find_free_controller(position=0)
free_scsi_controllers = []
available_controller = nil
@ -1298,11 +1389,18 @@ class VirtualMachine
return str
end
def to_one_template(template_name, template_ref, template_ccr, cluster_name,
ds, ds_list, default_ds, rp, rp_list, vcenter_uuid)
def to_one_template(template, ds, ds_list, default_ds,
rp, rp_list, vcenter_uuid)
template_name = template['name']
template_ref = template['_ref']
template_ccr = template['runtime.host.parent']
cluster_name = template['runtime.host.parent.name']
one_tmp = {}
one_tmp[:name] = "#{template_name} - #{cluster_name}"
one_tmp[:vcenter_ccr_ref] = template_ccr
one_tmp[:template_name] = template_name
one_tmp[:vcenter_ccr_ref] = template_ccr._ref
one_tmp[:one] = to_one(true)
one_tmp[:vcenter_ref] = template_ref
one_tmp[:vcenter_instance_uuid] = vcenter_uuid
@ -1311,7 +1409,8 @@ class VirtualMachine
one_tmp[:ds_list] = ds_list
one_tmp[:default_ds] = default_ds
one_tmp[:rp] = rp
one_tmp[:rp_list] = rp_list
one_tmp[:rp_list] = rp_list
one_tmp[:template] = template
return one_tmp
end