Class: Rubber::Cloud::Aws

Inherits:
Base
  • Object
show all
Defined in:
lib/rubber/cloud/aws.rb

Instance Attribute Summary

Attributes inherited from Base

#capistrano, #env

Instance Method Summary collapse

Constructor Details

#initialize(env, capistrano) ⇒ Aws

Returns a new instance of Aws.



10
11
12
13
14
15
16
# File 'lib/rubber/cloud/aws.rb', line 10

def initialize(env, capistrano)
  super(env, capistrano)
  @aws_env = env.cloud_providers.aws
  @ec2 = AWS::EC2::Base.new(:access_key_id => @aws_env.access_key, :secret_access_key => @aws_env.secret_access_key)
  @ec2elb = AWS::ELB::Base.new(:access_key_id => @aws_env.access_key, :secret_access_key => @aws_env.secret_access_key)
  AWS::S3::Base.establish_connection!(:access_key_id => @aws_env.access_key, :secret_access_key => @aws_env.secret_access_key)
end

Instance Method Details

#add_security_group_rule(group_name, protocol, from_port, to_port, source) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/rubber/cloud/aws.rb', line 131

def add_security_group_rule(group_name, protocol, from_port, to_port, source)
  opts = {:group_name => group_name}
  if source.instance_of? Hash
    opts = opts.merge(:source_security_group_name => source[:name], :source_security_group_owner_id => source[:account])
  else
    opts = opts.merge(:ip_protocol => protocol, :from_port => from_port, :to_port => to_port, :cidr_ip => source)
  end
  @ec2.authorize_security_group_ingress(opts)
end

#attach_static_ip(ip, instance_id) ⇒ Object



160
161
162
163
# File 'lib/rubber/cloud/aws.rb', line 160

def attach_static_ip(ip, instance_id)
  response = @ec2.associate_address(:instance_id => instance_id, :public_ip => ip)
  return response.return == "true"
end

#attach_volume(volume_id, instance_id, device) ⇒ Object



194
195
196
197
# File 'lib/rubber/cloud/aws.rb', line 194

def attach_volume(volume_id, instance_id, device)
  response = @ec2.attach_volume(:volume_id => volume_id, :instance_id => instance_id, :device => device)
  return response.status
end

#create_image(image_name) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/rubber/cloud/aws.rb', line 226

def create_image(image_name)
  ec2_key = @aws_env.key_file
  ec2_pk = @aws_env.pk_file
  ec2_cert = @aws_env.cert_file
  ec2_key_dest = "/mnt/#{File.basename(ec2_key)}"
  ec2_pk_dest = "/mnt/#{File.basename(ec2_pk)}"
  ec2_cert_dest = "/mnt/#{File.basename(ec2_cert)}"

  capistrano.put(File.read(ec2_key), ec2_key_dest)
  capistrano.put(File.read(ec2_pk), ec2_pk_dest)
  capistrano.put(File.read(ec2_cert), ec2_cert_dest)

  arch = capistrano.capture("uname -m").strip
  arch = case arch when /i\d86/ then "i386" else arch end

  capistrano.sudo_script "create_bundle", <<-CMD
    rvm use system
    export RUBYLIB=/usr/lib/site_ruby/
    unset RUBYOPT
    nohup ec2-bundle-vol --batch -d /mnt -k #{ec2_pk_dest} -c #{ec2_cert_dest} -u #{@aws_env.} -p #{image_name} -r #{arch} &> /tmp/ec2-bundle-vol.log &
    echo "Creating image from instance volume..."
    while true; do
      if ! ps ax | grep -q "[e]c2-bundle-vol"; then exit; fi
      echo -n .
      sleep 1
    done
  CMD

  capistrano.sudo_script "register_bundle", <<-CMD
    rvm use system
    export RUBYLIB=/usr/lib/site_ruby/
    unset RUBYOPT
    echo "Uploading image to S3..."
    ec2-upload-bundle --batch -b #{@aws_env.image_bucket} -m /mnt/#{image_name}.manifest.xml -a #{@aws_env.access_key} -s #{@aws_env.secret_access_key}
  CMD

  image_location = "#{@aws_env.image_bucket}/#{image_name}.manifest.xml"
  response = @ec2.register_image(:image_location => image_location)
  return response.imageId
