qvm_prefs.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # encoding=utf-8
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. ''' Manipulate VM properties.'''
  21. from __future__ import print_function
  22. import sys
  23. import textwrap
  24. import qubesadmin
  25. import qubesadmin.tools
  26. import qubesadmin.utils
  27. import qubesadmin.vm
  28. def get_parser(vmname_nargs=1):
  29. '''Return argument parser for generic property-related tool'''
  30. parser = qubesadmin.tools.QubesArgumentParser(
  31. vmname_nargs=vmname_nargs)
  32. parser.add_argument('--help-properties',
  33. action='store_true',
  34. help='list all available properties with short descriptions and exit')
  35. parser.add_argument('--hide-default',
  36. action='store_true',
  37. help='Do not show properties that are set to the default value.')
  38. parser.add_argument('--get', '-g',
  39. action='store_true',
  40. help='Ignored; for compatibility with older scripts.')
  41. parser.add_argument('--set', '-s',
  42. action='store_true',
  43. help='Ignored; for compatibility with older scripts.')
  44. parser.add_argument('property', metavar='PROPERTY',
  45. nargs='?',
  46. help='name of the property to show or change')
  47. parser_value = parser.add_mutually_exclusive_group()
  48. parser_value.add_argument('value', metavar='VALUE',
  49. nargs='?',
  50. help='new value of the property')
  51. parser.add_argument('--default', '-D',
  52. dest='delete',
  53. action='store_true',
  54. help='reset property to its default value')
  55. return parser
  56. def process_actions(parser, args, target):
  57. '''Handle actions for generic property-related tool
  58. :param parser: argument parser used to produce args
  59. :param args: arguments to handle
  60. :param target: object on which actions should be performed
  61. '''
  62. # pylint: disable=no-else-return
  63. if args.help_properties:
  64. properties = target.property_list()
  65. width = max(len(prop) for prop in properties)
  66. wrapper = textwrap.TextWrapper(width=80,
  67. initial_indent=' ', subsequent_indent=' ' * (width + 6))
  68. for prop in sorted(properties):
  69. help_text = target.property_help(prop)
  70. print(wrapper.fill('{name:{width}s} {help_text!s}'.format(
  71. name=prop, width=width, help_text=help_text)))
  72. return 0
  73. if args.property is None:
  74. # fetch all the properties with one Admin API call, instead of issuing
  75. # one call per property
  76. args.app.cache_enabled = True
  77. properties = target.property_list()
  78. width = max(len(prop) for prop in properties)
  79. for prop in sorted(properties):
  80. try:
  81. value = getattr(target, prop)
  82. except AttributeError:
  83. print('{name:{width}s} U'.format(
  84. name=prop, width=width))
  85. continue
  86. if not target.property_is_default(prop):
  87. print('{name:{width}s} - {value!s}'.format(
  88. name=prop, width=width, value=value))
  89. elif not args.hide_default:
  90. print('{name:{width}s} D {value!s}'.format(
  91. name=prop, width=width, value=value))
  92. return 0
  93. else:
  94. args.property = args.property.replace('-', '_')
  95. if args.value is not None:
  96. if str(args.value).lower() == "none":
  97. if args.property in ["default_dispvm", "netvm", "template",
  98. "guivm", "audiovm"]:
  99. args.value = ''
  100. try:
  101. setattr(target, args.property, args.value)
  102. except AttributeError:
  103. parser.error('no such property: {!r}'.format(args.property))
  104. except qubesadmin.exc.QubesException as e:
  105. parser.error_runtime(e)
  106. return 0
  107. if args.delete:
  108. try:
  109. delattr(target, args.property)
  110. except AttributeError:
  111. parser.error('no such property: {!r}'.format(args.property))
  112. except qubesadmin.exc.QubesException as e:
  113. parser.error_runtime(e)
  114. return 0
  115. try:
  116. value = getattr(target, args.property)
  117. if value is not None:
  118. print(str(value))
  119. except AttributeError:
  120. parser.error('no such property: {!r}'.format(args.property))
  121. except qubesadmin.exc.QubesException as e:
  122. parser.error_runtime(e)
  123. return 0
  124. def main(args=None, app=None): # pylint: disable=missing-docstring
  125. parser = get_parser(1)
  126. args = parser.parse_args(args, app=app)
  127. target = args.domains.pop()
  128. return process_actions(parser, args, target)
  129. if __name__ == '__main__':
  130. sys.exit(main())