qvm_features.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # coding=utf-8
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. # Copyright (C) 2017 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU Lesser General Public License as published by
  12. # the Free Software Foundation; either version 2.1 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Lesser General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. '''qvm-features - Manage domain's features'''
  25. from __future__ import print_function
  26. import sys
  27. import qubesadmin
  28. import qubesadmin.tools
  29. parser = qubesadmin.tools.QubesArgumentParser(
  30. vmname_nargs=1,
  31. description='manage domain\'s features')
  32. parser.add_argument('feature', metavar='FEATURE',
  33. action='store', nargs='?',
  34. help='name of the feature')
  35. parser.add_argument('value', metavar='VALUE',
  36. action='store', nargs='?',
  37. help='new value of the feature')
  38. parser.add_argument('--unset', '--default', '--delete', '-D',
  39. dest='delete',
  40. action='store_true',
  41. help='unset the feature')
  42. def main(args=None, app=None):
  43. '''Main routine of :program:`qvm-features`.
  44. :param list args: Optional arguments to override those delivered from \
  45. command line.
  46. '''
  47. args = parser.parse_args(args, app=app)
  48. vm = args.domains[0]
  49. if args.feature is None:
  50. if args.delete:
  51. parser.error('--unset requires a feature')
  52. try:
  53. features = [(feat, vm.features[feat]) for feat in vm.features]
  54. qubesadmin.tools.print_table(features)
  55. except qubesadmin.exc.QubesException as e:
  56. parser.error_runtime(e)
  57. elif args.delete:
  58. if args.value is not None:
  59. parser.error('cannot both set and unset a value')
  60. try:
  61. del vm.features[args.feature]
  62. except KeyError:
  63. pass
  64. except qubesadmin.exc.QubesException as e:
  65. parser.error_runtime(e)
  66. elif args.value is None:
  67. try:
  68. print(vm.features[args.feature])
  69. return 0
  70. except KeyError:
  71. return 1
  72. except qubesadmin.exc.QubesException as e:
  73. parser.error_runtime(e)
  74. else:
  75. try:
  76. vm.features[args.feature] = args.value
  77. except qubesadmin.exc.QubesException as e:
  78. parser.error_runtime(e)
  79. return 0
  80. if __name__ == '__main__':
  81. sys.exit(main())