diff options
author | Laurent <laurent+chef@u-picardie.fr> | 2012-04-21 11:54:11 +0200 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2012-06-08 08:42:52 -0700 |
commit | bd55928dca1381923eb957cb71aa93561a201c66 (patch) | |
tree | 1ed6a21610bb20701290a1148fd727f2116cd32b | |
parent | 7fbb125ee624aa839bb76979c4c49aac6e884346 (diff) | |
download | ohai-bd55928dca1381923eb957cb71aa93561a201c66.tar.gz |
some fixes
using netmask instead of prefixlen (the windows plugin doesn't set the prefixlen attribute)
-rw-r--r-- | lib/ohai/plugins/network.rb | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/lib/ohai/plugins/network.rb b/lib/ohai/plugins/network.rb index 33559d0e..86c13e3a 100644 --- a/lib/ohai/plugins/network.rb +++ b/lib/ohai/plugins/network.rb @@ -44,17 +44,20 @@ def find_ip_and_iface(family = "inet", match = nil) iface_v['addresses'].each do |addr, addr_v| next if addr_v.nil? or not addr_v.has_key? "family" or addr_v['family'] != family ipaddresses << { - :ipaddress => IPAddress("#{addr}/#{addr_v["prefixlen"]}"), + :ipaddress => IPAddress("#{addr}/#{addr_v["netmask"]}"), :scope => addr_v["scope"], :iface => iface } end end + # return if there isn't any #{family} address ! + return [ nil, nil ] if ipaddresses.empty? + if match.nil? # sort ip addresses by scope, by prefixlen and then by ip address # then return the first ip address - r = ipaddresses.sort_by! do |v| + r = ipaddresses.sort_by do |v| [ ( scope_prio.index(v[:scope].downcase) or 999999 ), v[:ipaddress].prefix, ( family == "inet" ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128 ) @@ -65,7 +68,8 @@ def find_ip_and_iface(family = "inet", match = nil) # return the first matching ip address r = ipaddresses.sort do |a,b| a[:ipaddress].prefix <=> b[:ipaddress].prefix - end.select do |v| + end + r = r.select do |v| v[:ipaddress].include? IPAddress(match) end.first end @@ -94,15 +98,15 @@ if network[:default_interface] and network[:default_gateway] != "0.0.0.0" and network["interfaces"][network[:default_interface]] and network["interfaces"][network[:default_interface]]["addresses"] - Ohai::Log.debug("Using default interface for default ip and mac address") + Ohai::Log.debug("Using default interface '#{network[:default_interface]}' and default gateway '#{network[:default_gateway]}' to set the default ip") ( ip, iface ) = find_ip_and_iface("inet", network[:default_gateway]) - raise "something wrong happened #{network[:default_interface]} != #{iface}" if network[:default_interface] != iface + raise "error: looking for the default ip on '#{network[:default_interface]}' gives an ip '#{ip}' on '#{iface}'" if network[:default_interface] != iface ipaddress ip else ( ip, iface ) = find_ip_and_iface("inet") ipaddress ip end -macaddress find_mac_from_iface(iface) +macaddress find_mac_from_iface(iface) unless iface.nil? ( ip6, iface6 ) = find_ip_and_iface("inet6") ip6address ip6 -Ohai::Log.warn("ipaddress and ip6address are set from different interfaces (#{iface} & #{iface6}), macaddress has been set using the ipaddress interface") if iface != iface6 +Ohai::Log.warn("ipaddress and ip6address are set from different interfaces (#{iface} & #{iface6}), macaddress has been set using the ipaddress interface") if iface and iface6 and iface != iface6 |