diff options
author | Tim Smith <tsmith@chef.io> | 2017-11-08 12:57:36 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-08 12:57:36 -0800 |
commit | 629ae2a25a43ef6625b53aa9ba54dc32fca9cc78 (patch) | |
tree | 9b07c0201999555ee9d17e51c4b2359713c7e6d3 | |
parent | f52260a52e1fec3b6ff8bed9778553b96c65e464 (diff) | |
parent | 8253ce9630be4718a325a6931e1dd7e4683c79ad (diff) | |
download | ohai-629ae2a25a43ef6625b53aa9ba54dc32fca9cc78.tar.gz |
Merge pull request #1083 from chef/ec2_hypervisor
Detect new Amazon hypervisor used by the C5 instances
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | appveyor.yml | 2 | ||||
-rw-r--r-- | lib/ohai/plugins/ec2.rb | 53 | ||||
-rw-r--r-- | spec/unit/plugins/ec2_spec.rb | 38 |
4 files changed, 72 insertions, 22 deletions
diff --git a/.travis.yml b/.travis.yml index dd49c7c8..4acca285 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,6 @@ branches: - 8-stable before_install: - - gem update --system - gem --version - rvm @global do gem uninstall bundler -a -x -I || true - gem install bundler diff --git a/appveyor.yml b/appveyor.yml index bf6cd845..7728c8ad 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -7,6 +7,7 @@ platform: environment: matrix: - ruby_version: "23" + - ruby_version: "24" clone_folder: c:\projects\ohai clone_depth: 1 @@ -19,7 +20,6 @@ install: - SET PATH=C:\Ruby%ruby_version%\bin;%PATH% - echo %PATH% - ruby --version - - gem update --system - gem --version - gem uninstall bundler -a -x -I - gem install bundler --quiet --no-ri --no-rdoc diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index 1a3598d8..fdf0084a 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -21,7 +21,8 @@ # How we detect EC2 from easiest to hardest & least reliable # 1. Ohai ec2 hint exists. This always works # 2. Xen hypervisor UUID starts with 'ec2'. This catches Linux HVM & paravirt instances -# 3. DMI data mentions amazon. This catches HVM instances in a VPC +# 3. DMI bios version data mentions amazon. This catches HVM instances in a VPC on the Xen based hypervisor +# 3. DMI bios vendor data mentions amazon. This catches HVM instances in a VPC on the non-Xen based hypervisor # 4. Kernel data mentioned Amazon. This catches Windows HVM & paravirt instances Ohai.plugin(:EC2) do @@ -33,32 +34,43 @@ Ohai.plugin(:EC2) do include Ohai::Mixin::HttpHelper provides "ec2" - depends "dmi" - # look for amazon string in dmi bios data + # look for amazon string in dmi vendor bios data within the sys tree. + # this works even if the system lacks dmidecode use by the Dmi plugin + # this gets us detection of new Xen-less HVM instances that are within a VPC + # @return [Boolean] do we have Amazon DMI data? + def has_ec2_amazon_dmi? + # detect a version of '4.2.amazon' + if file_val_if_exists("/sys/class/dmi/id/bios_vendor") =~ /Amazon/ + Ohai::Log.debug("Plugin EC2: has_ec2_amazon_dmi? == true") + true + else + Ohai::Log.debug("Plugin EC2: has_ec2_amazon_dmi? == false") + false + end + end + + # look for amazon string in dmi bios version data within the sys tree. + # this works even if the system lacks dmidecode use by the Dmi plugin # this gets us detection of HVM instances that are within a VPC # @return [Boolean] do we have Amazon DMI data? - def has_ec2_dmi? + def has_ec2_xen_dmi? # detect a version of '4.2.amazon' - if get_attribute(:dmi, :bios, :all_records, 0, :Version) =~ /amazon/ - Ohai::Log.debug("Plugin EC2: has_ec2_dmi? == true") + if file_val_if_exists("/sys/class/dmi/id/bios_version") =~ /amazon/ + Ohai::Log.debug("Plugin EC2: has_ec2_xen_dmi? == true") true else - Ohai::Log.debug("Plugin EC2: has_ec2_dmi? == false") + Ohai::Log.debug("Plugin EC2: has_ec2_xen_dmi? == false") false end end - # looks for a xen UUID that starts with ec2 - # uses the sys tree on Linux and a WMI query on windows - # this gets us detection of HVM and Paravirt hosts + # looks for a xen UUID that starts with ec2 from within the Linux sys tree # @return [Boolean] do we have a Xen UUID or not? def has_ec2_xen_uuid? - if ::File.exist?("/sys/hypervisor/uuid") - if ::File.read("/sys/hypervisor/uuid") =~ /^ec2/ - Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == true") - return true - end + if file_val_if_exists("/sys/hypervisor/uuid") =~ /^ec2/ + Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == true") + return true end Ohai::Log.debug("Plugin EC2: has_ec2_xen_uuid? == false") false @@ -82,13 +94,22 @@ Ohai.plugin(:EC2) do end end + # return the contents of a file if the file exists + # @param path[String] abs path to the file + # @return [String] contents of the file if it exists + def file_val_if_exists(path) + if ::File.exist?(path) + ::File.read(path) + end + end + # a single check that combines all the various detection methods for EC2 # @return [Boolean] Does the system appear to be on EC2 def looks_like_ec2? return true if hint?("ec2") # Even if it looks like EC2 try to connect first - if has_ec2_xen_uuid? || has_ec2_dmi? || has_ec2_identifying_number? + if has_ec2_xen_uuid? || has_ec2_amazon_dmi? || has_ec2_xen_dmi? || has_ec2_identifying_number? return true if can_socket_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 bf64da2b..00937a3a 100644 --- a/spec/unit/plugins/ec2_spec.rb +++ b/spec/unit/plugins/ec2_spec.rb @@ -28,6 +28,8 @@ describe Ohai::System, "plugin ec2" do before(:each) do allow(plugin).to receive(:hint?).with("ec2").and_return(false) allow(File).to receive(:exist?).with("/sys/hypervisor/uuid").and_return(false) + allow(File).to receive(:exist?).with("/sys/class/dmi/id/bios_vendor").and_return(false) + allow(File).to receive(:exist?).with("/sys/class/dmi/id/bios_version").and_return(false) end shared_examples_for "!ec2" do @@ -332,11 +334,39 @@ describe Ohai::System, "plugin ec2" do end end # shared examples for ec2 - describe "with ec2 dmi data" do + describe "with amazon dmi bios version data" do it_behaves_like "ec2" before(:each) do - plugin[:dmi] = { :bios => { :all_records => [ { :Version => "4.2.amazon" } ] } } + allow(File).to receive(:exist?).with("/sys/class/dmi/id/bios_version").and_return(true) + allow(File).to receive(:read).with("/sys/class/dmi/id/bios_version").and_return("4.2.amazon\n") + end + end + + describe "with non-amazon dmi bios version data" do + it_behaves_like "!ec2" + + before(:each) do + allow(File).to receive(:exist?).with("/sys/class/dmi/id/bios_version").and_return(true) + allow(File).to receive(:read).with("/sys/class/dmi/id/bios_version").and_return("1.0\n") + end + end + + describe "with amazon dmi bios vendor data" do + it_behaves_like "ec2" + + before(:each) do + allow(File).to receive(:exist?).with("/sys/class/dmi/id/bios_vendor").and_return(true) + allow(File).to receive(:read).with("/sys/class/dmi/id/bios_vendor").and_return("Amazon EC2\n") + end + end + + describe "with non-amazon dmi bios vendor data" do + it_behaves_like "!ec2" + + before(:each) do + allow(File).to receive(:exist?).with("/sys/class/dmi/id/bios_vendor").and_return(true) + allow(File).to receive(:read).with("/sys/class/dmi/id/bios_vendor").and_return("Xen\n") end end @@ -345,7 +375,7 @@ describe Ohai::System, "plugin ec2" do before(:each) do allow(File).to receive(:exist?).with("/sys/hypervisor/uuid").and_return(true) - allow(File).to receive(:read).with("/sys/hypervisor/uuid").and_return("ec2a0561-e4d6-8e15-d9c8-2e0e03adcde8") + allow(File).to receive(:read).with("/sys/hypervisor/uuid").and_return("ec2a0561-e4d6-8e15-d9c8-2e0e03adcde8\n") end end @@ -354,7 +384,7 @@ describe Ohai::System, "plugin ec2" do before(:each) do allow(File).to receive(:exist?).with("/sys/hypervisor/uuid").and_return(true) - allow(File).to receive(:read).with("/sys/hypervisor/uuid").and_return("123a0561-e4d6-8e15-d9c8-2e0e03adcde8") + allow(File).to receive(:read).with("/sys/hypervisor/uuid").and_return("123a0561-e4d6-8e15-d9c8-2e0e03adcde8\n") end end |