diff options
author | Michael Sprauer <Michael@Sprauer.net> | 2015-05-26 15:54:11 +0200 |
---|---|---|
committer | Michael Sprauer <Michael@Sprauer.net> | 2015-05-26 15:54:11 +0200 |
commit | 8cc3ccb57abb7f627822ccdf23cc9d7751c4ef60 (patch) | |
tree | a8942620a09a10023dd873269311f0675d85e9d2 | |
parent | a919f184525a17f6fd48399aa9bc792516823099 (diff) | |
download | ohai-8cc3ccb57abb7f627822ccdf23cc9d7751c4ef60.tar.gz |
return correct ipaddress for openvz guests (fixes #415)
-rw-r--r-- | lib/ohai/plugins/linux/network.rb | 40 | ||||
-rw-r--r-- | spec/unit/plugins/linux/network_spec.rb | 40 |
2 files changed, 67 insertions, 13 deletions
diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 3dd67f64..0f43702e 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -41,6 +41,14 @@ Ohai.plugin(:Network) do ["/sbin/ip", "/usr/bin/ip", "/bin/ip"].any? { |path| File.exist?(path) } end + def is_openvz? + ::File.directory?('/proc/vz') + end + + def is_openvz_host? + is_openvz? && ::File.directory?('/proc/bc') + end + def extract_neighbours(family, iface, neigh_attr) so = shell_out("ip -f #{family[:name]} neigh show") so.stdout.lines do |line| @@ -86,6 +94,7 @@ Ohai.plugin(:Network) do Ohai::Log.debug("Skipping route entry without a device: '#{line}'") next end + route_int = 'venet0:0' if is_openvz? && !is_openvz_host? && route_int == 'venet0' && iface['venet0:0'] unless iface[route_int] Ohai::Log.debug("Skipping previously unseen interface from 'ip route show': #{route_int}") @@ -109,6 +118,23 @@ Ohai.plugin(:Network) do iface end + # now looking at the routes to set the default attributes + # for information, default routes can be of this form : + # - default via 10.0.2.4 dev br0 + # - default dev br0 scope link + # - default via 10.0.3.1 dev eth1 src 10.0.3.2 metric 10 + # - default via 10.0.4.1 dev eth2 src 10.0.4.2 metric 20 + + # using a temporary var to hold routes and their interface name + def parse_routes(family, iface) + iface.collect do |i, iv| + iv[:routes].collect do |r| + r.merge(:dev => i) if r[:family] == family[:name] + end.compact if iv[:routes] + end.compact.flatten + end + + def link_statistics(iface, net_counters) so = shell_out("ip -d -s link") tmp_int = nil @@ -299,19 +325,7 @@ Ohai.plugin(:Network) do iface = check_routing_table(family, iface) - # now looking at the routes to set the default attributes - # for information, default routes can be of this form : - # - default via 10.0.2.4 dev br0 - # - default dev br0 scope link - # - default via 10.0.3.1 dev eth1 src 10.0.3.2 metric 10 - # - default via 10.0.4.1 dev eth2 src 10.0.4.2 metric 20 - - # using a temporary var to hold routes and their interface name - routes = iface.collect do |i,iv| - iv[:routes].collect do |r| - r.merge(:dev=>i) if r[:family] == family[:name] - end.compact if iv[:routes] - end.compact.flatten + routes = parse_routes(family, iface) # using a temporary var to hold the default route # in case there are more than 1 default route, sort it by its metric diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb index 696a874e..7b5dcbde 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -888,6 +888,46 @@ fe80::/64 dev eth0.11 proto kernel metric 256 end end + describe "with openvz setup" do + let(:linux_ip_route) {'default dev venet0 scope link' } + let(:linux_ip_addr) {'1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 + inet 127.0.0.1/8 scope host lo + inet6 ::1/128 scope host + valid_lft forever preferred_lft forever +2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN + link/void + inet 127.0.0.2/32 scope host venet0 + inet 10.116.201.76/24 brd 10.116.201.255 scope global venet0:0 + inet6 2001:44b8:4160:8f00:a00:27ff:fe13:eacd/64 scope global dynamic + valid_lft 6128sec preferred_lft 2526sec +'} + + before(:each) do + allow(plugin).to receive(:is_openvz?).and_return true + allow(plugin).to receive(:is_openvz_host?).and_return false + plugin.run + end + + it "completes the run" do + expect(Ohai::Log).not_to receive(:debug).with(/Plugin linux::network threw exception/) + expect(plugin['network']).not_to be_nil + end + + it "sets default ipv4 interface and gateway" do + expect(plugin['network']['default_interface']).to eq('venet0:0') + expect(plugin['network']['default_gateway']).to eq('0.0.0.0') + end + + it "sets correct routing information" do + expect(plugin['network']['interfaces']['venet0:0']['routes']).to eq([Mash.new( :destination => "default", :family => "inet", :scope => "link" )]) + end + + it "sets correct address information" do + expect(plugin['network']['interfaces']['venet0:0']['addresses']).to eq("10.116.201.76" => Mash.new(:family => 'inet', :prefixlen => '24', :netmask =>'255.255.255.0', :broadcast => '10.116.201.255', :scope => "Global")) + end + end + describe "with irrelevant routes (container setups)" do let(:linux_ip_route) { '10.116.201.0/26 dev eth0 proto kernel src 10.116.201.39 |