Class: EC2ResponseObject

Inherits:
Object show all
Defined in:
lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb

Overview

Convenience class to convert standard amazon-ec2 responses from their camel cased style to a hash using underscore style. For example: instanceId to instance_id

Class Method Summary collapse

Class Method Details

.convert_from_ec2_dns_to_ip(str) ⇒ Object



120
121
122
123
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 120

def self.convert_from_ec2_dns_to_ip(str)
  return nil if str.nil?
  str.scan(/-(\d{1,3})-(\d{1,3})-(\d{1,3})-(\d{1,3})/).flatten.join('.')
end

.describe_instance(response, index = 0) ⇒ Object

Convert the standard reponse into output similar to this example =>“ec2-75-101-175-49.compute-1.amazonaws.com”,

:private_dns_name  =>"domU-11-31-39-00-DC-78.compute-1.internal",
:reason            =>nil,
:instance_state    =>{"name"=>"running", "code"=>"16",
:kernel_id         =>"aki-a71cf9ce",
:ramdisk_id        =>"ari-a51cf9cc",
:placement         =>"availabilityZone"=>"us-east-1a",
:product_codes     =>nil,
:image_id          =>"ami-bf5eb9d6",
:launch_time       =>"2009-05-29T05:07:09.000Z",
:key_name          =>"poolname_cloudname",
:instance_id       =>"i-1b7b2942",
:ami_launch_index  =>"0",
:instance_type     =>"m1.small"}

Selects the first instance if an index is not given.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 86

def self.describe_instance(response, index=0)
  inst = if response.has_key?("reservationSet")
    response['reservationSet']['item'].first['instancesSet']['item'][index]
  elsif response.has_key?("instancesSet")
    response['instancesSet']['item'][index]
  else
    raise StandardError.new("EC2ResponseObject was given a response it doesn't know about\n\t#{response.inspect}")
  end
  
  # Ec2RemoteInstance.new(
  symbolize_and_snakecase(inst)
end

.describe_instances(response) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 99

def self.describe_instances(response)
  return [] if response['reservationSet'].nil?
  ec2_insts = response['reservationSet']['item'].collect do |ri|
    ri['instancesSet']['item'].collect{|i| i}
  end
  ec2_insts.flatten.collect {|i| symbolize_and_snakecase(i) }
end

.get_descriptions(resp) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 4

def self.get_descriptions(resp)          
  rs = get_instance_from_response(resp)
  group = get_group_from_response(resp)
  
  # puts rs.methods.sort - rs.ancestors.methods
  out = begin
    if rs.respond_to?(:instancesSet)
      [EC2ResponseObject.describe_instances(rs)]
    else
      rs.collect {|r| 
        if r.instancesSet.item.class == Array
          r.instancesSet.item.map {|t| EC2ResponseObject.describe_instances(t)}
        else
          [EC2ResponseObject.describe_instances(r)]
        end            
      }.flatten.reject {|a| a.nil? }
    end
  rescue Exception => e
    # Really weird bug with amazon's ec2 gem
    rs.collect {|r| EC2ResponseObject.describe_instances(r)}.reject {|a| a.nil? } rescue []
  end

  out
end

.get_group_from_response(resp) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 39

def self.get_group_from_response(resp)
  begin
    resp = resp.reservationSet.item.first if resp.reservationSet.item.is_a?(Array)
    group = resp.reservationSet.item.groupSet.item.groupId unless resp.reservationSet.nil?
    group ||= resp.groupSet.item[0].groupId rescue nil
    group ||= resp.DescribeInstancesResponse.reservationSet.item.groupSet.item.groupId
    #rs ||= rs.respond_to?(:instancesSet) ? rs.instancesSet : rs
    #rs.reject! {|a| a.nil? || a.empty? }
  rescue Exception => e
    resp
  end
  group
end

.get_hash_from_response(resp, group = 'default') ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 52

def self.get_hash_from_response(resp, group = 'default')
    symbolize_and_snakecase
    {
      :instance_id => resp.instanceId,
      :name => resp.instanceId,
      :status => resp.instanceState.name,
      :public_ip => resp.dnsName || "not-assigned",
      :ip => resp.dnsName || "not-assigned",
      :internal_ip => resp.privateDnsName,
      :launching_time => resp.launchTime.parse_datetime,
      :keypair => (resp.keyName rescue ""),
      :security_group => group
    }
end

.get_instance_from_response(resp) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 28

def self.get_instance_from_response(resp)
  begin
    rs = resp.reservationSet.item unless resp.reservationSet.nil?
    rs ||= resp.DescribeInstancesResponse.reservationSet.item
    rs ||= rs.respond_to?(:instancesSet) ? rs.instancesSet : rs
    rs.reject! {|a| a.nil? || a.empty? }
  rescue Exception => e
    resp
  end
  rs
end

.parse_datetime(str) ⇒ Object



125
126
127
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 125

def self.parse_datetime(str)
  DateTime.parse( str.chomp ) rescue self
end

.symbolize_and_snakecase(inst) ⇒ Object

Convert the standard response hash to format used throughout the rest of PoolParty code. And add in some more values we rely on



109
110
111
112
113
114
115
116
117
118
# File 'lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb', line 109

def self.symbolize_and_snakecase(inst)
  n = inst.symbolize_keys(:snake_case)
  n[:internal_ip] = convert_from_ec2_dns_to_ip(n[:private_dns_name])
  n[:public_ip]   = convert_from_ec2_dns_to_ip(n[:dns_name])
  n[:ip]          = n[:public_ip]
  n[:launch_time] = parse_datetime(n[:launch_time])
  n[:status]      = n[:instance_state][:name]
  n[:availability_zone] = n[:placement][:availability_zone]
  n
end