From 3e777bde927960ad223dc96b44daf4d3ee69f21c Mon Sep 17 00:00:00 2001 From: Jake Hillion Date: Sat, 14 Nov 2020 12:54:50 +0000 Subject: [PATCH] removed bridge subnet collisions --- runners/runners.py | 10 ++++++++-- structure/structure.py | 14 ++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/runners/runners.py b/runners/runners.py index fe92a52..0ed4ecf 100644 --- a/runners/runners.py +++ b/runners/runners.py @@ -136,7 +136,7 @@ class ProxmoxRunner: def build(self, bridge: structure.Bridge): bridges, nodes = bridge_node_search(bridge, lambda x: self._create_bridge(x), lambda x: self._create_node(x)) - self._build_bridges() + self._build_bridges(bridges) with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor: build_futures = [executor.submit(self._build_node, node) for node in nodes] @@ -177,10 +177,16 @@ class ProxmoxRunner: self._created_bridges.append(bridge_name) return bridge_name - def _build_bridges(self): + def _build_bridges(self, bridges: List[structure.Bridge]): network_task = self._proxmox.nodes(self._proxmox_node).network.put() self._await_task(network_task) + existing = [] + for bridge in bridges: + while True in [ipaddress.ip_network(bridge.get_network()).overlaps(x) for x in existing]: + bridge.new_network() + existing.append(bridge.get_network()) + def _create_node(self, node: structure.Node) -> int: while True: try: diff --git a/structure/structure.py b/structure/structure.py index e3c346a..ff11a73 100644 --- a/structure/structure.py +++ b/structure/structure.py @@ -69,8 +69,7 @@ class Bridge: # Generate a random class c private range by default (10.0.0.0) self.netmask = 24 - self._addr: ipaddress.ip_address = ipaddress.ip_address('10.0.0.0') + random.randint(0, 16777216) - self._network_iterator = ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False).hosts() + self.new_network() def get_interfaces(self) -> List[Interface]: return self._interfaces @@ -88,9 +87,16 @@ class Bridge: def get_ip_address(self) -> ipaddress.ip_address: return next(self._network_iterator) - def get_network(self) -> str: + def get_network(self) -> ipaddress.ip_network: + return ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False) + + def get_network_string(self) -> str: return str(ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False)) + def new_network(self): + self._addr: ipaddress.ip_address = ipaddress.ip_address('10.0.0.0') + random.randint(0, 16777216) + self._network_iterator = ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False).hosts() + class Node: def __init__(self, interfaces: List[Interface], setup_params: Dict = None): @@ -291,7 +297,7 @@ class LocalPortal(Node): policy_routing = '\n\n'.join([policy_routing_string.format( table_number=i + 10, device='eth{}'.format(i), - network=iface.get_bridge().get_network(), + network=iface.get_bridge().get_network_string(), local_address=iface.get_address(), ) for i, iface in enumerate(self.get_interfaces()[:-2])])