qvm_pool.py 7.4 KB

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