[web] implement communication between web+node(s)
This commit is contained in:
parent
99809e1139
commit
a534568de5
1 changed files with 35 additions and 20 deletions
|
@ -6,6 +6,7 @@ import random
|
|||
import threading
|
||||
import requests
|
||||
|
||||
|
||||
class NodeCommunicator:
|
||||
|
||||
def __init__(self, name: str, config):
|
||||
|
@ -13,9 +14,16 @@ class NodeCommunicator:
|
|||
self.__config = config
|
||||
self.__api_addr = config["api-con"]
|
||||
|
||||
def update(self, action:str, updated_peering:dict):
|
||||
print(requests.api.post(self.__api_addr+"peerings", json=updated_peering).content)
|
||||
input()
|
||||
def update(self, action: str, peering: dict):
|
||||
if action == "add":
|
||||
print(requests.post(f"{self.__api_addr}peerings", json=peering))
|
||||
elif action == "update":
|
||||
print(requests.put(f"{self.__api_addr}peerings", json=peering))
|
||||
elif action == "delete":
|
||||
print(requests.delete(f"{self.__api_addr}peerings", json=peering))
|
||||
else:
|
||||
return 400
|
||||
|
||||
|
||||
class PeeringManager:
|
||||
|
||||
|
@ -27,7 +35,8 @@ class PeeringManager:
|
|||
|
||||
self._nodes = {}
|
||||
for node in self.__config["nodes"]:
|
||||
self._nodes[node] = NodeCommunicator(name=node, config=self.__config["nodes"][node])
|
||||
self._nodes[node] = NodeCommunicator(
|
||||
name=node, config=self.__config["nodes"][node])
|
||||
|
||||
self._amounts = None
|
||||
self._threads = []
|
||||
|
@ -69,11 +78,15 @@ class PeeringManager:
|
|||
new_peering: if mode=="update" the new peering to update to
|
||||
"""
|
||||
if peering["node"] in self._nodes:
|
||||
thread = threading.Thread(target=self._nodes[peering["node"]].update,kwargs={"action":action,"updated_peering":peering if not new_peering else new_peering,})
|
||||
thread.start()
|
||||
self._threads.append(thread)
|
||||
#thread = threading.Thread(target=
|
||||
self._nodes[peering["node"]].update(#), kwargs={
|
||||
action=action, peering = peering if not new_peering else new_peering#, })
|
||||
)
|
||||
#thread.start()
|
||||
#self._threads.append(thread)
|
||||
|
||||
else: return False
|
||||
else:
|
||||
return False
|
||||
|
||||
def _update_amounts(self):
|
||||
__new = {}
|
||||
|
@ -84,7 +97,6 @@ class PeeringManager:
|
|||
__new[peering["node"]] += 1
|
||||
self._amounts = __new
|
||||
|
||||
|
||||
def amount_by_node(self, node_name: str):
|
||||
if self._amounts == None:
|
||||
self._update_amounts()
|
||||
|
@ -92,6 +104,7 @@ class PeeringManager:
|
|||
return self._amounts[node_name]
|
||||
except KeyError:
|
||||
return 0
|
||||
|
||||
def exists(self, asn, node, mnt=None, wg_key=None, endpoint=None, ipv6ll=None, ipv4=None, ipv6=None, bgp_mp=True, bgp_enh=True):
|
||||
"""checks if a peerings with specific data already exists"""
|
||||
# check if mnt is specified, already exists in the database and if that mnt has the specified ASn -> if not: return False
|
||||
|
@ -143,9 +156,10 @@ class PeeringManager:
|
|||
if peering["node"] == node:
|
||||
return False
|
||||
|
||||
self.peerings["asn"][asn].append({"MNT": mnt, "ASN": asn, "node": node, "wg_key": wg_key, "endpoint": endpoint,
|
||||
"ipv6ll": ipv6ll, "ipv4": ipv4, "ipv6": ipv6, "bgp_mp": bgp_mp, "bgp_enh": bgp_enh})
|
||||
|
||||
peering = {"MNT": mnt, "ASN": asn, "node": node, "wg_key": wg_key, "endpoint": endpoint,
|
||||
"ipv6ll": ipv6ll, "ipv4": ipv4, "ipv6": ipv6, "bgp_mp": bgp_mp, "bgp_enh": bgp_enh}
|
||||
self.peerings["asn"][asn].append(peering)
|
||||
self._update_nodes("add", peering=peering)
|
||||
self._save_peerings()
|
||||
return True
|
||||
|
||||
|
@ -187,6 +201,7 @@ class PeeringManager:
|
|||
continue
|
||||
self.peerings["asn"][asn].remove(p)
|
||||
self._save_peerings()
|
||||
self._update_nodes("delete", peering=p)
|
||||
return True
|
||||
# if nothing got found (should have been catched by self.exists)
|
||||
return False
|
||||
|
|
Loading…
Add table
Reference in a new issue