mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-03 00:58:18 +03:00
F #4913: tm/delete detaches each disk first and destroys the VM
This commit is contained in:
parent
3e7fb4e3e6
commit
6b450ce1eb
@ -21,14 +21,6 @@
|
|||||||
# - remote_system_ds is the path for the system datastore in the host
|
# - remote_system_ds is the path for the system datastore in the host
|
||||||
# - vmid is the id of the VM
|
# - vmid is the id of the VM
|
||||||
# - dsid is the target datastore (0 is the system datastore)
|
# - dsid is the target datastore (0 is the system datastore)
|
||||||
|
|
||||||
# Return if this has called for the whole directory, instead of for a specific
|
|
||||||
# disk.
|
|
||||||
|
|
||||||
if !ARGV[0].match(/disk\.\d+$/)
|
|
||||||
exit(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------- #
|
# ---------------------------------------------------------------------------- #
|
||||||
|
|
||||||
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
|
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
|
||||||
@ -58,7 +50,7 @@ end
|
|||||||
|
|
||||||
path = ARGV[0]
|
path = ARGV[0]
|
||||||
vmid = ARGV[1]
|
vmid = ARGV[1]
|
||||||
dsid = ARGV[2]
|
dsid = ARGV[2]
|
||||||
|
|
||||||
check_valid path, "path"
|
check_valid path, "path"
|
||||||
check_valid vmid, "vmid"
|
check_valid vmid, "vmid"
|
||||||
@ -70,26 +62,64 @@ hostname, img_path = path.split(":")
|
|||||||
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
|
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
|
||||||
host_id = host['ID']
|
host_id = host['ID']
|
||||||
|
|
||||||
# Get DS ref
|
# Get VM
|
||||||
one_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, dsid)
|
|
||||||
ds_ref = one_ds['TEMPLATE/VCENTER_DS_REF']
|
|
||||||
|
|
||||||
check_valid ds_ref, "ds_ref"
|
|
||||||
|
|
||||||
# Get image path
|
|
||||||
disk_id = img_path.split(".")[-1]
|
|
||||||
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
|
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
|
||||||
disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]")
|
vm_ref = one_vm['DEPLOY_ID']
|
||||||
img_path = VCenterDriver::FileHelper.get_img_name(disks.first, vmid)
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
||||||
|
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
|
||||||
ds_vc = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
|
|
||||||
|
|
||||||
ds_vc.delete_virtual_disk(img_path)
|
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
STDERR.puts "Error delete virtual disk #{img_path} in datastore #{dsid}."\
|
vi_client.close_connection
|
||||||
|
|
||||||
|
STDERR.puts "Error obtaining the vCenter client and VM object."\
|
||||||
" Reason: #{e.message}\n#{e.backtrace}"
|
" Reason: #{e.message}\n#{e.backtrace}"
|
||||||
exit -1
|
exit -1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if path.match(/disk\.\d+$/)
|
||||||
|
# Detach and remove the disk
|
||||||
|
|
||||||
|
# Get DS ref
|
||||||
|
one_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, dsid)
|
||||||
|
ds_ref = one_ds['TEMPLATE/VCENTER_DS_REF']
|
||||||
|
|
||||||
|
# Get image path
|
||||||
|
disk_id = img_path.split(".")[-1]
|
||||||
|
|
||||||
|
disk = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]").first
|
||||||
|
img_path = VCenterDriver::FileHelper.get_img_name(disk, vmid)
|
||||||
|
|
||||||
|
begin
|
||||||
|
# TODO: if the deploy has failed, the disks may exist, but the vm may
|
||||||
|
# not exist...
|
||||||
|
|
||||||
|
# detach the disk
|
||||||
|
vm.detach_disk(disk)
|
||||||
|
|
||||||
|
# delete the disk
|
||||||
|
ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
|
||||||
|
ds.delete_virtual_disk(img_path)
|
||||||
|
rescue Exception => e
|
||||||
|
STDERR.puts "Error delete virtual disk #{img_path} in datastore #{dsid}."\
|
||||||
|
" Reason: #{e.message}\n#{e.backtrace}"
|
||||||
|
exit -1
|
||||||
|
ensure
|
||||||
|
vi_client.close_connection
|
||||||
|
end
|
||||||
|
else
|
||||||
|
# Remove the VM
|
||||||
|
begin
|
||||||
|
# All OpenNebula managed disks have been detached. The VM may have still
|
||||||
|
# disks that belong to the template (VCENTER_MANAGED disks). These disks
|
||||||
|
# will be deleted with the destroy operation. If the user wants to
|
||||||
|
# save them to a VM, it can be done using the disk-saveas operation.
|
||||||
|
vm.destroy
|
||||||
|
rescue Exception => e
|
||||||
|
STDERR.puts "Error unregistering vm #{vmid} (#{vm_ref})."\
|
||||||
|
" Reason: #{e.message}\n#{e.backtrace}"
|
||||||
|
exit -1
|
||||||
|
ensure
|
||||||
|
vi_client.close_connection
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user