diff --git a/src/tm_mad/vcenter/delete b/src/tm_mad/vcenter/delete index e7be094b85..dfa8f72a1a 100755 --- a/src/tm_mad/vcenter/delete +++ b/src/tm_mad/vcenter/delete @@ -21,14 +21,6 @@ # - remote_system_ds is the path for the system datastore in the host # - vmid is the id of the VM # - 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) @@ -58,7 +50,7 @@ end path = ARGV[0] vmid = ARGV[1] -dsid = ARGV[2] +dsid = ARGV[2] check_valid path, "path" check_valid vmid, "vmid" @@ -70,26 +62,64 @@ hostname, img_path = path.split(":") host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) host_id = host['ID'] -# Get DS ref -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] +# Get VM one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid) -disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]") -img_path = VCenterDriver::FileHelper.get_img_name(disks.first, vmid) +vm_ref = one_vm['DEPLOY_ID'] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) - - ds_vc = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client) - - ds_vc.delete_virtual_disk(img_path) + vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client) 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}" exit -1 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