""" Manage virtual machines. Usage: na-vmctl [options] status na-vmctl [options] is-running na-vmctl [options] start na-vmctl [options] shutdown [-f|--force] [-9|--sigkill] Options: -c, --config Config file [default: /etc/node-agent/config.yaml] -l, --loglvl Logging level -f, --force Force action. On shutdown calls graceful destroy() -9, --sigkill Send SIGKILL to QEMU process. Not affects without --force """ import logging import pathlib import sys import libvirt from docopt import docopt from ..session import LibvirtSession from ..vm import VirtualMachine, VMError, VMNotFound logger = logging.getLogger(__name__) levels = logging.getLevelNamesMapping() class Color: RED = '\033[31m' GREEN = '\033[32m' YELLOW = '\033[33m' NONE = '\033[0m' def cli(): args = docopt(__doc__) config = pathlib.Path(args['--config']) or None loglvl = None machine = args[''] if args['--loglvl']: loglvl = args['--loglvl'].upper() if loglvl in levels: logging.basicConfig(level=levels[loglvl]) with LibvirtSession(config) as session: try: vm = VirtualMachine(session, machine) if args['status']: print(vm.status) if args['is-running']: if vm.is_running: print('running') else: sys.exit(vm.status) if args['start']: vm.start() print(f'{vm.name} started') if args['shutdown']: vm.shutdown(force=args['--force'], sigkill=args['sigkill']) except VMNotFound as nferr: sys.exit(f'{Color.RED}VM {machine} not found.{Color.NONE}') except VMError as vmerr: sys.exit(f'{Color.RED}{vmerr}{Color.NONE}') if __name__ == '__main__': cli()