Skip to content

Instantly share code, notes, and snippets.

@jovandeginste
Created May 3, 2016 13:00
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 jovandeginste/4c7da1392e52bc985c75ef4f872c7843 to your computer and use it in GitHub Desktop.
Save jovandeginste/4c7da1392e52bc985c75ef4f872c7843 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'getoptlong'
require 'net/http'
require 'json'
server = 'localhost:8500'
format = 'yaml'
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--server', '-s', GetoptLong::REQUIRED_ARGUMENT ],
[ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ],
)
opts.each do |opt, arg|
case opt
when '--help'
puts <<-EOF
Usage: #{$0} [--server|-s server[:port]] [--format|-f yaml|json]
Defaults:
server: #{server}
format: #{format}
EOF
exit
when '--format'
case arg
when 'yaml', 'json'
format = arg
else
exit 1
end
when '--server'
server = arg
end
end
class Object
def downcase_keys
self
end
end
class Hash
def downcase_keys
self.inject({}) do |hash, kv|
key, value = kv
hash[key.downcase] = value.downcase_keys
hash
end
end
end
def get_nodes(server)
nodes_url = "http://#{server}/v1/catalog/nodes"
uri = URI(nodes_url)
return JSON.parse(Net::HTTP.get(uri)).map(&:downcase_keys)
end
def get_node(server, node)
node_url = "http://#{server}/v1/catalog/node/#{node}"
uri = URI(node_url)
return JSON.parse(Net::HTTP.get(uri)).downcase_keys
end
result = {}
get_nodes(server).each do |node|
name = node['node']
node_data = get_node(server, name)
result[name] = node
result[name]['hostname'] = node['address']
result[name]['services'] = node_data['services']
result[name]['tags'] = node_data['services'].collect{|key, value|
s = value['service']
tags = value['tags'] || []
[s] + tags.map do |tag|
"#{s}:#{tag}"
end
}.flatten.compact.uniq.sort
end
case format
when 'yaml'
require 'yaml'
puts result.to_yaml
when 'json'
puts result.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment