Methods
public class
public instance
Included modules
Constants
KNOWN_STATES | = | [:running, :pending, :shutting_down, :terminated, :degraded].freeze |
Public class methods
h is a hash of instance properties in the format returned by EC2::Base#describe_instances:
kernelId: aki-9b00e5f2 amiLaunchIndex: "0" keyName: solutious-default launchTime: "2009-03-14T12:48:15.000Z" instanceType: m1.small imageId: ami-0734d36e privateDnsName: reason: placement: availabilityZone: us-east-1b dnsName: instanceId: i-cdaa34a4 instanceState: name: pending code: "0"
Returns an Instance object.
# File lib/rudy/aws/ec2/instance.rb, line 344 344: def self.from_hash(h) 345: inst = Rudy::AWS::EC2::Instance.new 346: inst.aki = h['kernelId'] 347: inst.ami = h['imageId'] 348: inst.created = h['launchTime'] 349: inst.keyname = h['keyName'] 350: inst.launch_index = h['amiLaunchIndex'] 351: inst.size = h['instanceType'] 352: inst.dns_private = h['privateDnsName'] 353: inst.dns_public = h['dnsName'] 354: inst.reason = h['reason'] 355: inst.zone = h['placement']['availabilityZone'] 356: inst.awsid = h['instanceId'] 357: inst.state = h['instanceState']['name'] 358: inst 359: end
Is state a known EC2 machine instance state? See: KNOWN_STATES
# File lib/rudy/aws/ec2/instance.rb, line 362 362: def self.known_state?(state) 363: return false unless state 364: state &&= state.to_sym 365: state = :shutting_down if state == :'shutting-down' 366: KNOWN_STATES.member?(state) 367: end
Public instance methods
# File lib/rudy/aws/ec2/instance.rb, line 287 287: def any?(state=:any, inst_ids=[]) 288: !list(state, inst_ids).nil? 289: end
# File lib/rudy/aws/ec2/instance.rb, line 295 295: def any_group?(group=nil, state=:any) 296: ret = list_group(group, state) 297: !ret.nil? 298: end
# File lib/rudy/aws/ec2/instance.rb, line 258 258: def attached_volume?(id, device) 259: list = volumes(id) 260: list.each do |v| 261: return true if v.device == device 262: end 263: false 264: end
System console output.
- inst_id instance ID (String) or Instance object.
NOTE: Amazon sends the console outputs as a Base64 encoded string. This method DOES NOT decode in order to remain compliant with the data formats returned by Amazon.
You can decode it like this:
require 'base64' Base64.decode64(output)
# File lib/rudy/aws/ec2/instance.rb, line 250 250: def console(inst_id, &each_inst) 251: inst_ids = objects_to_instance_ids([inst_id]) 252: response = Rudy::AWS::EC2.execute_request({}) { 253: @@ec2.get_console_output(:instance_id => inst_ids.first) 254: } 255: response['output'] 256: end
Return an Array of Instance objects. Note: These objects will not have DNS data because they will still be in pending state. The DNS info becomes available once the instance enters the running state.
opts supports the following parameters:
- :zone
- :ami
- :group
- :size
- :keypair
- :private true or false (default)
- :machine_data
- :min count
- :max count
# File lib/rudy/aws/ec2/instance.rb, line 65 65: def create(opts={}, &each_inst) 66: raise NoAMI unless opts[:ami] 67: raise NoGroup unless opts[:group] 68: 69: opts = { 70: :size => 'm1.small', 71: :min => 1, 72: :max => nil 73: }.merge(opts) 74: 75: old_opts = { 76: :image_id => opts[:ami].to_s, 77: :min_count => opts[:min], 78: :max_count => opts[:max] || opts[:min], 79: :key_name => (opts[:keypair] || '').to_s, 80: :security_group => [opts[:group]].flatten.compact, 81: #:user_data => opts[:machine_data], # Error: Invalid BASE64 encoding of user data ?? 82: :availability_zone => opts[:zone].to_s, 83: :instance_type => opts[:size].to_s, 84: :kernel_id => nil 85: } 86: 87: response = Rudy::AWS::EC2.execute_request({}) { @@ec2.run_instances(old_opts) } 88: return nil unless response['instancesSet'].is_a?(Hash) 89: instances = response['instancesSet']['item'].collect do |inst| 90: self.from_hash(inst) 91: end 92: instances.each { |inst| 93: each_inst.call(inst) 94: } if each_inst 95: instances 96: end
# File lib/rudy/aws/ec2/instance.rb, line 108 108: def destroy(inst_ids=[], &each_inst) 109: instances = list(:running, inst_ids, &each_inst) || [] 110: raise NoRunningInstances if instances.empty? 111: 112: inst_ids = objects_to_instance_ids(inst_ids) 113: 114: response = Rudy::AWS::EC2.execute_request({}) { 115: @@ec2.terminate_instances(:instance_id => inst_ids) 116: } 117: 118: #instancesSet: 119: # item: 120: # - instanceId: i-ebdcb882 121: # shutdownState: 122: # code: "48" 123: # name: terminated 124: # previousState: 125: # code: "48" 126: # name: terminated 127: 128: raise MalformedResponse unless response['instancesSet'].is_a?(Hash) 129: instances_shutdown = [] 130: response['instancesSet']['item'].collect do |inst| 131: next unless inst['shutdownState'].is_a?(Hash) && inst['shutdownState']['name'] == 'shutting-down' 132: instances_shutdown << inst['instanceId'] 133: end 134: success = instances_shutdown.size == inst_ids.size 135: success 136: end
# File lib/rudy/aws/ec2/instance.rb, line 144 144: def destroy_group(group, &each_inst) 145: instances = list_group(group, :running, &each_inst) || [] 146: inst_ids = objects_to_instance_ids(instances) 147: destroy(inst_ids, :skip_check) 148: end
# File lib/rudy/aws/ec2/instance.rb, line 273 273: def device_volume(id, device) 274: volumes(id).select { |v| v.device === device } 275: end
# File lib/rudy/aws/ec2/instance.rb, line 291 291: def exists?(inst_ids) 292: any?(:any, inst_ids) 293: end
inst_id is an instance ID Returns an Instance object
# File lib/rudy/aws/ec2/instance.rb, line 279 279: def get(inst_id) 280: return nil if inst_id.nil? 281: inst_id = inst_id.awsid if inst_id.is_a?(Rudy::AWS::EC2::Instance) 282: inst = list(:any, inst_id) 283: inst &&= inst.first 284: inst 285: end
- state is an optional instance state. If specified, must be one of: running (default), pending, terminated.
- inst_ids is an Array of instance IDs.
Returns an Array of Rudy::AWS::EC2::Instance objects.
# File lib/rudy/aws/ec2/instance.rb, line 153 153: def list(state=nil, inst_ids=[], &each_inst) 154: instances = list_as_hash(state, inst_ids, &each_inst) 155: instances &&= instances.values 156: instances = nil if instances && instances.empty? # Don't return an empty hash 157: instances 158: end
- state is an optional instance state. If specified, must be
one of: running (default), pending, terminated, any
- inst_ids is an Array of instance IDs or Rudy::AWS::EC2::Instance objects.
Returns a Hash of Rudy::AWS::EC2::Instance objects. The key is the instance ID.
- each_inst a block to execute for every instance in the list.
# File lib/rudy/aws/ec2/instance.rb, line 190 190: def list_as_hash(state=nil, inst_ids=[], &each_inst) 191: state &&= state.to_sym 192: state = nil if state == :any 193: raise "Unknown state: #{state}" if state && !Instances.known_state?(state) 194: state = :'shutting-down' if state == :shutting_down # EC2 uses a dash 195: 196: # If we got Instance objects, we want just the IDs. 197: # This method always returns an Array. 198: inst_ids = objects_to_instance_ids(inst_ids) 199: 200: response = Rudy::AWS::EC2.execute_request({}) { 201: @@ec2.describe_instances(:instance_id => inst_ids) 202: } 203: 204: # requestId: c16878ac-28e4-4859-9878-ef93af45789c 205: # reservationSet: 206: # item: 207: # - reservationId: r-e493148d 208: # groupSet: 209: # item: 210: # - groupId: default 211: # instancesSet: 212: # item: 213: return nil unless response['reservationSet'].is_a?(Hash) # No instances 214: 215: resids = [] 216: instances = {} 217: response['reservationSet']['item'].each do |res| 218: resids << res['reservationId'] 219: groups = res['groupSet']['item'].collect { |g| g['groupId'] } 220: # And each reservation can have 1 or more instances 221: next unless res['instancesSet'].is_a?(Hash) 222: res['instancesSet']['item'].each do |props| 223: inst = Instances.from_hash(props) 224: next if state && inst.state != state.to_s 225: inst.groups = groups 226: #puts "STATE: #{inst.state} #{state}" 227: instances[inst.awsid] = inst 228: end 229: end 230: 231: instances.each_value { |inst| each_inst.call(inst) } if each_inst 232: 233: instances = nil if instances.empty? # Don't return an empty hash 234: instances 235: end
- group is a security group name.
- state is an optional instance state. If specified, must be one of: running (default), pending, terminated.
- inst_ids is an Array of instance IDs.
# File lib/rudy/aws/ec2/instance.rb, line 163 163: def list_group(group=nil, state=nil, inst_ids=[], &each_inst) 164: instances = list_group_as_hash(group, state, inst_ids, &each_inst) 165: instances &&= instances.values 166: instances = nil if instances && instances.empty? # Don't return an empty hash 167: instances 168: end
- group is a security group name.
- state is an optional instance state. If specified, must be one of: running (default), pending, terminated.
- inst_ids is an Array of instance IDs.
# File lib/rudy/aws/ec2/instance.rb, line 174 174: def list_group_as_hash(group=nil, state=nil, inst_ids=[], &each_inst) 175: instances = list_as_hash(state, inst_ids) 176: # Remove instances that are not in the specified group 177: if instances 178: instances = instances.reject { |id,inst| !inst.groups.member?(group) } if group 179: instances.each_value { |inst| each_inst.call(inst) } if each_inst 180: end 181: instances = nil if instances && instances.empty? # Don't return an empty hash 182: instances 183: end
# File lib/rudy/aws/ec2/instance.rb, line 303 303: def pending?(inst_ids) 304: compare_instance_lists(list(:pending, inst_ids), inst_ids) 305: end
# File lib/rudy/aws/ec2/instance.rb, line 98 98: def restart(inst_ids=[], &each_inst) 99: instances = list(:running, inst_ids, &each_inst) || [] 100: raise NoRunningInstances if instances.empty? 101: inst_ids = objects_to_instance_ids(inst_ids) 102: response = Rudy::AWS::EC2.execute_request({}) { 103: @@ec2.reboot_instances(:instance_id => inst_ids) 104: } 105: response['return'] == 'true' 106: end
# File lib/rudy/aws/ec2/instance.rb, line 138 138: def restart_group(group, &each_inst) 139: instances = list_group(group, :running, &each_inst) || [] 140: inst_ids = objects_to_instance_ids(instances) 141: restart(inst_ids, :skip_check) 142: end
# File lib/rudy/aws/ec2/instance.rb, line 300 300: def running?(inst_ids) 301: compare_instance_lists(list(:running, inst_ids), inst_ids) 302: end
# File lib/rudy/aws/ec2/instance.rb, line 309 309: def shutting_down?(inst_ids) 310: compare_instance_lists(list(:shutting_down, inst_ids), inst_ids) 311: end
# File lib/rudy/aws/ec2/instance.rb, line 306 306: def terminated?(inst_ids) 307: compare_instance_lists(list(:terminated, inst_ids), inst_ids) 308: end
# File lib/rudy/aws/ec2/instance.rb, line 313 313: def unavailable?(inst_ids) 314: instances = list(:any, inst_ids) || [] 315: instances.reject! { |inst| 316: (inst.state == "shutting-down" || 317: inst.state == "pending" || 318: inst.state == "terminated") 319: } 320: compare_instance_lists(instances, inst_ids) 321: end
# File lib/rudy/aws/ec2/instance.rb, line 266 266: def volumes(id) 267: rvol = Rudy::AWS::EC2::Volumes.new 268: rvol.ec2 = @ec2 269: rvol.list || [] 270: list.select { |v| v.attached? && v.instid === id } 271: end