summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2016-02-29 14:30:59 -0800
committerTim Smith <tsmith84@gmail.com>2016-02-29 19:37:55 -0800
commit1b851ab0ce23b8cc5b1436b6109655176efc21da (patch)
treeb0f243d9e8c50751ebff41616b035866e120c41c
parentac9f5a2ae4ada9f2e6e821418ed3a9039bda9a91 (diff)
downloadohai-1b851ab0ce23b8cc5b1436b6109655176efc21da.tar.gz
Dont detect systems with a dhcp lease on eth0 as azure
-rw-r--r--lib/ohai/plugins/azure.rb16
-rw-r--r--spec/unit/plugins/azure_spec.rb26
2 files changed, 36 insertions, 6 deletions
diff --git a/lib/ohai/plugins/azure.rb b/lib/ohai/plugins/azure.rb
index 52fe1d9a..8bc29964 100644
--- a/lib/ohai/plugins/azure.rb
+++ b/lib/ohai/plugins/azure.rb
@@ -27,7 +27,7 @@ Ohai.plugin(:Azure) do
Ohai::Log.debug("azure plugin: azure_metadata_from_hints is present.")
azure Mash.new
azure_metadata_from_hints.each { |k, v| azure[k] = v }
- elsif looks_like_azure?
+ elsif has_waagent? || has_dhcp_option_245?
Ohai::Log.debug("azure plugin: No hints present, but system appears to be on azure.")
azure Mash.new
else
@@ -38,17 +38,25 @@ Ohai.plugin(:Azure) do
# check for either the waagent or the unknown-245 DHCP option that Azure uses
# http://blog.mszcool.com/index.php/2015/04/detecting-if-a-virtual-machine-runs-in-microsoft-azure-linux-windows-to-protect-your-software-when-distributed-via-the-azure-marketplace/
- def looks_like_azure?
+ def has_waagent?
if File.exist?("/usr/sbin/waagent") || Dir.exist?('C:\WindowsAzure')
Ohai::Log.debug("azure plugin: Found waagent used by MS Azure.")
return true
- elsif File.exist?("/var/lib/dhcp/dhclient.eth0.leases")
+ end
+ end
+
+ def has_dhcp_option_245?
+ has_245 = false
+ if File.exist?("/var/lib/dhcp/dhclient.eth0.leases")
File.open("/var/lib/dhcp/dhclient.eth0.leases").each do |line|
if line =~ /unknown-245/
Ohai::Log.debug("azure plugin: Found unknown-245 DHCP option used by MS Azure.")
- return true
+ has_245 = true
+ break
end
end
end
+ return has_245
end
+
end
diff --git a/spec/unit/plugins/azure_spec.rb b/spec/unit/plugins/azure_spec.rb
index 5d1de59d..0af7df24 100644
--- a/spec/unit/plugins/azure_spec.rb
+++ b/spec/unit/plugins/azure_spec.rb
@@ -50,7 +50,29 @@ describe Ohai::System, "plugin azure" do
allow(File).to receive(:exist?).with('C:\chef\ohai\hints/azure.json').and_return(false)
allow(File).to receive(:exist?).with("/usr/sbin/waagent").and_return(false)
allow(Dir).to receive(:exist?).with('C:\WindowsAzure').and_return(false)
- allow(File).to receive(:exist?).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(false)
+ allow(File).to receive(:exist?).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(true)
+ @double_file = double("/var/lib/dhcp/dhclient.eth0.leases")
+ allow(@double_file).to receive(:each).
+ and_yield("lease {").
+ and_yield(' interface "eth0";').
+ and_yield(" fixed-address 192.168.1.194;").
+ and_yield(" option subnet-mask 255.255.255.0;").
+ and_yield(" option routers 192.168.1.1;").
+ and_yield(" option dhcp-lease-time 86400;").
+ and_yield(" option dhcp-message-type 5;").
+ and_yield(" option domain-name-servers 8.8.8.8;").
+ and_yield(" option dhcp-server-identifier 192.168.1.2;").
+ and_yield(" option interface-mtu 1454;").
+ and_yield(" option dhcp-renewal-time 42071;").
+ and_yield(" option broadcast-address 192.168.1.255;").
+ and_yield(" option dhcp-rebinding-time 74471;").
+ and_yield(' option host-name "host-192-168-1-194";').
+ and_yield(' option domain-name "openstacklocal";').
+ and_yield(" renew 2 2016/03/01 01:49:41;").
+ and_yield(" rebind 2 2016/03/01 13:22:07;").
+ and_yield(" expire 2 2016/03/01 16:40:56;").
+ and_yield("}")
+ allow(File).to receive(:open).with("/var/lib/dhcp/dhclient.eth0.leases").and_return(@double_file)
@plugin.run
end
@@ -59,7 +81,7 @@ describe Ohai::System, "plugin azure" do
end
end
- describe "with rackspace hint file no agent and no dhcp options" do
+ describe "with rackspace hint file no agent and no dhcp lease" do
before(:each) do
allow(File).to receive(:exist?).with("/etc/chef/ohai/hints/rackspace.json").and_return(true)
allow(File).to receive(:read).with("/etc/chef/ohai/hints/rackspace.json").and_return("")