spawn_network: add basic REPL

This commit is contained in:
Théophile Bastian 2020-03-19 20:24:18 +01:00
parent b9f9e7d1b0
commit ceee1908a4
2 changed files with 64 additions and 2 deletions

View file

@ -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()

View file

@ -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)