qvm_pool.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.storage
  26. import qubesadmin.tools
  27. class _Info(qubesadmin.tools.PoolsAction):
  28. ''' Action for argument parser that displays pool info and exits. '''
  29. def __init__(self, option_strings, help='print pool info and exit',
  30. **kwargs):
  31. # pylint: disable=redefined-builtin
  32. super(_Info, self).__init__(option_strings, help=help, **kwargs)
  33. def __call__(self, parser, namespace, values, option_string=None):
  34. setattr(namespace, 'command', 'info')
  35. super(_Info, self).__call__(parser, namespace, values, option_string)
  36. def pool_info(pool):
  37. ''' Prints out pool name and config '''
  38. data = [("name", pool.name)]
  39. data += [i for i in sorted(pool.config.items()) if i[0] != 'name']
  40. qubesadmin.tools.print_table(data)
  41. def list_pools(app):
  42. ''' Prints out all known pools and their drivers '''
  43. result = [('NAME', 'DRIVER')]
  44. for pool in app.pools.values():
  45. result += [(pool.name, pool.driver)]
  46. qubesadmin.tools.print_table(result)
  47. class _Remove(argparse.Action):
  48. ''' Action for argument parser that removes a pool '''
  49. def __init__(self, option_strings, dest=None, default=None, metavar=None):
  50. super(_Remove, self).__init__(option_strings=option_strings,
  51. dest=dest,
  52. metavar=metavar,
  53. default=default,
  54. help='remove pool')
  55. def __call__(self, parser, namespace, name, option_string=None):
  56. setattr(namespace, 'command', 'remove')
  57. setattr(namespace, 'name', name)
  58. class _Add(argparse.Action):
  59. ''' Action for argument parser that adds a pool. '''
  60. def __init__(self, option_strings, dest=None, default=None, metavar=None):
  61. super(_Add, self).__init__(option_strings=option_strings,
  62. dest=dest,
  63. metavar=metavar,
  64. default=default,
  65. nargs=2,
  66. help='add pool')
  67. def __call__(self, parser, namespace, values, option_string=None):
  68. name, driver = values
  69. setattr(namespace, 'command', 'add')
  70. setattr(namespace, 'name', name)
  71. setattr(namespace, 'driver', driver)
  72. class _Set(qubesadmin.tools.PoolsAction):
  73. ''' Action for argument parser that sets pool options. '''
  74. def __init__(self, option_strings, dest=None, default=None, metavar=None):
  75. super(_Set, self).__init__(option_strings=option_strings,
  76. dest=dest,
  77. metavar=metavar,
  78. default=default,
  79. help='modify pool (use -o to specify '
  80. 'modifications)')
  81. def __call__(self, parser, namespace, name, option_string=None):
  82. setattr(namespace, 'command', 'set')
  83. super(_Set, self).__call__(parser, namespace, name, option_string)
  84. class _Options(argparse.Action):
  85. ''' Action for argument parser that parsers options. '''
  86. def __init__(self, option_strings, dest, default, metavar='options'):
  87. super(_Options, self).__init__(
  88. option_strings=option_strings,
  89. dest=dest,
  90. metavar=metavar,
  91. default=default,
  92. help='comma-separated list of driver options')
  93. def __call__(self, parser, namespace, options, option_string=None):
  94. setattr(namespace, 'options',
  95. dict([option.split('=', 1) for option in options.split(',')]))
  96. def get_parser():
  97. ''' Parses the provided args '''
  98. parser = qubesadmin.tools.QubesArgumentParser(description=__doc__)
  99. parser.add_argument('-o', action=_Options, dest='options', default={})
  100. group = parser.add_mutually_exclusive_group()
  101. group.add_argument('-l',
  102. '--list',
  103. dest='command',
  104. const='list',
  105. action='store_const',
  106. help='list all pools and exit (default action)')
  107. group.add_argument('-i', '--info', metavar='POOLNAME', dest='pools',
  108. action=_Info, default=[])
  109. group.add_argument('-a',
  110. '--add',
  111. action=_Add,
  112. dest='command',
  113. metavar=('NAME', 'DRIVER'))
  114. group.add_argument('-r', '--remove', metavar='NAME', action=_Remove)
  115. group.add_argument('-s', '--set', metavar='POOLNAME', dest='pool',
  116. action=_Set, default=[])
  117. group.add_argument('--help-drivers',
  118. dest='command',
  119. const='list-drivers',
  120. action='store_const',
  121. help='list all drivers with their options and exit')
  122. return parser
  123. def main(args=None, app=None):
  124. '''Main routine of :program:`qvm-pools`.
  125. :param list args: Optional arguments to override those delivered from \
  126. command line.
  127. '''
  128. parser = get_parser()
  129. try:
  130. args = parser.parse_args(args, app=app)
  131. except qubesadmin.exc.QubesException as e:
  132. parser.print_error(str(e))
  133. return 1
  134. if args.command is None or args.command == 'list':
  135. list_pools(args.app)
  136. elif args.command == 'list-drivers':
  137. result = [('DRIVER', 'OPTIONS')]
  138. for driver in sorted(args.app.pool_drivers):
  139. params = args.app.pool_driver_parameters(driver)
  140. driver_options = ', '.join(params)
  141. result += [(driver, driver_options)]
  142. qubesadmin.tools.print_table(result)
  143. elif args.command == 'add':
  144. try:
  145. args.app.add_pool(name=args.name, driver=args.driver,
  146. **args.options)
  147. except qubesadmin.exc.QubesException as e:
  148. parser.error('failed to add pool %s: %s\n' % (args.name, str(e)))
  149. elif args.command == 'remove':
  150. try:
  151. args.app.remove_pool(args.name)
  152. except KeyError:
  153. parser.print_error('no such pool %s\n' % args.name)
  154. except qubesadmin.exc.QubesException as e:
  155. parser.error('failed to remove pool %s: %s\n' % (args.name, str(e)))
  156. elif args.command == 'info':
  157. for pool in args.pools:
  158. pool_info(pool)
  159. elif args.command == 'set':
  160. pool = args.pool[0]
  161. for opt, value in args.options.items():
  162. if not hasattr(type(pool), opt):
  163. parser.error('setting pool option %s is not supported' % (
  164. pool.name))
  165. try:
  166. setattr(pool, opt, value)
  167. except qubesadmin.exc.QubesException as e:
  168. parser.error('failed to set pool %s option %s: %s\n' % (
  169. pool.name, opt, str(e)))
  170. return 0
  171. if __name__ == '__main__':
  172. sys.exit(main())