-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathbuild-topology.py
executable file
·75 lines (65 loc) · 2.41 KB
/
build-topology.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /usr/bin/env python
import json, sys, jinja2
class Cable(object):
def __init__(self, net_id, src_host, src_iface, dest_host, dest_iface):
self.net_id = net_id
self.src_host = src_host
self.src_iface = src_iface
self.dest_host = dest_host
self.dest_iface = dest_iface
class Device(object):
def __init__(self, hostname, category, mgmt_ip, mac_map, eth_map, memory, vagrant=False):
self.hostname = hostname
self.category = category
self.mac_map = mac_map
self.eth_map = eth_map
self.mgmt_ip = mgmt_ip
self.vagrant = vagrant
self.memory = memory
self.cabling = {}
if __name__ == "__main__":
data = json.loads(open(sys.argv[1]).read())
devices = []
cabling = []
for net_id, cable in enumerate(data['cabling']):
cabling.append(Cable(net_id,
cable['src_host'],
cable['src_iface'],
cable['dest_host'],
cable['dest_iface']))
for device in data['devices']:
d = Device(device['hostname'],
device['category'],
device['mgmt_ip'],
device['mac_map'],
device['eth_map'],
device['memory'],
device.get('vagrant', None))
for cable in cabling:
if d.hostname == cable.src_host:
d.cabling[cable.src_iface] = cable
if d.hostname == cable.dest_host:
d.cabling[cable.dest_iface] = cable
devices.append(d)
for device in devices:
if set(device.eth_map) != set(device.cabling.keys()):
print device.eth_map, device.cabling.keys()
# create remap files
for device in devices:
outfile = open('./helper_scripts/autogenerated/%s_remap_eth'%device.hostname, 'w')
outtext = 'MAP="\n'
n = 1
if device.vagrant:
outtext += 'eth0=%s\n'%device.vagrant
else:
outtext += 'eth0=vagrant\n'
for iface in device.eth_map:
outtext += 'eth%d=%s\n'%(n, iface)
n += 1
outtext += '"\n'
outfile.write(outtext)
outfile.close()
template = jinja2.Template(open('templates/Vagrantfile.j2').read())
outfile = open('./Vagrantfile', 'w')
outfile.write(template.render(devices=devices))
outfile.close()