summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Czarkowski <paul.czarkowski@rackspace.com>2014-01-27 13:07:06 -0600
committerPaul Czarkowski <paul.czarkowski@rackspace.com>2014-01-27 13:07:06 -0600
commit609516a8e67b0056d04a3983b67d49bf6f6dad1c (patch)
treec59123fd90a4da1b215172c7d26ec3d6261a6490
parent8890c7f6ac94bc50875726971a6ecc420811e8f7 (diff)
downloadohai-609516a8e67b0056d04a3983b67d49bf6f6dad1c.tar.gz
rackspace private network discovery
-rw-r--r--lib/ohai/plugins/rackspace.rb25
-rw-r--r--spec/unit/plugins/rackspace_spec.rb76
2 files changed, 95 insertions, 6 deletions
diff --git a/lib/ohai/plugins/rackspace.rb b/lib/ohai/plugins/rackspace.rb
index b96ebdf9..192445bc 100644
--- a/lib/ohai/plugins/rackspace.rb
+++ b/lib/ohai/plugins/rackspace.rb
@@ -93,6 +93,28 @@ Ohai.plugin(:Rackspace) do
Ohai::Log.debug("Unable to find xenstore-ls, cannot capture region information for Rackspace cloud")
end
+ # Get the rackspace private networks
+ #
+ def get_private_networks()
+ so = shell_out('xenstore-ls vm-data/networking')
+ if so.exitstatus == 0
+ networks = []
+ so.stdout.split("\n").map{|l| l.split('=').first.strip }.map do |item|
+ _so = shell_out("xenstore-read vm-data/networking/#{item}")
+ if _so.exitstatus == 0
+ networks.push(Yajl::Parser.new.parse(_so.stdout))
+ else
+ raise Ohai::Exceptions::Exec
+ end
+ end
+ # these networks are already known to ohai, and are not 'private networks'
+ networks.delete_if { |hash| hash['label'] == 'private' }
+ networks.delete_if { |hash| hash['label'] == 'public' }
+ end
+ rescue Ohai::Exceptions::Exec
+ Ohai::Log.debug('Unable to capture custom private networking information for Rackspace cloud')
+ end
+
collect_data do
# Adds rackspace Mash
if looks_like_rackspace?
@@ -109,6 +131,9 @@ Ohai.plugin(:Rackspace) do
rackspace[:local_ipv4] = rackspace[:private_ip]
get_global_ipv6_address(:local_ipv6, :eth1)
rackspace[:local_hostname] = hostname
+ private_networks = get_private_networks
+ rackspace[:private_networks] = private_networks if private_networks
end
end
+
end
diff --git a/spec/unit/plugins/rackspace_spec.rb b/spec/unit/plugins/rackspace_spec.rb
index 357905c8..72dc6e28 100644
--- a/spec/unit/plugins/rackspace_spec.rb
+++ b/spec/unit/plugins/rackspace_spec.rb
@@ -58,7 +58,7 @@ describe Ohai::System, "plugin rackspace" do
"40:40:F5:AB:28:36" => {
"family"=> "lladdr"
}
- }}
+ }}
# In olden days we could detect rackspace by a -rscloud suffix on the kernel
# This is here to make #has_rackspace_kernel? fail until we remove that check
@@ -133,7 +133,7 @@ OUT
describe 'with no public interfaces (empty eth0)' do
before do
# unset public (eth0) addresses
- @plugin[:network][:interfaces][:eth0]['addresses'] = {}
+ @plugin[:network][:interfaces][:eth0]['addresses'] = {}
end
it "should have all required attributes" do
@@ -149,7 +149,7 @@ OUT
@plugin[:rackspace][:local_ipv6].should be_nil
@plugin[:rackspace][:local_hostname].should_not be_nil
end
-
+
it "should have correct values for all attributes" do
@plugin.run
@plugin[:rackspace][:private_ip].should == "5.6.7.8"
@@ -161,16 +161,16 @@ OUT
describe "without cloud file" do
it_should_behave_like "!rackspace"
-
+
before(:each) do
File.stub(:exist?).with('/etc/chef/ohai/hints/rackspace.json').and_return(false)
File.stub(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(false)
end
end
-
+
describe "with ec2 cloud file" do
it_should_behave_like "!rackspace"
-
+
before(:each) do
File.stub(:exist?).with('/etc/chef/ohai/hints/ec2.json').and_return(true)
File.stub(:read).with('/etc/chef/ohai/hints/ec2.json').and_return('')
@@ -199,4 +199,68 @@ OUT
@plugin.stub(:shell_out).with("xenstore-read vm-data/provider_data/provider").and_return(mock_shell_out(0, stdout, "" ))
end
end
+
+ describe "does not have private networks" do
+ before do
+ stdout = 'BC764E20422B = "{"label": "public"}"\n'
+ @plugin.stub(:shell_out).with("xenstore-ls vm-data/networking").and_return(mock_shell_out(0, stdout, "" ))
+ stdout = '{"label": "public", "broadcast": "9.10.11.255", "ips": [{"ip": "9.10.11.12", "netmask": "255.255.255.0", "enabled": "1", "gateway": null}], "mac": "BC:76:4E:20:42:2B", "dns": ["69.20.0.164", "69.20.0.196"], "gateway": null}'
+ @plugin.stub(:shell_out).with("xenstore-read vm-data/networking/BC764E20422B").and_return(mock_shell_out(0, stdout, "" ))
+
+ File.stub(:exist?).with('/etc/chef/ohai/hints/rackspace.json').and_return(true)
+ File.stub(:read).with('/etc/chef/ohai/hints/rackspace.json').and_return('')
+ File.stub(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true)
+ File.stub(:read).with('C:\chef\ohai\hints/rackspace.json').and_return('')
+ end
+
+ it "should not have private_networks object" do
+ @plugin.run
+ @plugin[:rackspace][:private_networks].should == []
+ end
+ end
+
+ describe "has private networks" do
+ before do
+ @plugin[:network][:interfaces][:eth2] = {:addresses => {
+ "fe80::be76:4eff:fe20:422b" => {
+ "scope"=> "Link",
+ "prefixlen"=> "64",
+ "family"=> "inet6"
+ },
+ "9.10.11.12"=> {
+ "broadcast"=> "9.10.11.255",
+ "netmask"=> "255.255.255.0",
+ "family"=> "inet"
+ },
+ "BC:76:4E:20:42:2B" => {
+ "family"=> "lladdr"
+ }
+ }}
+ stdout = 'BC764E20422B = "{"label": "private-network"}"\n'
+ @plugin.stub(:shell_out).with("xenstore-ls vm-data/networking").and_return(mock_shell_out(0, stdout, "" ))
+ stdout = '{"label": "private-network", "broadcast": "9.10.11.255", "ips": [{"ip": "9.10.11.12", "netmask": "255.255.255.0", "enabled": "1", "gateway": null}], "mac": "BC:76:4E:20:42:2B", "dns": ["69.20.0.164", "69.20.0.196"], "gateway": null}'
+ @plugin.stub(:shell_out).with("xenstore-read vm-data/networking/BC764E20422B").and_return(mock_shell_out(0, stdout, "" ))
+
+ File.stub(:exist?).with('/etc/chef/ohai/hints/rackspace.json').and_return(true)
+ File.stub(:read).with('/etc/chef/ohai/hints/rackspace.json').and_return('')
+ File.stub(:exist?).with('C:\chef\ohai\hints/rackspace.json').and_return(true)
+ File.stub(:read).with('C:\chef\ohai\hints/rackspace.json').and_return('')
+ end
+
+ it "should private_networks object" do
+ @plugin.run
+ @plugin[:rackspace][:private_networks].should_not be_nil
+ end
+
+ it "should have correct values for all attributes" do
+ @plugin.run
+ @plugin[:rackspace][:private_networks][0][:label].should == "private-network"
+ @plugin[:rackspace][:private_networks][0][:broadcast].should == "9.10.11.255"
+ @plugin[:rackspace][:private_networks][0][:mac].should == "BC:76:4E:20:42:2B"
+ end
+
+ end
+
+
+
end