Statistics
| Branch: | Tag: | Revision:

one / src / market_mad / remotes / s3 / import @ f4f280ce

History | View | Annotate | Download (4.41 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2015, OpenNebula Project, OpenNebula Systems                #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
# -------------------------------------------------------------------------- #
18

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

    
25
ONE_LOCATION = ENV["ONE_LOCATION"]
26

    
27
if !ONE_LOCATION
28
    RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
29
else
30
    RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
31
end
32

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

    
35
$: << RUBY_LIB_LOCATION
36
$: << RUBY_LIB_LOCATION+"/cli"
37
$: << File.dirname(__FILE__)
38

    
39
require 'base64'
40
require 'rexml/document'
41
require 'getoptlong'
42
require 'open3'
43
require 'pp'
44

    
45
require 'S3'
46

    
47
def xpath(xml, xpath)
48
    xml.elements[xpath].text.to_s rescue nil
49
end
50

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

    
53
import_source = xpath(xml, 'IMPORT_SOURCE')
54
format        = xpath(xml, 'FORMAT')
55
dispose       = xpath(xml, 'DISPOSE')
56
size          = xpath(xml, 'SIZE')
57
md5           = xpath(xml, 'MD5')
58

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

    
61
# required
62
access_key_id     = xpath(xml, 'MARKETPLACE/TEMPLATE/ACCESS_KEY_ID')
63
secret_access_key = xpath(xml, 'MARKETPLACE/TEMPLATE/SECRET_ACCESS_KEY')
64
bucket            = xpath(xml, 'MARKETPLACE/TEMPLATE/BUCKET')
65
region            = xpath(xml, 'MARKETPLACE/TEMPLATE/REGION')
66

    
67
# optional
68
signature_version = xpath(xml, 'MARKETPLACE/TEMPLATE/SIGNATURE_VERSION')
69
endpoint          = xpath(xml, 'MARKETPLACE/TEMPLATE/ENDPOINT')
70
force_path_style  = xpath(xml, 'MARKETPLACE/TEMPLATE/FORCE_PATH_STYLE')
71
read_length       = xpath(xml, 'MARKETPLACE/TEMPLATE/READx_LENGTH')
72

    
73
name   = "marketapp-#{id}"
74
source = "s3://#{bucket}/#{name}"
75

    
76
s3_config = {
77
    :region            => region,
78
    :access_key_id     => access_key_id,
79
    :secret_access_key => secret_access_key
80
}
81

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

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

    
88
if read_length.to_s.empty?
89
    read_length = DEFAULT_READ_LENGTH
90
else
91
    read_length = 1024*1024*read_length.to_i
92
end
93

    
94
s3 = S3.new(s3_config)
95

    
96
s3.name   = name
97
s3.bucket = bucket
98

    
99
if s3.exists?
100
    STDERR.puts "Object '#{name}' already exists."
101
    exit 1
102
end
103

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

    
106
Open3.popen3(cmd) do |_, o, _, _|
107
    body = o.read(read_length)
108

    
109
    if o.eof?
110
        s3.put_object(body)
111
    else
112
        s3.create_multipart_upload
113

    
114
        begin
115
            s3.upload_part(body)
116

    
117
            until o.eof?
118
                body = o.read(read_length)
119
                s3.upload_part(body)
120
            end
121

    
122
            s3.complete_multipart_upload
123
        rescue Exception => e
124
            STDERR.puts(e.message)
125
            STDERR.puts(e.backtrace)
126

    
127
            resp = s3.abort_multipart_upload
128
            STDERR.puts(resp.inspect)
129

    
130
            exit 1
131
        end
132
    end
133
end
134

    
135
# Result
136

    
137
marketplaceapp = {
138
    :source => source,
139
    :md5    => md5,
140
    :size   => size,
141
    :format => format
142
}
143

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

    
148
exit 0