#!/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.                                             #
#--------------------------------------------------------------------------- #
ONE_LOCATION = ENV['ONE_LOCATION']

if !ONE_LOCATION
    RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
    GEMS_LOCATION     = '/usr/share/one/gems'
    VMDIR             = '/var/lib/one'
    CONFIG_FILE       = '/var/lib/one/config'
    VAR_LOCATION      = '/var/lib/one'
else
    RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     = ONE_LOCATION + '/share/gems'
    VMDIR             = ONE_LOCATION + '/var'
    CONFIG_FILE       = ONE_LOCATION + '/var/config'
    VAR_LOCATION      = ONE_LOCATION + '/var'
end

SERVERADMIN_AUTH = VAR_LOCATION + '/.one/onegate_auth'

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

$LOAD_PATH << RUBY_LIB_LOCATION

require 'base64'
require 'opennebula'
require 'opennebula/server_cipher_auth'
require 'pathname'
require 'rexml/document'

require_relative '../../tm/lib/backup'
require_relative '../../tm/lib/tm_action'

# ------------------------------------------------------------------------------
# Get backup information:
#   - vm.xml description
#   - list of disks in the backup
# ------------------------------------------------------------------------------
daction64   = STDIN.read
_request_id = ARGV[0]

begin
    action = Base64.decode64 daction64

    image = TransferManager::BackupImage.new(action)

    xml = REXML::Document.new(action).root

    ds_id      = xml.elements['DATASTORE/ID'].text.to_i
    base_path  = xml.elements['DATASTORE/BASE_PATH'].text
    rsync_user = xml.elements['DATASTORE/TEMPLATE/RSYNC_USER']&.text || 'oneadmin'
    rsync_host = xml.elements['DATASTORE/TEMPLATE/RSYNC_HOST'].text

    snap = image.selected || image.last
rescue StandardError => e
    STDERR.puts "Missing datastore or image attributes: #{e.message}"
    exit(-1)
end

begin
    username  = xml.elements['TEMPLATE/USERNAME'].text
    dst_ds_id = xml.elements['DESTINATION_DS_ID'].text.to_i
rescue StandardError
    STDERR.puts 'Cannot find USERNAME / DESTINATION_DS_ID'
    exit(-1)
end

begin
    script = [<<~EOS]
        set -e -o pipefail; shopt -qs failglob
    EOS

    snap_dir = %(#{base_path}/#{image.vm_id}/#{snap}/)
    snap_dir = Pathname.new(snap_dir).cleanpath.to_s

    script << %(find '#{snap_dir}' -type f -name 'disk.*' -or -name 'vm.xml')

    rc = TransferManager::Action.ssh 'list_files',
                                     :host     => "#{rsync_user}@#{rsync_host}",
                                     :forward  => true,
                                     :cmds     => script.join("\n"),
                                     :nostdout => false,
                                     :nostderr => false

    raise StandardError, "Unable to list backup files: #{rc.stderr}" if rc.code != 0

    stdout_lines = rc.stdout.lines.map(&:strip).reject(&:empty?)

    disk_paths = stdout_lines.select do |line|
        line.include?('/disk.')
    end

    tpm_path = disk_paths.find do |line|
        line.end_with?('/disk.tpm.0')
    end

    disk_paths.delete(tpm_path)

    vm_xml_path = stdout_lines.find do |line|
        line.include?("/#{snap}/vm.xml")
    end

    raise StandardError, 'Backup does not contain any disks or missing vm.xml' \
        if disk_paths.empty? || vm_xml_path.nil?
rescue StandardError => e
    STDERR.puts e.full_message
    exit(-1)
end

begin
    rc = TransferManager::Action.ssh 'read_vm_xml',
                                     :host     => "#{rsync_user}@#{rsync_host}",
                                     :cmds     => "cat '#{vm_xml_path}'",
                                     :nostdout => false,
                                     :nostderr => false

    raise StandardError, "Unable to read vm.xml: #{rc.stderr}" if rc.code != 0

    vm_xml = rc.stdout
rescue StandardError => e
    STDERR.puts e.full_message
    exit(-1)
end

# ------------------------------------------------------------------------------
# Prepare an OpenNebula client to impersonate the target user
# ------------------------------------------------------------------------------
ENV['ONE_CIPHER_AUTH'] = SERVERADMIN_AUTH

sauth = OpenNebula::ServerCipherAuth.new_client
token = sauth.login_token(Time.now.to_i + 120, username)

one_client = OpenNebula::Client.new token

# ------------------------------------------------------------------------------
# Create backup object templates for VM and associated disk images
# Monkey patch REXML::DOCUMENT to respond to []
# ------------------------------------------------------------------------------
xml.define_singleton_method('[]') {|xpath| elements[xpath].text }

restorer = TransferManager::BackupRestore.new(
    :vm_xml64  => vm_xml,
    :backup_id => snap,
    :bimage    => image,
    :ds_id     => ds_id,
    :txml      => xml,
    :proto     => image.proto('rsync')
)

br_disks = restorer.disk_images disk_paths

one_error       = ''
restored_images = []

# Create disk images
br_disks.each do |_id, disk|
    # Fix image name - maybe not necessary any longer
    # disk[:template].gsub!(%r{(NAME = "[0-9]+-)[0-9]+/}, '\1')
    restored_image = OpenNebula::Image.new OpenNebula::Image.build_xml, one_client
    rc             = restored_image.allocate disk[:template], dst_ds_id

    if OpenNebula.is_error?(rc)
        one_error = rc.message
        break
    end

    disk[:image_id] = restored_image.id
    restored_images << restored_image.id
end

if !one_error.empty?
    message = "Error restoring disk image: #{one_error}"

    if !restored_images.empty?
        message << " The following images were restored: #{restored_images.join(' ')}"
    end

    STDERR.puts message
    exit(-1)
end

# Create VM template
vm_template = restorer.vm_template br_disks

tmpl_id = -1

unless vm_template.nil?
    # Add saved TPM state to template, if there's one
    if tpm_path
        rc = TransferManager::Action.ssh 'get_tpm',
                                         :host     => "#{rsync_user}@#{rsync_host}",
                                         :forward  => true,
                                         :cmds     => "cat '#{tpm_path}'",
                                         :nostdout => false,
                                         :nostderr => false

        raise StandardError, "Unable to obtain TPM state: #{rc.stderr}" if rc.code != 0

        vm_template << "TPM_STATE = \"#{Base64.strict_encode64(rc.stdout)}\"\n"
    end

    # Fix template name (maybe not necessary any longer)
    # vm_template.gsub!(%r{(NAME= "[0-9]+-)[0-9]+/}, '\1')

    tmpl = OpenNebula::Template.new OpenNebula::Template.build_xml, one_client
    rc   = tmpl.allocate vm_template

    if OpenNebula.is_error?(rc)
        message = "Error creating VM template: #{rc.message}"

        if !restored_images.empty?
            message << " The following images were restored: #{restored_images.join(' ')}"
        end

        STDERR.puts message
        exit(-1)
    end

    tmpl_id = tmpl.id
end

STDOUT.puts "#{tmpl_id} #{restored_images.join(' ')}"

exit(0)
