qvm_service.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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-service - Manage domain's services'''
  25. from __future__ import print_function
  26. import argparse
  27. import sys
  28. import qubesadmin
  29. import qubesadmin.exc
  30. import qubesadmin.tools
  31. parser = qubesadmin.tools.QubesArgumentParser(
  32. vmname_nargs=1,
  33. argument_default=argparse.SUPPRESS,
  34. description='manage domain\'s services')
  35. parser.add_argument('service', metavar='SERVICE',
  36. action='store', nargs='?',
  37. help='name of the feature')
  38. parser.add_argument('value', metavar='VALUE',
  39. action='store', nargs='?',
  40. help='new value of the service (on/off)')
  41. parser.add_argument('--unset', '--default', '--delete', '-D',
  42. dest='delete', default=False,
  43. action='store_true',
  44. help='unset service (default to VM preference)')
  45. parser.add_argument('--list', '-l',
  46. dest='list',
  47. action='store_true',
  48. help='list services (default action)')
  49. parser.add_argument('--enable', '-e',
  50. dest='value',
  51. action='store_const', const='1',
  52. help='enable service (same as setting "on" value)')
  53. parser.add_argument('--disable', '-d',
  54. dest='value',
  55. action='store_const', const='0',
  56. help='disable service (same as setting "off" value)')
  57. def parse_bool(value):
  58. '''Convert string value to bool according to well known representations
  59. It accepts (case-insensitive) ``'0'``, ``'no'`` and ``false`` as
  60. :py:obj:`False` and ``'1'``, ``'yes'`` and ``'true'`` as
  61. :py:obj:`True`.
  62. '''
  63. if isinstance(value, str):
  64. lcvalue = value.lower()
  65. if lcvalue in ('0', 'no', 'false', 'off'):
  66. return False
  67. if lcvalue in ('1', 'yes', 'true', 'on'):
  68. return True
  69. raise qubesadmin.exc.QubesValueError(
  70. 'Invalid literal for boolean value: {!r}'.format(value))
  71. return bool(value)
  72. def main(args=None, app=None):
  73. '''Main routine of :program:`qvm-features`.
  74. :param list args: Optional arguments to override those delivered from \
  75. command line.
  76. '''
  77. args = parser.parse_args(args, app=app)
  78. vm = args.domains[0]
  79. if not hasattr(args, 'service'):
  80. if args.delete:
  81. parser.error('--unset requires a feature')
  82. services = [(feat[len('service.'):],
  83. 'on' if vm.features[feat] else 'off') for feat in
  84. vm.features if feat.startswith('service.')]
  85. qubesadmin.tools.print_table(services)
  86. elif args.delete:
  87. if hasattr(args, 'value'):
  88. parser.error('cannot both set and unset a value')
  89. try:
  90. del vm.features['service.' + args.service]
  91. except KeyError:
  92. pass
  93. except qubesadmin.exc.QubesException as err:
  94. parser.error_runtime(str(err))
  95. elif hasattr(args, 'value'):
  96. try:
  97. vm.features['service.' + args.service] = parse_bool(args.value)
  98. except qubesadmin.exc.QubesException as err:
  99. parser.error_runtime(str(err))
  100. else:
  101. try:
  102. print('on' if vm.features['service.' + args.service] else 'off')
  103. return 0
  104. except KeyError:
  105. return 1
  106. except qubesadmin.exc.QubesException as err:
  107. parser.error_runtime(str(err))
  108. return 0
  109. if __name__ == '__main__':
  110. sys.exit(main())