Commit 24623d23 authored by Simon Redman's avatar Simon Redman

Simplify interface to add_routeable_ipv6_addrs

parent a8929aed
......@@ -38,9 +38,8 @@ The suggested usage is:
- Construct a NetJSON NetworkGraph using topomap_parser
- Use the [netdiff](https://github.com/ninuxorg/netdiff) library to convert the NetJSON to a networkx graph
- Use ssh_helper to log in to each host in the network and annotate the graph
- Use the annotated graph to call parse_local_interface_addresses and parse_ipv4_networks
- Use the results from the previous calls to call construct_ULAs
- Use ssh_helper to log in to each host in the network and annotate the graph with logged-in sessions
- Use the netgraph to call construct_ULAs
- Use the result of construct_ULAs and the network graph to call add_ULAs_to_hosts
- Use the result of construct_ULAs to call add_interfaces_to_netgraph to add the interfaces and the associated IPv6 addresses to the NetGraph
- This adds an `interfaces` property to the nodes in the graph which maps the (Linux) interface name to the ULA IPv6 address
......
......@@ -25,7 +25,7 @@ import json
from netdiff import NetJsonParser
import re
LINK_LOCAL_COMMAND = "ip -6 address list"
LINK_LOCAL_COMMAND = "ip address list"
IPV4_COMMAND = "ip -4 address list"
ULA_ADDER_SCRIPT_FILENAME = "/tmp/add_ipv6.sh"
......@@ -66,8 +66,6 @@ def parse_ipv4_networks(netgraph):
if interface_name == 'lo':
# Don't mess with the loopback interface
continue
# Figure out whether the interface is alive. Look for "state" followed by "UP" or "DOWN"
# (This does not work on Ubuntu 12.04, but works on 16.04) -- One way to make it work on 12.04 would be to not pass `-6` to the ip command
state, *more_states = re.findall(r'\sstate\s+(UP|DOWN)\s+', remainder[0])
assert len(more_states) == 0, "Should have found only one state"
if state == "DOWN":
......@@ -113,8 +111,6 @@ def parse_local_interface_addresses(netgraph):
if interface_name == 'lo':
# Don't mess with the loopback interface
continue
# Figure out whether the interface is alive. Look for "state" followed by "UP" or "DOWN"
# (This does not work on Ubuntu 12.04, but works on 16.04) -- One way to make it work on 12.04 would be to not pass `-6` to the ip command
state, *more_states = re.findall(r'\sstate\s+(UP|DOWN)\s+', remainder[0])
assert len(more_states) == 0, "Should have found only one state"
if state == "DOWN":
......@@ -128,17 +124,19 @@ def parse_local_interface_addresses(netgraph):
return interface_map
def construct_ULAs(ipv6_link_local_map, ipv4_map, ignore_addrs=[]):
def construct_ULAs(netgraph, ignore_addrs=[]):
"""
Combine the per-host, per-interface IPv6 and IPv4 information into per-host, per-interface Unique Local Addresses
:param ipv6_link_local_map: per-host per-interface IPv6 link local address map as built by parse_local_interface_addresses
:param ipv4_map: per-host per-interface IPv4 address map as built by parse_ipv4_networks
:param netgraph: networkx.graph object
:param ignore_addrs: List of interface IPv4 or IPv6 addresses which should be ignored
:return: mapping of hosts to interfaces to ULAs
"""
ULA_map = {}
ipv6_link_local_map = parse_local_interface_addresses(netgraph)
ipv4_map = parse_ipv4_networks(netgraph)
# Convert the addresses to ignore into IPAddress objects. If they already were, this is a noop
ignore_addrs = [ipaddress.ip_address(addr) for addr in ignore_addrs]
......@@ -242,10 +240,8 @@ if __name__ == "__main__":
netgraph = NetJsonParser(file=args.in_file)
ssh_helper.network_graph_login(netgraph.graph, args.username)
management_ips = [node["management-ip"] for node in netgraph.graph._node.values()]
link_local_map = parse_local_interface_addresses(netgraph.graph)
ipv4_map = parse_ipv4_networks(netgraph.graph)
ULA_map = construct_ULAs(link_local_map, ipv4_map, management_ips)
ULA_map = construct_ULAs(netgraph.graph, ignore_addrs=management_ips)
add_ULAs_to_hosts(netgraph.graph, ULA_map)
add_interfaces_to_netgraph(netgraph.graph, ULA_map)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment