Skip to content

Instantly share code, notes, and snippets.

@wilbert
Created June 28, 2016 17:28
Show Gist options
  • Save wilbert/ca0482389a1ee968d1d713d41bac5039 to your computer and use it in GitHub Desktop.
Save wilbert/ca0482389a1ee968d1d713d41bac5039 to your computer and use it in GitHub Desktop.
require "rubygems"
require "aws-sdk"
require "pry-nav"
opsworks = Aws::OpsWorks::Client.new(
region: "us-east-1"
)
TrueInstance = Struct.new(:hostname, :private_ip, :layer, :stack)
true_instances = []
stacks = opsworks.describe_stacks.stacks
stacks.each do |stack|
layers = opsworks.describe_layers({ stack_id: stack.stack_id }).layers
layers.each do |layer|
instances = opsworks.describe_instances({ layer_id: layer.layer_id }).instances
instances.each do |instance|
unless instance.private_ip.nil?
true_instances << TrueInstance.new(instance.hostname, instance.private_ip, layer.name, stack.name)
end
end
end
end
true_instances.sort!{|a, b| a.hostname <=> b.hostname }
File.open("config", "w") do |f|
true_instances.each do |instance|
f.write("# Project #{instance.stack}\n")
f.write("# Layer #{instance.layer}\n")
f.write("Host #{instance.hostname}\n")
f.write("\s\sHostName #{instance.private_ip}\n")
f.write("\s\sPort 22\n")
f.write("\s\sUser ubuntu\n")
f.write("\s\sIdentityFile ~/.ssh/opsworks.pem\n")
f.write("\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment