Class: AmazonInstance

Inherits:
Instance show all
Defined in:
lib/cluster/infrastructures/amazon_instance.rb

Instance Attribute Summary collapse

Attributes inherited from Instance

#address, #disabled_services, #friendly_name, #label, #services, #size, #start_time, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Instance

create, #disable, #enable, #initialize, #to_s

Constructor Details

This class inherits a constructor from Instance

Instance Attribute Details

#aws_availability_zoneObject

Returns the value of attribute aws_availability_zone.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def aws_availability_zone
  @aws_availability_zone
end

#aws_groupsObject Also known as: groups

Returns the value of attribute aws_groups.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def aws_groups
  @aws_groups
end

#aws_idObject

Returns the value of attribute aws_id.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def aws_id
  @aws_id
end

#aws_image_idObject

Returns the value of attribute aws_image_id.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def aws_image_id
  @aws_image_id
end

#aws_instance_typeObject

Returns the value of attribute aws_instance_type.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def aws_instance_type
  @aws_instance_type
end

#aws_launch_timeObject

Returns the value of attribute aws_launch_time.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def aws_launch_time
  @aws_launch_time
end

#aws_stateObject

Returns the value of attribute aws_state.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def aws_state
  @aws_state
end

#dns_nameObject

Returns the value of attribute dns_name.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def dns_name
  @dns_name
end

#ec2_idObject Also known as: id, aws_instance_id

Returns the value of attribute ec2_id.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def ec2_id
  @ec2_id
end

#ip_addressObject

Returns the value of attribute ip_address.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def ip_address
  @ip_address
end

#private_dns_nameObject

Returns the value of attribute private_dns_name.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def private_dns_name
  @private_dns_name
end

#private_ip_addressObject

Returns the value of attribute private_ip_address.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def private_ip_address
  @private_ip_address
end

#spot_priceObject

Returns the value of attribute spot_price.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def spot_price
  @spot_price
end

#ssh_key_nameObject

Returns the value of attribute ssh_key_name.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def ssh_key_name
  @ssh_key_name
end

#start_time_sortedObject

Returns the value of attribute start_time_sorted.



2
3
4
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 2

def start_time_sorted
  @start_time_sorted
end

Class Method Details

.size_to_type(size) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 183

def size_to_type(size)
  case size
  when 'minimum', 'basic'
    'm1.small'
  when 'average'
    'm1.large'
  when 'power'
    'c1.medium'
  when 'super'
    'c1.xlarge'
  when /^\w+\.\w+$/
    size
  end
end

Instance Method Details

#amazonObject



167
168
169
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 167

def amazon
  Infrastructure.current
end

#attributesObject



89
90
91
92
93
94
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 89

def attributes
  args = fields.inject({'entry' => 'machine'}) {|m, attr|
    m.merge attr => self.send(attr)
  }
  Amazon.to_sdb_attributes(args)
end

#dnsObject



39
40
41
42
43
44
45
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 39

def dns
  if Infrastructure.in_cluster?
    private_dns_name
  else
    dns_name
  end
end

#fieldsObject



85
86
87
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 85

def fields
  %w(aws_id label start_time_sorted spot_price services disabled_services friendly_name ec2_id state ip_address private_ip_address dns_name private_dns_name)
end

#identified_by?(arg) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 24

def identified_by?(arg)
  arg = arg.downcase
  super(arg) or @private_dns_name == arg or @dns_name == arg
end

#imageObject



171
172
173
174
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 171

def image
  return @aws_image_id if @aws_image_id
  amazon.get_image 64
end

#ipObject



47
48
49
50
51
52
53
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 47

def ip
  if Infrastructure.in_cluster?
    private_ip_address
  else
    ip_address
  end
end

#keyObject



146
147
148
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 146

def key
  groups.include?('access') ? 'access' : 'cloud'
end

#no_sdb?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 55

def no_sdb?
  !@set_sdb
end

#optionsObject



159
160
161
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 159

def options
  amazon.options
end

#removeObject



35
36
37
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 35

def remove
  amazon.sdb.delete_attributes amazon.domain, aws_id
end

#set_sdb_attributes(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 59

def set_sdb_attributes(args)
  args.each do |k, v|
    case k
    when 'start_time_sorted'
      self.start_time = Time.parse v
    when 'entry'
      # NOP
    when 'services'
      @services = Array(v)
    when 'disabled_services'
      @disabled_services = Array(v)
    else
      self.send("#{k}=", v)
    end
  end
  @set_sdb = true
end

#set_state(state) ⇒ Object



154
155
156
157
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 154

def set_state(state)
  amazon.sdb.put_attributes amazon.domain, aws_id, Amazon.to_sdb_attributes(:state => state), :replace
  @state = state
end

#start!(price = nil) ⇒ Object



96
97
98
99
100
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
128
129
130
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 96

def start!(price = nil)
  args = {
    :key_name => key,
    :user_data => user_data,
    :instance_type => type,
    :availability_zone => zone,
  }

  resp = if spot_price
           args.merge! :spot_price => spot_price,
             :instance_count => 1,
             :groups => groups,
             :image_id => image
           amazon.ecc.request_spot_instances args
         else
           args.merge! :group_ids => groups,
             :min_count => 1,
             :max_count => 1
           amazon.ecc.launch_instances image, args
         end

  if resp
    puts "INS RESP -> #{resp.inspect}"
    resp.first.keys.each do |k|
      func = "#{k.to_s}="
      send(func, resp.first[k]) if respond_to? func
    end

    res = amazon.sdb.put_attributes(amazon.domain, aws_id, attributes, :replace)
    puts "Res #{res.inspect}"
    self
  else
    nil
  end
end

#stop!Object



29
30
31
32
33
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 29

def stop!
  if amazon.ecc.terminate_instances(ec2_id)
    amazon.sdb.delete_attributes amazon.domain, aws_id
  end
end

#typeObject



176
177
178
179
180
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 176

def type
  return @aws_instance_type if @aws_instance_type

  @aws_instance_type = self.class.size_to_type size
end

#user_dataObject



150
151
152
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 150

def user_data
  ''
end

#zoneObject



163
164
165
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 163

def zone
  amazon.options.zone
end