Created
September 27, 2014 02:51
-
-
Save heph/4843ec42795d6e348fc5 to your computer and use it in GitHub Desktop.
consul ohai plugin (only supports kv lookups at the moment)
This file contains 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
require 'net/http' | |
require 'uri' | |
require 'base64' | |
require 'json' | |
Ohai.plugin(:Consul) do | |
provides 'consul' | |
## | |
# Insert a value into a hash with a '/' delimited path | |
def hash_path(hash, path, value) | |
position = ObjectSpace._id2ref(hash.object_id) | |
keys = path.split('/') | |
while key = keys.shift do | |
if keys.length > 0 | |
if position["#{key}"].nil? | |
position["#{key}"] = Hash.new | |
end | |
position = ObjectSpace._id2ref(position["#{key}"].object_id) | |
else | |
position["#{key}"] = value | |
end | |
end | |
hash | |
end | |
def get_kv_store | |
kv = {} | |
uri = URI.parse('http://127.0.0.1:8500/v1/kv/?recurse=true') | |
response = Net::HTTP.get_response(uri).body | |
JSON.parse(response).each do |record| | |
if record.key?('Key') and record.key?('Value') | |
if record['Value'] | |
kv = hash_path(kv, record['Key'], Base64.decode64(record['Value'])) | |
end | |
end | |
end | |
kv | |
end | |
collect_data(:default) do | |
consul Mash.new | |
consul['kv'] = get_kv_store | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment