Module: Awsam::Ec2
- Defined in:
- lib/awsam/ec2.rb
Constant Summary collapse
- LOOKUP_TAGS =
["Name", "aws:autoscaling:groupName", "AlsoKnownAs"].freeze
Class Method Summary collapse
- .find(ec2, instance_id) ⇒ Object
- .find_by_instance_id(ec2, instance_id) ⇒ Object
- .find_by_tag(ec2, instance_id) ⇒ Object
- .find_instance(acct, instance_id) ⇒ Object
- .instance_hostname(inst) ⇒ Object
Class Method Details
.find(ec2, instance_id) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/awsam/ec2.rb', line 29 def self.find(ec2, instance_id) if instance_id =~ /^i-[0-9a-f]{8,17}$/ find_by_instance_id(ec2, instance_id) else find_by_tag(ec2, instance_id) end end |
.find_by_instance_id(ec2, instance_id) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/awsam/ec2.rb', line 37 def self.find_by_instance_id(ec2, instance_id) begin ec2.describe_instances(instance_id).first rescue RightAws::AwsError puts "instance_id does not exist" exit 1 end end |
.find_by_tag(ec2, instance_id) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 |
# File 'lib/awsam/ec2.rb', line 46 def self.find_by_tag(ec2, instance_id) results = [] ec2.(:filters => { "resource-type" => "instance" }).each do |tag| if LOOKUP_TAGS.include?(tag[:key]) && tag[:value].downcase.include?(instance_id.downcase) results << tag end end if !results || results.length == 0 puts "No tags by this name are available in your account" exit 1 end results.uniq! { |a| a[:resource_id] } results.sort! { |a,b| a[:value] <=> b[:value] } rmap = {} ec2.describe_instances(results.map{|a| a[:resource_id]}, :filters => { "instance-state-name" => "running" }).each do |inst| rmap[inst[:aws_instance_id]] = inst end results.reject! { |a| rmap[a[:resource_id]].nil? } if results.length == 0 puts "No running instances by that tag name are available" exit 1 end if $opts[:first_node] || results.length == 1 node = results.first else puts "Please select which node you wish to use:" puts results.each_with_index do |elem, i| inst = rmap[elem[:resource_id]] puts "%d) %s (%s, %s, %s, %s)" % [i, elem[:value], inst[:aws_instance_id], inst[:aws_instance_type], inst[:aws_availability_zone], inst[:aws_launch_time]] end puts "q) Quit" puts print "> " input = $stdin.gets puts exit unless input =~ /^\d+$/ node = results[input.to_i] end return rmap[node[:resource_id]] end |
.find_instance(acct, instance_id) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/awsam/ec2.rb', line 15 def self.find_instance(acct, instance_id) logger = Logger.new(File.open("/dev/null", "w")) ec2 = RightAws::Ec2.new(acct.access_key, acct.secret_key, :logger => logger, :endpoint_url => acct.ec2_url) unless ec2 puts "Unable to connect to EC2" return nil end find(ec2, instance_id) end |
.instance_hostname(inst) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/awsam/ec2.rb', line 6 def self.instance_hostname(inst) hostname = inst[:dns_name] # Are we in a VPC? if inst[:vpc_id] hostname = inst[:private_ip_address] end hostname end |