Commit 04154193 authored by Tom Niget's avatar Tom Niget

demo: handle errors in SVG generation and fix empty prefix handling

parent 64ab7e8c
...@@ -410,17 +410,17 @@ def node_by_ll(addr: str) -> tuple[Re6stNode, bool]: ...@@ -410,17 +410,17 @@ def node_by_ll(addr: str) -> tuple[Re6stNode, bool]:
_ll[a] = n, t _ll[a] = n, t
return _ll[addr] return _ll[addr]
def route_svg(ipv4, z=4): def route_svg(ipv4, z=4):
graph: dict[Re6stNode, dict[tuple[Re6stNode, bool], list[Re6stNode]]] = {} graph: dict[Re6stNode, dict[tuple[Re6stNode, bool], list[Re6stNode]]] = {}
for n in nodes: for n in nodes:
g = graph[n] = defaultdict(list) g = graph[n] = defaultdict(list)
g: dict[tuple[Re6stNode, bool], list[Re6stNode]] for route in n.get_routes():
for r in n.get_routes(): if (route.prefix and route.prefix.startswith('10.42.') if ipv4 else
if (r.prefix and r.prefix.startswith('10.42.') if ipv4 else route.prefix is None or route.prefix.startswith('2001:db8:')):
r.prefix is None or r.prefix.startswith('2001:db8:')):
try: try:
g[node_by_ll(r.nexthop)].append( if route.prefix:
node_by_ll(r.prefix)[0] if r.prefix else default) g[node_by_ll(route.nexthop)].append(node_by_ll(route.prefix)[0])
except KeyError: except KeyError:
pass pass
gv = ["digraph { splines = true; edge[color=grey, labelangle=0];"] gv = ["digraph { splines = true; edge[color=grey, labelangle=0];"]
...@@ -428,20 +428,16 @@ def route_svg(ipv4, z=4): ...@@ -428,20 +428,16 @@ def route_svg(ipv4, z=4):
a = 2 * math.pi / N a = 2 * math.pi / N
edges = set() edges = set()
for i, n in enumerate(nodes): for i, n in enumerate(nodes):
i: int
gv.append('%s[pos="%s,%s!"];' gv.append('%s[pos="%s,%s!"];'
% (n.name, z * math.cos(a * i), z * math.sin(a * i))) % (n.name, z * math.cos(a * i), z * math.sin(a * i)))
l = [] l = []
for p, r in graph[n].items(): for p, r in graph[n].items():
p: tuple[Re6stNode, bool] j = abs(nodes.index(p[0]) - i)
r: list[Re6stNode]
j: int = abs(nodes.index(p[0]) - i)
l.append((min(j, N - j), p, r)) l.append((min(j, N - j), p, r))
for j, (_, (p2, t), r) in enumerate(sorted(l, key=lambda x: x[0])): for j, (_, (p2, t), r) in enumerate(sorted(l, key=lambda x: x[0])):
p2: Re6stNode l2 = []
l2: list[str] = []
arrowhead = 'none' arrowhead = 'none'
for r2 in sorted(r2.short for r2 in r): for r2 in sorted(r2.short or '' for r2 in r):
if r2: if r2:
if r2 == p2.short: if r2 == p2.short:
r2 = '<font color="grey">%s</font>' % r2 r2 = '<font color="grey">%s</font>' % r2
...@@ -484,11 +480,14 @@ if args.port: ...@@ -484,11 +480,14 @@ if args.port:
if page < 2: if page < 2:
body = route_svg(page) body = route_svg(page)
else: else:
body = registry.Popen(('python3', '-c', r"""if 1: out, err = registry.Popen(('python3', '-c', r"""if 1:
import math, json import math, json
from re6st.registry import RegistryClient from re6st.registry import RegistryClient
g = json.loads(RegistryClient( topo = RegistryClient('http://localhost/').topology()
'http://localhost/').topology()) g = json.loads(topo)
if not g:
print('digraph { "empty topology" [shape="none"] }')
exit()
r = set(g.pop('', ())) r = set(g.pop('', ()))
a = set() a = set()
for v in g.values(): for v in g.values():
...@@ -507,13 +506,16 @@ if args.port: ...@@ -507,13 +506,16 @@ if args.port:
for v in v: for v in v:
print('"%s" -> "%s";' % (n, title(v))) print('"%s" -> "%s";' % (n, title(v)))
print('}') print('}')
"""), stdout=subprocess.PIPE, cwd="..").communicate()[0].decode("utf-8") """), stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd="..").communicate()
if body: if err:
body = subprocess.Popen(('neato', '-Tsvg'), self.send_error(500, explain='SVG generation failed: ' + err.decode(errors='replace'))
stdin=subprocess.PIPE, stdout=subprocess.PIPE, return
).communicate(body.encode("utf-8"))[0].decode("utf-8") graph_body = out.decode("utf-8")
if not body: try:
self.send_error(500) body = subprocess.run(('neato', '-Tsvg'), check=True, text=True, capture_output=True,
input=graph_body).stdout
except subprocess.CalledProcessError as e:
self.send_error(500, explain='neato failed: ' + e.stderr)
return return
if ext == 'svg': if ext == 'svg':
mt = 'image/svg+xml' mt = 'image/svg+xml'
......
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