diff options
author | makefu <makefu@pnp.retiolum> | 2015-09-10 15:48:38 +0200 |
---|---|---|
committer | makefu <makefu@pnp.retiolum> | 2015-09-10 15:48:50 +0200 |
commit | 219fab970c7fe455d3dd9bc48e909d96a234046b (patch) | |
tree | 9c41da16268c27d111cb98051fbd9769444bb2db /retiolum/scripts/adv_graphgen/tinc_graphs | |
parent | 7ba002c6424034390d1ea8c92e8eaacbfd482383 (diff) |
adv_graphgen: finish packaging
Diffstat (limited to 'retiolum/scripts/adv_graphgen/tinc_graphs')
5 files changed, 71 insertions, 54 deletions
diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py index 42f28ef9..888335a7 100755 --- a/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py @@ -1,11 +1,12 @@ #!/usr/bin/python +# TODO: Rewrite this shitty piece of software ... # -*- coding: utf8 -*- import sys,json,os """ TODO: Refactoring needed to pull the edges out of the node structures again, it should be easier to handle both structures""" -DUMP_FILE = os.environment.get("AVAILABILITY_FILE","tinc-availability.json") -hostpath=os.environment.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") +DUMP_FILE = os.environ.get("AVAILABILITY_FILE","tinc-availability.json") +hostpath=os.environ.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") def get_all_nodes(): return os.listdir(hostpath) @@ -16,12 +17,12 @@ def generate_stats(): import json jlines = [] try: - f = open(DUMP_FILE,'r') + f = open(DUMP_FILE,'r+') for line in f: jlines.append(json.loads(line)) f.close() except Exception as e: - print("Unable to open and parse Availability DB: {} (override with AVAILABILITY_FILE)".format(DUMP_FILE) + print("Unable to open and parse Availability DB: {} (override with AVAILABILITY_FILE)".format(DUMP_FILE)) sys.exit(1) all_nodes = {} diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Geo.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Geo.py index 78add918..bfa4ee56 100755 --- a/retiolum/scripts/adv_graphgen/tinc_graphs/Geo.py +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Geo.py @@ -2,24 +2,24 @@ # -*- coding: utf8 -*- import sys,json,os from .Graph import delete_unused_nodes,resolve_myself -GEODB=os.environ.get("GEOCITYDB","GeoLiteCity.dat") +GEODB=os.environ.get("GEODB","GeoLiteCity.dat") def copy_map(): - from shutil import copytree + from shutil import copy from os.path import dirname,join,realpath if len(sys.argv) != 2 or sys.argv[1] == "--help" : - print("usage: {} <destination>".format(sys.argv[0]) - print(" copies the map.html file to the <destination>") + print("usage: {} <destination>".format(sys.argv[0])) + print(" copies the map.html file to the <destination>") sys.exit(1) dstdir=sys.argv[1] - copytree(realpath(join(dirname(__file__),'static/map.html')),dstdir) + copy(realpath(join(dirname(__file__),'static/map.html')),dstdir) def add_geo(nodes): from pygeoip import GeoIP gi = GeoIP(GEODB) - for k,v in nodes.iteritems(): + for k,v in nodes.items(): try: nodes[k].update(gi.record_by_addr(v["external-ip"])) except Exception as e: @@ -31,7 +31,7 @@ def add_coords_to_edges(nodes): from pygeoip import GeoIP gi = GeoIP(GEODB) - for k,v in nodes.iteritems(): + for k,v in nodes.items(): for i,j in enumerate(v.get("to",[])): data=gi.record_by_addr(j["addr"]) try: @@ -45,13 +45,13 @@ def add_jitter(nodes): from random import random #add a bit of jitter to all of the coordinates max_jitter=0.005 - for k,v in nodes.iteritems(): + for k,v in nodes.items(): jitter_lat= max_jitter -random()*max_jitter*2 jitter_long= max_jitter -random()*max_jitter*2 try: v["latitude"]= v["latitude"] + jitter_lat v["longitude"]= v["longitude"] + jitter_long - for nodek,node in nodes.iteritems(): + for nodek,node in nodes.items(): for to in node['to']: if to['name'] == k: to['latitude'] = v["latitude"] @@ -61,10 +61,11 @@ def add_jitter(nodes): def main(): import json - try: - with open(GEODB) as f: f.read() - except: + try: + with open(GEODB,'rb') as f: f.read() + except Exception as e: print("cannot open {} (GEODB in env)".format(GEODB)) + print(e) sys.exit(1) try: nodes = add_jitter(add_coords_to_edges(add_geo(resolve_myself(delete_unused_nodes(json.load(sys.stdin)))))) diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Graph.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Graph.py index b001780d..29491997 100755 --- a/retiolum/scripts/adv_graphgen/tinc_graphs/Graph.py +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Graph.py @@ -6,8 +6,11 @@ from .Services import add_services from .Availability import get_node_availability import sys,json from time import time -DUMP_FILE = os.environment.get("AVAILABILITY_FILE", "tinc-availability.json") +DUMP_FILE = os.environ.get("AVAILABILITY_FILE", "tinc-availability.json") +hostpath=os.environ.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") +# will be filled later +supernodes= [] def resolve_myself(nodes): #resolve MYSELF to the real ip @@ -34,23 +37,24 @@ def generate_availability_stats(nodes): """ generates stats of from availability """ jlines = [] - try: - f = BackwardsReader(DUMP_FILE) - lines_to_use = 1000 - while True: - if lines_to_use == 0: break - line = f.readline() - if not line: break - jline = json.loads(line) - if not jline['nodes']: continue - - jlines.append(jline) - lines_to_use -=1 - except Exception as e: sys.stderr.write(str(e)) + # try: + # f = BackwardsReader(DUMP_FILE) + # lines_to_use = 1000 + # while True: + # if lines_to_use == 0: break + # line = f.readline() + # if not line: break + # jline = json.loads(line) + # if not jline['nodes']: continue + + # jlines.append(jline) + # lines_to_use -=1 + # except Exception as e: sys.stderr.write(str(e)) for k,v in nodes.items(): - v['availability'] = get_node_availability(k,jlines) - sys.stderr.write( "%s -> %f\n" %(k ,v['availability'])) + # TODO: get this information in a different way + v['availability'] = get_node_availability(k,[]) + def generate_stats(nodes): """ Generates some statistics of the network and nodes @@ -211,7 +215,6 @@ def anonymize_nodes(nodes): return newnodes def main(): - supernodes= [] if len(sys.argv) != 2 or sys.argv[1] not in ["anonymous","complete"]: print("usage: %s (anonymous|complete)") sys.exit(1) @@ -231,18 +234,25 @@ def main(): print_edge(k,v) elif sys.argv[1] == "complete": - for supernode,addr in check_all_the_super(): - supernodes.append(supernode) + try: + for supernode,addr in check_all_the_super(hostpath): + supernodes.append(supernode) + except FileNotFoundError as e: + print("!! cannot load list of supernodes ({})".format(hostpath)) + print("!! Use TINC_HOSTPATH env to override") + sys.exit(1) generate_availability_stats(nodes) add_services(nodes) for k,v in nodes.items(): print_node(k,v) print_edge(k,v) - try: - dump_graph(nodes) - except Exception as e: - sys.stderr.write("Cannot dump graph: %s" % str(e)) + + #TODO: get availability somehow else + # try: + # dump_graph(nodes) + # except Exception as e: + # sys.stderr.write("Cannot dump graph: %s" % str(e)) else: pass diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Services.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Services.py index 227e5aba..9581e21e 100644 --- a/retiolum/scripts/adv_graphgen/tinc_graphs/Services.py +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Services.py @@ -1,5 +1,5 @@ import os,sys -services_dir=os.environment.get("SERIVCES_DIR","/home/reaktor/nag.services") +services_dir=os.environ.get("SERIVCES_DIR","/home/reaktor/nag.services") def add_services(nodes): for k,v in nodes.items(): n = nodes[k] diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py index c8514c66..bc66b337 100755 --- a/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Supernodes.py @@ -7,21 +7,26 @@ def find_potential_super(path="/etc/tinc/retiolum/hosts"): needle_addr = re.compile("Address\s*=\s*(.*)") needle_port = re.compile("Port\s*=\s*(.*)") for f in os.listdir(path): - with open(path+"/"+f) as of: - addrs = [] - port = "655" + try: + with open(path+"/"+f) as of: + addrs = [] + port = "655" - for line in of.readlines(): + for line in of.readlines(): - addr_found = needle_addr.match(line) - if addr_found: - addrs.append(addr_found.group(1)) + addr_found = needle_addr.match(line) + if addr_found: + addrs.append(addr_found.group(1)) - port_found = needle_port.match(line) - if port_found: - port = port_found.group(1) - - if addrs : yield (f ,[(addr ,int(port)) for addr in addrs]) + port_found = needle_port.match(line) + if port_found: + port = port_found.group(1) + + if addrs : yield (f ,[(addr ,int(port)) for addr in addrs]) + except FileNotFoundError as e: + print("Cannot open hosts directory to be used to find potential supernodes") + print("Directory used: {}".format(path)) + raise def try_connect(addr): @@ -54,11 +59,11 @@ def check_all_the_super(path): def main(): import os - hostpath=os.environment.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") + hostpath=os.environ.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") for host,addrs in check_all_the_super(hostpath): print("%s %s" %(host,str(addrs))) - + if __name__ == "__main__": main() |