-
Notifications
You must be signed in to change notification settings - Fork 453
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
Changes from 5 commits
5904607
3dcb08c
5d40c3c
572dc05
509c105
8dc538c
13387c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||||
# Author:: Tim Dysinger (<tim@dysinger.net>) | ||||||||||||
# Author:: Benjamin Black (<bb@chef.io>) | ||||||||||||
# Author:: Christopher Brown (<cb@chef.io>) | ||||||||||||
# Author:: Tim Smith (<tsmith@chef.io>) | ||||||||||||
# 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 | ||||||||||||
|
@@ -69,11 +50,40 @@ def has_ec2_dmi? | |||||||||||
end | ||||||||||||
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 | ||||||||||||
else | ||||||||||||
Ohai::Log.debug("ec2 plugin: has_ec2_xen_uuid? == false") | ||||||||||||
return false | ||||||||||||
end | ||||||||||||
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") | ||||||||||||
true | ||||||||||||
end | ||||||||||||
rescue NoMethodError | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm slightly uncomfortable catching the nonexistance of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. There's a DSL method Lines 124 to 128 in 8f48805
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 |
||||||||||||
Ohai::Log.debug("ec2 plugin: has_amazon_org? == false") | ||||||||||||
false | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could do the same if else reduction here and save yourself a repeat of this debug line, but this one isn't as important since it works. |
||||||||||||
end | ||||||||||||
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 +100,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") | ||||||||||||
|
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.
This won't show up if the file exists and does not contain ec2. If you just remove the else line and put this at the end of the method unconditionally, it will probably do what you want since the if statement terminates with a return.