#!/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.                                             #
# -------------------------------------------------------------------------- #

###############################################################################
# This script is used to import a file into the marketplace. The source file
# is an opaque representation of an OpenNebula object, like a image file or a
# tar.gz with several vm template or flow disk images
###############################################################################

ONE_LOCATION = ENV['ONE_LOCATION']

if !ONE_LOCATION
    RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
    GEMS_LOCATION     = '/usr/share/one/gems'
else
    RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     = ONE_LOCATION + '/share/gems'
end

UTILS_PATH = File.join(File.dirname(__FILE__), '../../datastore')

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

$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << RUBY_LIB_LOCATION + '/cli'
$LOAD_PATH << File.dirname(__FILE__)

require 'base64'
require 'rexml/document'
require 'getoptlong'
require 'open3'
require 'pp'

require 'S3'

def xpath(xml, xpath)
    xml.elements[xpath].text.to_s rescue nil
end

xml = REXML::Document.new(Base64::decode64(ARGV[0])).root

import_source = xpath(xml, 'IMPORT_SOURCE')
format        = xpath(xml, 'FORMAT')
dispose       = xpath(xml, 'DISPOSE')
dispose_cmd   = xpath(xml, 'DISPOSE_CMD')
size          = xpath(xml, 'SIZE')
md5           = xpath(xml, 'MD5')

id = xpath(xml, 'MARKETPLACEAPP/ID')

# required
access_key_id     = xpath(xml, 'MARKETPLACE/TEMPLATE/ACCESS_KEY_ID')
secret_access_key = xpath(xml, 'MARKETPLACE/TEMPLATE/SECRET_ACCESS_KEY')
bucket            = xpath(xml, 'MARKETPLACE/TEMPLATE/BUCKET')
region            = xpath(xml, 'MARKETPLACE/TEMPLATE/REGION')

# optional
app_prefix        = xpath(xml, 'MARKETPLACE/TEMPLATE/APP_PREFIX')        || 'marketapp'
signature_version = xpath(xml, 'MARKETPLACE/TEMPLATE/SIGNATURE_VERSION')
endpoint          = xpath(xml, 'MARKETPLACE/TEMPLATE/ENDPOINT')
force_path_style  = xpath(xml, 'MARKETPLACE/TEMPLATE/FORCE_PATH_STYLE')
read_length       = xpath(xml, 'MARKETPLACE/TEMPLATE/READx_LENGTH')

name   = "#{app_prefix}-#{id}"
source = "s3://#{bucket}/#{name}"

s3_config = {
    :region            => region,
    :access_key_id     => access_key_id,
    :secret_access_key => secret_access_key
}

s3_config[:signature_version] = signature_version if !signature_version.to_s.empty?
s3_config[:endpoint]          = endpoint if !endpoint.to_s.empty?
s3_config[:force_path_style]  = true if force_path_style.to_s.downcase == "yes"

DEFAULT_READ_LENGTH = 32*1024*1024 # Read in chunks of 32MB

if read_length.to_s.empty?
    read_length = DEFAULT_READ_LENGTH
else
    read_length = 1024*1024*read_length.to_i
end

s3 = S3.new(s3_config)

s3.name   = name
s3.bucket = bucket

if s3.exists?
    STDERR.puts "Object '#{name}' already exists."
    exit 1
end

cmd = "#{UTILS_PATH}/downloader.sh #{import_source} -"

Open3.popen3(cmd) do |_, o, e, wt|
    body = o.read(read_length)

    if o.eof?
        exit_status = wt.value
        error = Thread.new { e.read }.value

        unless exit_status.success?
            STDERR.puts error
            exit 1
        end

        s3.put_object(body)
    else
        s3.create_multipart_upload

        begin
            s3.upload_part(body)

            until o.eof?
                body = o.read(read_length)
                s3.upload_part(body)
            end

            s3.complete_multipart_upload
        rescue Exception => e
            STDERR.puts(e.message)
            STDERR.puts(e.backtrace)

            resp = s3.abort_multipart_upload
            STDERR.puts(resp.inspect)

            exit 1
        end
    end
end

# Dispose

if dispose == 'YES' && !dispose_cmd.empty?
    _, stderr_str, status = Open3.capture3(dispose_cmd)

    unless status.success?
        STDERR.puts("Dispose of #{import_source} failed. "\
                    "Error: #{stderr_str}")
    end
end

# Result

marketplaceapp = {
    :source => source,
    :md5    => md5,
    :size   => size,
    :format => format
}

marketplaceapp.each do |k,v|
    puts "#{k.upcase}=\"#{v}\""
end

exit 0
