#!/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 'CommandManager'

require_relative 'restic'
require_relative '../../tm/lib/backup'

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

# ------------------------------------------------------------------------------
# Parse input data
# ------------------------------------------------------------------------------
begin
    action = Base64.decode64 daction64

    rds = Restic.new action, :prefix    => 'DATASTORE/',
                             :repo_type => :local,
                             :host_type => :hypervisor
    rds.resticenv_rb

    image = TransferManager::BackupImage.new action

    ds_id = rds['DATASTORE/ID']

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

# ------------------------------------------------------------------------------
# Process snapshot files
# ------------------------------------------------------------------------------
begin
    vm_xml, paths = rds.read_document(snap, /^vm\.xml$/)
    disks         = paths[:disks][:by_snap][snap]
rescue StandardError => e
    STDERR.puts "Unable to fetch vm.xml document: #{e.message}"
    exit(-1)
end

tpm_path = paths[:other][:by_snap][snap].find do |line|
    line.end_with?('/disk.tpm.0')
end

# ------------------------------------------------------------------------------
# Prepare an OpenNebula client to impersonate the target user
# ------------------------------------------------------------------------------
begin
    username  = rds['TEMPLATE/USERNAME']
    dst_ds_id = rds['DESTINATION_DS_ID'].to_i
rescue StandardError
    STDERR.puts 'Cannot find USERNAME / DESTINATION_DS_ID'
    exit(-1)
end

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
# ------------------------------------------------------------------------------
restorer = TransferManager::BackupRestore.new(
    :vm_xml64  => vm_xml,
    :backup_id => snap,
    :bimage    => image,
    :ds_id     => ds_id,
    :txml      => rds,
    :proto     => image.proto('restic')
)

br_disks = restorer.disk_images disks

one_error       = ''
restored_images = []

# Create disk images
br_disks.each do |_id, disk|
    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
        tpm_tgz, _paths = rds.read_document(snap, 'disk.tpm.0')
        vm_template << "TPM_STATE = \"#{Base64.strict_encode64(tpm_tgz)}\"\n"
    end

    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)
