-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
68 lines (53 loc) · 2.02 KB
/
main.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
"""Main module of the docker network simulator"""
import time
import argparse
import yaml
import hashlib
import os
import datetime
from subprocess import call
from threading import Thread
from threading import Timer
import collections
from eventWorker import eventWorker
from actions.networkActions import deleteAllNetworks
parser = argparse.ArgumentParser(prog='main.py', usage='%(prog)s configurationFile')
parser.add_argument('configurationFile', nargs='?', help='Specify the configuration file')
args = parser.parse_args()
configFile = args.configurationFile
# Read config file
yamlString = ""
with open(configFile, 'r') as fileContent:
yamlString = fileContent.read()
# Parse YAML
yamlConfig = yaml.load(yamlString)
# Write Docker Compose File
now = datetime.datetime.now()
dateHash = hashlib.sha256(str(now)).hexdigest()
dockerComposeFilename = str(dateHash)+".yaml"
dockerComposeProjectName = "networksimulator"
with open(dockerComposeFilename, 'w+') as filePointer:
filePointer.write(yaml.dump(yamlConfig['setup']))
# Start Docker Compose
call(["docker-compose", "-p", dockerComposeProjectName, "-f", dockerComposeFilename, "up", "-d"])
# Insert event management here:
print "##### ##### ##### ##### ##### #####\n##### ### EventManagement ##### ###\n##### ##### ##### ##### ##### #####"
events = yamlConfig['events']
threads = {}
threadsFinished = {}
# Create all workers
for event in events:
threadsFinished[event] = False
threads[event] = eventWorker(event, events[event], dockerComposeFilename, dockerComposeProjectName, threads, threadsFinished)
# start all threads
for thread in threads:
threads[thread].start()
# Wait for all before finishing
for thread in threads:
threads[thread].join()
print "Finished..."
print "##### ##### ##### ##### ##### #####\n##### ### EventManagement ##### ###\n##### ##### ##### ##### ##### #####"
# Stop docker compose and delete file
call(["docker-compose", "-p", dockerComposeProjectName, "-f", dockerComposeFilename, "down"])
os.remove(dockerComposeFilename)
deleteAllNetworks()