-
Notifications
You must be signed in to change notification settings - Fork 449
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 Linux EC2 detection, fix false detection, and add Windows detection #793
Merged
+109
−83
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5904607
Detect Windows EC2 nodes
tas50 3dcb08c
Remove the Xen MAC address detection method
tas50 5d40c3c
Detect EC2 via Xen Hypervisor UUID starting with ec2
tas50 572dc05
Add tests for the UUID EC2 detection method
tas50 509c105
Updates based on feeback
tas50 8dc538c
Ensure we when values exist, but they don't match
tas50 13387c2
Reduce duplicate code
tas50 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
# Author:: Tim Dysinger (<[email protected]>) | ||
# Author:: Benjamin Black (<[email protected]>) | ||
# Author:: Christopher Brown (<[email protected]>) | ||
# Author:: Tim Smith (<[email protected]>) | ||
# Copyright:: Copyright (c) 2009-2016 Chef Software, Inc. | ||
# License:: Apache License, Version 2.0 | ||
# | ||
|
@@ -19,8 +20,9 @@ | |
|
||
# 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 | ||
# 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 | ||
# 4. Kernel data mentioned Amazon. This catches Windows HVM & paravirt instances | ||
|
||
require "ohai/mixin/ec2_metadata" | ||
require "base64" | ||
|
@@ -30,29 +32,8 @@ | |
|
||
provides "ec2" | ||
|
||
depends "network/interfaces" | ||
depends "dmi" | ||
|
||
# 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? | ||
if iface[:arp].value?("fe:ff:ff:ff:ff:ff") | ||
# using MAC addresses from ARP is unreliable because they could time-out from the table | ||
# fe:ff:ff:ff:ff:ff is actually a sign of Xen, not specifically EC2 | ||
deprecation_message <<-EOM | ||
ec2 plugin: Detected EC2 by the presence of fe:ff:ff:ff:ff:ff in the ARP table. This method is unreliable and will be removed in a future version of ohai. Bootstrap using knife-ec2 or create "/etc/chef/ohai/hints/ec2.json" instead. | ||
EOM | ||
Ohai::Log.warn(deprecation_message) | ||
Ohai::Log.debug("ec2 plugin: has_xen_mac? == true") | ||
return true | ||
end | ||
end | ||
end | ||
Ohai::Log.debug("ec2 plugin: has_xen_mac? == false") | ||
false | ||
end | ||
depends "kernel" | ||
|
||
# look for amazon string in dmi bios data | ||
# this gets us detection of HVM instances that are within a VPC | ||
|
@@ -61,19 +42,49 @@ def has_ec2_dmi? | |
# detect a version of '4.2.amazon' | ||
if dmi[:bios][:all_records][0][:Version] =~ /amazon/ | ||
Ohai::Log.debug("ec2 plugin: has_ec2_dmi? == true") | ||
true | ||
return true | ||
end | ||
rescue NoMethodError | ||
Ohai::Log.debug("ec2 plugin: has_ec2_dmi? == false") | ||
false | ||
# dmi[:bios][:all_records][0][:Version] may not exist | ||
end | ||
Ohai::Log.debug("ec2 plugin: has_ec2_dmi? == false") | ||
return false | ||
end | ||
|
||
# looks for a xen UUID that starts with ec2 | ||
# this gets us detection of Linux HVM and Paravirt hosts | ||
def has_ec2_xen_uuid? | ||
if ::File.exist?("/sys/hypervisor/uuid") | ||
if ::File.read("/sys/hypervisor/uuid") =~ /^ec2/ | ||
Ohai::Log.debug("ec2 plugin: has_ec2_xen_uuid? == true") | ||
return true | ||
end | ||
end | ||
Ohai::Log.debug("ec2 plugin: has_ec2_xen_uuid? == false") | ||
return false | ||
end | ||
|
||
# looks for the Amazon.com Organization in Windows Kernel data | ||
# this gets us detection of Windows systems | ||
def has_amazon_org? | ||
begin | ||
# detect an Organization of 'Amazon.com' | ||
if kernel[:os_info][:organization] =~ /Amazon/ | ||
Ohai::Log.debug("ec2 plugin: has_amazon_org? == true") | ||
return true | ||
end | ||
rescue NoMethodError | ||
# kernel[:os_info][:organization] may not exist | ||
end | ||
Ohai::Log.debug("ec2 plugin: has_amazon_org? == false") | ||
return false | ||
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? | ||
if has_ec2_xen_uuid? || has_ec2_dmi? || has_amazon_org? | ||
return true if can_metadata_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR, 80) | ||
end | ||
end | ||
|
@@ -90,7 +101,7 @@ def looks_like_ec2? | |
next if k == "iam" && !hint?("iam") | ||
ec2[k] = v | ||
end | ||
ec2[:userdata] = self.fetch_userdata | ||
ec2[:userdata] = fetch_userdata | ||
# ASCII-8BIT is equivalent to BINARY in this case | ||
if ec2[:userdata] && ec2[:userdata].encoding.to_s == "ASCII-8BIT" | ||
Ohai::Log.debug("ec2 plugin: Binary UserData Found. Storing in base64") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm slightly uncomfortable catching the nonexistance of
kernel[:os_info][:organization]
with an exception. If it's set to not Amazon, then you'll also miss the logging.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was pretty aggressive there since the keys aren't going to exist on a lot of platforms. kernel[:os_info][:organization] only exists on Windows and I haven't checked, but I assume we don't have kernel on every platform. Do we have a better way to check for the existence of the complete path to that key and it's content?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a DSL method
attribute?
that may be helpfulohai/lib/ohai/dsl/plugin.rb
Lines 124 to 128 in 8f48805
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so switch that to this?:
if kernel.attribute?('os_info') && kernel[:os_info].attribute?('organization') && kernel[:os_info][:organization] =~ /Amazon/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some offline discussion we're going to stick with this for now and add a helper to allow better checking nested Mashes with logging #794