-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateLab.py
98 lines (88 loc) · 3.48 KB
/
createLab.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import json
import subprocess
import argparse
def read_json(lab_def):
with open(lab_def) as file:
data = json.load(file)
return data
def dot(lab_def="lab.json"):
data = read_json(lab_def)
domains = {}
for host in data['topo']:
for net in host['net']:
if not net['domain'] in domains:
domains[net['domain']] = []
domains[net['domain']].append(host['host'])
if "tap" in domains:
domains["tap"].append("HOST")
dot = "digraph net{\n"
for domain in domains:
hosts = []
for host in domains[domain]:
for host2 in domains[domain]:
if host == host2 or host2 in hosts:
continue
dot += "\t{} -> {} [dir=\"none\",label=\" {}\"];\n".format(host, host2, domain)
hosts.append(host)
dot += "}"
with open(data['name'] + ".dot", "w") as out:
out.write(dot)
subprocess.call(["dot", "-Tpng", data['name'] + ".dot", "-o", data['name'] + ".png"])
def create_lab(lab_def="lab.json"):
data = read_json(lab_def)
subprocess.call(["mkdir", data['name']])
# create lab.dep
subprocess.call(["touch", data['name'] + "/lab.dep"])
# create lab.conf
lab_file = "LAB_DESCRIPTION=\"{}\"\n".format(data['name'])
with open("templates/lab.conf") as tpl:
lab_file += "".join(tpl.readlines())
lab_file += "machines=\""
lab_file += " ".join(x['host'] for x in data['topo'])
lab_file += "\"\n\n"
for host in data['topo']:
host_conf = ""
for i, net in enumerate(host['net']):
if net['domain'] == 'tap':
continue
host_conf += "{}[{}]={}\n".format(host['host'], i, net['domain'])
if host['host'] == "tap":
host_conf+="tap[{}]={}\n".format(i+1, host['tap'])
lab_file += host_conf + "{}[mem]={}\n\n".format(host['host'], data['mem'])
with open(data['name'] + "/lab.conf", "w") as out:
out.write(lab_file)
# create *.startup
for host in data['topo']:
startup = "sleep 1\n"
for i, net in enumerate(host['net']):
startup += "ip addr add dev eth{} {}\n".format(i, net['ip'])
startup += "sleep 1\n"
for i, net in enumerate(host['net']):
startup += "ip link set eth{} up\n".format(i)
startup += "sleep 1\n"
with open(data['name'] + "/{}.startup".format(host['host']), "w") as startup_file:
startup_file.write(startup)
# copy files
for host in data['topo']:
subprocess.call(["cp", "-rv", "templates/conf/", data['name'] + "/{}/".format(host['host'])])
def wipe(lab_def='lab.json'):
data = read_json(lab_def)
input("DELETE " + data['name'] + "? [Y]")
subprocess.call(["rm", data['name'] + ".dot", data['name'] + ".png"])
subprocess.call(["rm", "-rfv", data['name']])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="create Netkit labs")
parser.add_argument("--lab", "-l", default="lab.json", help="Lab definition JSON")
parser.add_argument("--plot", "-p", action='store_true')
parser.add_argument("--create", "-c", action='store_true')
parser.add_argument("--force", "-f", action='store_true')
parser.add_argument("--wipe", "-w", action='store_true')
args = parser.parse_args()
if args.wipe:
wipe(args.lab)
if args.plot:
dot(args.lab)
if args.create:
if not args.force:
input("create lab? [Y]")
create_lab(args.lab)