Last active
April 27, 2017 17:55
-
-
Save altherlex/3915315e62ddc165444c18283e888de1 to your computer and use it in GitHub Desktop.
Script to map in ssh_config the opsworks instances alias for vary regions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rubygems" | |
require "aws-sdk" | |
require "pry-nav" | |
def run_ops_client(opsworks, file_path) | |
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, instance.public_ip, false) | |
else | |
true_instances << TrueInstance.new(instance.hostname, instance.private_ip, layer.name, stack.name, instance.public_ip, true) | |
end | |
end | |
end | |
end | |
true_instances.sort!{|a, b| a.hostname <=> b.hostname } | |
valid_instances = true_instances - true_instances.select(&:off) | |
File.open(file_path, 'a') do |f| | |
valid_instances.each do |instance| | |
f.write("# Project #{instance.stack}\n") | |
f.write("# Layer #{instance.layer}\n") | |
f.write("# Public IP #{instance.public_ip}\n") | |
f.write("Host #{instance.hostname}\n") | |
f.write("\s\sHostName #{instance.public_ip}\n") | |
f.write("\s\sPort 22\n") | |
f.write("\s\sUser ubuntu\n") | |
f.write("\s\sIdentityFile ~/.ssh/opsworks\n") | |
f.write("\n") | |
end | |
# true_instances.select(&:off).each do |instance| | |
# f.write("# Project #{instance.stack}\n") | |
# f.write("# Layer #{instance.layer}\n") | |
# f.write("# Public IP #{instance.public_ip}\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\n") | |
# f.write("\n") | |
# end | |
end | |
end | |
# -- Constructor | |
opsworks_cli = [] | |
opsworks_cli << Aws::OpsWorks::Client.new(region: "us-east-1") | |
opsworks_cli << Aws::OpsWorks::Client.new(region: "sa-east-1") | |
# Doctor account | |
c = Aws::Credentials.new(ENV['DR_AWS_ACCESS_KEY_ID'], ENV['DR_AWS_SECRET_ACCESS_KEY']) | |
opsworks_cli << Aws::OpsWorks::Client.new(region: "us-east-1", credentials:c) | |
TrueInstance = Struct.new(:hostname, :private_ip, :layer, :stack, :public_ip, :off) | |
file_path = File.join(File.dirname(__FILE__), '..', 'config') | |
File.open(file_path, 'w') {|f| f.write ''} | |
opsworks_cli.each{|i| run_ops_client(i, file_path)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment