class BundleDownloader

Public Instance Methods

download_bundle(url, user, pass, bucket, keyprefix, directory, manifest, privatekey, retry_stuff, sigv, region) click to toggle source

Main method.

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_manual() click to toggle source
# File lib/ec2/amitools/downloadbundle.rb, line 133
def get_manual()
  DOWNLOAD_BUNDLE_MANUAL
end
get_name() click to toggle source
# File lib/ec2/amitools/downloadbundle.rb, line 137
def get_name()
  DOWNLOAD_BUNDLE_NAME
end
get_part_filenames(manifest_xml) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 ) click to toggle source
# 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