qvm_pool.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 sys
  22. import qubesadmin
  23. import qubesadmin.exc
  24. import qubesadmin.storage
  25. import qubesadmin.tools
  26. def list_drivers(args):
  27. ''' Lists all drivers with their options '''
  28. result = [('DRIVER', 'OPTIONS')]
  29. for driver in sorted(args.app.pool_drivers):
  30. params = args.app.pool_driver_parameters(driver)
  31. driver_options = ', '.join(params)
  32. result += [(driver, driver_options)]
  33. qubesadmin.tools.print_table(result)
  34. def list_pools(args):
  35. ''' Lists all available pools '''
  36. result = [('NAME', 'DRIVER')]
  37. for pool in args.app.pools.values():
  38. result += [(pool.name, pool.driver)]
  39. qubesadmin.tools.print_table(result)
  40. def info_pools(args):
  41. ''' Prints info about the specified pools '''
  42. data = []
  43. for idx, pool in enumerate(args.pools):
  44. data += [("", "")] if idx > 0 else []
  45. data += [("name", pool.name)]
  46. data += [i for i in sorted(pool.config.items()) if i[0] != 'name']
  47. qubesadmin.tools.print_table(data)
  48. def add_pool(args):
  49. ''' Adds a new pool '''
  50. options = dict(opt.split('=', 1) for opt in args.option or [])
  51. try:
  52. args.app.add_pool(name=args.pool_name, driver=args.driver, **options)
  53. except qubesadmin.exc.QubesException as e:
  54. raise qubesadmin.exc.QubesException('Failed to add pool %s: %s\n',
  55. args.pool_name, str(e))
  56. def remove_pools(args):
  57. ''' Removes the specified pools '''
  58. errors = []
  59. for pool_name in args.pool_names:
  60. try:
  61. args.app.remove_pool(pool_name)
  62. except KeyError:
  63. errors.append('No such pool %s\n' % pool_name)
  64. except qubesadmin.exc.QubesException as e:
  65. errors.append(
  66. 'Failed to remove pool %s: %s\n' % (pool_name, str(e)))
  67. if errors:
  68. raise qubesadmin.exc.QubesException('\n'.join(errors))
  69. def set_pool(args):
  70. ''' Modifies driver options for a pool '''
  71. options = (opt.split('=', 1) for opt in args.option or [])
  72. pool = args.app.pools[args.pool_name]
  73. errors = []
  74. for opt, value in options:
  75. if not hasattr(type(pool), opt):
  76. errors.append(
  77. 'Setting option %s is not supported for pool %s\n' % (
  78. opt, pool.name))
  79. try:
  80. setattr(pool, opt, value)
  81. except qubesadmin.exc.QubesException as e:
  82. errors.append('Failed to set option %s for pool %s: %s\n' % (
  83. opt, pool.name, str(e)))
  84. if errors:
  85. raise qubesadmin.exc.QubesException('\n'.join(errors))
  86. def init_list_parser(sub_parsers):
  87. ''' Add 'list' action related options '''
  88. l_parser = sub_parsers.add_parser(
  89. 'list', aliases=('l', 'ls'), help='List all available pools')
  90. l_parser.set_defaults(func=list_pools)
  91. def init_info_parser(sub_parsers):
  92. ''' Add 'info' action related options '''
  93. i_parser = sub_parsers.add_parser(
  94. 'info', aliases=('i',), help='Print info about the specified pools')
  95. i_parser.add_argument(metavar='POOL_NAME', dest='pools',
  96. action=qubesadmin.tools.PoolsAction)
  97. i_parser.set_defaults(func=info_pools)
  98. def init_add_parser(sub_parsers):
  99. ''' Add 'add' action related options '''
  100. a_parser = sub_parsers.add_parser(
  101. 'add', aliases=('a',), help='Add a new pool')
  102. a_parser.add_argument(metavar='POOL_NAME', dest='pool_name')
  103. a_parser.add_argument(metavar='DRIVER', dest='driver')
  104. a_parser.add_argument('--option', '-o', action='append',
  105. help="Set option for the driver in opt=value form"
  106. "(can be specified multiple times) --"
  107. "see `man qvm-pool` for details")
  108. a_parser.set_defaults(func=add_pool)
  109. def init_remove_parser(sub_parsers):
  110. ''' Add 'remove' action related options '''
  111. r_parser = sub_parsers.add_parser(
  112. 'remove', aliases=('r', 'rm'), help='Remove the specified pools')
  113. r_parser.add_argument(metavar='POOL_NAME', dest='pool_names', nargs='+')
  114. r_parser.set_defaults(func=remove_pools)
  115. def init_set_parser(sub_parsers):
  116. ''' Add 'set' action related options '''
  117. s_parser = sub_parsers.add_parser(
  118. 'set', aliases=('s',), help='Modify driver options for a pool')
  119. s_parser.add_argument(metavar='POOL_NAME', dest='pool_name')
  120. s_parser.add_argument('--option', '-o', action='append',
  121. help="Set option for the driver in opt=value form"
  122. "(can be specified multiple times) --"
  123. "see `man qvm-pool` for details")
  124. s_parser.set_defaults(func=set_pool)
  125. def get_parser():
  126. '''Create :py:class:`argparse.ArgumentParser` suitable for
  127. :program:`qvm-pool`.
  128. '''
  129. parser = qubesadmin.tools.QubesArgumentParser(description=__doc__,
  130. want_app=True)
  131. parser.register('action', 'parsers',
  132. qubesadmin.tools.AliasedSubParsersAction)
  133. sub_parsers = parser.add_subparsers(
  134. title='commands', dest='command',
  135. description="For more information see qvm-pool command -h")
  136. d_parser = sub_parsers.add_parser(
  137. 'drivers', aliases=('d',), help='List all drivers with their options')
  138. d_parser.set_defaults(func=list_drivers)
  139. init_list_parser(sub_parsers)
  140. init_info_parser(sub_parsers)
  141. init_add_parser(sub_parsers)
  142. init_remove_parser(sub_parsers)
  143. init_set_parser(sub_parsers)
  144. # default action
  145. parser.set_defaults(func=list_pools)
  146. return parser
  147. def main(args=None, app=None):
  148. '''Main routine of :program:`qvm-pool`.'''
  149. parser = get_parser()
  150. args = parser.parse_args(args, app=app)
  151. try:
  152. args.func(args)
  153. except qubesadmin.exc.QubesException as e:
  154. parser.error_runtime(str(e))
  155. return 1
  156. return 0
  157. if __name__ == '__main__':
  158. sys.exit(main())