Created
November 14, 2012 01:49
-
-
Save jameskyle/4069731 to your computer and use it in GitHub Desktop.
Ohai plugin to collect openstack metadta (should work for ec2 as well)
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
require 'net/http' | |
provides "openstack" | |
openstack Mash.new | |
META_IP = "169.254.169.254" | |
BASE = "/latest/" | |
HTTP = Net::HTTP.new(META_IP) | |
HTTP.open_timeout = 1 | |
def build_method_mash(mash, method, prev_response=nil) | |
key = ::File.basename(method) | |
resp = HTTP.request(Net::HTTP::Get.new(method)) | |
code = resp.code | |
methods = resp.body.split | |
if code == "200" and not methods.eql? prev_response then | |
if key.eql? "public-keys" then | |
mash[key] = parse_public_keys(methods) | |
elsif key.eql? "security-groups" then | |
mash[key] = methods | |
elsif methods.length == 1 and not methods.eql? prev_response then | |
mash[key] = resp.body | |
else | |
mash[key] = Mash.new | |
methods.each do |meth| | |
build_method_mash(mash[key], [method, meth].join("/"), methods) | |
end | |
end | |
end | |
end | |
def parse_public_keys(keys) | |
key_mash = Mash.new | |
keys.each do |key| | |
id, name = key.split("=") | |
req = Net::HTTP::Get.new("#{BASE}/meta-data/public-keys/#{id}/openssh-key") | |
resp = HTTP.request(req) | |
key_mash[name] = resp.body | |
end | |
return key_mash | |
end | |
begin | |
build_method_mash(openstack, BASE) | |
rescue Timeout::Error | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment