summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-09-07 13:23:44 -0700
committerGitHub <noreply@github.com>2017-09-07 13:23:44 -0700
commit226637363cd61e8238f30e32dbe58231a1976422 (patch)
tree2e75ebed5fbd3b9e8eb8e9bd2981f877f88bd416
parent518d938ae4bcfa9da3f79030a70082cd02c30604 (diff)
parent4960a0764993191dbcc065ae17ffa67635fdf56b (diff)
downloadohai-226637363cd61e8238f30e32dbe58231a1976422.tar.gz
Merge pull request #1052 from chef/ec2_windows
Improve detection of Windows EC2 nodes by using UUID information
-rw-r--r--lib/ohai/plugins/ec2.rb40
-rw-r--r--spec/unit/plugins/ec2_spec.rb40
2 files changed, 56 insertions, 24 deletions
diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb
index f0e0f5eb..2dd0fd63 100644
--- a/lib/ohai/plugins/ec2.rb
+++ b/lib/ohai/plugins/ec2.rb
@@ -24,21 +24,20 @@
# 3. DMI data mentions amazon. This catches HVM instances in a VPC
# 4. Kernel data mentioned Amazon. This catches Windows HVM & paravirt instances
-require "ohai/mixin/ec2_metadata"
-require "ohai/mixin/http_helper"
-require "base64"
-
Ohai.plugin(:EC2) do
+ require "ohai/mixin/ec2_metadata"
+ require "ohai/mixin/http_helper"
+ require "base64"
+
include Ohai::Mixin::Ec2Metadata
include Ohai::Mixin::HttpHelper
provides "ec2"
-
depends "dmi"
- depends "kernel"
# look for amazon string in dmi bios data
# this gets us detection of HVM instances that are within a VPC
+ # @return [Boolean] do we have Amazon DMI data?
def has_ec2_dmi?
# detect a version of '4.2.amazon'
if get_attribute(:dmi, :bios, :all_records, 0, :Version) =~ /amazon/
@@ -51,7 +50,9 @@ Ohai.plugin(:EC2) do
end
# looks for a xen UUID that starts with ec2
- # this gets us detection of Linux HVM and Paravirt hosts
+ # uses the sys tree on Linux and a WMI query on windows
+ # this gets us detection of HVM and Paravirt hosts
+ # @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/
@@ -63,24 +64,31 @@ Ohai.plugin(:EC2) do
false
end
- # looks for the Amazon.com Organization in Windows Kernel data
- # this gets us detection of Windows systems
- def has_amazon_org?
- # detect an Organization of 'Amazon.com'
- if get_attribute(:kernel, :os_info, :organization) =~ /Amazon/
- Ohai::Log.debug("Plugin EC2: has_amazon_org? == true")
- true
+ # looks at the identifying number WMI value to see if it starts with ec2.
+ # this is actually the same value we're looking at in has_ec2_xen_uuid? on
+ # linux hosts
+ # @return [Boolean] do we have a Xen Identifying Number or not?
+ def has_ec2_identifying_number?
+ if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ # require "wmi-lite/wmi"
+ wmi = WmiLite::Wmi.new
+ if wmi.first_of("Win32_ComputerSystemProduct")["identifyingnumber"] =~ /^ec2/
+ Ohai::Log.debug("Plugin EC2: has_ec2_identifying_number? == true")
+ return true
+ end
else
- Ohai::Log.debug("Plugin EC2: has_amazon_org? == false")
+ Ohai::Log.debug("Plugin EC2: has_ec2_identifying_number? == false")
false
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_amazon_org?
+ if has_ec2_xen_uuid? || has_ec2_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 bf59c205..25c3eba7 100644
--- a/spec/unit/plugins/ec2_spec.rb
+++ b/spec/unit/plugins/ec2_spec.rb
@@ -342,14 +342,6 @@ describe Ohai::System, "plugin ec2" do
end
end
- describe "with amazon kernel data" do
- it_behaves_like "ec2"
-
- before(:each) do
- plugin[:kernel] = { :os_info => { :organization => "Amazon.com" } }
- end
- end
-
describe "with EC2 Xen UUID" do
it_behaves_like "ec2"
@@ -368,6 +360,38 @@ describe Ohai::System, "plugin ec2" do
end
end
+ describe "with EC2 Identifying Number", :windows_only do
+ it_behaves_like "ec2"
+
+ before do
+ allow_any_instance_of(WmiLite::Wmi).to receive(:first_of).and_return(
+ { "caption" => "Computer System Product",
+ "description" => "Computer System Product",
+ "identifyingnumber" => "ec2a355a-91cd-5fe8-bbfc-cc891d0bf9d6",
+ "name" => "HVM domU",
+ "skunumber" => nil,
+ "uuid" => "5A352AEC-CD91-E85F-BBFC-CC891D0BF9D6",
+ "vendor" => "Xen",
+ "version" => "4.2.amazon" })
+ end
+ end
+
+ describe "without EC2 Identifying Number", :windows_only do
+ it_behaves_like "!ec2"
+
+ before do
+ allow_any_instance_of(WmiLite::Wmi).to receive(:first_of).and_return(
+ { "caption" => "Computer System Product",
+ "description" => "Computer System Product",
+ "identifyingnumber" => "1234",
+ "name" => "HVM domU",
+ "skunumber" => nil,
+ "uuid" => "5A352AEC-CD91-E85F-BBFC-CC891D0BF9D6",
+ "vendor" => "Xen",
+ "version" => "1.2.3" })
+ end
+ end
+
describe "with ec2 hint file" do
it_behaves_like "ec2"