def do_peer_check(ztaddr): """ Try and ping the gateway/peer and goose the network if down. :param addr: target addr """ import os from node_tools.ctlr_funcs import netcfg_get_ipnet addr = ztaddr try: netobj = netcfg_get_ipnet(ztaddr) except ValueError as exc: logger.error('netobj error is {}'.format(exc)) raise exc for host in list(netobj.hosts()): if str(host) != ztaddr: addr = str(host) break logger.debug('PEER: found target IP addr {}'.format(addr)) home = NODE_SETTINGS['home_dir'] cmd_file = os.path.join(home, 'ping_gateway.sh') cmd = [cmd_file, addr] result = do_net_cmd(cmd) logger.debug('do_gateway_check {} returned: {}'.format(cmd, result)) return result
def get_neighbor_ids(trie, node_id): """ Given the node ID, get the payloads from the net trie and return the attached network and neighbor node IDs. :param trie: net data trie :param node_id: node ID to lookup :return: tuple of net and node IDs """ from node_tools.ctlr_funcs import ipnet_get_netcfg from node_tools.ctlr_funcs import is_exit_node from node_tools.ctlr_funcs import netcfg_get_ipnet from node_tools.helper_funcs import find_ipv4_iface node_list = [] key_list = [] src_net = None exit_net = None src_node = None exit_node = None for key in trie: if node_id in key: key_list.append(key[0:16]) node_list.append(trie[key]) if len(key_list) != 2 and (len(key_list) == 1 and not is_exit_node(node_id)): raise AssertionError('Node {} keys {} are invalid!'.format( node_id, key_list)) else: for key, data in zip(key_list, node_list): node_ip = data['ipAssignments'][0] ipnet = netcfg_get_ipnet(node_ip) netcfg = ipnet_get_netcfg(ipnet) gw_ip = find_ipv4_iface(netcfg.gateway[0]) if node_ip == gw_ip: src_net = key for node in trie.suffixes(src_net)[1:]: if node_id != node: src_node = node else: exit_net = key for node in trie.suffixes(exit_net)[1:]: if node_id != node: exit_node = node return src_net, exit_net, src_node, exit_node
def get_dangling_net_data(trie, net_id): """ Given the net ID, get the payload from the net data trie and return the target network cfg from the `routes` list. :notes: The return format matches the cfg_dict payload used by config_network_object() :param trie: net data trie :param net_id: network ID to retrive :return: Attrdict <netcfg> or None """ from node_tools.ctlr_funcs import ipnet_get_netcfg from node_tools.ctlr_funcs import netcfg_get_ipnet payload = trie[net_id] # logger.debug('TRIE: net {} has payload {}'.format(net_id, payload)) netcfg = None for route in payload['routes']: if route['via'] is not None: tgt_route = route['via'] netobj = netcfg_get_ipnet(tgt_route) netcfg = ipnet_get_netcfg(netobj) return netcfg
def test_get_invalid_obj(self): """Raise IPv4 address error""" with self.assertRaises(ipaddress.AddressValueError): netobj = netcfg_get_ipnet('172.16.0.261')
def test_get_valid_obj(self): """Return IPv4 network object""" netobj = netcfg_get_ipnet('172.16.0.241') self.assertIsInstance(netobj, ipaddress.IPv4Network) self.assertEqual(str(netobj), '172.16.0.240/30')