#!/usr/bin/env 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%%

$LOAD_PATH << RUBY_LIB_LOCATION

require 'rexml/document'

require_relative '../lib/tm_action'
require_relative '../lib/ceph'

#-------------------------------------------------------------------------------
# BACKUP tm_mad host:remote_dir DISK_ID:...:DISK_ID deploy_id bjid vmid dsid
#-------------------------------------------------------------------------------
TransferManager::Datastore.load_env

vm_xml = STDIN.read

dir       = ARGV[0].split ':'
disks     = ARGV[1].split ':'
deploy_id = ARGV[2]
_bjid     = ARGV[3]
vmid      = ARGV[4]
_dsid     = ARGV[5]

rdir      = dir[1]

base_path = ENV['BACKUP_BASE_PATH']

backup_dir =
    if base_path
        "#{base_path}/#{vmid}/backup"
    else
        "#{rdir}/backup"
    end

action   = TransferManager::Action.new(:action_name => 'prebackup_live',
                                       :vm_id => vmid)
ds       = TransferManager::Datastore.from_vm_backup_ds(:vm_xml => vm_xml.to_s)
vm       = TransferManager::Ceph::VM.new(action.vm.to_xml, rdir)

xml_data = REXML::Document.new(vm_xml)
rhost    = xml_data.elements['VM/BACKUPS/BACKUP_CONFIG/LAST_BRIDGE']&.text || dir[0]

script = vm.backup_disks_sh(:disks       => disks,
                            :backup_dir  => backup_dir,
                            :ds          => ds,
                            :live        => false,
                            :deploy_id   => deploy_id,
                            :bridge_host => rhost)

rc = action.ssh(:host => rhost,
                :cmds => script,
                :forward  => true,
                :nostdout => false,
                :nostderr => false)

if rc.code != 0
    STDERR.puts "Error preparing disk files: #{rc.stdout} #{rc.stderr}"
end

exit(rc.code)
