-
Notifications
You must be signed in to change notification settings - Fork 0
/
digsinet.py
executable file
·327 lines (283 loc) · 11.1 KB
/
digsinet.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python3
import asyncio
import json
import os
import signal
import sys
import importlib
import logging
from event.kafka import KafkaClient
from event.rabbit import RabbitClient
import yaml
from config import ArgParser, read_config
from interfaces.interface import Interface
logger = None
broker = None
def gracefull_shutdown_handler(sig, frame):
global broker
print("Shutting down gracefully...")
if broker:
broker.close()
# handle gRPC and gNMI connection loss messages etc.
sys.exit(0)
def main():
global logger, broker
signal.signal(signal.SIGINT, gracefull_shutdown_handler)
# Create an argument parser
parser = ArgParser()
args = parser.get_args()
# If the reconfigure flag is set, clab will be told to reconfigure existing containers
reconfigure_containers = "--reconfigure" if args.reconfigure else ""
logger = logging.getLogger(__name__)
# If the debug flag is set, the log level will be set to debug
if args.debug:
logger.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
else:
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
try:
config = read_config(args.config)
except Exception as e:
print(e)
exit(-1)
# Add config flags
# config['reconfigure_containers'] = reconfigure_containers
if args.cleanup:
if args.yes_i_really_mean_it:
os.system("clab destroy -a -c")
else:
print(
"Please confirm forcefull cleanup by using the --yes-i-really-mean-it flag"
)
exit(1)
elif args.stop:
os.system(f"clab destroy -t {config.topology.file}")
for sibling in config.siblings:
sibling_config = config.siblings.get(sibling)
if sibling_config:
if sibling_config.autostart:
os.system(
f"clab destroy -t {config.topology_name}_sib_{sibling}.clab.yml"
)
elif args.start:
clab_topology_definition = load_topology(config)
topology_name = clab_topology_definition.get("name")
topology_prefix = "clab"
controllers = load_controllers(config)
realnet_apps = load_realnet_apps(config)
realnet_interfaces = load_realnet_interfaces(
config, topology_name, topology_prefix
)
nodes = create_nodes(clab_topology_definition)
deploy_topology(reconfigure_containers, config)
broker = create_kafka_queues(config.siblings, config.kafka) if config.kafka is not None\
else create_rabbit_queues(config.siblings, config.rabbit)
siblings = create_siblings(
config.siblings,
controllers,
config,
clab_topology_definition,
nodes,
broker,
reconfigure_containers,
topology_name,
topology_prefix,
)
main_loop(config, realnet_interfaces, realnet_apps, siblings, nodes, broker)
def load_controllers(config):
controllers = {}
for controller in config.controllers:
logger.debug(f"Loading controller {controller}...")
module = importlib.import_module(config.controllers.get(controller).module)
controllers[controller] = module
return controllers
def load_realnet_apps(config):
realnet_apps = {}
if config.realnet.apps is not None:
for app in config.realnet.apps:
logger.debug(f"Loading app {app}...")
module = importlib.import_module(config.apps.get(app).module)
app_class = getattr(module, app)
app_instance = app_class(config, logger)
realnet_apps[app] = app_instance
return realnet_apps
def load_realnet_interfaces(config, topology_name, topology_prefix):
realnet_interfaces = {}
for interface in config.realnet.interfaces:
logger.debug(f"Loading interface {interface}...")
module = importlib.import_module(
config.interface_credentials.get(interface).module
)
interface_class = getattr(module, interface)
interface_instance = interface_class(
config, "realnet", logger, topology_prefix, topology_name
)
realnet_interfaces[interface] = interface_instance
return realnet_interfaces
def load_topology(config):
with open(config.topology.file, "r") as stream:
clab_topology_definition = yaml.safe_load(stream)
return clab_topology_definition
def create_nodes(clab_topology_definition):
nodes = dict()
for node in clab_topology_definition["topology"]["nodes"].items():
nodes[node[0]] = dict()
return nodes
def deploy_topology(reconfigureContainers, config):
os.system(f"clab deploy {reconfigureContainers} -t {config.topology.file}")
def create_rabbit_queues(siblings, stream_config):
queue_names = []
for sibling in siblings:
queue_names.append(sibling)
queue_names.append('realnet')
client = RabbitClient(stream_config, queue_names, logger)
return client
def create_kafka_queues(siblings, stream_config):
queue_names = []
for sibling in siblings:
queue_names.append(sibling)
queue_names.append("realnet")
client = KafkaClient(stream_config, queue_names, logger)
return client
def create_siblings(
siblings_config,
controllers,
config,
clab_topology_definition,
nodes,
kafka_client,
reconfigure_containers,
topology_name,
topology_prefix,
):
siblings = dict()
consumer, key = kafka_client.subscribe("realnet", "create_siblings")
for sibling in siblings_config:
siblings[sibling] = dict()
if siblings_config[sibling].controller:
logger.info(f"=== Start Controller for {sibling}...")
configured_sibling_controller = siblings_config[sibling].controller
controller_class = getattr(
controllers[configured_sibling_controller],
configured_sibling_controller,
)
controller_instance = controller_class(
config,
clab_topology_definition,
nodes,
sibling,
kafka_client,
logger,
reconfigure_containers,
topology_prefix,
topology_name,
)
siblings[sibling]["controller"] = controller_instance
logger.info(f"=== Build sibling {sibling} using its controller...")
kafka_client.publish(
sibling,
{
"type": "topology build request",
"source": "realnet",
"sibling": sibling,
},
)
timeout = config.sibling_timeout
try:
logger.info(f"Waiting for topology build response for realnet...")
while True:
message = kafka_client.poll(consumer, timeout=timeout)
if message is None:
logger.error(
f"Timeout while waiting for topology build response from sibling {sibling}"
)
kafka_client.close()
exit(1)
elif message.error():
logger.error(f"Consumer error: {message.error()}")
kafka_client.close()
exit(1)
else:
task = json.loads(message.value())
if (
task["type"] == "topology build response"
and task["sibling"] == sibling
):
siblings[sibling].update(
{
"topology": task["topology"],
"nodes": task["nodes"],
"interfaces": task["interfaces"],
"running": task["running"],
}
)
break
finally:
logger.debug(f"Topology build response for sibling {sibling} received.")
logger.debug(f"Closing consumer...")
kafka_client.close_consumer(key)
return siblings
def main_loop(
config, realnet_interfaces, realnet_apps, siblings, nodes, kafka_client: KafkaClient
):
logger.info("=== Entering main Loop...")
# stats_interval = 10
consumer, key = kafka_client.subscribe("realnet", "main_loop")
try:
while True:
# Stats removed for now - will be used in dashboard
# Not required with new event stream design
# stats_interval -= 1
# if stats_interval == 0:
# stats_interval = 10
# queue_stats = ""
# # TODO Replace queues
# # for queue in queues:
# # queue_stats += f" ({queue}, size: {str(queues[queue].qsize())})"
# logger.info(f"=== Queue stats: {queue_stats}")
for interface in realnet_interfaces:
interface_instance: Interface = realnet_interfaces[interface]
logger.info(
f"=== Pass Siblings {siblings} to interface {interface} for getNodesUpdate..."
)
nodes = interface_instance.getNodesUpdate(
nodes, siblings, kafka_client, diff=True
)
task = None
logger.info(f"Checking for consumer message in main loop for realnet...")
message = kafka_client.poll(consumer, config.sync_interval)
if message is None:
logger.error(f"Timeout while waiting for task for realnet")
# kafka_client.close()
# exit(1)
elif message.error():
logger.error(f"Consumer error: {message.error()}")
kafka_client.close()
exit(1)
else:
task = json.loads(message.value())
logger.info(f"Got task {task}...")
logger.debug(f"*** Realnet got task: {str(task)}")
if task["type"] == "topology build response":
sibling = task["sibling"]
siblings[sibling].update(
{
"topology": task["topology"],
"nodes": task["nodes"],
"interfaces": task["interfaces"],
"running": task["running"],
}
)
for app in realnet_apps:
logger.debug(f"=== Running App {app[0]} on realnet...")
asyncio.run(
app[1].run(
config, siblings[task["sibling"]], kafka_client, task
)
)
# queues["realnet"].task_done()
finally:
kafka_client.close_consumer(key)
if __name__ == "__main__":
main()