This commit is contained in:
ge
2023-09-02 00:52:28 +03:00
parent 62388e8b67
commit 43033b5a0d
13 changed files with 110 additions and 110 deletions

View File

@ -10,11 +10,9 @@ Usage: na-vmctl [options] status <machine>
na-vmctl [options] list [-a|--all]
Options:
-c, --config <file> Config file [default: /etc/node-agent/config.yaml]
-l, --loglvl <lvl> Logging level
-a, --all List all machines including inactive
-f, --force Force action. On shutdown calls graceful destroy()
-9, --sigkill Send SIGKILL to QEMU process. Not affects without --force
-c, --config <file> config file [default: /etc/node-agent/config.yaml]
-l, --loglvl <lvl> logging level
-a, --all list all machines including inactive
"""
import logging
@ -25,13 +23,13 @@ import libvirt
from docopt import docopt
from ..session import LibvirtSession
from ..vm import VirtualMachine, VMError, VMNotFound
from ..vm import VirtualMachine
from ..exceptions import VMError, VMNotFound
logger = logging.getLogger(__name__)
levels = logging.getLevelNamesMapping()
# Supress libvirt errors
libvirt.registerErrorHandler(lambda userdata, err: None, ctx=None)
@ -43,21 +41,6 @@ class Color:
class Table:
"""Print table. Example::
t = Table()
t.header(['KEY', 'VALUE']) # header is optional
t.row(['key 1', 'value 1'])
t.row(['key 2', 'value 2'])
t.rows(
[
['key 3', 'value 3'],
['key 4', 'value 4']
]
)
t.print()
"""
def __init__(self, whitespace: str = '\t'):
self.__rows = []

View File

@ -4,10 +4,11 @@ Execute shell commands on guest via guest agent.
Usage: na-vmexec [options] <machine> <command>
Options:
-c, --config <file> Config file [default: /etc/node-agent/config.yaml]
-l, --loglvl <lvl> Logging level
-s, --shell <shell> Guest shell [default: /bin/sh]
-c, --config <file> config file [default: /etc/node-agent/config.yaml]
-l, --loglvl <lvl> logging level
-s, --shell <shell> guest shell [default: /bin/sh]
-t, --timeout <sec> QEMU timeout in seconds to stop polling command status [default: 60]
-p, --pid <PID> PID on guest to poll output
"""
import logging
@ -18,13 +19,13 @@ import libvirt
from docopt import docopt
from ..session import LibvirtSession
from ..vm import GuestAgent, GuestAgentError, VMNotFound
from ..vm import GuestAgent
from ..exceptions import GuestAgentError, VMNotFound
logger = logging.getLogger(__name__)
levels = logging.getLevelNamesMapping()
# Supress libvirt errors
libvirt.registerErrorHandler(lambda userdata, err: None, ctx=None)
@ -58,31 +59,24 @@ def cli():
exited, exitcode, stdout, stderr = ga.shellexec(
cmd, executable=shell, capture_output=True, decode_output=True,
timeout=int(args['--timeout']))
except GuestAgentError as qemuerr:
errmsg = f'{Color.RED}{qemuerr}{Color.NONE}'
if str(qemuerr).startswith('Polling command pid='):
except GuestAgentError as gaerr:
errmsg = f'{Color.RED}{gaerr}{Color.NONE}'
if str(gaerr).startswith('Polling command pid='):
errmsg = (errmsg + Color.YELLOW +
'\n[NOTE: command may still running]' + Color.NONE)
'\n[NOTE: command may still running on guest '
'pid={ga.last_pid}]' + Color.NONE)
sys.exit(errmsg)
except VMNotFound as err:
sys.exit(f'{Color.RED}VM {machine} not found{Color.NONE}')
if not exited:
print(Color.YELLOW + '[NOTE: command may still running]' + Color.NONE,
file=sys.stderr)
else:
if exitcode == 0:
exitcolor = Color.GREEN
else:
exitcolor = Color.RED
print(exitcolor + f'[command exited with exit code {exitcode}]' +
Color.NONE,
file=sys.stderr)
print(Color.YELLOW +
'[NOTE: command may still running on guest pid={ga.last_pid}]' +
Color.NONE, file=sys.stderr)
if stderr:
print(Color.RED + stderr.strip() + Color.NONE, file=sys.stderr)
print(stderr.strip(), file=sys.stderr)
if stdout:
print(Color.GREEN + stdout.strip() + Color.NONE, file=sys.stdout)
print(stdout.strip(), file=sys.stdout)
sys.exit(exitcode)