qvm_pool_legacy.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # pylint: disable=too-few-public-methods
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation; either version 2.1 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License along
  18. # with this program; if not, see <http://www.gnu.org/licenses/>.
  19. '''Manages Qubes pools and their options'''
  20. from __future__ import print_function
  21. import argparse
  22. import sys
  23. import qubesadmin
  24. import qubesadmin.exc
  25. import qubesadmin.tools
  26. class _Info(qubesadmin.tools.PoolsAction):
  27. ''' Action for argument parser that displays pool info and exits. '''
  28. def __init__(self, option_strings, help='print pool info and exit',
  29. **kwargs):
  30. # pylint: disable=redefined-builtin
  31. super().__init__(option_strings, help=help, **kwargs)
  32. def __call__(self, parser, namespace, values, option_string=None):
  33. setattr(namespace, 'command', 'info')
  34. super().__call__(parser, namespace, values, option_string)
  35. def pool_info(pool):
  36. ''' Prints out pool name and config '''
  37. data = [("name", pool.name)]
  38. data += [i for i in sorted(pool.config.items()) if i[0] != 'name']
  39. qubesadmin.tools.print_table(data)
  40. def list_pools(app):
  41. ''' Prints out all known pools and their drivers '''
  42. result = [('NAME', 'DRIVER')]
  43. for pool in app.pools.values():
  44. result += [(pool.name, pool.driver)]
  45. qubesadmin.tools.print_table(result)
  46. class _Remove(argparse.Action):
  47. ''' Action for argument parser that removes a pool '''
  48. def __init__(self, option_strings, dest=None, default=None, metavar=None):
  49. super().__init__(option_strings=option_strings,
  50. dest=dest,
  51. metavar=metavar,
  52. default=default,
  53. help='remove pool')
  54. def __call__(self, parser, namespace, name, option_string=None):
  55. setattr(namespace, 'command', 'remove')
  56. setattr(namespace, 'name', name)
  57. class _Add(argparse.Action):
  58. ''' Action for argument parser that adds a pool. '''
  59. def __init__(self, option_strings, dest=None, default=None, metavar=None):
  60. super().__init__(option_strings=option_strings,
  61. dest=dest,
  62. metavar=metavar,
  63. default=default,
  64. nargs=2,
  65. help='add pool')
  66. def __call__(self, parser, namespace, values, option_string=None):
  67. name, driver = values
  68. setattr(namespace, 'command', 'add')
  69. setattr(namespace, 'name', name)
  70. setattr(namespace, 'driver', driver)
  71. class _Set(qubesadmin.tools.PoolsAction):
  72. ''' Action for argument parser that sets pool options. '''
  73. def __init__(self, option_strings, dest=None, default=None, metavar=None):
  74. super().__init__(option_strings=option_strings,
  75. dest=dest,
  76. metavar=metavar,
  77. default=default,
  78. help='modify pool (use -o to specify '
  79. 'modifications)')
  80. def __call__(self, parser, namespace, name, option_string=None):
  81. setattr(namespace, 'command', 'set')
  82. super().__call__(parser, namespace, name, option_string)
  83. class _Options(argparse.Action):
  84. ''' Action for argument parser that parsers options. '''
  85. def __init__(self, option_strings, dest, default, metavar='options'):
  86. super().__init__(
  87. option_strings=option_strings,
  88. dest=dest,
  89. metavar=metavar,
  90. default=default,
  91. help='comma-separated list of driver options')
  92. def __call__(self, parser, namespace, options, option_string=None):
  93. setattr(namespace, 'options',
  94. dict([option.split('=', 1) for option in options.split(',')]))
  95. def get_parser():
  96. ''' Parses the provided args '''
  97. parser = qubesadmin.tools.QubesArgumentParser(description=__doc__)
  98. parser.add_argument('-o', action=_Options, dest='options', default={})
  99. group = parser.add_mutually_exclusive_group()
  100. group.add_argument('-l',
  101. '--list',
  102. dest='command',
  103. const='list',
  104. action='store_const',
  105. help='list all pools and exit (default action)')
  106. group.add_argument('-i', '--info', metavar='POOLNAME', dest='pools',
  107. action=_Info, default=[])
  108. group.add_argument('-a',
  109. '--add',
  110. action=_Add,
  111. dest='command',
  112. metavar=('NAME', 'DRIVER'))
  113. group.add_argument('-r', '--remove', metavar='NAME', action=_Remove)
  114. group.add_argument('-s', '--set', metavar='POOLNAME', dest='pool',
  115. action=_Set, default=[])
  116. group.add_argument('--help-drivers',
  117. dest='command',
  118. const='list-drivers',
  119. action='store_const',
  120. help='list all drivers with their options and exit')
  121. return parser
  122. def main(args=None, app=None):
  123. '''Main routine of :program:`qvm-pools`.
  124. :param list args: Optional arguments to override those delivered from \
  125. command line.
  126. '''
  127. parser = get_parser()
  128. try:
  129. args = parser.parse_args(args, app=app)
  130. except qubesadmin.exc.QubesException as e:
  131. parser.print_error(str(e))
  132. return 1
  133. if args.command is None or args.command == 'list':
  134. list_pools(args.app)
  135. elif args.command == 'list-drivers':
  136. result = [('DRIVER', 'OPTIONS')]
  137. for driver in sorted(args.app.pool_drivers):
  138. params = args.app.pool_driver_parameters(driver)
  139. driver_options = ', '.join(params)
  140. result += [(driver, driver_options)]
  141. qubesadmin.tools.print_table(result)
  142. elif args.command == 'add':
  143. try:
  144. args.app.add_pool(name=args.name, driver=args.driver,
  145. **args.options)
  146. except qubesadmin.exc.QubesException as e:
  147. parser.error('failed to add pool %s: %s\n' % (args.name, str(e)))
  148. elif args.command == 'remove':
  149. try:
  150. args.app.remove_pool(args.name)
  151. except KeyError:
  152. parser.print_error('no such pool %s\n' % args.name)
  153. except qubesadmin.exc.QubesException as e:
  154. parser.error('failed to remove pool %s: %s\n' % (args.name, str(e)))
  155. elif args.command == 'info':
  156. for pool in args.pools:
  157. pool_info(pool)
  158. elif args.command == 'set':
  159. pool = args.pool[0]
  160. for opt, value in args.options.items():
  161. if not hasattr(type(pool), opt):
  162. parser.error('setting pool option %s is not supported' % (
  163. pool.name))
  164. try:
  165. setattr(pool, opt, value)
  166. except qubesadmin.exc.QubesException as e:
  167. parser.error('failed to set pool %s option %s: %s\n' % (
  168. pool.name, opt, str(e)))
  169. return 0
  170. if __name__ == '__main__':
  171. sys.exit(main())