diff options
author | Tim Smith <tsmith@chef.io> | 2016-02-02 12:10:46 -0800 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2016-02-02 12:10:46 -0800 |
commit | 493143c55236b90bcd6f5f085dc8b9a8d03b0dc9 (patch) | |
tree | 614c159d03389cb19747ec529529e0de913c2c8e | |
parent | 50e002ef9227318b89242803f647b6c51a1e1a74 (diff) | |
parent | bc60199541f62bec7fc23ccc260abe334a6b3e77 (diff) | |
download | ohai-493143c55236b90bcd6f5f085dc8b9a8d03b0dc9.tar.gz |
Merge pull request #711 from tas50/ec2_detection
Improve EC2 detection when hint isn't present
-rw-r--r-- | lib/ohai/plugins/ec2.rb | 22 | ||||
-rw-r--r-- | spec/unit/plugins/ec2_spec.rb | 16 |
2 files changed, 36 insertions, 2 deletions
diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index c6355dbc..7ad80e5e 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -26,7 +26,9 @@ Ohai.plugin(:EC2) do provides "ec2" depends "network/interfaces" + depends "dmi" + # look for arp address that non-VPC hosts will have def has_ec2_mac? network[:interfaces].values.each do |iface| unless iface[:arp].nil? @@ -40,10 +42,26 @@ Ohai.plugin(:EC2) do false end + # look for amazon string in dmi bios data + # this only works on hvm instances as paravirt instances have no dmi data + def has_ec2_dmi? + begin + # detect a version of '4.2.amazon' + if dmi[:bios][:all_records][0][:Version] =~ /amazon/ + Ohai::Log.debug("has_ec2_dmi? == true") + true + end + rescue NoMethodError + Ohai::Log.debug("has_ec2_dmi? == false") + false + end + end + + def looks_like_ec2? # Try non-blocking connect so we don't "block" if # the Xen environment is *not* EC2 - hint?('ec2') || has_ec2_mac? && can_metadata_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR,80) + hint?('ec2') || ( has_ec2_dmi? || has_ec2_mac?) && can_metadata_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR,80) end collect_data do @@ -60,7 +78,7 @@ Ohai.plugin(:EC2) do end ec2[:userdata] = self.fetch_userdata #ASCII-8BIT is equivalent to BINARY in this case - if ec2[:userdata].encoding.to_s == "ASCII-8BIT" + if ec2[:userdata] && ec2[:userdata].encoding.to_s == "ASCII-8BIT" Ohai::Log.debug("Binary UserData Found. Storing in base64") ec2[:userdata] = Base64.encode64(ec2[:userdata]) end diff --git a/spec/unit/plugins/ec2_spec.rb b/spec/unit/plugins/ec2_spec.rb index 86b53d2c..0b02df1c 100644 --- a/spec/unit/plugins/ec2_spec.rb +++ b/spec/unit/plugins/ec2_spec.rb @@ -270,6 +270,22 @@ end end end + describe "with ec2 dmi data" do + it_should_behave_like "ec2" + + before(:each) do + @plugin[:dmi] = { :bios => { :all_records => [ { :Version => "4.2.amazon" } ] } } + end + end + + describe "without ec2 dmi data" do + it_should_behave_like "!ec2" + + before(:each) do + @plugin[:dmi] = nil + end + end + describe "with ec2 cloud file" do it_should_behave_like "ec2" |