#!/usr/bin/env ruby

# ---------------------------------------------------------------------------- #
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                  #
#                                                                              #
# Licensed under the Apache License, Version 2.0 (the "License"); you may      #
# not use this file except in compliance with the License. You may obtain      #
# a copy of the License at                                                     #
#                                                                              #
# http://www.apache.org/licenses/LICENSE-2.0                                   #
#                                                                              #
# Unless required by applicable law or agreed to in writing, software          #
# distributed under the License is distributed on an "AS IS" BASIS,            #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     #
# See the License for the specific language governing permissions and          #
# limitations under the License.                                               #
# ---------------------------------------------------------------------------- #

# DELETE <host:remote_system_ds/disk.i|host:remote_system_ds/> vmid dsid
#   - host is the target host to deploy the VM
#   - 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)
# ---------------------------------------------------------------------------- #

ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)

if !ONE_LOCATION
    RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION)
else
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION)
end

$: << RUBY_LIB_LOCATION
$: << File.dirname(__FILE__)

require 'vcenter_driver'

VM_PREFIX_DEFAULT = "one-$i-"

path = ARGV[0]
vmid = ARGV[1]
dsid = ARGV[2]

check_valid path, "path"
check_valid vmid, "vmid"
check_valid dsid, "dsid"

hostname, img_path = path.split(":")

# Get host ID
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
host_id = host['ID']

# Get VM
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
vm_ref = one_vm['DEPLOY_ID']

vm = nil

begin
    vi_client = VCenterDriver::VIClient.new_from_host(host_id)

    if !!vm_ref && !vm_ref.empty?
        vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
    else
        vcenter_vm = VCenterDriver::VIHelper.find_vcenter_vm_by_name(one_vm, host, vi_client)

        # If no VM object retrieved, raise an exception
        raise "Could not find the undeployed VM in vCenter's inventory using it's name" if !vcenter_vm

        vm_ref = vcenter_vm._ref
        vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
    end
rescue Exception => e
    vi_client.close_connection if vi_client

    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 (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
        if !vm.has_snapshots?
            # TODO: if the deploy has failed, the disks may exist, but the vm may
            # not exist...
            vm.one_item = one_vm

            # detach the disk or cdrom
            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)
                        vcenter_version = vi_client.vim.serviceContent.about.apiVersion
                        ds.rm_directory(img_dir) if ds.dir_empty?(img_dir) && vcenter_version != "6.5"

                    rescue Exception => e
                        if !e.message.start_with?('FileNotFound')
                            raise e.message # Ignore FileNotFound
                        end
                    end
                end
            end
        end

    rescue Exception => e
        message = "Error delete virtual disk #{img_path} in datastore #{dsid}."\
                  " Reason: #{e.message}\n#{e.backtrace}"
        STDERR.puts error_message(message)
        exit -1
    ensure
        vi_client.close_connection if vi_client
    end
else
    # Remove the VM
    begin

        # All OpenNebula managed disks have been detached. The VM may have still
        # disks that belong to the template (OPENNEBULA_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.poweroff_hard if vm.is_powered_on?

        # If the VM has snapshots the TM could not detach disks so we
        # will try to detach persistent disks once we have removed all snapshots
        # that way they won't be removed. If the vm has been marked as template
        # persistent disks shouldn't be detached
        if vm.has_snapshots? && !vm.instantiated_as_persistent?
            vm.remove_all_snapshots
            disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK[PERSISTENT=\"YES\"]")
            disks.each do |d|
                vm.detach_disk(d)
            end
        end

        # If the VM was instantiated to persistent keep the VM
        if vm.instantiated_as_persistent?

            #Convert VM to template in vCenter
            vm.mark_as_template

            # Create new Opennebula template and set VCENTER_TEMPLATE_REF
            one_client = OpenNebula::Client.new
            template_id = vm.one_item['TEMPLATE/TEMPLATE_ID']
            new_template = OpenNebula::Template.new_with_id(template_id, one_client)
            new_template.info
            new_template.update("VCENTER_TEMPLATE_REF= #{vm.item._ref}", true)
        end

        # Destroy the VM unless the instantiate as persistent is used
        vm.destroy if !vm.instantiated_as_persistent?
    rescue Exception => e
        message = "Error unregistering vm #{vmid} (#{vm_ref})."\
                  " Reason: #{e.message}\n#{e.backtrace}"
        STDERR.puts error_message(message)
        exit -1
    ensure
        vi_client.close_connection if vi_client
    end
end
