2017-02-27 23:50:36 +01:00
|
|
|
# encoding=utf-8
|
2017-02-27 23:39:25 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
2017-03-08 21:53:46 +01:00
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2.1 of the License, or
|
2017-02-27 23:39:25 +01:00
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-03-08 21:53:46 +01:00
|
|
|
# GNU Lesser General Public License for more details.
|
2017-02-27 23:39:25 +01:00
|
|
|
#
|
2017-03-08 21:53:46 +01:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
'''Qubes' command line tools
|
|
|
|
'''
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import importlib
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import textwrap
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.log
|
|
|
|
import qubesadmin.exc
|
|
|
|
import qubesadmin.vm
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
#: constant returned when some action should be performed on all qubes
|
|
|
|
VM_ALL = object()
|
|
|
|
|
|
|
|
|
|
|
|
class QubesAction(argparse.Action):
|
|
|
|
''' Interface providing a convinience method to be called, after
|
|
|
|
`namespace.app` is instantiated.
|
|
|
|
'''
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
def parse_qubes_app(self, parser, namespace):
|
|
|
|
''' This method is called by :py:class:`qubes.tools.QubesArgumentParser`
|
|
|
|
after the `namespace.app` is instantiated. Oerwrite this method when
|
|
|
|
extending :py:class:`qubes.tools.QubesAction` to initialized values
|
|
|
|
based on the `namespace.app`
|
|
|
|
'''
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class PropertyAction(argparse.Action):
|
|
|
|
'''Action for argument parser that stores a property.'''
|
|
|
|
# pylint: disable=redefined-builtin,too-few-public-methods
|
|
|
|
def __init__(self,
|
|
|
|
option_strings,
|
|
|
|
dest,
|
|
|
|
metavar='NAME=VALUE',
|
|
|
|
required=False,
|
|
|
|
help='set property to a value'):
|
|
|
|
super(PropertyAction, self).__init__(option_strings, 'properties',
|
|
|
|
metavar=metavar, default={}, help=help)
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
try:
|
|
|
|
prop, value = values.split('=', 1)
|
|
|
|
except ValueError:
|
|
|
|
parser.error('invalid property token: {!r}'.format(values))
|
|
|
|
|
2017-04-28 02:04:54 +02:00
|
|
|
properties = getattr(namespace, self.dest)
|
|
|
|
# copy it, to not modify _mutable_ self.default
|
|
|
|
if not properties:
|
|
|
|
properties = properties.copy()
|
|
|
|
properties[prop] = value
|
|
|
|
setattr(namespace, self.dest, properties)
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SinglePropertyAction(argparse.Action):
|
|
|
|
'''Action for argument parser that stores a property.'''
|
|
|
|
|
|
|
|
# pylint: disable=redefined-builtin,too-few-public-methods
|
|
|
|
def __init__(self,
|
|
|
|
option_strings,
|
|
|
|
dest,
|
|
|
|
metavar='VALUE',
|
|
|
|
const=None,
|
|
|
|
nargs=None,
|
|
|
|
required=False,
|
|
|
|
help=None):
|
|
|
|
if help is None:
|
|
|
|
help = 'set {!r} property to a value'.format(dest)
|
|
|
|
if const is not None:
|
|
|
|
help += ' {!r}'.format(const)
|
|
|
|
|
|
|
|
if const is not None:
|
|
|
|
nargs = 0
|
|
|
|
|
|
|
|
super(SinglePropertyAction, self).__init__(option_strings, 'properties',
|
|
|
|
metavar=metavar, help=help, default={}, const=const,
|
|
|
|
nargs=nargs)
|
|
|
|
|
|
|
|
self.name = dest
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
2017-04-28 02:07:12 +02:00
|
|
|
if values is self.default and self.default == {}:
|
|
|
|
return
|
|
|
|
|
2017-04-28 02:04:54 +02:00
|
|
|
properties = getattr(namespace, self.dest)
|
|
|
|
# copy it, to not modify _mutable_ self.default
|
|
|
|
if not properties:
|
|
|
|
properties = properties.copy()
|
|
|
|
properties[self.name] = values \
|
2017-02-27 23:39:25 +01:00
|
|
|
if self.const is None else self.const
|
2017-04-28 02:04:54 +02:00
|
|
|
setattr(namespace, self.dest, properties)
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class VmNameAction(QubesAction):
|
|
|
|
''' Action for parsing one ore multiple domains from provided VMNAMEs '''
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods,redefined-builtin
|
|
|
|
def __init__(self, option_strings, nargs=1, dest='vmnames', help=None,
|
|
|
|
**kwargs):
|
|
|
|
if help is None:
|
|
|
|
if nargs == argparse.OPTIONAL:
|
|
|
|
help = 'at most one domain name'
|
|
|
|
elif nargs == 1:
|
|
|
|
help = 'a domain name'
|
|
|
|
elif nargs == argparse.ZERO_OR_MORE:
|
|
|
|
help = 'zero or more domain names'
|
|
|
|
elif nargs == argparse.ONE_OR_MORE:
|
|
|
|
help = 'one or more domain names'
|
|
|
|
elif nargs > 1:
|
|
|
|
help = '%s domain names' % nargs
|
|
|
|
else:
|
|
|
|
raise argparse.ArgumentError(
|
|
|
|
nargs, "Passed unexpected value {!s} as {!s} nargs ".format(
|
|
|
|
nargs, dest))
|
|
|
|
|
|
|
|
super(VmNameAction, self).__init__(option_strings, dest=dest, help=help,
|
|
|
|
nargs=nargs, **kwargs)
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
''' Set ``namespace.vmname`` to ``values`` '''
|
|
|
|
setattr(namespace, self.dest, values)
|
|
|
|
|
|
|
|
def parse_qubes_app(self, parser, namespace):
|
|
|
|
assert hasattr(namespace, 'app')
|
|
|
|
setattr(namespace, 'domains', [])
|
|
|
|
app = namespace.app
|
|
|
|
if hasattr(namespace, 'all_domains') and namespace.all_domains:
|
|
|
|
namespace.domains = [
|
|
|
|
vm
|
|
|
|
for vm in app.domains
|
2017-10-02 20:43:01 +02:00
|
|
|
if not vm.klass == 'AdminVM' and
|
2017-04-15 14:57:30 +02:00
|
|
|
vm.name not in namespace.exclude
|
2017-02-27 23:39:25 +01:00
|
|
|
]
|
|
|
|
else:
|
|
|
|
if hasattr(namespace, 'exclude') and namespace.exclude:
|
|
|
|
parser.error('--exclude can only be used with --all')
|
|
|
|
|
2017-05-18 09:50:25 +02:00
|
|
|
if self.nargs == argparse.OPTIONAL:
|
|
|
|
vm_name = getattr(namespace, self.dest, None)
|
|
|
|
if vm_name is not None:
|
|
|
|
try:
|
|
|
|
namespace.domains += [app.domains[vm_name]]
|
|
|
|
except KeyError:
|
|
|
|
parser.error('no such domain: {!r}'.format(vm_name))
|
|
|
|
else:
|
|
|
|
for vm_name in getattr(namespace, self.dest):
|
|
|
|
try:
|
|
|
|
namespace.domains += [app.domains[vm_name]]
|
|
|
|
except KeyError:
|
|
|
|
parser.error('no such domain: {!r}'.format(vm_name))
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class RunningVmNameAction(VmNameAction):
|
|
|
|
''' Action for argument parser that gets a running domain from VMNAME '''
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
def __init__(self, option_strings, nargs=1, dest='vmnames', help=None,
|
|
|
|
**kwargs):
|
|
|
|
# pylint: disable=redefined-builtin
|
|
|
|
if help is None:
|
|
|
|
if nargs == argparse.OPTIONAL:
|
|
|
|
help = 'at most one running domain'
|
|
|
|
elif nargs == 1:
|
|
|
|
help = 'running domain name'
|
|
|
|
elif nargs == argparse.ZERO_OR_MORE:
|
|
|
|
help = 'zero or more running domains'
|
|
|
|
elif nargs == argparse.ONE_OR_MORE:
|
|
|
|
help = 'one or more running domains'
|
|
|
|
elif nargs > 1:
|
|
|
|
help = '%s running domains' % nargs
|
|
|
|
else:
|
|
|
|
raise argparse.ArgumentError(
|
|
|
|
nargs, "Passed unexpected value {!s} as {!s} nargs ".format(
|
|
|
|
nargs, dest))
|
|
|
|
super(RunningVmNameAction, self).__init__(
|
|
|
|
option_strings, dest=dest, help=help, nargs=nargs, **kwargs)
|
|
|
|
|
|
|
|
def parse_qubes_app(self, parser, namespace):
|
|
|
|
super(RunningVmNameAction, self).parse_qubes_app(parser, namespace)
|
|
|
|
for vm in namespace.domains:
|
|
|
|
if not vm.is_running():
|
|
|
|
parser.error_runtime("domain {!r} is not running".format(
|
|
|
|
vm.name))
|
|
|
|
|
|
|
|
|
|
|
|
class VolumeAction(QubesAction):
|
|
|
|
''' Action for argument parser that gets the
|
|
|
|
:py:class:``qubes.storage.Volume`` from a POOL_NAME:VOLUME_ID string.
|
|
|
|
'''
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
def __init__(self, help='A pool & volume id combination',
|
|
|
|
required=True, **kwargs):
|
|
|
|
# pylint: disable=redefined-builtin
|
|
|
|
super(VolumeAction, self).__init__(help=help, required=required,
|
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
''' Set ``namespace.vmname`` to ``values`` '''
|
|
|
|
setattr(namespace, self.dest, values)
|
|
|
|
|
|
|
|
def parse_qubes_app(self, parser, namespace):
|
|
|
|
''' Acquire the :py:class:``qubes.storage.Volume`` object from
|
|
|
|
``namespace.app``.
|
|
|
|
'''
|
|
|
|
assert hasattr(namespace, 'app')
|
|
|
|
app = namespace.app
|
|
|
|
|
|
|
|
try:
|
|
|
|
pool_name, vid = getattr(namespace, self.dest).split(':')
|
|
|
|
try:
|
|
|
|
pool = app.pools[pool_name]
|
|
|
|
volume = [v for v in pool.volumes if v.vid == vid]
|
|
|
|
assert volume > 1, 'Duplicate vids in pool %s' % pool_name
|
2017-04-21 02:47:23 +02:00
|
|
|
if not volume:
|
2017-02-27 23:39:25 +01:00
|
|
|
parser.error_runtime(
|
|
|
|
'no volume with id {!r} pool: {!r}'.format(vid,
|
|
|
|
pool_name))
|
|
|
|
else:
|
|
|
|
setattr(namespace, self.dest, volume[0])
|
|
|
|
except KeyError:
|
|
|
|
parser.error_runtime('no pool {!r}'.format(pool_name))
|
|
|
|
except ValueError:
|
|
|
|
parser.error('expected a pool & volume id combination like foo:bar')
|
|
|
|
|
|
|
|
|
2017-03-13 04:33:12 +01:00
|
|
|
class VMVolumeAction(QubesAction):
|
|
|
|
''' Action for argument parser that gets the
|
|
|
|
:py:class:``qubes.storage.Volume`` from a VM:VOLUME string.
|
|
|
|
'''
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
def __init__(self, help='A pool & volume id combination',
|
|
|
|
required=True, **kwargs):
|
|
|
|
# pylint: disable=redefined-builtin
|
|
|
|
super(VMVolumeAction, self).__init__(help=help, required=required,
|
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
''' Set ``namespace.vmname`` to ``values`` '''
|
|
|
|
setattr(namespace, self.dest, values)
|
|
|
|
|
|
|
|
def parse_qubes_app(self, parser, namespace):
|
|
|
|
''' Acquire the :py:class:``qubes.storage.Volume`` object from
|
|
|
|
``namespace.app``.
|
|
|
|
'''
|
|
|
|
assert hasattr(namespace, 'app')
|
|
|
|
app = namespace.app
|
|
|
|
|
|
|
|
try:
|
|
|
|
vm_name, vol_name = getattr(namespace, self.dest).split(':')
|
|
|
|
try:
|
|
|
|
vm = app.domains[vm_name]
|
|
|
|
try:
|
|
|
|
volume = vm.volumes[vol_name]
|
|
|
|
setattr(namespace, self.dest, volume)
|
|
|
|
except KeyError:
|
|
|
|
parser.error_runtime('vm {!r} has no volume {!r}'.format(
|
|
|
|
vm_name, vol_name))
|
|
|
|
except KeyError:
|
|
|
|
parser.error_runtime('no vm {!r}'.format(vm_name))
|
|
|
|
except ValueError:
|
|
|
|
parser.error('expected a vm & volume combination like foo:bar')
|
|
|
|
|
|
|
|
|
2017-02-27 23:39:25 +01:00
|
|
|
class PoolsAction(QubesAction):
|
|
|
|
''' Action for argument parser to gather multiple pools '''
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
''' Set ``namespace.vmname`` to ``values`` '''
|
|
|
|
if hasattr(namespace, self.dest) and getattr(namespace, self.dest):
|
|
|
|
names = getattr(namespace, self.dest)
|
|
|
|
else:
|
|
|
|
names = []
|
|
|
|
names += [values]
|
|
|
|
setattr(namespace, self.dest, names)
|
|
|
|
|
|
|
|
def parse_qubes_app(self, parser, namespace):
|
|
|
|
app = namespace.app
|
|
|
|
pool_names = getattr(namespace, self.dest)
|
|
|
|
if pool_names:
|
|
|
|
try:
|
2017-03-13 04:33:12 +01:00
|
|
|
pools = [app.pools[name] for name in pool_names]
|
2017-02-27 23:39:25 +01:00
|
|
|
setattr(namespace, self.dest, pools)
|
2017-05-11 23:21:04 +02:00
|
|
|
except qubesadmin.exc.QubesException as e:
|
2017-02-27 23:39:25 +01:00
|
|
|
parser.error(str(e))
|
|
|
|
sys.exit(2)
|
2018-04-12 23:19:34 +02:00
|
|
|
except KeyError:
|
|
|
|
parser.error('No such pools: %s' % pool_names)
|
|
|
|
sys.exit(2)
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class QubesArgumentParser(argparse.ArgumentParser):
|
|
|
|
'''Parser preconfigured for use in most of the Qubes command-line tools.
|
|
|
|
|
|
|
|
:param bool want_app: instantiate :py:class:`qubes.Qubes` object
|
|
|
|
:param bool want_app_no_instance: don't actually instantiate \
|
|
|
|
:py:class:`qubes.Qubes` object, just add argument for custom xml file
|
|
|
|
:param mixed vmname_nargs: The number of ``VMNAME`` arguments that should be
|
2018-12-08 23:53:55 +01:00
|
|
|
consumed. Values include:
|
|
|
|
* N (an integer) consumes N arguments (and produces a list)
|
|
|
|
* '?' consumes zero or one arguments
|
|
|
|
* '*' consumes zero or more arguments (and produces a list)
|
|
|
|
* '+' consumes one or more arguments (and produces a list)
|
|
|
|
|
2017-02-27 23:39:25 +01:00
|
|
|
*kwargs* are passed to :py:class:`argparser.ArgumentParser`.
|
|
|
|
|
|
|
|
Currenty supported options:
|
2017-03-08 23:28:20 +01:00
|
|
|
``--force-root`` (optional, ignored, help is suppressed)
|
2017-02-27 23:39:25 +01:00
|
|
|
``--offline-mode`` do not talk to hypervisor (help is suppressed)
|
|
|
|
``--verbose`` and ``--quiet``
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, want_app=True, want_app_no_instance=False,
|
2017-03-08 23:28:20 +01:00
|
|
|
vmname_nargs=None, **kwargs):
|
2017-02-27 23:39:25 +01:00
|
|
|
|
2018-03-18 20:47:00 +01:00
|
|
|
super(QubesArgumentParser, self).__init__(add_help=False, **kwargs)
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
self._want_app = want_app
|
|
|
|
self._want_app_no_instance = want_app_no_instance
|
|
|
|
self._vmname_nargs = vmname_nargs
|
|
|
|
if self._want_app:
|
|
|
|
self.add_argument('--qubesxml', metavar='FILE', action='store',
|
|
|
|
dest='app', help=argparse.SUPPRESS)
|
|
|
|
self.add_argument('--offline-mode', action='store_true',
|
|
|
|
default=None, dest='offline_mode', help=argparse.SUPPRESS)
|
|
|
|
|
|
|
|
|
|
|
|
self.add_argument('--verbose', '-v', action='count',
|
|
|
|
help='increase verbosity')
|
|
|
|
|
|
|
|
self.add_argument('--quiet', '-q', action='count',
|
|
|
|
help='decrease verbosity')
|
|
|
|
|
2017-03-08 23:28:20 +01:00
|
|
|
self.add_argument('--force-root', action='store_true',
|
|
|
|
default=False, help=argparse.SUPPRESS)
|
2017-02-27 23:39:25 +01:00
|
|
|
|
2018-03-18 20:47:00 +01:00
|
|
|
self.add_argument('--help', '-h', action=SubParsersHelpAction,
|
|
|
|
help='show this help message and exit')
|
|
|
|
|
2017-02-27 23:39:25 +01:00
|
|
|
if self._vmname_nargs in [argparse.ZERO_OR_MORE, argparse.ONE_OR_MORE]:
|
2017-12-22 23:03:26 +01:00
|
|
|
vm_name_group = VmNameGroup(self,
|
|
|
|
required=(self._vmname_nargs
|
|
|
|
not in [argparse.ZERO_OR_MORE, argparse.OPTIONAL]))
|
2017-02-27 23:39:25 +01:00
|
|
|
self._mutually_exclusive_groups.append(vm_name_group)
|
|
|
|
elif self._vmname_nargs is not None:
|
|
|
|
self.add_argument('VMNAME', nargs=self._vmname_nargs,
|
|
|
|
action=VmNameAction)
|
|
|
|
|
|
|
|
self.set_defaults(verbose=1, quiet=0)
|
|
|
|
|
2017-04-21 02:47:23 +02:00
|
|
|
def parse_args(self, *args, **kwargs): # pylint: disable=arguments-differ
|
2017-03-08 23:41:39 +01:00
|
|
|
# hack for tests
|
|
|
|
app = kwargs.pop('app', None)
|
2017-02-27 23:39:25 +01:00
|
|
|
namespace = super(QubesArgumentParser, self).parse_args(*args, **kwargs)
|
|
|
|
|
|
|
|
if self._want_app and not self._want_app_no_instance:
|
|
|
|
self.set_qubes_verbosity(namespace)
|
2017-03-08 23:41:39 +01:00
|
|
|
if app is not None:
|
|
|
|
namespace.app = app
|
|
|
|
else:
|
2017-05-11 23:21:04 +02:00
|
|
|
namespace.app = qubesadmin.Qubes()
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
for action in self._actions:
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
if issubclass(action.__class__, QubesAction):
|
|
|
|
action.parse_qubes_app(self, namespace)
|
|
|
|
elif issubclass(action.__class__,
|
|
|
|
argparse._SubParsersAction): # pylint: disable=no-member
|
|
|
|
assert hasattr(namespace, 'command')
|
|
|
|
command = namespace.command
|
2017-05-30 01:39:35 +02:00
|
|
|
if command is None:
|
|
|
|
continue
|
2017-02-27 23:39:25 +01:00
|
|
|
subparser = action._name_parser_map[command]
|
|
|
|
for subaction in subparser._actions:
|
|
|
|
if issubclass(subaction.__class__, QubesAction):
|
|
|
|
subaction.parse_qubes_app(self, namespace)
|
|
|
|
|
|
|
|
return namespace
|
|
|
|
|
|
|
|
|
2020-01-16 12:31:55 +01:00
|
|
|
def error_runtime(self, message, exit_code=1):
|
2017-02-27 23:39:25 +01:00
|
|
|
'''Runtime error, without showing usage.
|
|
|
|
|
|
|
|
:param str message: message to show
|
|
|
|
'''
|
2020-01-16 12:31:55 +01:00
|
|
|
self.exit(exit_code, '{}: error: {}\n'.format(self.prog, message))
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_loglevel_from_verbosity(namespace):
|
|
|
|
''' Return loglevel calculated from quiet and verbose arguments '''
|
|
|
|
return (namespace.quiet - namespace.verbose) * 10 + logging.WARNING
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_qubes_verbosity(namespace):
|
|
|
|
'''Apply a verbosity setting.
|
|
|
|
|
|
|
|
This is done by configuring global logging.
|
|
|
|
:param argparse.Namespace args: args as parsed by parser
|
|
|
|
'''
|
|
|
|
|
|
|
|
verbose = namespace.verbose - namespace.quiet
|
|
|
|
|
|
|
|
if verbose >= 2:
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.log.enable_debug()
|
2017-02-27 23:39:25 +01:00
|
|
|
elif verbose >= 1:
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.log.enable()
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
def print_error(self, *args, **kwargs):
|
|
|
|
''' Print to ``sys.stderr``'''
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
|
|
|
|
2018-03-18 20:47:00 +01:00
|
|
|
class SubParsersHelpAction(argparse._HelpAction):
|
2018-12-08 23:53:55 +01:00
|
|
|
''' Print help for all options and all subparsers '''
|
2018-03-18 20:47:00 +01:00
|
|
|
# source https://stackoverflow.com/a/24122778
|
|
|
|
# pylint: disable=protected-access,too-few-public-methods
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _indent(indent, text):
|
|
|
|
'''Indent *text* by *indent* spaces'''
|
|
|
|
return '\n'.join((' ' * indent) + l for l in text.splitlines())
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
parser.print_help()
|
|
|
|
|
|
|
|
# retrieve subparsers from parser
|
|
|
|
subparsers_actions = [
|
|
|
|
action for action in parser._actions
|
|
|
|
if isinstance(action, argparse._SubParsersAction)]
|
|
|
|
# there will probably only be one subparser_action,
|
|
|
|
# but better save than sorry
|
|
|
|
for subparsers_action in subparsers_actions:
|
|
|
|
# get all subparsers and print help
|
|
|
|
for pseudo_action in subparsers_action._choices_actions:
|
|
|
|
choice = pseudo_action.dest.split(' ', 1)[0]
|
|
|
|
subparser = subparsers_action.choices[choice]
|
|
|
|
print("\nCommand '{}':".format(choice))
|
|
|
|
choice_help = subparser.format_usage()
|
|
|
|
choice_help = self._indent(2, choice_help)
|
|
|
|
print(choice_help)
|
|
|
|
parser.exit()
|
|
|
|
|
|
|
|
|
2017-02-27 23:39:25 +01:00
|
|
|
class AliasedSubParsersAction(argparse._SubParsersAction):
|
2017-02-27 23:50:36 +01:00
|
|
|
'''SubParser with support for action aliases'''
|
2017-02-27 23:39:25 +01:00
|
|
|
# source https://gist.github.com/sampsyo/471779
|
2017-02-27 23:50:36 +01:00
|
|
|
# pylint: disable=protected-access,too-few-public-methods,missing-docstring
|
2017-02-27 23:39:25 +01:00
|
|
|
class _AliasedPseudoAction(argparse.Action):
|
|
|
|
# pylint: disable=redefined-builtin
|
|
|
|
def __init__(self, name, aliases, help):
|
|
|
|
dest = name
|
|
|
|
if aliases:
|
|
|
|
dest += ' (%s)' % ','.join(aliases)
|
2017-02-27 23:50:36 +01:00
|
|
|
super(AliasedSubParsersAction._AliasedPseudoAction, self).\
|
|
|
|
__init__(option_strings=[], dest=dest, help=help)
|
2017-02-27 23:39:25 +01:00
|
|
|
|
2017-04-21 02:47:23 +02:00
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
pass
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
def add_parser(self, name, **kwargs):
|
|
|
|
if 'aliases' in kwargs:
|
|
|
|
aliases = kwargs['aliases']
|
|
|
|
del kwargs['aliases']
|
|
|
|
else:
|
|
|
|
aliases = []
|
|
|
|
|
|
|
|
local_parser = super(AliasedSubParsersAction, self).add_parser(
|
|
|
|
name, **kwargs)
|
|
|
|
|
|
|
|
# Make the aliases work.
|
|
|
|
for alias in aliases:
|
|
|
|
self._name_parser_map[alias] = local_parser
|
|
|
|
# Make the help text reflect them, first removing old help entry.
|
|
|
|
if 'help' in kwargs:
|
|
|
|
self._choices_actions.pop()
|
|
|
|
pseudo_action = self._AliasedPseudoAction(name, aliases,
|
|
|
|
kwargs.pop('help'))
|
|
|
|
self._choices_actions.append(pseudo_action)
|
|
|
|
|
|
|
|
return local_parser
|
|
|
|
|
|
|
|
|
|
|
|
def get_parser_for_command(command):
|
|
|
|
'''Get parser for given qvm-tool.
|
|
|
|
|
|
|
|
:param str command: command name
|
|
|
|
:rtype: argparse.ArgumentParser
|
|
|
|
:raises ImportError: when command's module is not found
|
|
|
|
:raises AttributeError: when parser was not found
|
|
|
|
'''
|
|
|
|
|
|
|
|
module = importlib.import_module(
|
2017-05-11 23:21:04 +02:00
|
|
|
'.' + command.replace('-', '_'), 'qubesadmin.tools')
|
2017-02-27 23:39:25 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
parser = module.parser
|
|
|
|
except AttributeError:
|
|
|
|
try:
|
|
|
|
parser = module.get_parser()
|
|
|
|
except AttributeError:
|
|
|
|
raise AttributeError('cannot find parser in module')
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
class VmNameGroup(argparse._MutuallyExclusiveGroup):
|
|
|
|
''' Adds an a VMNAME, --all & --exclude parameters to a
|
|
|
|
:py:class:``argparse.ArgumentParser```.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, container, required, vm_action=VmNameAction, help=None):
|
|
|
|
# pylint: disable=redefined-builtin
|
|
|
|
super(VmNameGroup, self).__init__(container, required=required)
|
|
|
|
if not help:
|
|
|
|
help = 'perform the action on all qubes'
|
|
|
|
self.add_argument('--all', action='store_true', dest='all_domains',
|
|
|
|
help=help)
|
|
|
|
container.add_argument('--exclude', action='append', default=[],
|
|
|
|
help='exclude the qube from --all')
|
|
|
|
|
|
|
|
# ⚠ the default parameter below is important! ⚠
|
|
|
|
# See https://stackoverflow.com/questions/35044288 and
|
|
|
|
# `argparse.ArgumentParser.parse_args()` implementation
|
|
|
|
self.add_argument('VMNAME', action=vm_action, nargs='*', default=[])
|
|
|
|
|
|
|
|
|
2017-03-11 01:47:12 +01:00
|
|
|
def print_table(table, stream=None):
|
2017-02-27 23:39:25 +01:00
|
|
|
''' Uses the unix column command to print pretty table.
|
|
|
|
|
|
|
|
:param str text: list of lists/sets
|
|
|
|
'''
|
|
|
|
unit_separator = chr(31)
|
|
|
|
cmd = ['column', '-t', '-s', unit_separator]
|
|
|
|
text_table = '\n'.join([unit_separator.join(row) for row in table])
|
|
|
|
text_table += '\n'
|
|
|
|
|
2017-03-11 01:47:12 +01:00
|
|
|
if stream is None:
|
|
|
|
stream = sys.stdout
|
|
|
|
|
2017-02-27 23:39:25 +01:00
|
|
|
# for tests...
|
2017-03-11 01:47:12 +01:00
|
|
|
if stream != sys.__stdout__:
|
2017-02-27 23:39:25 +01:00
|
|
|
p = subprocess.Popen(cmd + ['-c', '80'], stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
p.stdin.write(text_table.encode())
|
|
|
|
(out, _) = p.communicate()
|
2017-03-12 00:45:44 +01:00
|
|
|
stream.write(str(out.decode()))
|
2017-02-27 23:39:25 +01:00
|
|
|
else:
|
|
|
|
p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
|
|
|
|
p.communicate(text_table.encode())
|