2023-11-23 02:34:02 +03:00
|
|
|
# This file is part of Compute
|
|
|
|
#
|
|
|
|
# Compute is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2023-12-03 23:25:34 +03:00
|
|
|
# Compute is distributed in the hope that it will be useful,
|
2023-11-23 02:34:02 +03:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2023-12-03 23:25:34 +03:00
|
|
|
# along with Compute. If not, see <http://www.gnu.org/licenses/>.
|
2023-11-23 02:34:02 +03:00
|
|
|
|
2023-11-06 12:52:19 +03:00
|
|
|
"""Command line interface."""
|
|
|
|
|
|
|
|
import argparse
|
2023-12-03 23:25:34 +03:00
|
|
|
import json
|
2023-11-06 12:52:19 +03:00
|
|
|
import logging
|
|
|
|
import os
|
2023-12-03 23:25:34 +03:00
|
|
|
import re
|
2023-11-06 12:52:19 +03:00
|
|
|
import shlex
|
2023-12-03 23:25:34 +03:00
|
|
|
import string
|
2023-11-06 12:52:19 +03:00
|
|
|
import sys
|
2023-12-03 23:25:34 +03:00
|
|
|
import uuid
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
import libvirt
|
2023-11-09 01:17:50 +03:00
|
|
|
import yaml
|
|
|
|
from pydantic import ValidationError
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
from compute import __version__
|
2023-12-01 01:39:26 +03:00
|
|
|
from compute.exceptions import ComputeError, GuestAgentTimeoutError
|
2023-12-03 23:25:34 +03:00
|
|
|
from compute.instance import GuestAgent, Instance, InstanceSchema
|
|
|
|
from compute.instance.devices import DiskConfig, DiskDriver
|
2023-11-06 12:52:19 +03:00
|
|
|
from compute.session import Session
|
2023-12-03 23:25:34 +03:00
|
|
|
from compute.utils import dictutil, ids
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2023-11-09 20:32:57 +03:00
|
|
|
log_levels = [lv.lower() for lv in logging.getLevelNamesMapping()]
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
libvirt.registerErrorHandler(
|
|
|
|
lambda userdata, err: None, # noqa: ARG005
|
|
|
|
ctx=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Table:
|
|
|
|
"""Minimalistic text table constructor."""
|
|
|
|
|
|
|
|
def __init__(self, whitespace: str | None = None):
|
|
|
|
"""Initialise Table."""
|
|
|
|
self.whitespace = whitespace or '\t'
|
|
|
|
self.header = []
|
2023-11-09 01:17:50 +03:00
|
|
|
self.rows = []
|
|
|
|
self.table = ''
|
2023-11-06 12:52:19 +03:00
|
|
|
|
2023-11-09 01:17:50 +03:00
|
|
|
def add_row(self, row: list) -> None:
|
2023-11-06 12:52:19 +03:00
|
|
|
"""Add table row."""
|
2023-11-09 01:17:50 +03:00
|
|
|
self.rows.append([str(col) for col in row])
|
2023-11-06 12:52:19 +03:00
|
|
|
|
2023-11-09 01:17:50 +03:00
|
|
|
def add_rows(self, rows: list[list]) -> None:
|
2023-11-06 12:52:19 +03:00
|
|
|
"""Add multiple rows."""
|
|
|
|
for row in rows:
|
2023-11-09 01:17:50 +03:00
|
|
|
self.add_row(row)
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
"""Build table and return."""
|
2023-11-09 01:17:50 +03:00
|
|
|
widths = [max(map(len, col)) for col in zip(*self.rows, strict=True)]
|
|
|
|
self.rows.insert(0, [str(h).upper() for h in self.header])
|
|
|
|
for row in self.rows:
|
|
|
|
self.table += self.whitespace.join(
|
2023-11-06 12:52:19 +03:00
|
|
|
(
|
|
|
|
val.ljust(width)
|
|
|
|
for val, width in zip(row, widths, strict=True)
|
|
|
|
)
|
|
|
|
)
|
2023-11-09 01:17:50 +03:00
|
|
|
self.table += '\n'
|
|
|
|
return self.table.strip()
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
def _list_instances(session: Session) -> None:
|
|
|
|
table = Table()
|
|
|
|
table.header = ['NAME', 'STATE']
|
|
|
|
for instance in session.list_instances():
|
2023-11-09 01:17:50 +03:00
|
|
|
table.add_row(
|
2023-11-06 12:52:19 +03:00
|
|
|
[
|
|
|
|
instance.name,
|
2023-11-09 01:17:50 +03:00
|
|
|
instance.get_status(),
|
2023-11-06 12:52:19 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
print(table)
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
def _exec_guest_agent_command(
|
|
|
|
session: Session, args: argparse.Namespace
|
|
|
|
) -> None:
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
ga = GuestAgent(instance.domain, timeout=args.timeout)
|
|
|
|
arguments = args.arguments.copy()
|
2023-11-09 02:21:42 +03:00
|
|
|
if len(arguments) > 1 and not args.no_join_args:
|
2023-11-06 12:52:19 +03:00
|
|
|
arguments = [shlex.join(arguments)]
|
2023-11-09 02:21:42 +03:00
|
|
|
if not args.no_join_args:
|
2023-11-06 12:52:19 +03:00
|
|
|
arguments.insert(0, '-c')
|
|
|
|
stdin = None
|
|
|
|
if not sys.stdin.isatty():
|
|
|
|
stdin = sys.stdin.read()
|
|
|
|
try:
|
|
|
|
output = ga.guest_exec(
|
2023-11-09 02:21:42 +03:00
|
|
|
path=args.executable,
|
2023-11-06 12:52:19 +03:00
|
|
|
args=arguments,
|
|
|
|
env=args.env,
|
|
|
|
stdin=stdin,
|
|
|
|
capture_output=True,
|
|
|
|
decode_output=True,
|
|
|
|
poll=True,
|
|
|
|
)
|
2023-12-01 01:39:26 +03:00
|
|
|
except GuestAgentTimeoutError as e:
|
2023-11-06 12:52:19 +03:00
|
|
|
sys.exit(
|
|
|
|
f'{e}. NOTE: command may still running in guest, '
|
|
|
|
f'PID={ga.last_pid}'
|
|
|
|
)
|
|
|
|
if output.stderr:
|
|
|
|
print(output.stderr.strip(), file=sys.stderr)
|
|
|
|
if output.stdout:
|
|
|
|
print(output.stdout.strip(), file=sys.stdout)
|
|
|
|
sys.exit(output.exitcode)
|
|
|
|
|
|
|
|
|
2023-12-03 23:25:34 +03:00
|
|
|
def _init_instance(session: Session, args: argparse.Namespace) -> None:
|
2023-11-09 01:17:50 +03:00
|
|
|
try:
|
2023-12-03 23:25:34 +03:00
|
|
|
data = yaml.load(args.file.read(), Loader=yaml.SafeLoader)
|
2023-11-09 01:17:50 +03:00
|
|
|
log.debug('Read from file: %s', data)
|
|
|
|
except yaml.YAMLError as e:
|
|
|
|
sys.exit(f'error: cannot parse YAML: {e}')
|
|
|
|
capabilities = session.get_capabilities()
|
|
|
|
node_info = session.get_node_info()
|
2023-12-03 23:25:34 +03:00
|
|
|
base_instance_config = {
|
|
|
|
'name': str(uuid.uuid4()),
|
|
|
|
'title': None,
|
|
|
|
'description': None,
|
|
|
|
'arch': capabilities.arch,
|
|
|
|
'machine': capabilities.machine,
|
|
|
|
'emulator': capabilities.emulator,
|
|
|
|
'max_vcpus': node_info.cpus,
|
|
|
|
'max_memory': node_info.memory,
|
|
|
|
'cpu': {
|
|
|
|
'emulation_mode': 'host-passthrough',
|
|
|
|
'model': None,
|
|
|
|
'vendor': None,
|
|
|
|
'topology': None,
|
|
|
|
'features': None,
|
|
|
|
},
|
|
|
|
'network_interfaces': [
|
|
|
|
{
|
|
|
|
'source': 'default',
|
|
|
|
'mac': ids.random_mac(),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'boot': {'order': ['cdrom', 'hd']},
|
2023-11-09 01:17:50 +03:00
|
|
|
}
|
2023-12-03 23:25:34 +03:00
|
|
|
data = dictutil.override(base_instance_config, data)
|
|
|
|
volumes = []
|
|
|
|
for volume in data['volumes']:
|
|
|
|
base_disk_config = {
|
|
|
|
'bus': 'virtio',
|
|
|
|
'is_readonly': False,
|
|
|
|
'driver': {
|
|
|
|
'name': 'qemu',
|
|
|
|
'type': 'qcow2',
|
|
|
|
'cache': 'writethrough',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
base_cdrom_config = {
|
|
|
|
'bus': 'ide',
|
|
|
|
'target': 'hda',
|
|
|
|
'is_readonly': True,
|
|
|
|
'driver': {
|
|
|
|
'name': 'qemu',
|
|
|
|
'type': 'raw',
|
|
|
|
'cache': 'writethrough',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if volume.get('device') is None:
|
|
|
|
volume['device'] = 'disk'
|
|
|
|
if volume['device'] == 'disk':
|
|
|
|
volumes.append(dictutil.override(base_disk_config, volume))
|
|
|
|
if volume['device'] == 'cdrom':
|
|
|
|
volumes.append(dictutil.override(base_cdrom_config, volume))
|
|
|
|
data['volumes'] = volumes
|
2023-11-09 01:17:50 +03:00
|
|
|
try:
|
|
|
|
log.debug('Input data: %s', data)
|
2023-12-03 23:25:34 +03:00
|
|
|
if args.test:
|
|
|
|
_ = InstanceSchema(**data)
|
|
|
|
print(json.dumps(dict(data), indent=4, sort_keys=True))
|
|
|
|
sys.exit()
|
|
|
|
instance = session.create_instance(**data)
|
|
|
|
print(f'initialised: {instance.name}')
|
|
|
|
if args.start:
|
|
|
|
instance.start()
|
2023-11-09 01:17:50 +03:00
|
|
|
except ValidationError as e:
|
|
|
|
for error in e.errors():
|
|
|
|
fields = '.'.join([str(lc) for lc in error['loc']])
|
|
|
|
print(
|
|
|
|
f"validation error: {fields}: {error['msg']}",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
2023-11-23 02:34:02 +03:00
|
|
|
def _shutdown_instance(session: Session, args: argparse.Namespace) -> None:
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
if args.soft:
|
|
|
|
method = 'SOFT'
|
|
|
|
elif args.hard:
|
|
|
|
method = 'HARD'
|
|
|
|
elif args.unsafe:
|
|
|
|
method = 'UNSAFE'
|
|
|
|
else:
|
|
|
|
method = 'NORMAL'
|
|
|
|
instance.shutdown(method)
|
|
|
|
|
|
|
|
|
2023-12-03 23:25:34 +03:00
|
|
|
def _confirm(message: str, *, default: bool | None = None) -> None:
|
|
|
|
while True:
|
|
|
|
match default:
|
|
|
|
case True:
|
|
|
|
prompt = 'default: yes'
|
|
|
|
case False:
|
|
|
|
prompt = 'default: no'
|
|
|
|
case _:
|
|
|
|
prompt = 'no default'
|
|
|
|
try:
|
|
|
|
answer = input(f'{message} ({prompt}) ')
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit('aborted')
|
|
|
|
if not answer and isinstance(default, bool):
|
|
|
|
return default
|
|
|
|
if re.match(r'^y(es)?$', answer, re.I):
|
|
|
|
return True
|
|
|
|
if re.match(r'^no?$', answer, re.I):
|
|
|
|
return False
|
|
|
|
print("Please respond 'yes' or 'no'")
|
|
|
|
|
|
|
|
|
|
|
|
def _delete_instance(session: Session, args: argparse.Namespace) -> None:
|
|
|
|
if args.yes is True or _confirm(
|
|
|
|
'this action is irreversible, continue?',
|
|
|
|
default=False,
|
|
|
|
):
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
if args.save_volumes is False:
|
|
|
|
instance.delete(with_volumes=True)
|
|
|
|
else:
|
|
|
|
instance.delete()
|
|
|
|
else:
|
|
|
|
print('aborted')
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
def _get_disk_target(instance: Instance, prefix: str = 'hd') -> str:
|
|
|
|
disks_live = instance.list_disks(persistent=False)
|
|
|
|
disks_inactive = instance.list_disks(persistent=True)
|
|
|
|
disks = [d for d in disks_inactive if d not in disks_live]
|
|
|
|
devs = [d.target[-1] for d in disks if d.target.startswith(prefix)]
|
|
|
|
return prefix + [x for x in string.ascii_lowercase if x not in devs][0] # noqa: RUF015
|
|
|
|
|
|
|
|
|
|
|
|
def _manage_cdrom(session: Session, args: argparse.Namespace) -> None:
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
if args.detach:
|
|
|
|
for disk in instance.list_disks(persistent=True):
|
|
|
|
if disk.device == 'cdrom' and disk.source == args.source:
|
|
|
|
instance.detach_disk(disk.target, live=False)
|
|
|
|
print(
|
|
|
|
f"disk '{disk.target}' detached, "
|
|
|
|
'perform power reset to apply changes'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
target = _get_disk_target(instance, 'hd')
|
|
|
|
cdrom = DiskConfig(
|
|
|
|
type='file',
|
|
|
|
device='cdrom',
|
|
|
|
source=args.source,
|
|
|
|
target=target,
|
|
|
|
is_readonly=True,
|
|
|
|
bus='ide',
|
|
|
|
driver=DiskDriver('qemu', 'raw', 'writethrough'),
|
|
|
|
)
|
|
|
|
instance.attach_device(cdrom, live=False)
|
|
|
|
print(
|
|
|
|
f"CDROM attached as disk '{target}', "
|
|
|
|
'perform power reset to apply changes'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-06 12:52:19 +03:00
|
|
|
def main(session: Session, args: argparse.Namespace) -> None:
|
|
|
|
"""Perform actions."""
|
|
|
|
match args.command:
|
2023-11-23 02:34:02 +03:00
|
|
|
case 'init':
|
2023-12-03 23:25:34 +03:00
|
|
|
_init_instance(session, args)
|
2023-11-06 12:52:19 +03:00
|
|
|
case 'exec':
|
|
|
|
_exec_guest_agent_command(session, args)
|
|
|
|
case 'ls':
|
|
|
|
_list_instances(session)
|
|
|
|
case 'start':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.start()
|
|
|
|
case 'shutdown':
|
2023-11-23 02:34:02 +03:00
|
|
|
_shutdown_instance(session, args)
|
2023-11-06 12:52:19 +03:00
|
|
|
case 'reboot':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.reboot()
|
|
|
|
case 'reset':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.reset()
|
2023-11-23 02:34:02 +03:00
|
|
|
case 'powrst':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.power_reset()
|
2023-11-09 22:35:19 +03:00
|
|
|
case 'pause':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.pause()
|
|
|
|
case 'resume':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.resume()
|
2023-11-06 12:52:19 +03:00
|
|
|
case 'status':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
print(instance.status)
|
|
|
|
case 'setvcpus':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.set_vcpus(args.nvcpus, live=True)
|
2023-11-09 22:35:19 +03:00
|
|
|
case 'setmem':
|
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.set_memory(args.memory, live=True)
|
2023-11-23 02:34:02 +03:00
|
|
|
case 'setpass':
|
2023-11-11 02:28:46 +03:00
|
|
|
instance = session.get_instance(args.instance)
|
|
|
|
instance.set_user_password(
|
|
|
|
args.username,
|
|
|
|
args.password,
|
|
|
|
encrypted=args.encrypted,
|
|
|
|
)
|
2023-12-03 23:25:34 +03:00
|
|
|
case 'setcdrom':
|
|
|
|
_manage_cdrom(session, args)
|
|
|
|
case 'delete':
|
|
|
|
_delete_instance(session, args)
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
|
2023-12-03 23:25:34 +03:00
|
|
|
def get_parser() -> argparse.ArgumentParser: # noqa: PLR0915
|
2023-11-11 02:28:46 +03:00
|
|
|
"""Return command line arguments parser."""
|
2023-11-06 12:52:19 +03:00
|
|
|
root = argparse.ArgumentParser(
|
|
|
|
prog='compute',
|
2023-12-03 23:25:34 +03:00
|
|
|
description='Manage compute instances.',
|
2023-11-06 12:52:19 +03:00
|
|
|
formatter_class=argparse.RawTextHelpFormatter,
|
|
|
|
)
|
|
|
|
root.add_argument(
|
|
|
|
'-v',
|
|
|
|
'--verbose',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='enable verbose mode',
|
|
|
|
)
|
|
|
|
root.add_argument(
|
|
|
|
'-c',
|
|
|
|
'--connect',
|
|
|
|
metavar='URI',
|
|
|
|
help='libvirt connection URI',
|
|
|
|
)
|
|
|
|
root.add_argument(
|
|
|
|
'-l',
|
|
|
|
'--log-level',
|
2023-11-09 20:32:57 +03:00
|
|
|
type=str.lower,
|
2023-11-06 12:52:19 +03:00
|
|
|
metavar='LEVEL',
|
|
|
|
choices=log_levels,
|
2023-11-23 02:34:02 +03:00
|
|
|
help='log level',
|
2023-11-06 12:52:19 +03:00
|
|
|
)
|
|
|
|
root.add_argument(
|
|
|
|
'-V',
|
|
|
|
'--version',
|
|
|
|
action='version',
|
|
|
|
version=__version__,
|
|
|
|
)
|
|
|
|
subparsers = root.add_subparsers(dest='command', metavar='COMMAND')
|
|
|
|
|
2023-11-23 02:34:02 +03:00
|
|
|
# init command
|
|
|
|
init = subparsers.add_parser(
|
|
|
|
'init', help='initialise instance using YAML config file'
|
2023-11-09 01:17:50 +03:00
|
|
|
)
|
2023-11-23 02:34:02 +03:00
|
|
|
init.add_argument(
|
2023-11-09 01:17:50 +03:00
|
|
|
'file',
|
|
|
|
type=argparse.FileType('r', encoding='UTF-8'),
|
2023-11-23 02:34:02 +03:00
|
|
|
nargs='?',
|
|
|
|
default='instance.yaml',
|
|
|
|
help='instance config [default: instance.yaml]',
|
2023-11-06 12:52:19 +03:00
|
|
|
)
|
2023-12-03 23:25:34 +03:00
|
|
|
init.add_argument(
|
|
|
|
'-s',
|
|
|
|
'--start',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='start instance after init',
|
|
|
|
)
|
|
|
|
init.add_argument(
|
|
|
|
'-t',
|
|
|
|
'--test',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='just print resulting instance config as JSON and exit',
|
|
|
|
)
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
# exec subcommand
|
|
|
|
execute = subparsers.add_parser(
|
|
|
|
'exec',
|
|
|
|
help='execute command in guest via guest agent',
|
|
|
|
description=(
|
2023-12-03 23:25:34 +03:00
|
|
|
'Execute command in guest via guest agent. '
|
2023-11-06 12:52:19 +03:00
|
|
|
'NOTE: any argument after instance name will be passed into '
|
|
|
|
'guest as shell command.'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
execute.add_argument('instance')
|
|
|
|
execute.add_argument('arguments', nargs=argparse.REMAINDER)
|
|
|
|
execute.add_argument(
|
|
|
|
'-t',
|
|
|
|
'--timeout',
|
|
|
|
type=int,
|
|
|
|
default=60,
|
|
|
|
help=(
|
|
|
|
'waiting time in seconds for a command to be executed '
|
2023-11-23 02:34:02 +03:00
|
|
|
'in guest [default: 60]'
|
2023-11-06 12:52:19 +03:00
|
|
|
),
|
|
|
|
)
|
|
|
|
execute.add_argument(
|
2023-11-09 02:21:42 +03:00
|
|
|
'-x',
|
|
|
|
'--executable',
|
2023-11-06 12:52:19 +03:00
|
|
|
default='/bin/sh',
|
2023-11-23 02:34:02 +03:00
|
|
|
help='path to executable in guest [default: /bin/sh]',
|
2023-11-06 12:52:19 +03:00
|
|
|
)
|
|
|
|
execute.add_argument(
|
|
|
|
'-e',
|
|
|
|
'--env',
|
|
|
|
type=str,
|
|
|
|
nargs='?',
|
|
|
|
action='append',
|
|
|
|
help='environment variables to pass to executable in guest',
|
|
|
|
)
|
|
|
|
execute.add_argument(
|
|
|
|
'-n',
|
2023-11-09 02:21:42 +03:00
|
|
|
'--no-join-args',
|
2023-11-06 12:52:19 +03:00
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help=(
|
2023-11-09 02:21:42 +03:00
|
|
|
"do not join arguments list and add '-c' option, suitable "
|
2023-11-06 12:52:19 +03:00
|
|
|
'for non-shell executables and other specific cases.'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# ls subcommand
|
|
|
|
listall = subparsers.add_parser('ls', help='list instances')
|
|
|
|
listall.add_argument(
|
|
|
|
'-a',
|
|
|
|
'--all',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='list all instances including inactive',
|
|
|
|
)
|
|
|
|
|
|
|
|
# start subcommand
|
|
|
|
start = subparsers.add_parser('start', help='start instance')
|
|
|
|
start.add_argument('instance')
|
|
|
|
|
|
|
|
# shutdown subcommand
|
|
|
|
shutdown = subparsers.add_parser('shutdown', help='shutdown instance')
|
|
|
|
shutdown.add_argument('instance')
|
2023-11-23 02:34:02 +03:00
|
|
|
shutdown_opts = shutdown.add_mutually_exclusive_group()
|
|
|
|
shutdown_opts.add_argument(
|
|
|
|
'-s',
|
|
|
|
'--soft',
|
|
|
|
action='store_true',
|
|
|
|
help='normal guest OS shutdown, guest agent is used',
|
|
|
|
)
|
|
|
|
shutdown_opts.add_argument(
|
|
|
|
'-n',
|
|
|
|
'--normal',
|
|
|
|
action='store_true',
|
|
|
|
help='shutdown with hypervisor selected method [default]',
|
|
|
|
)
|
|
|
|
shutdown_opts.add_argument(
|
|
|
|
'-H',
|
|
|
|
'--hard',
|
|
|
|
action='store_true',
|
|
|
|
help=(
|
|
|
|
"gracefully destroy instance, it's like long "
|
|
|
|
'pressing the power button'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
shutdown_opts.add_argument(
|
|
|
|
'-u',
|
|
|
|
'--unsafe',
|
|
|
|
action='store_true',
|
|
|
|
help=(
|
|
|
|
'destroy instance, this is similar to a power outage '
|
|
|
|
'and may result in data loss or corruption'
|
|
|
|
),
|
2023-11-06 12:52:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# reboot subcommand
|
|
|
|
reboot = subparsers.add_parser('reboot', help='reboot instance')
|
|
|
|
reboot.add_argument('instance')
|
|
|
|
|
|
|
|
# reset subcommand
|
|
|
|
reset = subparsers.add_parser('reset', help='reset instance')
|
|
|
|
reset.add_argument('instance')
|
|
|
|
|
2023-11-23 02:34:02 +03:00
|
|
|
# powrst subcommand
|
|
|
|
powrst = subparsers.add_parser('powrst', help='power reset instance')
|
|
|
|
powrst.add_argument('instance')
|
|
|
|
|
2023-11-09 22:35:19 +03:00
|
|
|
# pause subcommand
|
|
|
|
pause = subparsers.add_parser('pause', help='pause instance')
|
|
|
|
pause.add_argument('instance')
|
|
|
|
|
|
|
|
# resume subcommand
|
|
|
|
resume = subparsers.add_parser('resume', help='resume paused instance')
|
|
|
|
resume.add_argument('instance')
|
|
|
|
|
2023-11-06 12:52:19 +03:00
|
|
|
# status subcommand
|
|
|
|
status = subparsers.add_parser('status', help='display instance status')
|
|
|
|
status.add_argument('instance')
|
|
|
|
|
|
|
|
# setvcpus subcommand
|
|
|
|
setvcpus = subparsers.add_parser('setvcpus', help='set vCPU number')
|
|
|
|
setvcpus.add_argument('instance')
|
|
|
|
setvcpus.add_argument('nvcpus', type=int)
|
|
|
|
|
2023-11-09 22:35:19 +03:00
|
|
|
# setmem subcommand
|
|
|
|
setmem = subparsers.add_parser('setmem', help='set memory size')
|
|
|
|
setmem.add_argument('instance')
|
|
|
|
setmem.add_argument('memory', type=int, help='memory in MiB')
|
|
|
|
|
2023-11-23 02:34:02 +03:00
|
|
|
# setpass subcommand
|
|
|
|
setpass = subparsers.add_parser(
|
|
|
|
'setpass',
|
2023-11-11 02:28:46 +03:00
|
|
|
help='set user password in guest',
|
|
|
|
)
|
2023-11-23 02:34:02 +03:00
|
|
|
setpass.add_argument('instance')
|
|
|
|
setpass.add_argument('username')
|
|
|
|
setpass.add_argument('password')
|
|
|
|
setpass.add_argument(
|
2023-11-11 02:28:46 +03:00
|
|
|
'-e',
|
|
|
|
'--encrypted',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='set it if password is already encrypted',
|
|
|
|
)
|
|
|
|
|
2023-12-03 23:25:34 +03:00
|
|
|
# setcdrom subcommand
|
|
|
|
setcdrom = subparsers.add_parser('setcdrom', help='manage CDROM devices')
|
|
|
|
setcdrom.add_argument('instance')
|
|
|
|
setcdrom.add_argument('source', help='source for CDROM')
|
|
|
|
setcdrom.add_argument(
|
|
|
|
'-d',
|
|
|
|
'--detach',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='detach CDROM device',
|
|
|
|
)
|
|
|
|
|
|
|
|
# delete subcommand
|
|
|
|
delete = subparsers.add_parser(
|
|
|
|
'delete',
|
|
|
|
help='delete instance',
|
|
|
|
)
|
|
|
|
delete.add_argument('instance')
|
|
|
|
delete.add_argument(
|
|
|
|
'-y',
|
|
|
|
'--yes',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='automatic yes to prompt',
|
|
|
|
)
|
|
|
|
delete.add_argument(
|
|
|
|
'--save-volumes',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='do not delete local storage volumes',
|
|
|
|
)
|
|
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
|
|
def cli() -> None:
|
|
|
|
"""Run arguments parser."""
|
|
|
|
root = get_parser()
|
2023-11-06 12:52:19 +03:00
|
|
|
args = root.parse_args()
|
|
|
|
if args.command is None:
|
|
|
|
root.print_help()
|
|
|
|
sys.exit()
|
2023-11-09 20:32:57 +03:00
|
|
|
log_level = args.log_level or os.getenv('CMP_LOG')
|
|
|
|
if isinstance(log_level, str) and log_level.lower() in log_levels:
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.getLevelNamesMapping()[log_level.upper()]
|
|
|
|
)
|
2023-11-09 01:17:50 +03:00
|
|
|
log.debug('CLI started with args: %s', args)
|
2023-11-23 02:34:02 +03:00
|
|
|
connect_uri = (
|
|
|
|
args.connect
|
|
|
|
or os.getenv('CMP_LIBVIRT_URI')
|
|
|
|
or os.getenv('LIBVIRT_DEFAULT_URI')
|
|
|
|
or 'qemu:///system'
|
|
|
|
)
|
2023-11-06 12:52:19 +03:00
|
|
|
try:
|
2023-11-23 02:34:02 +03:00
|
|
|
with Session(connect_uri) as session:
|
2023-11-06 12:52:19 +03:00
|
|
|
main(session, args)
|
2023-11-23 02:34:02 +03:00
|
|
|
except ComputeError as e:
|
2023-11-06 12:52:19 +03:00
|
|
|
sys.exit(f'error: {e}')
|
2023-11-09 01:17:50 +03:00
|
|
|
except KeyboardInterrupt:
|
2023-11-06 12:52:19 +03:00
|
|
|
sys.exit()
|
2023-11-09 01:17:50 +03:00
|
|
|
except SystemExit as e:
|
|
|
|
sys.exit(e)
|
2023-11-06 12:52:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
cli()
|