Created
April 23, 2021 21:42
-
-
Save lgfa29/46079e4459817bac8c4f0b402a6680e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import shutil | |
from mininet.cli import CLI | |
from mininet.net import Mininet | |
from mininet.topo import SingleSwitchTopo | |
from mininet.util import pmonitor | |
servers_count = 3 | |
clients_count = 50 | |
server_config = """ | |
data_dir = "{data_dir}" | |
name = "{name}" | |
server {{ | |
enabled = true | |
bootstrap_expect = {bootstrap_expect} | |
server_join {{ | |
retry_join = [{servers}] | |
}} | |
}} | |
""" | |
client_config = """ | |
data_dir = "{data_dir}" | |
name = "{name}" | |
client {{ | |
enabled = true | |
servers = [{servers}] | |
network_interface = "{network_interface}" | |
}} | |
plugin "raw_exec" {{ | |
config {{ | |
enabled = true | |
}} | |
}} | |
""" | |
topo = SingleSwitchTopo(k=servers_count+clients_count) | |
net = Mininet(topo=topo) | |
servers = [net.hosts[i] for i in range(servers_count)] | |
clients = [net.hosts[i] for i in range(servers_count, clients_count)] | |
popens = {} | |
for s in servers: | |
config_path = "/tmp/{}.hcl".format(s.name) | |
data_dir_path = "/tmp/nomad-server-{}".format(s.name) | |
config = server_config.format( | |
name = s.name, | |
data_dir = data_dir_path, | |
bootstrap_expect = servers_count, | |
servers = ",".join(["\"{}:4648\"".format(s.IP()) for s in servers]) | |
) | |
f = open(config_path, "w") | |
f.write(config) | |
f.close() | |
cmd = "/usr/local/bin/nomad agent -config {}".format(config_path) | |
p = s.popen(cmd) | |
popens[s.name] = p | |
for c in clients: | |
config_path = "/tmp/{}.hcl".format(c.name) | |
data_dir_path = "/tmp/nomad-client-{}".format(c.name) | |
config = client_config.format( | |
name = c.name, | |
data_dir = data_dir_path, | |
servers = ",".join(["\"{}:4647\"".format(s.IP()) for s in servers]), | |
network_interface = "{}-eth0".format(c.name) | |
) | |
f = open(config_path, "w") | |
f.write(config) | |
f.close() | |
cmd = "/usr/local/bin/nomad agent -config {}".format(config_path) | |
p = c.popen(cmd) | |
popens[c.name] = p | |
net.start() | |
CLI(net) | |
# Cleanup | |
for p in popens.values(): | |
p.terminate() | |
for s in servers: | |
shutil.rmtree("/tmp/nomad-server-{}".format(s.name)) | |
for c in clients: | |
shutil.rmtree("/tmp/nomad-client-{}".format(c.name)) | |
net.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment