Move loading of ipaddress module where it belongs

This commit is contained in:
Ronald van Zantvoort 2015-05-31 21:04:54 +02:00
parent 65096c407b
commit 15e85184dc
3 changed files with 63 additions and 33 deletions

View File

@ -2302,6 +2302,7 @@ class Matcher(object):
'''
Matches based on IP address or CIDR notation
'''
try:
tgt = ipaddress.ip_network(tgt)
# Target is a network

View File

@ -19,6 +19,10 @@ from salt.exceptions import CommandExecutionError
# Import 3rd-party libs
import salt.ext.six as six
if six.PY3:
import ipaddress
else:
import salt.ext.ipaddress as ipaddress
HAS_RANGE = False
try:
import seco.range # pylint: disable=import-error
@ -314,27 +318,31 @@ class CkMinions(object):
grains = self.serial.load(fp_).get('grains')
except (IOError, OSError):
continue
num_parts = len(expr.split('/'))
if num_parts > 2:
# Target is not valid CIDR, no minions match
return []
elif num_parts == 2:
# Target is CIDR
if not salt.utils.network.in_subnet(
expr,
addrs=grains.get('ipv4', [])) and id_ in minions:
minions.remove(id_)
else:
# Target is an IPv4 address
import socket
try:
socket.inet_aton(expr)
except socket.error:
# Not a valid IPv4 address, no minions match
return []
match = True
tgt = expr
try:
tgt = ipaddress.ip_network(tgt)
# Target is a network
proto = 'ipv{0}'.format(tgt.version)
if proto not in self.opts['grains']:
match=False
else:
if expr not in grains.get('ipv4', []) and id_ in minions:
minions.remove(id_)
match=salt.utils.network.in_subnet(tgt, self.opts['grains'][proto])
except: # pylint: disable=bare-except
try:
# Target should be an address
proto = 'ipv{0}'.format(ipaddress.ip_address(tgt).version)
if proto not in self.opts['grains']:
match=False
else:
match=tgt in self.opts['grains'][proto]
except: # pylint: disable=bare-except
log.error('Invalid IP/CIDR target {0}"'.format(tgt))
if not match and id_ in minions:
minions.remove(id_)
return list(minions)
def _check_range_minions(self, expr, greedy):

View File

@ -846,20 +846,36 @@ def interface_ip(iface):
return error
def subnets():
def _subnets(proto='inet'):
'''
Returns a list of subnets to which the host belongs
'''
ifaces = interfaces()
subnetworks = []
ret = set()
for ipv4_info in six.itervalues(ifaces):
for ipv4 in ipv4_info.get('inet', []):
if ipv4['address'] == '127.0.0.1':
continue
network = calculate_subnet(ipv4['address'], ipv4['netmask'])
subnetworks.append(network)
return subnetworks
for ip_info in six.itervalues(ifaces):
addrs = ip_info.get(proto, [])
addrs.extend([addr for addr in ip_info.get('secondary', []) if addr.get('type') == proto])
for intf in addrs:
intf = ipaddress.ip_interface('{0.address}/{0.netmask}'.format(intf))
if not intf.is_loopback:
ret.add(intf.network)
return [str(net) for net in sorted(ret)]
def subnets():
'''
Returns a list of subnets to which the host belongs
'''
return _subnets('inet')
def subnets6():
'''
Returns a list of subnets to which the host belongs
'''
return _subnets('inet6')
def in_subnet(cidr, addr=None):
@ -894,6 +910,11 @@ def ip_in_subnet(addr, cidr):
def _ip_addrs(interface=None, include_loopback=False, interface_data=None, proto='inet'):
'''
Return the full list of IP adresses matching the criteria
proto = inet|inet6
'''
ret = set()
ifaces = interface_data \
@ -910,11 +931,11 @@ def _ip_addrs(interface=None, include_loopback=False, interface_data=None, proto
addrs = ip_info.get(proto, [])
addrs.extend([addr for addr in ip_info.get('secondary', []) if addr.get('type') == proto])
for ip in addrs:
addr = ipaddress.ip_address(ip.get('address'))
for addr in addrs:
addr = ipaddress.ip_address(addr.get('address'))
if not addr.is_loopback or include_loopback:
ret.add(str(addr))
return sorted(list(ret))
ret.add(addr)
return [str(addr) for addr in sorted(ret)]
def ip_addrs(interface=None, include_loopback=False, interface_data=None):