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

#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

#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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 239

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



223
224
225
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 223

def amazon
  Infrastructure.current
end

#attributesObject



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

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

#dnsObject



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

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

#ec2_id=(id) ⇒ Object



69
70
71
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 69

def ec2_id=(id)
  aws_instance_id = id
end

#fieldsObject



81
82
83
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 81

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

#idObject Also known as: ec2_id



40
41
42
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 40

def id
  @aws_instance_id
end

#identified_by?(arg) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 17

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

#imageObject



227
228
229
230
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 227

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

#ipObject



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

def ip
  Infrastructure.dns.getaddress(dns).to_s
end

#keyObject



142
143
144
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 142

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

#no_sdb?Boolean

Returns:

  • (Boolean)


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

def no_sdb?
  !@set_sdb
end

#optionsObject



215
216
217
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 215

def options
  amazon.options
end

#set_sdb_attributes(args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 51

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 = v.is_a?(Array) ? v : Array(v)
    when 'disabled_services'
      @disabled_services = v.is_a?(Array) ? v : Array(v)
    else
      self.send("#{k}=", v)
    end
  end
  @set_sdb = true
end

#set_state(state) ⇒ Object



210
211
212
213
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 210

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



92
93
94
95
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
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 92

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



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

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

#typeObject



232
233
234
235
236
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 232

def type
  return @aws_instance_type if @aws_instance_type

  @aws_instance_type = self.class.size_to_type size
end

#user_dataObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 146

def user_data
  %Q@#!/bin/bash

  CLUSTER_USER=ubuntu
  CLUSTER_DIR=/home/${CLUSTER_USER}/.cluster
  LOGGER=${CLUSTER_DIR}/cluster.log
  MONITOR=${CLUSTER_DIR}/monitor.god
  CREDENTIALS=${CLUSTER_DIR}/credentials.yml
  CLUSTER_EXECUTABLE=$(which cluster)
  GEM_PATH=$(gem env gempath)
  GEM_BIN_DIR=$(dirname ${CLUSTER_EXECUTABLE})
  GOD=$(type -p god)

  if [[ ! -e $CLUSTER_DIR ]]; then
    mkdir -m 700 $CLUSTER_DIR
  fi

  if [[ ! -e $CREDENTIALS ]]; then
    wget --no-check-certificate -O $CREDENTIALS '#{amazon.credentials_url}'
  fi

  if [[ ! -e $CREDENTIALS ]]; then
    echo "Cannot start without credentials! (perhaps a cluster save_credentials is in order)"
    exit 1
  fi

  chmod 600 $CREDENTIALS
  chown $CLUSTER_USER $CREDENTIALS
  cat >>$CREDENTIALS <<EOF
  cluster:
    id: #{aws_id}
    services: #{services.join(' ')}
  EOF

  CLUSTER='X'
  if type -P cluster >/dev/null; then
    CLUSTER=cluster
  elif [[ -x $GEM_BIN_DIR/cluster ]]; then
    CLUSTER="$GEM_BIN_DIR/cluster"
  fi

  if [[ ! $CLUSTER == 'X' ]]; then
    CLUSTER="$CLUSTER --credentials=${CREDENTIALS} --logger=${LOGGER}"
    $CLUSTER instance_state starting

    $CLUSTER fetch_monitor ${MONITOR}
    if [[ -e $MONITOR ]]; then
      chmod 600 $MONITOR
      chown $CLUSTER_USER $MONITOR
      mv $MONITOR /etc/god/init.d/cluster.god
      $GOD load /etc/god/init.d/010_cluster.god
    else
      echo "NO MONITOR FILE!"
      exit 1
    fi
  else
    echo "NO CLUSTER!"
    exit 1
  fi

  chown -R $CLUSTER_USER:$CLUSTER_USER $CLUSTER_DIR
  @.gsub(/^    /, '')
end

#zoneObject



219
220
221
# File 'lib/cluster/infrastructures/amazon_instance.rb', line 219

def zone
  amazon.options.zone
end