~awstools-dev/ubuntu/maverick/ec2-ami-tools/maverick

« back to all changes in this revision

Viewing changes to lib/ec2/amitools/downloadbundle.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2008-10-14 08:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20081014083525-c0n69wr7r7aqfb8w
Tags: 1.3-26357-0ubuntu2
* New upstream version.
* Update the debian copyright file.
* Added quilt patch system to make it easier to maintain. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
10
 
 
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'
 
16
require 'getoptlong'
 
17
require 'net/http'
 
18
require 'rexml/document'
 
19
 
 
20
# Download AMI downloads the specified AMI from S3.
 
21
 
 
22
#------------------------------------------------------------------------------#
 
23
 
 
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 ) }
 
35
    else
 
36
      plaintext_manifest = encrypted_manifest
 
37
    end
 
38
    return plaintext_manifest
 
39
  end
 
40
  
 
41
  #----------------------------------------------------------------------------#
 
42
 
 
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 )
 
46
  end
 
47
  
 
48
  #----------------------------------------------------------------------------#
 
49
 
 
50
  def DownloadBundleAPI::get_part_filenames( manifest_xml )
 
51
    manifest = ManifestV3.new( manifest_xml )
 
52
    manifest.parts.collect { |part| part.filename }.sort
 
53
  end
 
54
end
 
55
 
 
56
#------------------------------------------------------------------------------#
 
57
 
 
58
# Main method.
 
59
def main
 
60
  status = 1
 
61
  begin
 
62
    p = DownloadBundleParameters.new( ARGV )
 
63
  rescue Exception => e
 
64
    STDERR.puts e.message
 
65
    STDERR.puts "Try '#{DownloadBundleAPI::NAME} --help'"
 
66
    return status
 
67
  end
 
68
  
 
69
  if p.show_help
 
70
    STDOUT.puts p.help
 
71
    return 0
 
72
  end
 
73
  
 
74
  begin
 
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 
 
82
    )
 
83
    
 
84
    # Download AMI parts.
 
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 ),
 
89
        p.user,
 
90
        p.pass,
 
91
        p.debug
 
92
      )
 
93
      STDOUT.puts "Downloaded #{filename} from #{bucket_url}."
 
94
    end
 
95
    status = 0
 
96
  rescue ParameterError => e
 
97
    STDERR.puts e.message
 
98
    STDERR.puts p.help
 
99
  rescue EC2::Common::HTTP::Error => e
 
100
    STDERR.puts e.message
 
101
    status = e.code
 
102
  rescue StandardError => e
 
103
    STDERR.puts e.message
 
104
    STDERR.puts e.backtrace if p.debug
 
105
  end
 
106
  
 
107
  if status == 0
 
108
    STDOUT.puts 'Bundle download completed.'
 
109
  else
 
110
    STDOUT.puts 'Bundle download failed.'
 
111
  end
 
112
  
 
113
  return status
 
114
end
 
115
 
 
116
#------------------------------------------------------------------------------#
 
117
# Script entry point. Execute only if this file is being executed.
 
118
if __FILE__ == $0
 
119
  begin
 
120
    status = main
 
121
  rescue Interrupt
 
122
    STDERR.puts "\n#{DownloadBundleAPI::NAME} interrupted."
 
123
    status = 255
 
124
  end
 
125
  exit status
 
126
end