class EC2::EC2Client

Attributes

aws_access_key_id[RW]
aws_secret_access_key[RW]
http[RW]
verbose[RW]

Public Class Methods

new(akid, secretkey, url) click to toggle source
# File lib/ec2/amitools/minimalec2.rb, line 36
def initialize(akid, secretkey, url)
  @aws_access_key_id = akid
  @aws_secret_access_key = secretkey
  server, port, is_secure = parse_url(url)
  @http = Net::HTTP.new(server, port)
  @http.use_ssl = is_secure
  @verbose = false
end

Public Instance Methods

describe_images(imageIds=[], kwargs={}) click to toggle source
# File lib/ec2/amitools/minimalec2.rb, line 58
def describe_images(imageIds=[], kwargs={})
  params = pathlist("ImageId", imageIds)
  params.merge!(pathlist("Owner", kwargs[:owners])) if kwargs[:owners]
  params.merge!(pathlist("ExecutableBy", kwargs[:executableBy])) if kwargs[:executableBy]
  make_request("DescribeImages", params)
end
describe_regions(regionNames=[]) click to toggle source
# File lib/ec2/amitools/minimalec2.rb, line 53
def describe_regions(regionNames=[])
  params = pathlist("regionName", regionNames)
  make_request("DescribeRegions", params)
end
encode(aws_secret_access_key, str, urlencode=true) click to toggle source

Encodes the given string with the aws_secret_access_key, by taking the hmac-sha1 sum, and then base64 encoding it. Optionally, it will also url encode the result of that to protect the string if it's going to be used as a query string parameter.

# File lib/ec2/amitools/minimalec2.rb, line 106
def encode(aws_secret_access_key, str, urlencode=true)
  digest = OpenSSL::Digest::Digest.new('sha1')
  b64_hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, aws_secret_access_key, str)).strip
  if urlencode
    return CGI::escape(b64_hmac)
  else
    return b64_hmac
  end
end
make_request(action, params, data='') click to toggle source
# File lib/ec2/amitools/minimalec2.rb, line 65
def make_request(action, params, data='')
  resp = nil
  @http.start do
    params.merge!({ "Action"=>action,
                    "SignatureVersion"=>"1",
                    "AWSAccessKeyId"=>@aws_access_key_id,
                    "Version"=> "2008-12-01",
                    "Timestamp"=>Time.now.getutc.iso8601,
                  })
    p params if @verbose
    
    canonical_string = params.sort_by { |param| param[0].downcase }.map { |param| param.join }.join
    puts canonical_string if @verbose
    sig = encode(@aws_secret_access_key, canonical_string)
    
    path = "?" + params.sort.collect do |param|
      CGI::escape(param[0]) + "=" + CGI::escape(param[1])
    end.join("&") + "&Signature=" + sig
    
    puts path if @verbose
    
    req = Net::HTTP::Get.new("/#{path}")
    
    # ruby will automatically add a random content-type on some verbs, so
    # here we add a dummy one to 'supress' it.  change this logic if having
    # an empty content-type header becomes semantically meaningful for any
    # other verb.
    req['Content-Type'] ||= ''
    req['User-Agent'] = 'ec2-migrate-manifest #{PKG_VERSION}-#{PKG_RELEASE}'

    data = nil unless req.request_body_permitted?
    resp = @http.request(req, data)

  end
  REXML::Document.new(resp.body)
end
parse_url(url) click to toggle source
# File lib/ec2/amitools/minimalec2.rb, line 27
def parse_url(url)
  bits = url.split(":")
  secure = {"https"=>true, "http"=>false}[bits[0]]
  port = secure ? 443 : 80
  port = Integer(bits[2]) if bits.size > 2
  server = bits[1][2..-1]
  [server, port, secure]
end
pathlist(key, arr) click to toggle source
# File lib/ec2/amitools/minimalec2.rb, line 45
def pathlist(key, arr)
  params = {}
  arr.each_with_index do |value, i|
    params["#{key}.#{i+1}"] = value
  end
  params
end