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_idObject

Returns the value of attribute aws_instance_id.



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

def aws_instance_id
  @aws_instance_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

#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



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

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



169
170
171
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 169

def amazon
  Infrastructure.current
end

#attributesObject



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

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

#dnsObject



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

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

#ec2_id=(id) ⇒ Object



75
76
77
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 75

def ec2_id=(id)
  aws_instance_id = id
end

#fieldsObject



87
88
89
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 87

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

#idObject Also known as: ec2_id



46
47
48
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 46

def id
  @aws_instance_id
end

#identified_by?(arg) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 19

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

#imageObject



173
174
175
176
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 173

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

#ipObject



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

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

#keyObject



148
149
150
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 148

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

#no_sdb?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 53

def no_sdb?
  !@set_sdb
end

#optionsObject



161
162
163
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 161

def options
  amazon.options
end

#set_sdb_attributes(args) ⇒ Object



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

def set_sdb_attributes(args)
  args.each do |k, v|
    case k
    when 'start_time_sorted'
      self.start_time = Time.parse v
    when 'entry', 'ec2_id'
      # 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



156
157
158
159
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 156

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



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
131
132
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 98

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



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

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

#typeObject



178
179
180
181
182
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 178

def type
  return @aws_instance_type if @aws_instance_type

  @aws_instance_type = self.class.size_to_type size
end

#user_dataObject



152
153
154
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 152

def user_data
  ''
end

#zoneObject



165
166
167
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 165

def zone
  amazon.options.zone
end