diff options
author | Adam Edwards <adamed@opscode.com> | 2013-10-10 12:42:04 -0700 |
---|---|---|
committer | Adam Edwards <adamed@opscode.com> | 2013-10-10 12:42:04 -0700 |
commit | f3dd943f5f59b2ccb9716e14fcce41dd4ce4d762 (patch) | |
tree | cee3d0eafb9f188f05e4699b310fdfe3c7865267 | |
parent | ede12159bfbfbb85b943b786a0557a04c949c365 (diff) | |
parent | 31be96b5888f5f75c475bcb4f1a026d1ce1e84ab (diff) | |
download | ohai-f3dd943f5f59b2ccb9716e14fcce41dd4ce4d762.tar.gz |
Merge pull request #209 from opscode/adamed-ohai-507-dmi
OHAI-507: GCE Ohai plugin update for GCE v1beta1 compatibility
-rw-r--r-- | lib/ohai/mixin/gce_metadata.rb | 2 | ||||
-rw-r--r-- | lib/ohai/plugins/cloud.rb | 19 | ||||
-rw-r--r-- | lib/ohai/plugins/gce.rb | 26 | ||||
-rw-r--r-- | lib/ohai/plugins/linux/platform.rb | 9 | ||||
-rw-r--r-- | spec/unit/plugins/gce_spec.rb | 96 | ||||
-rw-r--r-- | spec/unit/plugins/linux/platform_spec.rb | 6 |
6 files changed, 67 insertions, 91 deletions
diff --git a/lib/ohai/mixin/gce_metadata.rb b/lib/ohai/mixin/gce_metadata.rb index 5188b381..109b25a6 100644 --- a/lib/ohai/mixin/gce_metadata.rb +++ b/lib/ohai/mixin/gce_metadata.rb @@ -24,7 +24,7 @@ module Ohai extend self GCE_METADATA_ADDR = "metadata.google.internal" unless defined?(GCE_METADATA_ADDR) - GCE_METADATA_URL = "/0.1/meta-data" unless defined?(GCE_METADATA_URL) + GCE_METADATA_URL = "/computeMetadata/v1beta1/?recursive=true" unless defined?(GCE_METADATA_URL) def can_metadata_connect?(addr, port, timeout=2) t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0) diff --git a/lib/ohai/plugins/cloud.rb b/lib/ohai/plugins/cloud.rb index 9d0001af..84f0b364 100644 --- a/lib/ohai/plugins/cloud.rb +++ b/lib/ohai/plugins/cloud.rb @@ -35,20 +35,27 @@ end # Google Compute Engine (gce) #-------------------------------------- +# Is current cloud gce? +# +# === Return +# true:: If gce Hash is defined +# false:: Otherwise def on_gce? gce != nil end + +# Fill cloud hash with gce values def get_gce_values cloud[:public_ipv4] = [] cloud[:local_ipv4] = [] - public_ips = gce['network']["networkInterface"].collect do |interface| - if interface.has_key?('accessConfiguration') - interface['accessConfiguration'].collect{|ac| ac['externalIp']} + public_ips = gce['instance']["networkInterfaces"].collect do |interface| + if interface.has_key?('accessConfigs') + interface['accessConfigs'].collect{|ac| ac['externalIp']} end end.flatten.compact - private_ips = gce['network']["networkInterface"].collect do |interface| + private_ips = gce['instance']["networkInterfaces"].collect do |interface| interface['ip'] end.compact @@ -57,7 +64,7 @@ def get_gce_values cloud[:public_ipv4] += public_ips cloud[:public_hostname] = nil cloud[:local_ipv4] += private_ips - cloud[:local_hostname] = gce['hostname'] + cloud[:local_hostname] = gce['instance']['hostname'] cloud[:provider] = "gce" end @@ -244,4 +251,4 @@ end if on_azure? create_objects get_azure_values -end
\ No newline at end of file +end diff --git a/lib/ohai/plugins/gce.rb b/lib/ohai/plugins/gce.rb index de5b0b4f..1439d3c5 100644 --- a/lib/ohai/plugins/gce.rb +++ b/lib/ohai/plugins/gce.rb @@ -18,18 +18,36 @@ provides "gce" require 'ohai/mixin/gce_metadata' - +# Checks for matching gce dmi +# https://developers.google.com/compute/docs/instances#dmi +# +# === Return +# true:: If gce dmi matches +# false:: Otherwise GOOGLE_SYSFS_DMI = '/sys/firmware/dmi/entries/1-0/raw' - -#https://developers.google.com/compute/docs/instances#dmi def has_google_dmi? ::File.read(GOOGLE_SYSFS_DMI).include?('Google') end +# Checks for gce metadata server +# +# === Return +# true:: If gce metadata server found +# false:: Otherwise +def has_gce_metadata? + Ohai::Mixin::GCEMetadata.can_metadata_connect?(Ohai::Mixin::GCEMetadata::GCE_METADATA_ADDR,80) +end + +# Identifies gce +# +# === Return +# true:: If gce can be identified +# false:: Otherwise def looks_like_gce? - hint?('gce') || (has_google_dmi? && Ohai::Mixin::GCEMetadata.can_metadata_connect?(Ohai::Mixin::GCEMetadata::GCE_METADATA_ADDR,80)) + hint?('gce') || has_google_dmi? || has_gce_metadata? end +# Adds the gce Mash if looks_like_gce? Ohai::Log.debug("looks_like_gce? == true") gce Mash.new diff --git a/lib/ohai/plugins/linux/platform.rb b/lib/ohai/plugins/linux/platform.rb index a7bb6dd0..2d1b62db 100644 --- a/lib/ohai/plugins/linux/platform.rb +++ b/lib/ohai/plugins/linux/platform.rb @@ -38,14 +38,11 @@ elsif File.exists?("/etc/enterprise-release") platform "oracle" platform_version get_redhatish_version(contents) elsif File.exists?("/etc/debian_version") - # Ubuntu, GCEL and Debian both have /etc/debian_version - # Ubuntu, GCEL should always have a working lsb, debian does not by default + # Ubuntu and Debian both have /etc/debian_version + # Ubuntu should always have a working lsb, debian does not by default if lsb[:id] =~ /Ubuntu/i platform "ubuntu" platform_version lsb[:release] - elsif lsb[:id] =~ /gcel/i - platform "gcel" - platform_version lsb[:release] elsif lsb[:id] =~ /LinuxMint/i platform "linuxmint" platform_version lsb[:release] @@ -99,7 +96,7 @@ end case platform - when /debian/, /ubuntu/, /linuxmint/, /raspbian/, /gcel/ + when /debian/, /ubuntu/, /linuxmint/, /raspbian/ platform_family "debian" when /fedora/ platform_family "fedora" diff --git a/spec/unit/plugins/gce_spec.rb b/spec/unit/plugins/gce_spec.rb index 090d24ae..12233afc 100644 --- a/spec/unit/plugins/gce_spec.rb +++ b/spec/unit/plugins/gce_spec.rb @@ -21,99 +21,55 @@ require 'ohai/mixin/gce_metadata' describe Ohai::System, "plugin gce" do before(:each) do @ohai = Ohai::System.new - @ohai.stub!(:require_plugin).and_return(true) + @ohai.stub(:require_plugin).and_return(true) end shared_examples_for "!gce" do - it "should NOT attempt to fetch the gce metadata" do - Ohai::Mixin::GCEMetadata.should_not_receive(:http_client) - @ohai._require_plugin("gce") + it 'should not behave like gce' do + @ohai[:gce].should be_nil end end shared_examples_for "gce" do before(:each) do - @http_client = mock("Net::HTTP client") + @http_client = double("Net::HTTP client") Ohai::Mixin::GCEMetadata.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) - Socket.stub!(:pack_sockaddr_in).and_return(nil) + IO.stub(:select).and_return([[],[1],[]]) + t = double("connection") + t.stub(:connect_nonblock).and_raise(Errno::EINPROGRESS) + Socket.stub(:new).and_return(t) + Socket.stub(:pack_sockaddr_in).and_return(nil) end - it "should recursively fetch metadata" do - @http_client.should_receive(:get). - with("/0.1/meta-data/"). - and_return(mock("Net::HTTPOK", - :body => "domain\nhostname\ndescription", :code=>"200")) - @http_client.should_receive(:get). - with("/0.1/meta-data/domain"). - and_return(mock("Net::HTTPOK", :body => "test-domain", :code=>"200")) - @http_client.should_receive(:get). - with("/0.1/meta-data/hostname"). - and_return(mock("Net::HTTPOK", :body => "test-host", :code=>"200")) + it "should recursively fetch and properly parse json metadata" do @http_client.should_receive(:get). - with("/0.1/meta-data/description"). - and_return(mock("Net::HTTPOK", :body => "test-description", :code=>"200")) + with("/computeMetadata/v1beta1/?recursive=true/"). + and_return(double("Net::HTTP Response", :body => '{"instance":{"hostname":"test-host"}}', :code=>"200")) @ohai._require_plugin("gce") @ohai[:gce].should_not be_nil - @ohai[:gce]['hostname'].should == "test-host" - @ohai[:gce]['domain'].should == "test-domain" - @ohai[:gce]['description'].should == "test-description" - end - - it "should properly parse json metadata" do - @http_client.should_receive(:get). - with("/0.1/meta-data/"). - and_return(mock("Net::HTTP Response", :body => "attached-disks\n", :code=>"200")) - @http_client.should_receive(:get). - with("/0.1/meta-data/attached-disks"). - and_return(mock("Net::HTTP Response", :body => '{"disks":[{"deviceName":"boot", - "index":0,"mode":"READ_WRITE","type":"EPHEMERAL"}]}', :code=>"200")) - - @ohai._require_plugin("gce") - - @ohai[:gce].should_not be_nil - @ohai[:gce]['attached_disks'].should eq({"disks"=>[{"deviceName"=>"boot", - "index"=>0,"mode"=>"READ_WRITE", - "type"=>"EPHEMERAL"}]}) + @ohai[:gce]['instance'].should eq("hostname"=>"test-host") end end - describe "with dmi and metadata address connected" do - it_should_behave_like "gce" - before(:each) do - File.should_receive(:read).with('/sys/firmware/dmi/entries/1-0/raw').and_return('Google') - end - end - - describe "without dmi and metadata address connected" do - it_should_behave_like "!gce" - before(:each) do - File.should_receive(:read).with('/sys/firmware/dmi/entries/1-0/raw').and_return('Test') - end - end - describe "with hint file" do it_should_behave_like "gce" before(:each) do - File.stub!(:exist?).with('/etc/chef/ohai/hints/gce.json').and_return(true) - File.stub!(:read).with('/etc/chef/ohai/hints/gce.json').and_return('') - File.stub!(:exist?).with('C:\chef\ohai\hints/gce.json').and_return(true) - File.stub!(:read).with('C:\chef\ohai\hints/gce.json').and_return('') + File.stub(:exist?).with('/etc/chef/ohai/hints/gce.json').and_return(true) + File.stub(:read).with('/etc/chef/ohai/hints/gce.json').and_return('') + File.stub(:exist?).with('C:\chef\ohai\hints/gce.json').and_return(true) + File.stub(:read).with('C:\chef\ohai\hints/gce.json').and_return('') end end describe "without hint file" do it_should_behave_like "!gce" - + before(:each) do - File.stub!(:exist?).with('/etc/chef/ohai/hints/gce.json').and_return(false) - File.stub!(:exist?).with('C:\chef\ohai\hints/gce.json').and_return(false) + File.stub(:exist?).with('/etc/chef/ohai/hints/gce.json').and_return(false) + File.stub(:exist?).with('C:\chef\ohai\hints/gce.json').and_return(false) end end @@ -121,10 +77,14 @@ describe Ohai::System, "plugin gce" do it_should_behave_like "!gce" before(:each) do - File.stub!(:exist?).with('/etc/chef/ohai/hints/ec2.json').and_return(true) - File.stub!(:read).with('/etc/chef/ohai/hints/ec2.json').and_return('') - File.stub!(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(true) - File.stub!(:read).with('C:\chef\ohai\hints/ec2.json').and_return('') + File.stub(:exist?).with('/etc/chef/ohai/hints/gce.json').and_return(false) + File.stub(:exist?).with('C:\chef\ohai\hints/gce.json').and_return(false) + + File.stub(:exist?).with('/etc/chef/ohai/hints/ec2.json').and_return(true) + File.stub(:read).with('/etc/chef/ohai/hints/ec2.json').and_return('') + File.stub(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(true) + File.stub(:read).with('C:\chef\ohai\hints/ec2.json').and_return('') end end + end diff --git a/spec/unit/plugins/linux/platform_spec.rb b/spec/unit/plugins/linux/platform_spec.rb index 189b3430..1a194702 100644 --- a/spec/unit/plugins/linux/platform_spec.rb +++ b/spec/unit/plugins/linux/platform_spec.rb @@ -72,12 +72,6 @@ describe Ohai::System, "Linux plugin platform" do @ohai[:platform].should == "linuxmint" @ohai[:platform_family].should == "debian" end - it "should set platform to gcel and platform_family to debian [:lsb][:id] contains GCEL" do - @ohai[:lsb][:id] = "GCEL" - @ohai._require_plugin("linux::platform") - @ohai[:platform].should == "gcel" - @ohai[:platform_family].should == "debian" - end it "should set platform to debian and platform_family to debian [:lsb][:id] contains Debian" do @ohai[:lsb][:id] = "Debian" @ohai._require_plugin("linux::platform") |