Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve detection of Windows EC2 nodes by using UUID information #1052

Merged
merged 6 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions lib/ohai/plugins/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -51,7 +50,9 @@ def has_ec2_dmi?
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/
Expand All @@ -63,24 +64,31 @@ def has_ec2_xen_uuid?
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
Expand Down
40 changes: 32 additions & 8 deletions spec/unit/plugins/ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,6 @@
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"

Expand All @@ -368,6 +360,38 @@
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"

Expand Down