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



116
117
118
119
120
121
122
123
124
# File 'lib/rubber/cloud/aws.rb', line 116

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



145
146
147
148
# File 'lib/rubber/cloud/aws.rb', line 145

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



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

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



216
217
218
219
220
221
222
223
224
225
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
# File 'lib/rubber/cloud/aws.rb', line 216

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



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

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



140
141
142
143
# File 'lib/rubber/cloud/aws.rb', line 140

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

#create_volume(size, zone, base_volume_id = nil) ⇒ Object



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

def create_volume(size, zone, base_volume_id = nil)
  snapshot = {}
  if base_volume_id
    base_snapshots = @ec2.describe_snapshots["snapshotSet"]["item"].select{|s|s["volumeId"] == base_volume_id}
    snapshot = base_snapshots.sort{|a,b| b["startTime"] <=> a["startTime"]}.first
  end
  response = @ec2.create_volume(:size => size.to_s, :availability_zone => zone, :snapshot_id => snapshot["snapshotId"])
  return response.volumeId
end

#describe_availability_zonesObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubber/cloud/aws.rb', line 57

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



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rubber/cloud/aws.rb', line 257

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
    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
# 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] = IPSocket.getaddress(instance[:external_host]) rescue nil
      instance[:internal_host] = item.privateDnsName
      instance[:state] = item.instanceState.name
      instance[:zone] = item.placement.availabilityZone
      instance[:platform] = item.platform || 'linux'
      instances << instance
    end
  end if response.reservationSet

  return instances
end

#describe_load_balancers(name = nil) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rubber/cloud/aws.rb', line 293

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
# File 'lib/rubber/cloud/aws.rb', line 73

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



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/rubber/cloud/aws.rb', line 322

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



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rubber/cloud/aws.rb', line 155

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rubber/cloud/aws.rb', line 193

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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rubber/cloud/aws.rb', line 271

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



53
54
55
# File 'lib/rubber/cloud/aws.rb', line 53

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

#destroy_security_group(group_name) ⇒ Object



136
137
138
# File 'lib/rubber/cloud/aws.rb', line 136

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

#destroy_spot_instance_request(request_id) ⇒ Object



289
290
291
# File 'lib/rubber/cloud/aws.rb', line 289

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

#destroy_static_ip(ip) ⇒ Object



169
170
171
172
# File 'lib/rubber/cloud/aws.rb', line 169

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

#destroy_volume(volume_id) ⇒ Object



212
213
214
# File 'lib/rubber/cloud/aws.rb', line 212

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

#detach_static_ip(ip) ⇒ Object



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

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

#detach_volume(volume_id) ⇒ Object



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

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

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



126
127
128
129
130
131
132
133
134
# File 'lib/rubber/cloud/aws.rb', line 126

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