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.
85 lines
1.7 KiB
Python
85 lines
1.7 KiB
Python
3 years ago
|
#!/bin/python3
|
||
|
|
||
|
import ipaddress
|
||
|
import jinja2
|
||
|
import os
|
||
|
import sys
|
||
3 years ago
|
from pathlib import Path
|
||
3 years ago
|
|
||
|
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)
|
||
3 years ago
|
bridge.computers.append(self)
|
||
3 years ago
|
|
||
|
def gen_files(self):
|
||
|
files = [
|
||
|
'qemu.sh',
|
||
|
'interfaces',
|
||
|
'bird.conf',
|
||
3 years ago
|
'hosts',
|
||
|
'hostname',
|
||
3 years ago
|
]
|
||
|
context = {'c': self}
|
||
|
|
||
|
loader = jinja2.FileSystemLoader('templates/')
|
||
3 years ago
|
environment = jinja2.Environment(loader=loader, trim_blocks=True, keep_trailing_newline=True)
|
||
3 years ago
|
for f in files:
|
||
|
template = environment.get_template(f)
|
||
3 years ago
|
(Path('output') / self.name).mkdir(parents=True,exist_ok=True)
|
||
|
template.stream(context).dump('output/' + self.name + '/' + f)
|
||
3 years ago
|
mode = os.stat('output/' + self.name + '/qemu.sh').st_mode
|
||
|
os.chmod('output/' + self.name + '/qemu.sh', mode | 0o0100)
|
||
3 years ago
|
|
||
|
# Computers:
|
||
|
comps = {}
|
||
3 years ago
|
for i, n in enumerate('ABCDEFGHIX', start=1):
|
||
3 years ago
|
comps[n] = Computer(n, i)
|
||
|
|
||
|
# Bridges
|
||
|
bridges = {}
|
||
|
for i, n in [(x, 'net_'+str(x)) for x in range(1, 8)]:
|
||
3 years ago
|
bridges[i] = Bridge(n, i)
|
||
3 years ago
|
|
||
|
def dump_bridges(fn):
|
||
|
with open(fn, 'w+') as f:
|
||
|
for br in bridges:
|
||
3 years ago
|
# Generate bridge.conf for the host
|
||
|
f.write('allow ' + bridges[br].name + '\n')
|
||
3 years ago
|
|
||
|
# Connections
|
||
|
# dict[ net_num -> [comp_names]
|
||
|
conns = {
|
||
|
1: 'IGH',
|
||
|
2: 'HCE',
|
||
|
3: 'GDF',
|
||
|
4: 'ACD',
|
||
|
5: 'BEF',
|
||
|
6: 'ABX',
|
||
|
7: 'X',
|
||
|
}
|
||
|
|
||
3 years ago
|
print(conns)
|
||
|
print(comps)
|
||
|
print(bridges)
|
||
|
|
||
3 years ago
|
for net, cs in conns.items():
|
||
|
for c in cs:
|
||
|
comps[c].add_to_bridge(bridges[net])
|
||
|
|
||
3 years ago
|
os.mkdir('output')
|
||
3 years ago
|
dump_bridges('output/bridge.conf')
|
||
3 years ago
|
|
||
|
for c in comps.values():
|
||
|
c.gen_files()
|