python-compute/computelib/cli/vmctl.py

109 lines
2.9 KiB
Python
Raw Normal View History

2023-07-22 23:59:49 +03:00
"""
Manage virtual machines.
Usage: na-vmctl [options] status <machine>
na-vmctl [options] is-running <machine>
na-vmctl [options] start <machine>
2023-08-31 20:37:41 +03:00
na-vmctl [options] shutdown <machine>
2023-08-27 23:42:56 +03:00
na-vmctl [options] set-vcpus <machine> <nvcpus>
na-vmctl [options] set-memory <machine> <memory>
na-vmctl [options] list [-a|--all]
2023-07-22 23:59:49 +03:00
Options:
2023-09-02 00:52:28 +03:00
-c, --config <file> config file [default: /etc/node-agent/config.yaml]
-l, --loglvl <lvl> logging level
-a, --all list all machines including inactive
2023-07-22 23:59:49 +03:00
"""
import logging
2023-08-24 22:36:12 +03:00
import pathlib
import sys
2023-07-22 23:59:49 +03:00
2023-07-28 01:01:32 +03:00
import libvirt
2023-07-22 23:59:49 +03:00
from docopt import docopt
2023-09-23 21:24:56 +03:00
from ..exceptions import VMError, VMNotFound
2023-08-24 22:36:12 +03:00
from ..session import LibvirtSession
2023-09-02 00:52:28 +03:00
from ..vm import VirtualMachine
2023-07-22 23:59:49 +03:00
logger = logging.getLogger(__name__)
levels = logging.getLevelNamesMapping()
2023-08-31 20:37:41 +03:00
libvirt.registerErrorHandler(lambda userdata, err: None, ctx=None)
2023-07-22 23:59:49 +03:00
class Color:
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
NONE = '\033[0m'
2023-08-27 23:42:56 +03:00
class Table:
def __init__(self, whitespace: str = '\t'):
self.__rows = []
self.__whitespace = whitespace
def header(self, columns: list):
self.__rows.insert(0, [str(col) for col in columns])
def row(self, row: list):
self.__rows.append([str(col) for col in row])
def rows(self, rows: list):
for row in rows:
self.row(row)
def print(self):
widths = [max(map(len, col)) for col in zip(*self.__rows)]
for row in self.__rows:
print(self.__whitespace.join(
(val.ljust(width) for val, width in zip(row, widths))))
2023-07-22 23:59:49 +03:00
def cli():
args = docopt(__doc__)
config = pathlib.Path(args['--config']) or None
2023-07-29 15:10:30 +03:00
loglvl = None
2023-07-28 01:01:32 +03:00
machine = args['<machine>']
2023-07-22 23:59:49 +03:00
2023-07-29 15:10:30 +03:00
if args['--loglvl']:
loglvl = args['--loglvl'].upper()
2023-07-22 23:59:49 +03:00
if loglvl in levels:
logging.basicConfig(level=levels[loglvl])
2023-08-31 20:37:41 +03:00
with LibvirtSession() as session:
2023-07-22 23:59:49 +03:00
try:
2023-08-27 23:42:56 +03:00
if args['list']:
table = Table()
table.header(['NAME', 'STATE', 'AUTOSTART'])
2023-08-31 20:37:41 +03:00
for vm_ in session.list_machines():
2023-08-27 23:42:56 +03:00
table.row([vm_.name, vm_.status, vm_.is_autostart])
table.print()
sys.exit()
2023-08-31 20:37:41 +03:00
vm = session.get_machine(machine)
2023-07-22 23:59:49 +03:00
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']:
2023-08-31 20:37:41 +03:00
vm.shutdown('NORMAL')
2023-07-22 23:59:49 +03:00
except VMNotFound as nferr:
2023-07-28 01:01:32 +03:00
sys.exit(f'{Color.RED}VM {machine} not found.{Color.NONE}')
2023-07-22 23:59:49 +03:00
except VMError as vmerr:
sys.exit(f'{Color.RED}{vmerr}{Color.NONE}')
if __name__ == '__main__':
cli()