-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_container.py
214 lines (169 loc) · 7.69 KB
/
multi_container.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from mininet.net import Containernet
from mininet.node import Controller
from mininet.cli import CLI
from mininet.link import TCLink
from mininet.log import debug, info, error, setLogLevel
import argparse
from shutil import copyfile
import itertools
import os
import time
import xml.etree.ElementTree as ET
PWD = os.getcwd()
VERSION = 0.2
TOTAL_BROKERS = 7
DELAY = 10
IP_ADDR = '10.0.0.'
IMAGES = {
"EMQX": "flipperthedog/emqx-ip:latest",
"VERNEMQ": "francigjeci/vernemq-debian:latest",
"RABBITMQ": "flipperthedog/rabbitmq:ping",
"HIVEMQ": "francigjeci/hivemq:dns-image"
}
def arg_parse():
parser = argparse.ArgumentParser(description='MQTT cluster simulation')
parser.add_argument('-n', '--broker-number', dest='broker_num', default=TOTAL_BROKERS,
help='specify the size of the cluster', type=int)
parser.add_argument('-t', '--type', dest='cluster_type', default='rabbitmq',
help='broker type (EMQX, RABBITMQ, VERNEMQ, HIVEMQ)')
parser.add_argument('-d', '--delay', dest='link_delay', default=DELAY,
help='delay over a simple link', type=int)
return parser.parse_args()
def start_emqx(cont_name, cont_address, bind_ip, master_node):
return net.addDocker(hostname=cont_name, name=cont_name, ip=cont_address,
ports=[1883], port_bindings={1883: bind_ip},
dimage=IMAGES["EMQX"],
environment={"EMQX_NAME": cont_name,
"EMQX_HOST": cont_address,
"EMQX_NODE__DIST_LISTEN_MAX": 6379,
"EMQX_LISTENER__TCP__EXTERNAL": 1883,
"EMQX_CLUSTER__DISCOVERY": "static",
"EMQX_CLUSTER__STATIC__SEEDS": master_node
})
def start_rabbitmq(cont_name, cont_address, bind_ip, master_node):
dest_file = "{}/confiles/rabbitmq_{}.conf".format(PWD, cont_address[-3:])
copyfile(PWD + "/confiles/rabbitmq.conf", dest_file)
with open(dest_file, "a") as f:
c = 0
for i in range(2, args.broker_num + 2):
if IP_ADDR + str(200 + i) != cont_address:
c += 1
f.write("cluster_formation.classic_config.nodes.{} = rabbit@rabbitmq_{}\n".format(c, i))
d = net.addDocker(hostname=cont_name, name=cont_name, ip=cont_address,
dimage=IMAGES["RABBITMQ"], \
ports=[5672, 1883],
port_bindings={5672: 5670 + (bind_ip - 1880),
1883: bind_ip},
volumes=[
PWD + "/confiles/rabbitmq_{}.conf:/etc/rabbitmq/rabbitmq.conf".format(cont_address[-3:]),
PWD + "/confiles/enabled_plugins:/etc/rabbitmq/enabled_plugins"],
environment={"RABBITMQ_ERLANG_COOKIE": "GPLDKBRJYMSKLTLZQDVG"})
for i in range(2, args.broker_num + 2):
if IP_ADDR + str(200 + i) != cont_address:
d.cmd('echo "{} {}" >> /etc/hosts'.format(IP_ADDR + str(200 + i), "rabbitmq_" + str(i)))
return d
def start_hivemq(cont_name, cont_address, bind_ip, master_node):
source_config_file_path = os.path.join(PWD, 'confiles/config-dns.xml')
dest_file = "{}/confiles/config-dns_{}.xml".format(PWD, cont_address[-3:])
__port = 8000
config_file = ET.parse(source_config_file_path)
cluster_nodes = config_file.getroot().find('cluster').find('discovery').find('static')
if cluster_nodes is None:
raise ModuleNotFoundError('Element not found')
else:
# Remove the existing elements
for node in cluster_nodes:
cluster_nodes.remove(node)
# Add all cluster's nodes into config file
_brokers_index = range(2, args.broker_num + 2)
_brokers_addr = [IP_ADDR + str(200 + _broker_index) for _broker_index in range(2, args.broker_num + 2)]
for broker_addr in _brokers_addr:
_new_node = ET.Element('node')
_host = ET.Element('host')
_host.text = str(broker_addr)
_port = ET.Element('port')
_port.text = str(__port)
_new_node.append(_host)
_new_node.append(_port)
cluster_nodes.append(_new_node)
config_file.write(dest_file)
return net.addDocker(hostname=cont_name, name=cont_name, ip=cont_address,
ports=[1883], port_bindings={1883: bind_ip},
dimage=IMAGES["HIVEMQ"],
volumes=[
PWD + "/confiles/config-dns_{}.xml:/opt/hivemq/conf/config.xml".format(cont_address[-3:])],
environment={
"HIVEMQ_BIND_ADDRESS": cont_address
})
def start_vernemq(cont_name, cont_address, bind_ip, master_node):
return net.addDocker(hostname=cont_name, name=cont_name, ip=cont_address,
ports=[1883], port_bindings={1883: bind_ip},
dimage=IMAGES["VERNEMQ"],
environment={
"DOCKER_VERNEMQ_NODENAME": cont_address,
"DOCKER_VERNEMQ_DISCOVERY_NODE": master_node[master_node.index("@") + 1:],
"DOCKER_VERNEMQ_ACCEPT_EULA": "yes",
"DOCKER_VERNEMQ_ALLOW_ANONYMOUS": "on"
})
def invalid(broker):
info('Invalid {}'.format(broker))
def cluster_type(argument):
switcher = {
'EMQX': start_emqx,
'RABBITMQ': start_rabbitmq,
'VERNEMQ': start_vernemq,
'HIVEMQ': start_hivemq
}
func = switcher.get(argument, lambda: invalid())
local_list = []
my_master = None
for cnt in range(2, args.broker_num + 2):
container_name = "{}_{}".format(args.cluster_type, cnt)
local_address = IP_ADDR + str(200 + cnt)
bind_addr = 1880 + cnt
if my_master is None:
my_master = "{}@{}".format(container_name, local_address)
local_list.append(func(container_name, local_address, bind_addr, my_master))
return local_list
def main(_args):
info('\n*** Multi containernet generator v{} ***'.format(VERSION))
info('\nargs:')
info('\n\tCLUSTER TYPE: {}'.format(args.cluster_type))
info('\n\tNUMBER OF BROKERS: {}'.format(args.broker_num))
info('\n\tDELAY: {}'.format(args.link_delay))
info('\n')
info('\n*** Adding controller')
net.addController('c0', port=6654)
info(' DONE\n')
info('\n*** Adding docker containers, type: {}\n'.format(_args.cluster_type.upper()))
container_list = cluster_type(_args.cluster_type.upper())
info('\n*** Adding switch\t')
s1 = net.addSwitch('s1')
info(s1)
info('\n*** Adding container-switch links\n')
for c in container_list:
info(net.addLink(c, s1, cls=TCLink, delay='{}ms'.format(_args.link_delay / 2), bw=1))
info('\n*** Starting network\n')
net.start()
net.staticArp()
info('\n*** Testing connectivity\n')
net.pingAll()
info('\n*** Waiting the network start up ({} secs)...\n'.format(_args.link_delay / 2))
time.sleep(_args.link_delay / 2)
info('\n*** Starting the entrypoints\n')
# START CONTAINERS
for c in container_list:
c.start()
info('*** Waiting the boot ({} secs)...\n'.format(_args.link_delay))
time.sleep(_args.link_delay)
info('*** Running CLI\n')
CLI(net)
info('*** Stopping network')
net.stop()
if __name__ == "__main__":
setLogLevel('info')
net = Containernet(controller=Controller)
args = arg_parse()
main(args)