diff options
author | Tim Smith <tsmith@chef.io> | 2016-04-01 09:22:57 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2016-04-01 09:22:57 -0700 |
commit | af291324a73ac44836cadafd96114a535b681b43 (patch) | |
tree | bb7e47b0e3d4c96a35815d25305e157ed2d796a3 | |
parent | 2766d816cab002bb6360e53392e592e33b97a199 (diff) | |
parent | 3301715e5b7924bcdba7cf30c2f382f10b801066 (diff) | |
download | ohai-af291324a73ac44836cadafd96114a535b681b43.tar.gz |
Merge pull request #787 from tas50/gce_fix
Remove ec2metadata CLI as a EC2 detection method
-rw-r--r-- | lib/ohai/plugins/ec2.rb | 28 | ||||
-rw-r--r-- | spec/unit/plugins/ec2_spec.rb | 22 |
2 files changed, 10 insertions, 40 deletions
diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index 1f383035..a9c7eff7 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -17,6 +17,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +# How we detect EC2 from easiest to hardest & least reliable +# 1. Ohai ec2 hint exists. This always works +# 2. DMI data mentions amazon. This catches HVM instances in a VPC +# 3. Has a xen MAC + can connect to metadata. This catches paravirt instances not in a VPC + require "ohai/mixin/ec2_metadata" require "base64" @@ -28,18 +33,8 @@ Ohai.plugin(:EC2) do depends "network/interfaces" depends "dmi" - # look for ec2metadata which is included on paravirt / hvm AMIs - def has_ec2metadata_bin? - if File.exist?("/usr/bin/ec2metadata") - Ohai::Log.debug("ec2 plugin: has_ec2metadata_bin? == true") - true - else - Ohai::Log.debug("ec2 plugin: has_ec2metadata_bin? == false") - false - end - end - - # look for arp address that non-VPC hosts will have + # look for xen arp address + # this gets us detection of paravirt instances that are NOT within a VPC def has_xen_mac? network[:interfaces].values.each do |iface| unless iface[:arp].nil? @@ -60,7 +55,7 @@ EOM end # look for amazon string in dmi bios data - # this only works on hvm instances as paravirt instances have no dmi data + # this gets us detection of HVM instances that are within a VPC def has_ec2_dmi? begin # detect a version of '4.2.amazon' @@ -74,16 +69,11 @@ EOM end end - # rackspace systems look like ec2 so instead of timing out dig a bit deeper - def looks_like_rackspace? - return true if File.exist?("/usr/bin/rackspace-monitoring-agent") - end - def looks_like_ec2? return true if hint?("ec2") # Even if it looks like EC2 try to connect first - if has_ec2_dmi? || has_xen_mac? || (has_ec2metadata_bin? && !looks_like_rackspace?) + if has_ec2_dmi? || has_xen_mac? return true if can_metadata_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80) end end diff --git a/spec/unit/plugins/ec2_spec.rb b/spec/unit/plugins/ec2_spec.rb index bddd887b..1098b5fa 100644 --- a/spec/unit/plugins/ec2_spec.rb +++ b/spec/unit/plugins/ec2_spec.rb @@ -27,8 +27,6 @@ describe Ohai::System, "plugin ec2" do @plugin[:network] = { :interfaces => { :eth0 => {} } } allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(false) allow(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(false) - allow(File).to receive(:exist?).with("/usr/bin/ec2metadata").and_return(false) - allow(File).to receive(:exist?).with("/usr/bin/rackspace-monitoring-agent").and_return(false) end shared_examples_for "!ec2" do @@ -256,7 +254,7 @@ describe Ohai::System, "plugin ec2" do end end # shared examples for ec2 - describe "without dmi or ec2metadata binary, with xen mac, and metadata address connected" do + describe "without dmi data, kernel organization, with xen mac, and metadata address connected" do before(:each) do allow(IO).to receive(:select).and_return([[], [1], []]) @plugin[:network][:interfaces][:eth0][:arp] = { "169.254.1.0" => "fe:ff:ff:ff:ff:ff" } @@ -270,14 +268,6 @@ describe Ohai::System, "plugin ec2" do end end - describe "with ec2metadata binary" do - it_should_behave_like "ec2" - - before(:each) do - allow(File).to receive(:exist?).with("/usr/bin/ec2metadata").and_return(true) - end - end - describe "with ec2 dmi data" do it_should_behave_like "ec2" @@ -311,22 +301,12 @@ describe Ohai::System, "plugin ec2" do end end - describe "with ec2metadata, but with rackspace-monitoring-agent" do - it_should_behave_like "!ec2" - - before(:each) do - allow(File).to receive(:exist?).with("/usr/bin/ec2metadata").and_return(true) - allow(File).to receive(:exist?).with("/usr/bin/rackspace-monitoring-agent").and_return(true) - end - end - describe "without any hints that it is an ec2 system" do it_should_behave_like "!ec2" before(:each) do allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/ec2.json").and_return(false) allow(File).to receive(:exist?).with('C:\chef\ohai\hints/ec2.json').and_return(false) - allow(File).to receive(:exist?).with("/usr/bin/ec2metadata").and_return(false) @plugin[:dmi] = nil @plugin[:network][:interfaces][:eth0][:arp] = { "169.254.1.0" => "00:50:56:c0:00:08" } end |