Skip to content

Instantly share code, notes, and snippets.

@jbradberry
Last active July 26, 2021 16:17
Show Gist options
  • Save jbradberry/147e1d731d028226ec3e0245d30b24bb to your computer and use it in GitHub Desktop.
Save jbradberry/147e1d731d028226ec3e0245d30b24bb to your computer and use it in GitHub Desktop.
Receptor mesh inventory processing
In [1]: import json, networkx
In [2]: import matplotlib.pyplot as plt
In [3]: with open("test-inventory.json") as f:
...: data = json.load(f)
...:
In [4]: G = networkx.DiGraph()
In [5]: control_nodes = data['control_nodes']['hosts']
In [6]: for ind, node in enumerate(control_nodes[:-1]):
...: for peer in control_nodes[ind + 1:]:
...: G.add_edge(node, peer)
...:
In [7]: for node in data['control_nodes']['hosts'] + data['execution_nodes']['hosts']:
...: peers = data['_meta']['hostvars'][node].get('peers', '').split(',')
...:
...: for peer in peers:
...: if not peer: continue
...: if peer in data['_meta']['hostvars']:
...: G.add_edge(node, peer)
...: elif peer in data:
...: for sub_peer in set(data[peer]['hosts']) - {node,}: # FIXME: needs recursive group expansion
...: G.add_edge(node, sub_peer)
In [8]: for node in set(data['control_nodes']['hosts']) & set(data['execution_nodes']['hosts']):
...: G.nodes[node]['fillcolor'] = 'grey'
...: G.nodes[node]['style'] = 'filled'
In [8]: G.nodes
Out[8]: NodeView(('controllerA.tatu.home', 'controllerB.tatu.home', 'controllerC.tatu.home', 'p50.tatu.home', 'p70.tatu.home', 'p90.tatu.home', 'storm.tatu.home', 't470n1.tatu.home', 't470n2.tatu.home'))
In [9]: G.edges
Out[9]: OutEdgeView([('controllerA.tatu.home', 'storm.tatu.home'), ('controllerB.tatu.home', 'p50.tatu.home'), ('controllerB.tatu.home', 'storm.tatu.home'), ('p50.tatu.home', 'p90.tatu.home'), ('p70.tatu.home', 'p50.tatu.home'), ('p70.tatu.home', 'storm.tatu.home'), ('storm.tatu.home', 'p90.tatu.home'), ('storm.tatu.home', 'p70.tatu.home'), ('storm.tatu.home', 't470n2.tatu.home'), ('storm.tatu.home', 't470n1.tatu.home'), ('storm.tatu.home', 'controllerC.tatu.home'), ('storm.tatu.home', 'p50.tatu.home'), ('t470n1.tatu.home', 'p70.tatu.home'), ('t470n1.tatu.home', 'p50.tatu.home'), ('t470n2.tatu.home', 'controllerC.tatu.home')])
In [10]: plt.subplot(111)
Out[10]: <AxesSubplot:>
In [11]: networkx.draw(G, with_labels=True)
In [12]: plt.show()
In [13]: networkx.is_directed_acyclic_graph(G)
Out[13]: False
In [14]: list(networkx.simple_cycles(G))
Out[14]:
[['p70.tatu.home', 'storm.tatu.home', 't470n1.tatu.home'], # This one is OK
['p70.tatu.home', 'storm.tatu.home']] # This one is a problem
In [15]: networkx.drawing.nx_agraph.write_dot(G, '/home/jrb/test-inventory.dot')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment