qvm_pool.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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:
  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 _Options(argparse.Action):
  73. ''' Action for argument parser that parsers options. '''
  74. def __init__(self, option_strings, dest, default, metavar='options'):
  75. super(_Options, self).__init__(
  76. option_strings=option_strings,
  77. dest=dest,
  78. metavar=metavar,
  79. default=default,
  80. help='comma-separated list of driver options')
  81. def __call__(self, parser, namespace, options, option_string=None):
  82. setattr(namespace, 'options',
  83. dict([option.split('=', 1) for option in options.split(',')]))
  84. def get_parser():
  85. ''' Parses the provided args '''
  86. parser = qubesadmin.tools.QubesArgumentParser(description=__doc__)
  87. parser.add_argument('-o', action=_Options, dest='options', default={})
  88. group = parser.add_mutually_exclusive_group()
  89. group.add_argument('-l',
  90. '--list',
  91. dest='command',
  92. const='list',
  93. action='store_const',
  94. help='list all pools and exit (default action)')
  95. group.add_argument('-i', '--info', metavar='POOLNAME', dest='pools',
  96. action=_Info, default=[])
  97. group.add_argument('-a',
  98. '--add',
  99. action=_Add,
  100. dest='command',
  101. metavar=('NAME', 'DRIVER'))
  102. group.add_argument('-r', '--remove', metavar='NAME', action=_Remove)
  103. group.add_argument('--help-drivers',
  104. dest='command',
  105. const='list-drivers',
  106. action='store_const',
  107. help='list all drivers with their options and exit')
  108. return parser
  109. def main(args=None, app=None):
  110. '''Main routine of :program:`qvm-pools`.
  111. :param list args: Optional arguments to override those delivered from \
  112. command line.
  113. '''
  114. parser = get_parser()
  115. try:
  116. args = parser.parse_args(args, app=app)
  117. except qubesadmin.exc.QubesException as e:
  118. parser.print_error(str(e))
  119. return 1
  120. if args.command is None or args.command == 'list':
  121. list_pools(args.app)
  122. elif args.command == 'list-drivers':
  123. result = [('DRIVER', 'OPTIONS')]
  124. for driver in sorted(args.app.pool_drivers):
  125. params = args.app.pool_driver_parameters(driver)
  126. driver_options = ', '.join(params)
  127. result += [(driver, driver_options)]
  128. qubesadmin.tools.print_table(result)
  129. elif args.command == 'add':
  130. try:
  131. args.app.add_pool(name=args.name, driver=args.driver,
  132. **args.options)
  133. except qubesadmin.exc.QubesException as e:
  134. parser.error('failed to add pool %s: %s\n' % (args.name, str(e)))
  135. elif args.command == 'remove':
  136. try:
  137. args.app.remove_pool(args.name)
  138. except KeyError:
  139. parser.print_error('no such pool %s\n' % args.name)
  140. elif args.command == 'info':
  141. for pool in args.pools:
  142. pool_info(pool)
  143. return 0
  144. if __name__ == '__main__':
  145. sys.exit(main())