summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@opscode.com>2012-11-13 10:40:35 -0800
committerBryan McLellan <btm@opscode.com>2012-11-13 10:40:35 -0800
commitcc5a46b0fce6a1c2d1dc7da18f0f928f1ba1b716 (patch)
tree194e273e8167d4a5fe4fff579f7fac3ffb822db2
parentfa85f82da168e959c1d516b7c2fbff97bfd110b6 (diff)
parent55821e1c417e40128a7ae45ba0bcf28641de650b (diff)
downloadohai-cc5a46b0fce6a1c2d1dc7da18f0f928f1ba1b716.tar.gz
Merge branch 'OHAI-400'
-rw-r--r--lib/ohai/mixin/ec2_metadata.rb65
-rw-r--r--spec/ohai/plugins/chef_spec.rb1
-rw-r--r--spec/ohai/plugins/cloud_spec.rb2
-rw-r--r--spec/ohai/plugins/ec2_spec.rb69
-rw-r--r--spec/ohai/plugins/eucalyptus_spec.rb10
5 files changed, 121 insertions, 26 deletions
diff --git a/lib/ohai/mixin/ec2_metadata.rb b/lib/ohai/mixin/ec2_metadata.rb
index c2d87095..60844c51 100644
--- a/lib/ohai/mixin/ec2_metadata.rb
+++ b/lib/ohai/mixin/ec2_metadata.rb
@@ -25,9 +25,11 @@ module Ohai
module Ec2Metadata
EC2_METADATA_ADDR = "169.254.169.254" unless defined?(EC2_METADATA_ADDR)
- EC2_METADATA_URL = "/2008-02-01/meta-data" unless defined?(EC2_METADATA_URL)
- EC2_USERDATA_URL = "/2008-02-01/user-data" unless defined?(EC2_USERDATA_URL)
+ EC2_METADATA_URL = "/2012-01-12/meta-data" unless defined?(EC2_METADATA_URL)
+ EC2_USERDATA_URL = "/2012-01-12/user-data" unless defined?(EC2_USERDATA_URL)
EC2_ARRAY_VALUES = %w(security-groups)
+ EC2_ARRAY_DIR = %w(network/interfaces/macs)
+ EC2_JSON_DIR = %w(iam)
def can_metadata_connect?(addr, port, timeout=2)
t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
@@ -62,16 +64,53 @@ module Ohai
def fetch_metadata(id='')
metadata = Hash.new
http_client.get("#{EC2_METADATA_URL}/#{id}").body.split("\n").each do |o|
- key = "#{id}#{o.gsub(/\=.*$/, '/')}"
+ key = expand_path("#{id}#{o}")
if key[-1..-1] != '/'
- metadata[key.gsub(/\-|\//, '_').to_sym] =
+ metadata[metadata_key(key)] =
if EC2_ARRAY_VALUES.include? key
http_client.get("#{EC2_METADATA_URL}/#{key}").body.split("\n")
else
http_client.get("#{EC2_METADATA_URL}/#{key}").body
end
- else
- fetch_metadata(key).each{|k,v| metadata[k] = v}
+ elsif not key.eql?(id) and not key.eql?("/")
+ name = key[0..-2]
+ sym = metadata_key(name)
+ if EC2_ARRAY_DIR.include?(name)
+ metadata[sym] = fetch_dir_metadata(key)
+ elsif EC2_JSON_DIR.include?(name)
+ metadata[sym] = fetch_json_dir_metadata(key)
+ else
+ fetch_metadata(key).each{|k,v| metadata[k] = v}
+ end
+ end
+ end
+ metadata
+ end
+
+ def fetch_dir_metadata(id)
+ metadata = Hash.new
+ http_client.get("#{EC2_METADATA_URL}/#{id}").body.split("\n").each do |o|
+ key = expand_path(o)
+ if key[-1..-1] != '/'
+ metadata[metadata_key(key)] = http_client.get("#{EC2_METADATA_URL}/#{id}#{key}").body
+ elsif not key.eql?('/')
+ metadata[key[0..-2]] = fetch_dir_metadata("#{id}#{key}")
+ end
+ end
+ metadata
+ end
+
+ def fetch_json_dir_metadata(id)
+ metadata = Hash.new
+ http_client.get("#{EC2_METADATA_URL}/#{id}").body.split("\n").each do |o|
+ key = expand_path(o)
+ if key[-1..-1] != '/'
+ data = http_client.get("#{EC2_METADATA_URL}/#{id}#{key}").body
+ json = StringIO.new(data)
+ parser = Yajl::Parser.new
+ metadata[metadata_key(key)] = parser.parse(json)
+ elsif not key.eql?('/')
+ metadata[key[0..-2]] = fetch_json_dir_metadata("#{id}#{key}")
end
end
metadata
@@ -81,6 +120,20 @@ module Ohai
response = http_client.get("#{EC2_USERDATA_URL}/")
response.code == "200" ? response.body : nil
end
+
+ private
+
+ def expand_path(file_name)
+ path = File.expand_path(file_name.gsub(/\=.*$/, '/'), '/')
+ path[0] = '' # remove the initial "/"
+ path << '/' if file_name[-1..-1].eql?('/') # it was a directory
+ path
+ end
+
+ def metadata_key(key)
+ key.gsub(/\-|\//, '_')
+ end
+
end
end
end
diff --git a/spec/ohai/plugins/chef_spec.rb b/spec/ohai/plugins/chef_spec.rb
index 7def18b3..3acfe469 100644
--- a/spec/ohai/plugins/chef_spec.rb
+++ b/spec/ohai/plugins/chef_spec.rb
@@ -19,7 +19,6 @@
#
begin
- require 'chef'
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
describe Ohai::System, "plugin chef" do
diff --git a/spec/ohai/plugins/cloud_spec.rb b/spec/ohai/plugins/cloud_spec.rb
index b72bd0d6..97188511 100644
--- a/spec/ohai/plugins/cloud_spec.rb
+++ b/spec/ohai/plugins/cloud_spec.rb
@@ -17,8 +17,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
-require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
-
describe Ohai::System, "plugin cloud" do
before(:each) do
@ohai = Ohai::System.new
diff --git a/spec/ohai/plugins/ec2_spec.rb b/spec/ohai/plugins/ec2_spec.rb
index 7da7f0ff..e46dd12f 100644
--- a/spec/ohai/plugins/ec2_spec.rb
+++ b/spec/ohai/plugins/ec2_spec.rb
@@ -38,35 +38,80 @@ describe Ohai::System, "plugin ec2" do
before(:each) do
@http_client = mock("Net::HTTP client")
@ohai.stub!(:http_client).and_return(@http_client)
+ IO.stub!(:select).and_return([[],[1],[]])
+ t = mock("connection")
+ t.stub!(:connect_nonblock).and_raise(Errno::EINPROGRESS)
+ Socket.stub!(:new).and_return(t)
+ end
+ it "should recursively fetch all the ec2 metadata" do
@http_client.should_receive(:get).
- with("/2008-02-01/meta-data/").
+ with("/2012-01-12/meta-data/").
and_return(mock("Net::HTTP Response", :body => "instance_type\nami_id\nsecurity-groups"))
@http_client.should_receive(:get).
- with("/2008-02-01/meta-data/instance_type").
+ with("/2012-01-12/meta-data/instance_type").
and_return(mock("Net::HTTP Response", :body => "c1.medium"))
@http_client.should_receive(:get).
- with("/2008-02-01/meta-data/ami_id").
+ with("/2012-01-12/meta-data/ami_id").
and_return(mock("Net::HTTP Response", :body => "ami-5d2dc934"))
@http_client.should_receive(:get).
- with("/2008-02-01/meta-data/security-groups").
+ with("/2012-01-12/meta-data/security-groups").
and_return(mock("Net::HTTP Response", :body => "group1\ngroup2"))
@http_client.should_receive(:get).
- with("/2008-02-01/user-data/").
+ with("/2012-01-12/user-data/").
and_return(mock("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200"))
- end
-
- it "should recursively fetch all the ec2 metadata" do
- IO.stub!(:select).and_return([[],[1],[]])
- t = mock("connection")
- t.stub!(:connect_nonblock).and_raise(Errno::EINPROGRESS)
- Socket.stub!(:new).and_return(t)
@ohai._require_plugin("ec2")
+
@ohai[:ec2].should_not be_nil
@ohai[:ec2]['instance_type'].should == "c1.medium"
@ohai[:ec2]['ami_id'].should == "ami-5d2dc934"
@ohai[:ec2]['security_groups'].should eql ['group1', 'group2']
end
+
+ it "should parse ec2 network/ directory as a multi-level hash" do
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/").
+ and_return(mock("Net::HTTP Response", :body => "network/"))
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/network/").
+ and_return(mock("Net::HTTP Response", :body => "interfaces/"))
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/network/interfaces/").
+ and_return(mock("Net::HTTP Response", :body => "macs/"))
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/network/interfaces/macs/").
+ and_return(mock("Net::HTTP Response", :body => "fe:ff:ff:ff:ff:ff/"))
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/network/interfaces/macs/fe:ff:ff:ff:ff:ff/").
+ and_return(mock("Net::HTTP Response", :body => "public_hostname"))
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/network/interfaces/macs/fe:ff:ff:ff:ff:ff/public_hostname").
+ and_return(mock("Net::HTTP Response", :body => "server17.opscode.com"))
+ @ohai._require_plugin("ec2")
+
+ @ohai[:ec2].should_not be_nil
+ @ohai[:ec2]['network_interfaces_macs']['fe:ff:ff:ff:ff:ff']['public_hostname'].should eql('server17.opscode.com')
+ end
+
+ it "should parse ec2 iam/ directory and its JSON files properly" do
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/").
+ and_return(mock("Net::HTTP Response", :body => "iam/"))
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/iam/").
+ and_return(mock("Net::HTTP Response", :body => "security-credentials/"))
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/iam/security-credentials/").
+ and_return(mock("Net::HTTP Response", :body => "MyRole"))
+ @http_client.should_receive(:get).
+ with("/2012-01-12/meta-data/iam/security-credentials/MyRole").
+ and_return(mock("Net::HTTP Response", :body => "{\n \"Code\" : \"Success\",\n \"LastUpdated\" : \"2012-08-22T07:47:22Z\",\n \"Type\" : \"AWS-HMAC\",\n \"AccessKeyId\" : \"AAAAAAAA\",\n \"SecretAccessKey\" : \"SSSSSSSS\",\n \"Token\" : \"12345678\",\n \"Expiration\" : \"2012-08-22T11:25:52Z\"\n}"))
+ @ohai._require_plugin("ec2")
+
+ @ohai[:ec2].should_not be_nil
+ @ohai[:ec2]['iam']['security-credentials']['MyRole']['Code'].should eql 'Success'
+ @ohai[:ec2]['iam']['security-credentials']['MyRole']['Token'].should eql '12345678'
+ end
end
describe "with ec2 mac and metadata address connected" do
diff --git a/spec/ohai/plugins/eucalyptus_spec.rb b/spec/ohai/plugins/eucalyptus_spec.rb
index a9b2c29a..d78b9951 100644
--- a/spec/ohai/plugins/eucalyptus_spec.rb
+++ b/spec/ohai/plugins/eucalyptus_spec.rb
@@ -39,19 +39,19 @@ describe Ohai::System, "plugin eucalyptus" do
@ohai.stub!(:http_client).and_return(@http_client)
@http_client.should_receive(:get).
- with("/2008-02-01/meta-data/").
+ with("/2012-01-12/meta-data/").
and_return(mock("Net::HTTP Response", :body => "instance_type\nami_id\nsecurity-groups"))
@http_client.should_receive(:get).
- with("/2008-02-01/meta-data/instance_type").
+ with("/2012-01-12/meta-data/instance_type").
and_return(mock("Net::HTTP Response", :body => "c1.medium"))
@http_client.should_receive(:get).
- with("/2008-02-01/meta-data/ami_id").
+ with("/2012-01-12/meta-data/ami_id").
and_return(mock("Net::HTTP Response", :body => "ami-5d2dc934"))
@http_client.should_receive(:get).
- with("/2008-02-01/meta-data/security-groups").
+ with("/2012-01-12/meta-data/security-groups").
and_return(mock("Net::HTTP Response", :body => "group1\ngroup2"))
@http_client.should_receive(:get).
- with("/2008-02-01/user-data/").
+ with("/2012-01-12/user-data/").
and_return(mock("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200"))
end