diff --git a/lxc_net/container.py b/lxc_net/container.py index 659e3ba..f08a586 100644 --- a/lxc_net/container.py +++ b/lxc_net/container.py @@ -209,6 +209,7 @@ class Container(util.LibvirtObject): if not str(exn).startswith("Domain not found:"): raise exn # Else, the machine was already stopped: everything is fine + self.lxc_container = None def __enter__(self): self.create() diff --git a/spawn_network.py b/spawn_network.py index f169280..651b52a 100755 --- a/spawn_network.py +++ b/spawn_network.py @@ -5,6 +5,7 @@ import signal import libvirt import argparse import logging +import sys def parse_args(): @@ -15,6 +16,66 @@ def parse_args(): return args +def handle_dom(topology, cmd): + if not cmd: + print("Missing argument.") + return + + if cmd[0] in ["up", "down"]: + dom_id = None + if len(cmd) > 1: + try: + dom_id = [int(x) for x in cmd[1:]] + except ValueError: + print("Bad id: {}".format(cmd[1])) + return + state = cmd[0] == "up" + if dom_id: + for dom in dom_id: + topology.dom_setstate_single(dom, state, verbose=True) + else: + topology.dom_setstate(state, verbose=True) + elif cmd[0] == "restart": + handle_dom(topology, ["down"] + cmd[1:]) + handle_dom(topology, ["up"] + cmd[1:]) + elif cmd[0] == "help": + print("Available commands: up, down, restart, help") + else: + print("Bad command.") + + +def main_loop(topology): + actions = { + "dom": handle_dom, + "exit": "_exit", + "help": "_help", + } + + while True: + print(">> ", end="") + sys.stdout.flush() + + try: + line = input().strip().split() + if not line: # Empty line + continue + + action = actions.get(line[0], "_bad") + if isinstance(action, str): + if action == "_bad": + print("Unknown command: {}.".format(line[0])) + if action in ["_help", "_bad"]: + print("Available commands: dom, exit, help") + elif action == "_exit": + return + else: + print("Unknown action.") + else: + action(topology, line[1:]) + except Exception as exn: + print(exn) + + def main(): args = parse_args() @@ -40,8 +101,8 @@ def main(): topology.dom_start(verbose=True) print("Network running. Press ^C to terminate.") - while not received_sigint: # Wait for SIGINT - signal.pause() + + main_loop(topology) topology.dom_stop(verbose=True) topology.net_stop(verbose=True)