Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Object wrapper for the EC2 Metadata URL on Openstack & EC2 vm's
class EC2Metadata
def initialize()
meta_url = "169.254.169.254"
@meta_base = "/2008-02-01/meta-data/"
@conn = Net::HTTP.new(meta_url)
end
def method_missing(name, *args)
name = name.to_s.gsub("_", "-")
req = get_url(name)
ret = get_data(req)
if ret.include?("\n")
ret = ret.split
end
return ret
end
def ec2_methods
methods = get_data(get_url("")).split
return methods.collect {|m| m.gsub("-", "_").gsub("/","")}
end
private
def get_url(arg)
return "#{@meta_base}/#{arg}"
end
def get_data(request_uri)
request = Net::HTTP::Get.new(request_uri)
response = @conn.request(request)
raise Net::HTTPNotFound if response.code != "200"
return response.body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.