end

#create_instance(ami, ami_type, security_groups, availability_zone) ⇒ Object



18
19
20
21
22
# File 'lib/rubber/cloud/aws.rb', line 18

def create_instance(ami, ami_type, security_groups, availability_zone)
  response = @ec2.run_instances(:image_id => ami, :key_name => @aws_env.key_name, :instance_type => ami_type, :security_group => security_groups, :availability_zone => availability_zone)
  instance_id = response.instancesSet.item[0].instanceId
  return instance_id
end

#create_security_group(group_name, group_description) ⇒ Object



84
85
86
# File 'lib/rubber/cloud/aws.rb', line 84

def create_security_group(group_name, group_description)
  @ec2.create_security_group(:group_name => group_name, :group_description => group_description)
end

#create_spot_instance_request(spot_price, ami, ami_type, security_groups, availability_zone) ⇒ Object



24
25
26
27
28
# File 'lib/rubber/cloud/aws.rb', line 24

def create_spot_instance_request(spot_price, ami, ami_type, security_groups, availability_zone)
  response = @ec2.request_spot_instances(:spot_price => spot_price, :image_id => ami, :key_name => @aws_env.key_name, :instance_type => ami_type, :security_group => security_groups, :availability_zone => availability_zone)
  request_id = response.spotInstanceRequestSet.item[0].spotInstanceRequestId
  return request_id
end

#create_static_ipObject



155
156
157
158
# File 'lib/rubber/cloud/aws.rb', line 155

def create_static_ip
  response = @ec2.allocate_address()
  return response.publicIp
end

#create_volume(size, zone) ⇒ Object



189
190
191
192
# File 'lib/rubber/cloud/aws.rb', line 189

def create_volume(size, zone)
  response = @ec2.create_volume(:size => size.to_s, :availability_zone => zone)
  return response.volumeId
end

#describe_availability_zonesObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubber/cloud/aws.rb', line 72

def describe_availability_zones
  zones = []
  response = @ec2.describe_availability_zones()
  response.availabilityZoneInfo.item.each do |item|
    zone = {}
    zone[:name] = item.zoneName
    zone[:state] =item.zoneState
    zones << zone
  end if response.availabilityZoneInfo
  return zones
end

#describe_images(image_id = nil) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/rubber/cloud/aws.rb', line 267

def describe_images(image_id=nil)
  images = []
  opts = {:owner_id => 'self'}
  opts[:image_id] = image_id if image_id
  response = @ec2.describe_images(opts)
  response.imagesSet.item.each do |item|
    image = {}
    image[:id] = item.imageId
    image[:location] = item.imageLocation
    image[:root_device_type] = item.rootDeviceType
    images << image
  end if response.imagesSet
  return images
end

#describe_instances(instance_id = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubber/cloud/aws.rb', line 30

def describe_instances(instance_id=nil)
  instances = []
  opts = {}
  opts[:instance_id] = instance_id if instance_id

  response = @ec2.describe_instances(opts)
  response.reservationSet.item.each do |ritem|
    ritem.instancesSet.item.each do |item|
      instance = {}
      instance[:id] = item.instanceId
      instance[:external_host] = item.dnsName
      instance[:external_ip] = item.ipAddress
      instance[:internal_host] = item.privateDnsName
      instance[:internal_ip] = item.privateIpAddress
      instance[:state] = item.instanceState.name
      instance[:zone] = item.placement.availabilityZone
      instance[:platform] = item.platform || 'linux'
      instance[:root_device_type] = item.rootDeviceType
      instances << instance
    end
  end if response.reservationSet

  return instances
end

#describe_load_balancers(name = nil) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/rubber/cloud/aws.rb', line 304

def describe_load_balancers(name=nil)
  lbs = []
  opts = {}
  opts[:load_balancer_names] = name if name
  response = @ec2elb.describe_load_balancers(opts)
  response.describeLoadBalancersResult.member.each do |member|
    lb = {}
    lb[:name] = member.loadBalancerName
    lb[:dns_name] = member.dNSName

    member.availabilityZones.member.each do |zone|
      lb[:zones] ||= []
      lb[:zones] << zone
    end

    member.listeners.member.each do |member|
      listener = {}
      listener[:protocol] = member.protocol
      listener[:port] = member.loadBalancerPort
      listener[:instance_port] = member.instancePort
      lb[:listeners] ||= []
      lb[:listeners] << listener
    end

    lbs << lb
  end if response.describeLoadBalancersResult
  return lbs
end

#describe_security_groups(group_name = nil) ⇒ Object



88
89
90
91
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
127
128
129
# File 'lib/rubber/cloud/aws.rb', line 88

def describe_security_groups(group_name=nil)
  groups = []

  opts = {}
  opts[:group_name] = group_name if group_name
  response = @ec2.describe_security_groups(opts)

  response.securityGroupInfo.item.each do |item|
    group = {}
    group[:name] = item.groupName
    group[:description] = item.groupDescription

    item.ipPermissions.item.each do |ip_item|
      group[:permissions] ||= []
      rule = {}

      rule[:protocol] = ip_item.ipProtocol
      rule[:from_port] = ip_item.fromPort
      rule[:to_port] = ip_item.toPort

      ip_item.groups.item.each do |rule_group|
        rule[:source_groups] ||= []
        source_group = {}
        source_group[:account] = rule_group.userId
        source_group[:name] = rule_group.groupName
        rule[:source_groups] << source_group
      end if ip_item.groups

      ip_item.ipRanges.item.each do |ip_range|
        rule[:source_ips] ||= []
        rule[:source_ips] << ip_range.cidrIp
      end if ip_item.ipRanges

      group[:permissions] << rule
    end if item.ipPermissions

    groups << group
    
  end if response.securityGroupInfo

  return groups
end

#describe_spot_instance_requests(request_id = nil) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/rubber/cloud/aws.rb', line 333

def describe_spot_instance_requests(request_id=nil)
  requests = []
  opts = {}
  opts[:spot_instance_request_id] = request_id if request_id
  response = @ec2.describe_spot_instance_requests(opts)
  response.spotInstanceRequestSet.item.each do |item|
    request = {}
    request[:id] = item.spotInstanceRequestId
    request[:spot_price] = item.spotPrice
    request[:state] = item.state
    request[:created_at] = item.createTime
    request[:type] = item.launchSpecification.instanceType
    request[:image_id] = item.launchSpecification.imageId
    request[:instance_id] = item.instanceId
    requests << request
  end if response.spotInstanceRequestSet
  return requests
end

#describe_static_ips(ip = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rubber/cloud/aws.rb', line 170

def describe_static_ips(ip=nil)
  ips = []
  opts = {}
  opts[:public_ip] = ip if ip
  response = @ec2.describe_addresses(opts)
  response.addressesSet.item.each do |item|
    ip = {}
    ip[:instance_id] = item.instanceId
    ip[:ip] = item.publicIp
    ips << ip
  end if response.addressesSet
  return ips
end

#describe_volumes(volume_id = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rubber/cloud/aws.rb', line 203

def describe_volumes(volume_id=nil)
  volumes = []
  opts = {}
  opts[:volume_id] = volume_id if volume_id
  response = @ec2.describe_volumes(opts)
  response.volumeSet.item.each do |item|
    volume = {}
    volume[:id] = item.volumeId
    volume[:status] = item.status
    if item.attachmentSet
      attach = item.attachmentSet.item[0]
      volume[:attachment_instance_id] = attach.instanceId
      volume[:attachment_status] = attach.status
    end
    volumes << volume
  end if response.volumeSet
  return volumes
end

#destroy_image(image_id) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/rubber/cloud/aws.rb', line 282

def destroy_image(image_id)
  image = describe_images(image_id).first
  raise "Could not find image: #{image_id}, aborting destroy_image" if image.nil?
  image_location = image[:location]
  bucket = image_location.split('/').first
  image_name = image_location.split('/').last.gsub(/\.manifest\.xml$/, '')

  @ec2.deregister_image(:image_id => image_id)

  s3_bucket = AWS::S3::Bucket.find(bucket)
  s3_bucket.objects(:prefix => image_name).clone.each do |obj|
    obj.delete
  end
  if s3_bucket.empty?
    s3_bucket.delete
  end
end

#destroy_instance(instance_id) ⇒ Object



55
56
57
# File 'lib/rubber/cloud/aws.rb', line 55

def destroy_instance(instance_id)
  response = @ec2.terminate_instances(:instance_id => instance_id)
end

#destroy_security_group(group_name) ⇒ Object



151
152
153
# File 'lib/rubber/cloud/aws.rb', line 151

def destroy_security_group(group_name)
  @ec2.delete_security_group(:group_name => group_name)
end

#destroy_spot_instance_request(request_id) ⇒ Object



300
301
302
# File 'lib/rubber/cloud/aws.rb', line 300

def destroy_spot_instance_request(request_id)
  @ec2.cancel_spot_instance_requests :spot_instance_request_id => request_id
end

#destroy_static_ip(ip) ⇒ Object



184
185
186
187
# File 'lib/rubber/cloud/aws.rb', line 184

def destroy_static_ip(ip)
  response = @ec2.release_address(:public_ip => ip)
  return response.return == "true"
end

#destroy_volume(volume_id) ⇒ Object



222
223
224
# File 'lib/rubber/cloud/aws.rb', line 222

def destroy_volume(volume_id)
  @ec2.delete_volume(:volume_id => volume_id)
end

#detach_static_ip(ip) ⇒ Object



165
166
167
168
# File 'lib/rubber/cloud/aws.rb', line 165

def detach_static_ip(ip)
  response = @ec2.disassociate_address(:public_ip => ip)
  return response.return == "true"
end

#detach_volume(volume_id) ⇒ Object



199
200
201
# File 'lib/rubber/cloud/aws.rb', line 199

def detach_volume(volume_id)
  @ec2.detach_volume(:volume_id => volume_id, :force => 'true')
end

#reboot_instance(instance_id) ⇒ Object



59
60
61
# File 'lib/rubber/cloud/aws.rb', line 59

def reboot_instance(instance_id)
  response = @ec2.reboot_instances(:instance_id => instance_id)
end

#remove_security_group_rule(group_name, protocol, from_port, to_port, source) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/rubber/cloud/aws.rb', line 141

def remove_security_group_rule(group_name, protocol, from_port, to_port, source)
  opts = {:group_name => group_name}
  if source.instance_of? Hash
    opts = opts.merge(:source_security_group_name => source[:name], :source_security_group_owner_id => source[:account])
  else
    opts = opts.merge(:ip_protocol => protocol, :from_port => from_port, :to_port => to_port, :cidr_ip => source)
  end
  @ec2.revoke_security_group_ingress(opts)
end

#start_instance(instance_id) ⇒ Object



68
69
70
# File 'lib/rubber/cloud/aws.rb', line 68

def start_instance(instance_id)
  response = @ec2.start_instances(:instance_id => instance_id)
end

#stop_instance(instance_id) ⇒ Object



63
64
65
66
# File 'lib/rubber/cloud/aws.rb', line 63

def stop_instance(instance_id)
  # Don't force the stop process. I.e., allow the instance to flush its file system operations.
  response = @ec2.stop_instances(:instance_id => instance_id, :force => false)
end