public
Last active

Find EC2 worker instances

  • Download Gist
find_workers.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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
#!/usr/bin/env ruby
 
require 'rubygems'
require 'fileutils'
require 'right_aws'
require 'optparse'
require 'pp'
 
 
 
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: bootstrap.rb [options]"
# Define the options, and what they do
options[:"access-key"] = nil
opts.on( '-a', '--access-key ACCESS_KEY', 'AWS Access Key' ) do |x|
options[:"access-key"] = x
end
 
options[:"secret-key"] = nil
opts.on( '-s', '--secret-key SECRET_KEY', 'AWS Secret Key' ) do |x|
options[:"secret-key"] = x
end
 
options[:"stack-name"] = nil
opts.on( '-n', '--stack-name STACK_NAME', 'CloudFormation Stack Name' ) do |x|
options[:"stack-name"] = x
end
end
 
optparse.parse!
 
puts("parsed args")
 
 
logger = Logger::new(STDOUT)
logger.level = Logger::ERROR
 
 
instance_id = `curl -s http://169.254.169.254/latest/meta-data/instance-id`.chomp
puts("instance_id = #{instance_id}")
 
 
ec2 = RightAws::Ec2.new(options[:'access-key'], options[:'secret-key'], :logger => logger)
puts("signed into ec2")
 
workers = []
puts(" finding workers:")
instances = ec2.describe_instances
for instance in instances
if instance.has_key? :tags \
and instance[:tags].has_key? "aws:autoscaling:groupName" \
and instance[:tags].has_key? "aws:cloudformation:stack-name" \
and instance[:tags]["aws:cloudformation:stack-name"] == \
options[:'stack-name'] and instance[:aws_state] == "running"
puts("got a stack member")
workers.push instance
end
end
puts("found #{workers.size} workers")
 
 
numcores = 4
 
master_ipaddr = `curl -s http://169.254.169.254/latest/meta-data/local-ipv4`.chomp
puts "#{master_ipaddr} #{numcores}"
workers.each_with_index do |worker, i|
puts "#{worker[:private_ip_address]} #{numcores}"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.