lxc-network/spawn_network.py

62 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python3
from lxc_net import parse_network
import sys
import signal
import libvirt
import argparse
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
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()
for link in topology.links:
if received_sigint:
return
print(link.name, end="... ")
sys.stdout.flush()
link.create()
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()