1
# Copyright 2008 Amazon.com, Inc. or its affiliates. All Rights
2
# Reserved. Licensed under the Amazon Software License (the
3
# "License"). You may not use this file except in compliance with the
4
# License. A copy of the License is located at
5
# http://aws.amazon.com/asl or in the "license" file accompanying this
6
# file. This file is distributed on an "AS IS" BASIS, WITHOUT
7
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
8
# the License for the specific language governing permissions and
9
# limitations under the License.
11
require 'ec2/amitools/crypto'
12
require 'ec2/common/http'
13
require 'ec2/amitools/downloadbundleparameters'
14
require 'ec2/amitools/exception'
15
require 'ec2/amitools/manifestv3'
18
require 'rexml/document'
20
# Download AMI downloads the specified AMI from S3.
22
#------------------------------------------------------------------------------#
24
module DownloadBundleAPI
25
NAME = 'ec2-download-bundle'
26
def DownloadBundleAPI::download_manifest( url, path, privatekey, user, pass, debug)
27
STDOUT.puts "Downloading manifest #{url} to #{path} ..."
28
EC2::Common::HTTP::get( url, path, {}, user, pass, debug )
29
encrypted_manifest = File::open( path ) { |f| f.read() }
30
plaintext_manifest = nil
31
if (encrypted_manifest !~ /^\s*<\?/)
32
STDOUT.puts "Decrypting manifest ..."
33
plaintext_manifest = Crypto::decryptasym( encrypted_manifest, privatekey )
34
File::open( path +'.plaintext', 'w' ) { |f| f.write( plaintext_manifest ) }
36
plaintext_manifest = encrypted_manifest
38
return plaintext_manifest
41
#----------------------------------------------------------------------------#
43
def DownloadBundleAPI::download_part( url, path, user, pass, debug )
44
STDOUT.puts "Downloading part #{url} to #{path} ..."
45
EC2::Common::HTTP::get( url, path, {}, user, pass, debug )
48
#----------------------------------------------------------------------------#
50
def DownloadBundleAPI::get_part_filenames( manifest_xml )
51
manifest = ManifestV3.new( manifest_xml )
52
manifest.parts.collect { |part| part.filename }.sort
56
#------------------------------------------------------------------------------#
62
p = DownloadBundleParameters.new( ARGV )
65
STDERR.puts "Try '#{DownloadBundleAPI::NAME} --help'"
75
# Download and decrypt manifest.
76
bucket_url = File::join( p.url, p.bucket )
77
manifest_url = File::join( bucket_url, p.manifest )
78
manifest_path = File.join( p.directory, p.manifest )
79
manifest_xml = DownloadBundleAPI::download_manifest(
80
manifest_url, manifest_path,
81
p.privatekey, p.user, p.pass, p.debug
85
DownloadBundleAPI::get_part_filenames( manifest_xml ).each do |filename|
86
DownloadBundleAPI::download_part(
87
File::join( bucket_url, filename ),
88
File::join( p.directory, filename ),
93
STDOUT.puts "Downloaded #{filename} from #{bucket_url}."
96
rescue ParameterError => e
99
rescue EC2::Common::HTTP::Error => e
100
STDERR.puts e.message
102
rescue StandardError => e
103
STDERR.puts e.message
104
STDERR.puts e.backtrace if p.debug
108
STDOUT.puts 'Bundle download completed.'
110
STDOUT.puts 'Bundle download failed.'
116
#------------------------------------------------------------------------------#
117
# Script entry point. Execute only if this file is being executed.
122
STDERR.puts "\n#{DownloadBundleAPI::NAME} interrupted."