Skip to content

Instantly share code, notes, and snippets.

@ursm
Created May 22, 2014 11:02
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 ursm/873206e1df18d76647d2 to your computer and use it in GitHub Desktop.
Save ursm/873206e1df18d76647d2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
Instance = Struct.new(:hostname, :amazon_id, :role)
def environment
basename = File.basename(__FILE__)
unless basename.include?('@')
warn <<-EOF
Choose an EYC's environment as follows:
$ ln -s #{basename} #{__FILE__}@production
EOF
exit 1
end
basename.split('@', 2).last
end
def instances(environment)
stdout = `ey servers --environment #{environment} --simple`
exit 1 unless $?.success?
stdout.lines.map {|line|
Instance.new(*line.chomp.split)
}
end
def hostvars(instances)
instances.each_with_object({}) {|instance, memo|
memo[instance.hostname] = {
ansible_ssh_user: 'deploy',
amazon_id: instance.amazon_id
}
}
end
instances = instances(environment)
json = instances.group_by(&:role).each_with_object({}) {|(role, is), memo|
memo[role] = {
hosts: is.map(&:hostname)
}
}.merge(
_meta: {
hostvars: hostvars(instances)
}
)
puts JSON.pretty_generate(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment