#!/usr/bin/ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2025, 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.                                             #
#--------------------------------------------------------------------------- #

# %%RUBYGEMS_SETUP_BEGIN%%
require 'load_opennebula_paths'
# %%RUBYGEMS_SETUP_END%%

###############################################################################
# This script removes the either system datastore’s directory of the VM or a
# disk itself.
#
# Whenever a VM is terminated, it is called in the following way:
# - For each non-persistent disk:
#       host:remote_system_ds/disk_path vm_id img_ds_id
# - For each volatile disk:
#       host:remote_system_ds/disk_path vm_id sys_ds_id
# - Finally:
#       host:remote_system_ds vm_id sys_ds_id
###############################################################################
#
# ARGUMENTS: host:remote_system_ds/disk.i|host:remote_system_ds/ vm_id ds_id
#   ds_id is the source datastore (the images datastore) for normal disks or
#   target datastore (the system datastore) for volatile disks
# STDIN: -
# RETURNS: -
#
###############################################################################

require_relative '../../datastore/lvm'
require_relative '../lib/tm_action'

arg_dst = ARGV[0]
arg_vmid = ARGV[1]
arg_ds_id = ARGV[2] # TODO: can be either SYS or IMG but we always need IMG. Volatiles?

dst = TransferManager::Action::Location.new(arg_dst)

action = TransferManager::Action.new(:vm_id => arg_vmid, :action_name => 'delete')

host =
    if action.undeployed?
        MAD::LVMDatastore.get_from_id(arg_ds_id).pick_bridge
    else
        dst.host
    end

lvmvm = MAD::LVMVM.new(action.vm.to_xml)

if (disk_id = dst.disk_id)
    lvmdisk = lvmvm.disk(disk_id)
    unless lvmdisk.persistent
        MAD::LVMWrapper.with_lvm_lock(lvmdisk.lv.vgname) do
            MAD.run(host, MAD::LVMWrapper.lvmsync(
                              lvmdisk.lv.delete_sh(:with_snapshots => true)
                          ), 'Error deleting thin LV')
        end
    end
else
    MAD::LVMWrapper.with_lvm_lock(lvmvm.pool.vgname) do
        MAD.run(host, MAD::LVMWrapper.lvmsync(lvmvm.pool.delete_sh(:safe => false)),
                'Error deleting thin pool')
    end
end

# Remove file leftovers
MAD.run(dst.host, "rm -rf '#{dst.path}'")
