Class: BundleDownloader

Inherits:
AMITool show all
Defined in:
lib/ec2/amitools/downloadbundle.rb

Constant Summary

Constants inherited from AMITool

AMITool::BACKOFF_PERIOD, AMITool::MAX_TRIES, AMITool::PROMPT_TIMEOUT

Instance Method Summary collapse

Methods inherited from AMITool

#get_parameters, #handle_early_exit_parameters, #interactive?, #interactive_prompt, #retry_s3, #run, #warn_confirm

Instance Method Details

#download_bundle(url, user, pass, bucket, keyprefix, directory, manifest, privatekey, retry_stuff, sigv, region) ⇒ Object

Main method.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ec2/amitools/downloadbundle.rb', line 101

def download_bundle(url,
                    user,
                    pass,
                    bucket,
                    keyprefix,
                    directory,
                    manifest,
                    privatekey,
                    retry_stuff,
                    sigv,
                    region)
  begin
    s3_conn = make_s3_connection(url, user, pass, bucket, sigv, region)
    # Download and decrypt manifest.
    manifest_path = File.join(directory, manifest)
    manifest_xml = download_manifest(s3_conn, bucket, keyprefix+manifest, manifest_path, privatekey, retry_stuff)
    
    # Download AMI parts.
    get_part_filenames(manifest_xml).each do |filename|
      download_part(s3_conn, bucket, keyprefix+filename, File::join(directory, filename), retry_stuff)
      $stdout.puts "Downloaded #{filename} from #{bucket}"
    end
  rescue EC2::Common::HTTP::Error => e
    $stderr.puts e.backtrace if @debug
    raise S3Error.new(e.message)
  end
end

#download_file(s3_conn, bucket, file, file_path, retry_download = false) ⇒ Object

—————————————————————————-#



63
64
65
66
67
68
69
70
71
72
# File 'lib/ec2/amitools/downloadbundle.rb', line 63

def download_file(s3_conn, bucket, file, file_path, retry_download=false)
  retry_s3(retry_download) do
    begin
      s3_conn.get(bucket, file, file_path)
      return
    rescue => e
      raise TryFailed.new("Failed to download \"#{file}\": #{e.message}")
    end
  end
end

#download_manifest(s3_conn, bucket, manifest_name, manifest_path, privatekey, retry_download = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ec2/amitools/downloadbundle.rb', line 39

def download_manifest(s3_conn, bucket, manifest_name, manifest_path, privatekey, retry_download=false)
  $stdout.puts "Downloading manifest #{manifest_name} from #{bucket} to #{manifest_path} ..."
  download_file(s3_conn, bucket, manifest_name, manifest_path, retry_download)
  encrypted_manifest = File::open(manifest_path) { |f| f.read() }
  plaintext_manifest = nil
  if (encrypted_manifest !~ /^\s*<\?/)
    $stdout.puts "Decrypting manifest ..."
    plaintext_manifest = Crypto::decryptasym(encrypted_manifest, privatekey)
    File::open(manifest_path+'.plaintext', 'w') { |f| f.write(plaintext_manifest) }
  else
    plaintext_manifest = encrypted_manifest
  end
  plaintext_manifest
end

#download_part(s3_conn, bucket, part, part_path, retry_download = false) ⇒ Object

—————————————————————————-#



56
57
58
59
# File 'lib/ec2/amitools/downloadbundle.rb', line 56

def download_part(s3_conn, bucket, part, part_path, retry_download=false)
  $stdout.puts "Downloading part #{part} to #{part_path} ..."
  download_file(s3_conn, bucket, part, part_path, retry_download)
end

#get_manualObject

——————————————————————————# Overrides ——————————————————————————#



133
134
135
# File 'lib/ec2/amitools/downloadbundle.rb', line 133

def get_manual()
  DOWNLOAD_BUNDLE_MANUAL
end

#get_nameObject



137
138
139
# File 'lib/ec2/amitools/downloadbundle.rb', line 137

def get_name()
  DOWNLOAD_BUNDLE_NAME
end

#get_part_filenames(manifest_xml) ⇒ Object

—————————————————————————-#



76
77
78
79
# File 'lib/ec2/amitools/downloadbundle.rb', line 76

def get_part_filenames(manifest_xml)
  manifest = ManifestV20071010.new(manifest_xml)
  manifest.parts.collect { |part| part.filename }.sort
end

#main(p) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ec2/amitools/downloadbundle.rb', line 141

def main(p)
  download_bundle(p.url,
                  p.user,
                  p.pass,
                  p.bucket,
                  p.keyprefix,
                  p.directory,
                  p.manifest,
                  p.privatekey,
                  p.retry,
                  p.sigv,
                  p.region)
end

#make_s3_connection(s3_url, user, pass, bucket, sigv, region) ⇒ Object

—————————————————————————-#



91
92
93
94
95
96
# File 'lib/ec2/amitools/downloadbundle.rb', line 91

def make_s3_connection(s3_url, user, pass, bucket, sigv, region)
  s3_uri = URI.parse(s3_url)
  s3_url = uri2string(s3_uri)
  v2_bucket = EC2::Common::S3Support::bucket_name_s3_v2_safe?(bucket)
  EC2::Common::S3Support.new(s3_url, user, pass, (v2_bucket ? nil : :path), @debug, sigv, region)
end

#uri2string(uri) ⇒ Object

—————————————————————————-#



83
84
85
86
87
# File 'lib/ec2/amitools/downloadbundle.rb', line 83

def uri2string( uri )
  s = "#{uri.scheme}://#{uri.host}:#{uri.port}#{uri.path}"
  # Remove the trailing '/'.
  return ( s[-1..-1] == "/" ? s[0..-2] : s )
end