#!/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"
                    ds.delete_virtual_disk(img_path)
                    img_dir = File.dirname(img_path)
                    ds.rm_directory(img_dir) if ds.dir_empty?(img_dir)
                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.

        # If we have NICs associated to VN_MAD=vcenter we must check if pgs and
        # switches must be deleted

        # track pg or dpg in case they must be removed
        vcenter_uuid = vm.get_vcenter_instance_uuid
        networks = {}
        npool = VCenterDriver::VIHelper.one_pool(OpenNebula::VirtualNetworkPool, false)
        if npool.respond_to?(:message)
            raise "Could not get OpenNebula VirtualNetworkPool: #{npool.message}"
        end

        # Check nics in VM
        vm.item["config.hardware.device"].each do |dv|
            if vm.is_nic?(dv)
                if dv.backing.respond_to?(:network)
                    vnet_ref = dv.backing.network._ref
                end

                if dv.backing.respond_to?(:port) &&
                   dv.backing.port.respond_to?(:portgroupKey)
                    vnet_ref  = dv.backing.port.portgroupKey
                end

                one_network = VCenterDriver::VIHelper.find_by_ref(OpenNebula::VirtualNetworkPool,
                                                                  "TEMPLATE/VCENTER_NET_REF",
                                                                  vnet_ref,
                                                                  vcenter_uuid,
                                                                  npool)
                next if !one_network
                if one_network["VN_MAD"] == "vcenter" && !networks.key?(one_network["BRIDGE"])
                    networks[one_network["BRIDGE"]] = one_network
                end
            end
        end

        #Remove pgs and switches if not needed
        if !networks.empty?

            esx_host = VCenterDriver::ESXHost.new_from_ref(vm.item.runtime.host._ref, vi_client)
            dc = vm.cluster.get_dc # Get datacenter

            networks.each do |pg_name, one|

                if one["TEMPLATE/VCENTER_PORTGROUP_TYPE"] == "Port Group"
                    begin
                        esx_host.lock # Exclusive lock for ESX host operation

                        next if !esx_host.pg_exists(pg_name)
                        swname = esx_host.remove_pg(pg_name)
                        next if !swname

                        # We must update XML so the VCENTER_NET_REF is unset
                        VCenterDriver::Network.remove_net_ref(one["ID"])

                        next if !esx_host.vss_exists(swname)
                        swname = esx_host.remove_vss(swname)

                    rescue Exception => e
                        raise e
                    ensure
                        esx_host.unlock if esx_host # Remove lock
                    end
                end

                if one["TEMPLATE/VCENTER_PORTGROUP_TYPE"] == "Distributed Port Group"
                    begin
                        dc.lock

                        # Explore network folder in search of dpg and dvs
                        net_folder = dc.network_folder
                        net_folder.fetch!

                        # Get distributed port group if it exists
                        dpg = dc.dpg_exists(pg_name, net_folder)
                        dc.remove_dpg(dpg) if dpg

                        # We must update XML so the VCENTER_NET_REF is unset
                        VCenterDriver::Network.remove_net_ref(one["ID"])

                        # Get distributed virtual switch and try to remove it
                        switch_name =  one["TEMPLATE/VCENTER_SWITCH_NAME"]
                        dvs = dc.dvs_exists(switch_name, net_folder)
                        dc.remove_dvs(dvs) if dvs

                    rescue Exception => e
                        #TODO rollback
                        raise e
                    ensure
                        dc.unlock if dc
                    end
                end
            end

        end

        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
