70 lines
1.6 KiB
Python
Executable file
70 lines
1.6 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
from lxc_net import parse_network
|
|
import sys
|
|
import signal
|
|
import libvirt
|
|
import argparse
|
|
import asyncio
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(description="Spawns a network of LXC containers.")
|
|
parser.add_argument("topology", help="A YAML file defining the network topology")
|
|
|
|
args = parser.parse_args()
|
|
return args
|
|
|
|
|
|
async def spawn_network(link):
|
|
await link.async_create()
|
|
print(link.name, end="... ")
|
|
sys.stdout.flush()
|
|
|
|
|
|
async def spawn_networks(links):
|
|
link_tasks = [asyncio.create_task(spawn_network(link)) for link in links]
|
|
for link_task in link_tasks:
|
|
await link_task
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
received_sigint = False
|
|
|
|
def handle_sigint(signum, frame):
|
|
""" Called upon SIGINT (^C) """
|
|
nonlocal received_sigint
|
|
|
|
print(" >> Received SIGINT, stopping network...")
|
|
received_sigint = True
|
|
|
|
signal.signal(signal.SIGINT, handle_sigint)
|
|
|
|
conn = libvirt.open("lxc:///")
|
|
|
|
topology = parse_network.YamlTopology(args.topology, conn)
|
|
|
|
print(">> Spawning networks: ", end="")
|
|
sys.stdout.flush()
|
|
asyncio.run(spawn_networks(topology.links))
|
|
print("Done.")
|
|
|
|
print(">> Spawning containers: ", end="")
|
|
sys.stdout.flush()
|
|
for c_dom in topology.domains:
|
|
if received_sigint:
|
|
return
|
|
print(c_dom.name, end="... ")
|
|
sys.stdout.flush()
|
|
c_dom.create()
|
|
print("Done.")
|
|
|
|
print("Network running. Press ^C to terminate.")
|
|
while not received_sigint: # Wait for SIGINT
|
|
signal.pause()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|