forked from uncloud/uncloud
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
import argparse
|
|
import os
|
|
|
|
from pathlib import Path
|
|
from uncloud.vmm import VMM
|
|
|
|
from . import virtualmachine, logger
|
|
|
|
arg_parser = argparse.ArgumentParser('oneshot', add_help=False)
|
|
arg_parser.add_argument('--workdir', default=Path.home())
|
|
arg_parser.add_argument('--list-vms', action='store_true')
|
|
arg_parser.add_argument('--start-vm', action='store_true')
|
|
arg_parser.add_argument('--stop-vm', action='store_true')
|
|
arg_parser.add_argument('--name')
|
|
arg_parser.add_argument('--image')
|
|
arg_parser.add_argument('--uuid')
|
|
arg_parser.add_argument('--mac')
|
|
arg_parser.add_argument('--get_vm_status', action='store_true')
|
|
arg_parser.add_argument('--setup-network')
|
|
|
|
def setup_network():
|
|
print("Not implemented yet.")
|
|
exit(1)
|
|
|
|
def require_with(arguments, required, mode):
|
|
if not arguments[required]:
|
|
print("--{} is required with the {} flag. Exiting.".format(required, mode))
|
|
exit(1)
|
|
|
|
def main(arguments):
|
|
# Initialize VMM
|
|
workdir = arguments['workdir']
|
|
vmm = VMM(vmm_backend=workdir)
|
|
|
|
# Initialize workdir directory.
|
|
# TODO: copy ifup, ifdown.
|
|
|
|
# Build VM configuration.
|
|
vm_config = {}
|
|
for spec in ['uuid', 'memory', 'cores', 'threads', 'image', 'image_format', 'name']:
|
|
if arguments.get(spec):
|
|
vm_config[spec] = arguments[spec]
|
|
|
|
# Execute requested VM action.
|
|
vm = virtualmachine.VM(vmm, vm_config)
|
|
if arguments['setup_network']:
|
|
setup_network()
|
|
elif arguments['start_vm']:
|
|
require_with(arguments, 'image', 'start_vm')
|
|
vm.start()
|
|
logger.info("Created VM {}".format(vm.get_uuid))
|
|
elif arguments['get_vm_status']:
|
|
require_with(arguments, 'uuid', 'get_vm_status')
|
|
print("VM: {} {} {}".format(vm.get_uuid(), vm.get_name(), vm.get_status()))
|
|
elif arguments['stop_vm']:
|
|
require_with(arguments, 'uuid', 'stop_vm')
|
|
vm.stop()
|
|
elif arguments['list_vms']:
|
|
discovered = vmm.discover()
|
|
print("Found {} VMs.".format(len(discovered)))
|
|
for uuid in vmm.discover():
|
|
vmi = virtualmachine.VM(vmm, {'uuid': uuid})
|
|
print("VM: {} {} {}".format(vmi.get_uuid, vmi.get_name, vmi.get_status))
|
|
else:
|
|
print('No action requested. Exiting.')
|