2017-03-08 21:53:46 +01:00
|
|
|
# encoding=utf-8
|
2017-03-01 15:21:55 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-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-03-01 15:21:55 +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-03-01 15:21:55 +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-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
''' Manipulate VM properties.'''
|
|
|
|
# TODO list only non-default properties
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import sys
|
2017-03-11 01:49:04 +01:00
|
|
|
import textwrap
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin
|
|
|
|
import qubesadmin.tools
|
|
|
|
import qubesadmin.utils
|
|
|
|
import qubesadmin.vm
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
def get_parser(vmname_nargs=1):
|
|
|
|
'''Return argument parser for generic property-related tool'''
|
2017-05-11 23:21:04 +02:00
|
|
|
parser = qubesadmin.tools.QubesArgumentParser(
|
2017-03-08 16:19:27 +01:00
|
|
|
vmname_nargs=vmname_nargs)
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-03-11 01:49:04 +01:00
|
|
|
parser.add_argument('--help-properties',
|
|
|
|
action='store_true',
|
|
|
|
help='list all available properties with short descriptions and exit')
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
parser.add_argument('--get', '-g',
|
|
|
|
action='store_true',
|
|
|
|
help='Ignored; for compatibility with older scripts.')
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
parser.add_argument('--set', '-s',
|
|
|
|
action='store_true',
|
|
|
|
help='Ignored; for compatibility with older scripts.')
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
parser.add_argument('property', metavar='PROPERTY',
|
|
|
|
nargs='?',
|
|
|
|
help='name of the property to show or change')
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
parser_value = parser.add_mutually_exclusive_group()
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
parser_value.add_argument('value', metavar='VALUE',
|
|
|
|
nargs='?',
|
|
|
|
help='new value of the property')
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-08-12 22:33:36 +02:00
|
|
|
parser.add_argument('--default', '-D',
|
2017-03-08 16:19:27 +01:00
|
|
|
dest='delete',
|
|
|
|
action='store_true',
|
2017-08-12 22:33:36 +02:00
|
|
|
help='reset property to its default value')
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
return parser
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
def process_actions(parser, args, target):
|
|
|
|
'''Handle actions for generic property-related tool
|
|
|
|
|
|
|
|
:param parser: argument parser used to produce args
|
|
|
|
:param args: arguments to handle
|
|
|
|
:param target: object on which actions should be performed
|
|
|
|
'''
|
2018-07-16 02:25:25 +02:00
|
|
|
# pylint: disable=no-else-return
|
2017-03-11 01:49:04 +01:00
|
|
|
if args.help_properties:
|
|
|
|
properties = target.property_list()
|
|
|
|
width = max(len(prop) for prop in properties)
|
|
|
|
wrapper = textwrap.TextWrapper(width=80,
|
|
|
|
initial_indent=' ', subsequent_indent=' ' * (width + 6))
|
|
|
|
|
|
|
|
for prop in sorted(properties):
|
|
|
|
help_text = target.property_help(prop)
|
|
|
|
|
|
|
|
print(wrapper.fill('{name:{width}s} {help_text!s}'.format(
|
|
|
|
name=prop, width=width, help_text=help_text)))
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2017-03-01 15:21:55 +01:00
|
|
|
if args.property is None:
|
2017-03-08 16:19:27 +01:00
|
|
|
properties = target.property_list()
|
2017-03-09 01:39:11 +01:00
|
|
|
width = max(len(prop) for prop in properties)
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
for prop in sorted(properties):
|
|
|
|
try:
|
2017-03-09 01:39:11 +01:00
|
|
|
value = getattr(target, prop)
|
2017-03-01 15:21:55 +01:00
|
|
|
except AttributeError:
|
|
|
|
print('{name:{width}s} U'.format(
|
2017-03-09 01:39:11 +01:00
|
|
|
name=prop, width=width))
|
2017-03-01 15:21:55 +01:00
|
|
|
continue
|
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
if target.property_is_default(prop):
|
2017-03-01 15:21:55 +01:00
|
|
|
print('{name:{width}s} D {value!s}'.format(
|
2017-03-09 01:39:11 +01:00
|
|
|
name=prop, width=width, value=value))
|
2017-03-01 15:21:55 +01:00
|
|
|
else:
|
|
|
|
print('{name:{width}s} - {value!s}'.format(
|
2017-03-09 01:39:11 +01:00
|
|
|
name=prop, width=width, value=value))
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
args.property = args.property.replace('-', '_')
|
|
|
|
|
|
|
|
if args.value is not None:
|
2018-06-08 00:52:37 +02:00
|
|
|
if str(args.value).lower() == "none":
|
|
|
|
if args.property in ["default_dispvm", "netvm", "template"]:
|
|
|
|
args.value = ''
|
2017-03-09 01:39:55 +01:00
|
|
|
try:
|
|
|
|
setattr(target, args.property, args.value)
|
|
|
|
except AttributeError:
|
|
|
|
parser.error('no such property: {!r}'.format(args.property))
|
2018-03-18 21:38:49 +01:00
|
|
|
except qubesadmin.exc.QubesException as e:
|
|
|
|
parser.error_runtime(e)
|
2017-03-01 15:21:55 +01:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if args.delete:
|
2017-03-09 01:39:55 +01:00
|
|
|
try:
|
|
|
|
delattr(target, args.property)
|
|
|
|
except AttributeError:
|
|
|
|
parser.error('no such property: {!r}'.format(args.property))
|
2018-03-18 21:38:49 +01:00
|
|
|
except qubesadmin.exc.QubesException as e:
|
|
|
|
parser.error_runtime(e)
|
2017-03-01 15:21:55 +01:00
|
|
|
return 0
|
|
|
|
|
2017-03-09 01:39:55 +01:00
|
|
|
try:
|
2018-03-02 02:41:47 +01:00
|
|
|
value = getattr(target, args.property)
|
|
|
|
if value is not None:
|
|
|
|
print(str(value))
|
2017-03-09 01:39:55 +01:00
|
|
|
except AttributeError:
|
|
|
|
parser.error('no such property: {!r}'.format(args.property))
|
2018-03-18 21:38:49 +01:00
|
|
|
except qubesadmin.exc.QubesException as e:
|
|
|
|
parser.error_runtime(e)
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2017-03-08 23:41:39 +01:00
|
|
|
def main(args=None, app=None): # pylint: disable=missing-docstring
|
2017-03-08 16:19:27 +01:00
|
|
|
parser = get_parser(1)
|
2017-03-08 23:41:39 +01:00
|
|
|
args = parser.parse_args(args, app=app)
|
2017-03-08 16:19:27 +01:00
|
|
|
target = args.domains.pop()
|
|
|
|
return process_actions(parser, args, target)
|
|
|
|
|
|
|
|
|
2017-03-01 15:21:55 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|