Class: EC2::EC2Client
- Inherits:
-
Object
- Object
- EC2::EC2Client
- Defined in:
- lib/ec2/amitools/minimalec2.rb
Instance Attribute Summary collapse
-
#aws_access_key_id ⇒ Object
Returns the value of attribute aws_access_key_id.
-
#aws_secret_access_key ⇒ Object
Returns the value of attribute aws_secret_access_key.
-
#http ⇒ Object
Returns the value of attribute http.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #describe_images(imageIds = [], kwargs = {}) ⇒ Object
- #describe_regions(regionNames = []) ⇒ Object
-
#encode(aws_secret_access_key, str, urlencode = true) ⇒ Object
Encodes the given string with the aws_secret_access_key, by taking the hmac-sha1 sum, and then base64 encoding it.
-
#initialize(akid, secretkey, url) ⇒ EC2Client
constructor
A new instance of EC2Client.
- #make_request(action, params, data = '') ⇒ Object
- #parse_url(url) ⇒ Object
- #pathlist(key, arr) ⇒ Object
Constructor Details
#initialize(akid, secretkey, url) ⇒ EC2Client
Returns a new instance of EC2Client.
36 37 38 39 40 41 42 43 |
# 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 |
Instance Attribute Details
#aws_access_key_id ⇒ Object
Returns the value of attribute aws_access_key_id.
23 24 25 |
# File 'lib/ec2/amitools/minimalec2.rb', line 23 def aws_access_key_id @aws_access_key_id end |
#aws_secret_access_key ⇒ Object
Returns the value of attribute aws_secret_access_key.
24 25 26 |
# File 'lib/ec2/amitools/minimalec2.rb', line 24 def aws_secret_access_key @aws_secret_access_key end |
#http ⇒ Object
Returns the value of attribute http.
25 26 27 |
# File 'lib/ec2/amitools/minimalec2.rb', line 25 def http @http end |
#verbose ⇒ Object
Returns the value of attribute verbose.
22 23 24 |
# File 'lib/ec2/amitools/minimalec2.rb', line 22 def verbose @verbose end |
Instance Method Details
#describe_images(imageIds = [], kwargs = {}) ⇒ Object
58 59 60 61 62 63 |
# 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 = []) ⇒ Object
53 54 55 56 |
# 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) ⇒ Object
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.
106 107 108 109 110 111 112 113 114 |
# 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 = '') ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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) ⇒ Object
27 28 29 30 31 32 33 34 |
# 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) ⇒ Object
45 46 47 48 49 50 51 |
# 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 |