This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
executable file
·213 lines (171 loc) · 6.78 KB
/
controller.py
File metadata and controls
executable file
·213 lines (171 loc) · 6.78 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""Controller to handle all blockchains in the backend and connect to the aggregation server"""
import json
import logging
import os
import subprocess
import socket
import atexit
import time
import daemonize
import yaml
from websocket import create_connection
PID = "./controller.pid"
CONFIG = {}
ACTIVE_CHAIN_NAMES = []
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.WARN)
LOGGER.propagate = False
FH = logging.FileHandler("./logfile.log", "w")
FORMATTER = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s at: '%(lineno)d'")
FH.setLevel(logging.WARN)
FH.setFormatter(FORMATTER)
LOGGER.addHandler(FH)
CONFIG_FILE = open(os.path.join(os.path.dirname(__file__), 'config.yaml'))
CONFIG_FILE_ID = CONFIG_FILE.fileno()
KEEP_FDS = [FH.stream.fileno(), CONFIG_FILE_ID]
# pylint: disable=broad-except
# pylint: disable=global-statement
# pylint: disable=too-many-nested-blocks
def start_chain(chain_name):
"""Start a given chain."""
global ACTIVE_CHAIN_NAMES
path = CONFIG['chainScripts']['start'].format(str(chain_name))
LOGGER.info("path %s", path)
result = subprocess.run([str(path)], stdout=subprocess.PIPE)
LOGGER.info("return code %d", result.returncode)
LOGGER.info(result)
ACTIVE_CHAIN_NAMES.append(chain_name)
def stop_chain(chain_name):
"""Stop a given chain."""
path = CONFIG['chainScripts']['stop'].format(str(chain_name))
LOGGER.info('stopping: %a', path)
subprocess.Popen([str(path)], stdout=open(os.devnull, 'wb'))
def scale_hosts(chain_name, value):
"""Scale the number of Hosts"""
global ACTIVE_CHAIN_NAMES
if chain_name in ACTIVE_CHAIN_NAMES:
path = CONFIG['chainScripts']['scaleLazy'].format(str(chain_name))
subprocess.Popen([str(path), str(value)],
stdout=open(os.devnull, 'wb'))
def scale_miners(chain_name, value):
"""Scale the number of Miners."""
global ACTIVE_CHAIN_NAMES
if chain_name in ACTIVE_CHAIN_NAMES:
path = CONFIG['chainScripts']['scaleMiner'].format(str(chain_name))
subprocess.Popen([str(path), str(value)],
stdout=open(os.devnull, 'wb'))
def set_scenario_parameters(chain_name, scenario):
"""Set scenario period and payloadSize."""
LOGGER.info(scenario)
data = json.dumps(scenario['logContent'])
LOGGER.info(data)
if chain_name in ACTIVE_CHAIN_NAMES:
LOGGER.debug('Setting for %s is %s ', chain_name, data)
port = CONFIG['{}Port'.format(chain_name)]
docker_websocket = create_connection("ws://localhost:{}".format(port))
docker_websocket.send(data)
docker_websocket.close()
def dispatch_action(chain_name, parameters=None, scenario=None):
"""Dispatch the parameters and values to the chain."""
LOGGER.debug(parameters)
parameters = {key.lower(): value for key, value in parameters.items()}
if 'numberofhosts' in parameters:
LOGGER.debug('Scale %s hosts to %d', chain_name, parameters['numberofhosts'])
scale_hosts(chain_name, parameters['numberofhosts'])
if 'numberofminers' in parameters:
LOGGER.debug('Scale %s miners to %d', chain_name, parameters['numberofminers'])
scale_miners(chain_name, parameters['numberofminers'])
if 'startchain' in parameters:
LOGGER.debug('Start %s', chain_name)
start_chain(chain_name)
if 'stopchain' in parameters:
LOGGER.debug('Stop %s', chain_name)
stop_chain(chain_name)
ACTIVE_CHAIN_NAMES.remove(chain_name)
LOGGER.info("ACTIVE CHAIN NAMES %s", ACTIVE_CHAIN_NAMES)
if scenario:
LOGGER.debug('Sending scenario parameters to %s', chain_name)
set_scenario_parameters(chain_name, scenario)
LOGGER.debug('Tried to dispatch %s %s %s', chain_name, parameters, scenario)
def enact_job(job):
"""Enact the retrieved job on the given chain."""
LOGGER.debug(job)
for chain in CONFIG['chains']:
if chain['chainName'].lower() == job['chainName'].lower():
chain_name = chain['chainName']
LOGGER.debug(job['parameters'])
try:
scenario = job['scenario']
LOGGER.info(scenario)
dispatch_action(
chain_name,
job['parameters'],
scenario)
except Exception as exception:
LOGGER.error('Error occured when dispatching job %s', exception)
def init_controller():
"""Load and parse the config file."""
try:
global CONFIG
CONFIG = yaml.safe_load(CONFIG_FILE)
except Exception as exception:
LOGGER.error('Error occured while parsing config.yaml')
LOGGER.error(exception)
def start_socket():
"""Start the websocket and connect to the API Server."""
reconnect = 0
while reconnect < 20:
try:
reconnect, api_server_connection = connect_to_api_server()
waiting_for_inputs = True
while waiting_for_inputs:
message = api_server_connection.recv()
LOGGER.debug('Received %s', message)
try:
job = json.loads(message)
enact_job(job)
except Exception as exception:
LOGGER.warn('Error occured. Can not parse JSON %s', exception)
except Exception as exception:
LOGGER.error('Connection error occured %s', exception)
reconnect += 1
LOGGER.warn('Lost connection to server')
time.sleep(5)
LOGGER.warn('Try to reconnect')
def stop_all_chains():
"""Stops all active chains. Sets the active chain list to an empty list"""
global ACTIVE_CHAIN_NAMES
for chain in ACTIVE_CHAIN_NAMES:
stop_chain(chain)
ACTIVE_CHAIN_NAMES = []
def connect_to_api_server():
"""
Connects to the api server at the address provided in the config
:return: 0 connection retries and the websocket connection
"""
stop_all_chains()
hostname = socket.gethostname()
web_socket = create_connection(CONFIG['url'])
LOGGER.debug('Created connection')
LOGGER.debug('Hostname: %s', hostname)
LOGGER.debug('Send chain configuration options')
data = {
'target': hostname,
'chains': CONFIG['chains'],
}
web_socket.send(json.dumps(data))
LOGGER.debug('Chain configuration options sent')
return 0, web_socket
def exit_controller():
"""Exit from the controller and stop all active chains."""
LOGGER.debug('Stopping active chains %s', ACTIVE_CHAIN_NAMES)
stop_all_chains()
def main():
"""Main method to init the controller and start the websocket."""
init_controller()
start_socket()
atexit.register(exit_controller)
DAEMON = daemonize.Daemonize(
app="blockchainController", pid=PID, action=main, keep_fds=KEEP_FDS, chdir='./')
DAEMON.start()