Created
October 22, 2021 22:59
-
-
Save renatoalmeidaoliveira/fdb772a5a02f3cfc0b5fbe7e8b7586a2 to your computer and use it in GitHub Desktop.
ContainerLab random labs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import networkx as nx | |
from jinja2 import Environment, FileSystemLoader | |
nodes = 10 | |
G = nx.erdos_renyi_graph(nodes, 0.6) | |
links = [] | |
nodes_links = {} | |
for edge in G.edges(): | |
for node_n in edge: | |
node_name = f"R_{node_n}" | |
if node_name not in nodes_links: | |
nodes_links[node_name] = 1 | |
a_name = f"R_{edge[0]}" | |
b_name = f"R_{edge[1]}" | |
a_idx = nodes_links[a_name] | |
b_idx = nodes_links[b_name] | |
nodes_links[a_name] += 1 | |
nodes_links[b_name] += 1 | |
link = [f"{a_name}:eth{a_idx}" , f"{b_name}:eth{b_idx}"] | |
links.append(link) | |
topology = { | |
'nodes': [f"R_{node}" for node in G.nodes()], | |
'links': links | |
} | |
env = Environment( | |
loader=FileSystemLoader(f"."), | |
line_statement_prefix='#' | |
) | |
templ = env.get_template(f"topology.j2") | |
config = templ.render(topology) | |
fh = open('lab_topo.yml', 'w') | |
fh.write(config) | |
fh.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: nx-lab | |
topology: | |
nodes: | |
# for node in nodes: | |
{{ node }}: | |
kind: ceos | |
image: ceos | |
# endfor | |
links: | |
# for link in links: | |
- endpoints: {{link}} | |
# endfor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment