You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.2 KiB
Python
71 lines
1.2 KiB
Python
3 years ago
|
#!/bin/python3
|
||
|
|
||
|
import ipaddress
|
||
|
import jinja2
|
||
|
import os
|
||
|
import sys
|
||
|
import pathlib
|
||
|
|
||
|
class Bridge:
|
||
|
def __init__(self, name, num):
|
||
|
self.name = name
|
||
|
self.num = num
|
||
|
self.computers = []
|
||
|
|
||
|
class Computer:
|
||
|
def __init__(self, name, num):
|
||
|
self.name = name
|
||
|
self.num = num
|
||
|
self.bridges = []
|
||
|
|
||
|
def add_to_bridge(self, bridge):
|
||
|
self.bridges.append(bridge)
|
||
|
bridge.computers.append(computer)
|
||
|
|
||
|
def gen_files(self):
|
||
|
files = [
|
||
|
'qemu.sh',
|
||
|
'interfaces',
|
||
|
'bird.conf',
|
||
|
]
|
||
|
context = {'c': self}
|
||
|
|
||
|
loader = jinja2.FileSystemLoader('templates/')
|
||
|
environment = jinja2.Environment(loader=loader, trim_blocks=True)
|
||
|
for f in files:
|
||
|
template = environment.get_template(f)
|
||
|
template.stream(context).dump(self.name + '/' + f)
|
||
|
|
||
|
# Computers:
|
||
|
comps = {}
|
||
|
for i, n in enumerate('ABCDEFGHIX'):
|
||
|
comps[n] = Computer(n, i)
|
||
|
|
||
|
# Bridges
|
||
|
bridges = {}
|
||
|
for i, n in [(x, 'net_'+str(x)) for x in range(1, 8)]:
|
||
|
bridges[n] = Bridge(n, i)
|
||
|
|
||
|
def dump_bridges(fn):
|
||
|
with open(fn, 'w+') as f:
|
||
|
for br in bridges:
|
||
|
f.write(br + '\n')
|
||
|
|
||
|
# Connections
|
||
|
# dict[ net_num -> [comp_names]
|
||
|
conns = {
|
||
|
1: 'IGH',
|
||
|
2: 'HCE',
|
||
|
3: 'GDF',
|
||
|
4: 'ACD',
|
||
|
5: 'BEF',
|
||
|
6: 'ABX',
|
||
|
7: 'X',
|
||
|
}
|
||
|
|
||
|
for net, cs in conns.items():
|
||
|
for c in cs:
|
||
|
comps[c].add_to_bridge(bridges[net])
|
||
|
|
||
|
dump_bridges('./bridges')
|