From 86db7d4abda0830718780367bb6a1941afe53515 Mon Sep 17 00:00:00 2001 From: Kyle Bader Date: Mon, 30 Dec 2013 15:31:07 -0800 Subject: [PATCH] Refactor find_node_ip_in_network Refactor find_node_ip_in_network so that it is simplier and works with multiple interfaces, networks and ipv4/ipv6. --- libraries/default.rb | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/libraries/default.rb b/libraries/default.rb index a3bd7fc..d2d061f 100644 --- a/libraries/default.rb +++ b/libraries/default.rb @@ -32,32 +32,12 @@ end def find_node_ip_in_network(network, nodeish=nil) nodeish = node unless nodeish net = IPAddr.new(network) - nodeish["network"]["interfaces"].each do |iface| - if iface[1]["routes"].nil? - next - end - if net.ipv4? - iface[1]["routes"].each_with_index do |route, index| - if iface[1]["routes"][index]["destination"] == network - return "#{iface[1]["routes"][index]["src"]}:6789" - end - end - else - # Here we are getting an IPv6. We assume that - # the configuration is stateful. - # For this configuration to not fail in a stateless - # configuration, you should run: - # echo "0" > /proc/sys/net/ipv6/conf/*/use_tempaddr - # on each server, this will disabe temporary addresses - # See: http://en.wikipedia.org/wiki/IPv6_address#Temporary_addresses - iface[1]["routes"].each_with_index do |route, index| - if iface[1]["routes"][index]["destination"] == network - iface[1]["addresses"].each do |k,v| - if v["scope"] == "Global" and v["family"] == "inet6" - return "[#{k}]:6789" - end - end - end + nodeish["network"]["interfaces"].each do |iface, addrs| + addrs["addresses"].each do |ip, params| + if params['family'].eql?("inet6") && net.include?(ip) + return "[#{ip}]:6789" + elsif params['family'].eql?("inet") && net.include?(ip) + return "#{ip}:6789" end end end