1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-04-01 06:50:25 +03:00

solved vCenter TM_BUG: delete disk (#2227)

This commit is contained in:
Sergio Semedi Barranco 2018-06-28 12:22:47 +02:00 committed by Tino Vázquez
parent 777f435313
commit 7ab59e48be
2 changed files with 71 additions and 49 deletions

View File

@ -38,6 +38,52 @@ require 'vcenter_driver'
VM_PREFIX_DEFAULT = "one-$i-"
# Don't do a detach if unmanaged disks and state is terminate (EPILOG)
def can_detach(disk, one_vm)
!(one_vm["LCM_STATE"].to_i == 11 && disk["OPENNEBULA_MANAGED"] && disk["OPENNEBULA_MANAGED"].upcase == "NO")
end
# it's not a CDROM (CLONE=NO)
def not_a_cd(disk)
disk["CLONE"].nil? || disk["CLONE"] == "YES"
end
# detach disk from vCenter vm if possible, destroy the disk on FS
def detach_and_destroy(disk, vm, disk_id, prev_ds_ref, vi_client)
begin
# Detach disk if possible (VM is reconfigured) and gather vCenter info
# Needed for poweroff machines too
ds_ref, img_path = vm.detach_disk(disk)
# Disk could't be detached, use OpenNebula info
if !(ds_ref && img_path && !img_path.empty?)
img_path = vm.disk_real_path(disk, disk_id)
ds_ref = prev_ds_ref
end
# If disk was already detached we have no way to remove it
ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
search_params = ds.get_search_params(ds['name'],
File.dirname(img_path),
File.basename(img_path))
# Perform search task and return results
search_task = ds['browser'].SearchDatastoreSubFolders_Task(search_params)
search_task.wait_for_completion
ds.delete_virtual_disk(img_path)
img_dir = File.dirname(img_path)
ds.rm_directory(img_dir) if ds.dir_empty?(img_dir)
rescue Exception => e
if !e.message.start_with?('FileNotFound')
raise e.message # Ignore FileNotFound
end
end
end
path = ARGV[0]
vmid = ARGV[1]
dsid = ARGV[2]
@ -58,6 +104,7 @@ vm_ref = one_vm['DEPLOY_ID']
vm = nil
# tm:delete INIT block:
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
@ -73,6 +120,8 @@ begin
vm_ref = vcenter_vm._ref
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
end
vm.one_item = one_vm
rescue Exception => e
vi_client.close_connection if vi_client
@ -81,59 +130,23 @@ rescue Exception => e
exit -1
end
# Detach and remove the disk (if it is not a CDROM)
if path.match(/disk\.\d+$/)
# Detach and remove the disk (if it is not a CDROM)
# Get DS ref
dsid = img_path.split("/")[-3] # get dsid from path
one_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, dsid)
ds_ref = one_ds['TEMPLATE/VCENTER_DS_REF']
# Get disk info
disk_id = img_path.split(".")[-1]
disk = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]").first
begin
# Get DS ref
dsid = img_path.split("/")[-3] # get dsid from path
one_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, dsid)
ds_ref = one_ds['TEMPLATE/VCENTER_DS_REF']
# Get disk info
disk_id = img_path.split(".")[-1]
disk = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]").first
if !vm.has_snapshots?
vm.one_item = one_vm
# Don't do a detach if unmanaged disks and state is terminate (EPILOG)
unless one_vm["LCM_STATE"].to_i == 11 && disk["OPENNEBULA_MANAGED"] && disk["OPENNEBULA_MANAGED"].upcase == "NO"
# Detach disk
ds_ref, img_path = vm.detach_disk(disk)
# If disk was already detached we have no way to remove it
if ds_ref && img_path && !img_path.empty?
ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
# delete the disk if it's not a CDROM (CLONE=NO)
if disk["CLONE"].nil? || disk["CLONE"] == "YES"
search_params = ds.get_search_params(ds['name'],
File.dirname(img_path),
File.basename(img_path))
# Perform search task and return results
begin
search_task = ds['browser'].SearchDatastoreSubFolders_Task(search_params)
search_task.wait_for_completion
ds.delete_virtual_disk(img_path)
img_dir = File.dirname(img_path)
ds.rm_directory(img_dir) if ds.dir_empty?(img_dir)
rescue Exception => e
if !e.message.start_with?('FileNotFound')
raise e.message # Ignore FileNotFound
end
end
end
end
if can_detach(disk, one_vm) && not_a_cd(disk)
detach_and_destroy(disk, vm, disk_id, ds_ref, vi_client)
end
end
rescue Exception => e
message = "Error delete virtual disk #{img_path} in datastore #{dsid}."\
" Reason: #{e.message}\n#{e.backtrace}"
@ -142,8 +155,9 @@ if path.match(/disk\.\d+$/)
ensure
vi_client.close_connection if vi_client
end
# Is not a Disk, remove the VM
else
# Remove the VM
begin
# All OpenNebula managed disks have been detached unless it has snapshots.

View File

@ -1142,6 +1142,14 @@ class VirtualMachine < Template
@item = item
end
def disk_real_path(disk, disk_id)
sppath = disk["SOURCE"].split(".")
raise "vm image path error!" if sppath.size != 2 || sppath.last != 'vmdk'
img_path = "#{sppath[0]}-#{@vm_id}-#{disk_id}.#{sppath[1]}"
end
# The OpenNebula host
# @return OpenNebula::Host or XMLElement
def host