qvm_prefs.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. properties = target.property_list()
  75. width = max(len(prop) for prop in properties)
  76. for prop in sorted(properties):
  77. try:
  78. value = getattr(target, prop)
  79. except AttributeError:
  80. print('{name:{width}s} U'.format(
  81. name=prop, width=width))
  82. continue
  83. if not target.property_is_default(prop):
  84. print('{name:{width}s} - {value!s}'.format(
  85. name=prop, width=width, value=value))
  86. elif not args.hide_default:
  87. print('{name:{width}s} D {value!s}'.format(
  88. name=prop, width=width, value=value))
  89. return 0
  90. else:
  91. args.property = args.property.replace('-', '_')
  92. if args.value is not None:
  93. if str(args.value).lower() == "none":
  94. if args.property in ["default_dispvm", "netvm", "template",
  95. "guivm"]:
  96. args.value = ''
  97. try:
  98. setattr(target, args.property, args.value)
  99. except AttributeError:
  100. parser.error('no such property: {!r}'.format(args.property))
  101. except qubesadmin.exc.QubesException as e:
  102. parser.error_runtime(e)
  103. return 0
  104. if args.delete:
  105. try:
  106. delattr(target, args.property)
  107. except AttributeError:
  108. parser.error('no such property: {!r}'.format(args.property))
  109. except qubesadmin.exc.QubesException as e:
  110. parser.error_runtime(e)
  111. return 0
  112. try:
  113. value = getattr(target, args.property)
  114. if value is not None:
  115. print(str(value))
  116. except AttributeError:
  117. parser.error('no such property: {!r}'.format(args.property))
  118. except qubesadmin.exc.QubesException as e:
  119. parser.error_runtime(e)
  120. return 0
  121. def main(args=None, app=None): # pylint: disable=missing-docstring
  122. parser = get_parser(1)
  123. args = parser.parse_args(args, app=app)
  124. target = args.domains.pop()
  125. return process_actions(parser, args, target)
  126. if __name__ == '__main__':
  127. sys.exit(main())