Class: EC2InstanceIdentifier

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2_instance_identifier.rb

Overview

Fetch an instance from its private ip address

Instance Method Summary collapse

Constructor Details

#initialize(aki, sak) ⇒ EC2InstanceIdentifier

Need access_key_id, secret_access_key



8
9
10
# File 'lib/ec2_instance_identifier.rb', line 8

def initialize(aki, sak)
  @compute = Fog::Compute.new({:provider => 'AWS', :aws_access_key_id => aki, :aws_secret_access_key => sak})
end

Instance Method Details

#get_instance(hostname) ⇒ Object

Returns the instance corresponding to the provided hostname

Raises:

  • (InstanceNotFoundException)


12
13
14
15
16
17
# File 'lib/ec2_instance_identifier.rb', line 12

def get_instance(hostname)
  ip = Resolv.getaddress(hostname)
  instance = @compute.servers.all().find { |i| i.private_ip_address  == ip || i.public_ip_address == ip }
  raise InstanceNotFoundException.new(hostname) if ! instance
  return instance
end