Skip to content

Instantly share code, notes, and snippets.

@cbare
Created December 2, 2012 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbare/4187774 to your computer and use it in GitHub Desktop.
Save cbare/4187774 to your computer and use it in GitHub Desktop.
Find EC2 worker instances
#!/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
@cbare
Copy link
Author

cbare commented Dec 6, 2012

Credit due to Dan Tenenbaum for the original, which can be found here:

https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/BiocCloud/inst/CloudFormation/bootstrap_plain.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment