Skip to content

Instantly share code, notes, and snippets.

@jameskyle
Created November 14, 2012 01:49
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 jameskyle/4069731 to your computer and use it in GitHub Desktop.
Save jameskyle/4069731 to your computer and use it in GitHub Desktop.
Ohai plugin to collect openstack metadta (should work for ec2 as well)
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