Class: Rubber::Cloud::Aws
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #add_security_group_rule(group_name, protocol, from_port, to_port, source) ⇒ Object
- #attach_static_ip(ip, instance_id) ⇒ Object
- #attach_volume(volume_id, instance_id, device) ⇒ Object
- #create_image(image_name) ⇒ Object
- #create_instance(ami, ami_type, security_groups, availability_zone) ⇒ Object
- #create_security_group(group_name, group_description) ⇒ Object
- #create_spot_instance_request(spot_price, ami, ami_type, security_groups, availability_zone) ⇒ Object
- #create_static_ip ⇒ Object
- #create_volume(size, zone) ⇒ Object
- #describe_availability_zones ⇒ Object
- #describe_images(image_id = nil) ⇒ Object
- #describe_instances(instance_id = nil) ⇒ Object
- #describe_load_balancers(name = nil) ⇒ Object
- #describe_security_groups(group_name = nil) ⇒ Object
- #describe_spot_instance_requests(request_id = nil) ⇒ Object
- #describe_static_ips(ip = nil) ⇒ Object
- #describe_volumes(volume_id = nil) ⇒ Object
- #destroy_image(image_id) ⇒ Object
- #destroy_instance(instance_id) ⇒ Object
- #destroy_security_group(group_name) ⇒ Object
- #destroy_spot_instance_request(request_id) ⇒ Object
- #destroy_static_ip(ip) ⇒ Object
- #destroy_volume(volume_id) ⇒ Object
- #detach_static_ip(ip) ⇒ Object
- #detach_volume(volume_id) ⇒ Object
-
#initialize(env, capistrano) ⇒ Aws
constructor
A new instance of Aws.
- #remove_security_group_rule(group_name, protocol, from_port, to_port, source) ⇒ Object
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
115 116 117 118 119 120 121 122 123 |
# File 'lib/rubber/cloud/aws.rb', line 115 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.(opts) end |
#attach_static_ip(ip, instance_id) ⇒ Object
144 145 146 147 |
# File 'lib/rubber/cloud/aws.rb', line 144 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
178 179 180 181 |
# File 'lib/rubber/cloud/aws.rb', line 178 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
210 211 212 213 214 215 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 |
# File 'lib/rubber/cloud/aws.rb', line 210 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" arch = case arch when /i\d86/ then "i386" else arch end capistrano.sudo_script "create_bundle", <<-CMD export RUBYLIB=/usr/lib/site_ruby/ nohup ec2-bundle-vol --batch -d /mnt -k #{ec2_pk_dest} -c #{ec2_cert_dest} -u #{@aws_env.account} -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 export RUBYLIB=/usr/lib/site_ruby/ 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
68 69 70 |
# File 'lib/rubber/cloud/aws.rb', line 68 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.create_spot_instances_request(: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_ip ⇒ Object
139 140 141 142 |
# File 'lib/rubber/cloud/aws.rb', line 139 def create_static_ip response = @ec2.allocate_address() return response.publicIp end |
#create_volume(size, zone) ⇒ Object
173 174 175 176 |
# File 'lib/rubber/cloud/aws.rb', line 173 def create_volume(size, zone) response = @ec2.create_volume(:size => size.to_s, :availability_zone => zone) return response.volumeId end |
#describe_availability_zones ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rubber/cloud/aws.rb', line 56 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
247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/rubber/cloud/aws.rb', line 247 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 |
# 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 instances << instance end end if response.reservationSet return instances end |
#describe_load_balancers(name = nil) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rubber/cloud/aws.rb', line 283 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
72 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 |
# File 'lib/rubber/cloud/aws.rb', line 72 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
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/rubber/cloud/aws.rb', line 312 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
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/rubber/cloud/aws.rb', line 154 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
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/rubber/cloud/aws.rb', line 187 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. attach = item..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
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rubber/cloud/aws.rb', line 261 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
52 53 54 |
# File 'lib/rubber/cloud/aws.rb', line 52 def destroy_instance(instance_id) response = @ec2.terminate_instances(:instance_id => instance_id) end |
#destroy_security_group(group_name) ⇒ Object
135 136 137 |
# File 'lib/rubber/cloud/aws.rb', line 135 def destroy_security_group(group_name) @ec2.delete_security_group(:group_name => group_name) end |
#destroy_spot_instance_request(request_id) ⇒ Object
279 280 281 |
# File 'lib/rubber/cloud/aws.rb', line 279 def destroy_spot_instance_request(request_id) @ec2.destroy_spot_instance_requests :spot_instance_request_id => request_id end |
#destroy_static_ip(ip) ⇒ Object
168 169 170 171 |
# File 'lib/rubber/cloud/aws.rb', line 168 def destroy_static_ip(ip) response = @ec2.release_address(:public_ip => ip) return response.return == "true" end |
#destroy_volume(volume_id) ⇒ Object
206 207 208 |
# File 'lib/rubber/cloud/aws.rb', line 206 def destroy_volume(volume_id) @ec2.delete_volume(:volume_id => volume_id) end |
#detach_static_ip(ip) ⇒ Object
149 150 151 152 |
# File 'lib/rubber/cloud/aws.rb', line 149 def detach_static_ip(ip) response = @ec2.disassociate_address(:public_ip => ip) return response.return == "true" end |
#detach_volume(volume_id) ⇒ Object
183 184 185 |
# File 'lib/rubber/cloud/aws.rb', line 183 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
125 126 127 128 129 130 131 132 133 |
# File 'lib/rubber/cloud/aws.rb', line 125 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 |