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
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (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
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
''' Manipulate VM properties.'''
|
|
|
|
# TODO list properties for all classes
|
|
|
|
# TODO list only non-default properties
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import qubesmgmt
|
|
|
|
import qubesmgmt.tools
|
|
|
|
import qubesmgmt.utils
|
|
|
|
import qubesmgmt.vm
|
|
|
|
|
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
def get_parser(vmname_nargs=1):
|
|
|
|
'''Return argument parser for generic property-related tool'''
|
|
|
|
parser = qubesmgmt.tools.QubesArgumentParser(
|
|
|
|
want_force_root=True,
|
|
|
|
vmname_nargs=vmname_nargs)
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
# parser.add_argument('--help-properties',
|
|
|
|
# action=qubesmgmt.tools.HelpPropertiesAction,
|
|
|
|
# klass=qubesmgmt.vm.QubesVM)
|
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-03-08 16:19:27 +01:00
|
|
|
parser.add_argument('--unset', '--default', '--delete', '-D',
|
|
|
|
dest='delete',
|
|
|
|
action='store_true',
|
|
|
|
help='unset the property; '
|
|
|
|
'if property has default value, it will be used instead')
|
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
|
|
|
|
'''
|
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-01 15:21:55 +01:00
|
|
|
width = max(len(prop.__name__) for prop in properties)
|
|
|
|
|
|
|
|
for prop in sorted(properties):
|
|
|
|
try:
|
2017-03-08 16:19:27 +01:00
|
|
|
value = getattr(target, prop.__name__)
|
2017-03-01 15:21:55 +01:00
|
|
|
except AttributeError:
|
|
|
|
print('{name:{width}s} U'.format(
|
|
|
|
name=prop.__name__, width=width))
|
|
|
|
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(
|
|
|
|
name=prop.__name__, width=width, value=value))
|
|
|
|
else:
|
|
|
|
print('{name:{width}s} - {value!s}'.format(
|
|
|
|
name=prop.__name__, width=width, value=value))
|
|
|
|
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
args.property = args.property.replace('-', '_')
|
|
|
|
|
|
|
|
if args.property not in [prop.__name__
|
2017-03-08 16:19:27 +01:00
|
|
|
for prop in target.property_list()]:
|
2017-03-01 15:21:55 +01:00
|
|
|
parser.error('no such property: {!r}'.format(args.property))
|
|
|
|
|
|
|
|
if args.value is not None:
|
2017-03-08 16:19:27 +01:00
|
|
|
setattr(target, args.property, args.value)
|
2017-03-01 15:21:55 +01:00
|
|
|
args.app.save()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if args.delete:
|
2017-03-08 16:19:27 +01:00
|
|
|
delattr(target, args.property)
|
2017-03-01 15:21:55 +01:00
|
|
|
args.app.save()
|
|
|
|
return 0
|
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
print(str(getattr(target, args.property)))
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2017-03-08 16:19:27 +01:00
|
|
|
def main(args=None): # pylint: disable=missing-docstring
|
|
|
|
parser = get_parser(1)
|
|
|
|
args = parser.parse_args(args)
|
|
|
|
target = args.domains.pop()
|
|
|
|
return process_actions(parser, args, target)
|
|
|
|
|
|
|
|
|
2017-03-01 15:21:55 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|