#!/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?

            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
            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 unless it has snapshots.
        # The VM may have still unmanaged 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, 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
        vm.remove_all_snapshots if vm.has_snapshots?

        disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK")
        if vm.has_snapshots? || vm.instantiated_as_persistent?
            disks.each do |d|
                if d["PERSISTENT"] == "YES"
                    vm.detach_disk(d)
                else
                    # Remove any disk that is not unmanaged from the VM if
                    # instantiate as persistent was set
                    if vm.instantiated_as_persistent? &&
                    (!d["OPENNEBULA_MANAGED"] || d["OPENNEBULA_MANAGED"].upcase != "NO")
                        vm.detach_disk(d)
                    end
                end
            end
        end

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

            # Remove the MAC addresses so they cannot be in conflict
            # with OpenNebula assigned mac addresses.

            # We detach all nics from the VM
            vm.detach_all_nics

            # We attach new NICs where the MAC address is assigned by vCenter
            nic_specs = []
            nics = one_vm.retrieve_xmlelements("TEMPLATE/NIC")
            nics.each do |nic|
                if (nic["OPENNEBULA_MANAGED"] && nic["OPENNEBULA_MANAGED"].upcase == "NO")
                    nic_specs << vm.calculate_add_nic_spec_autogenerate_mac(nic)
                end
            end

            # Reconfigure VM to add unmanaged nics
            spec_hash = {}
            spec_hash[:deviceChange] = nic_specs
            spec = RbVmomi::VIM.VirtualMachineConfigSpec(spec_hash)
            vm.item.ReconfigVM_Task(:spec => spec).wait_for_completion

            # Convert VM to template in vCenter
            vm.mark_as_template

            # Edit the Opennebula template
            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

            # Update the template reference
            new_template.update("VCENTER_TEMPLATE_REF=#{vm.item._ref}", true)

        else
            # Destroy the VM and remaining disks
            vm.destroy
        end

    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
