qvm_check.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. ''' Exits sucessfull if the provided domains exists, else returns failure '''
  22. from __future__ import print_function
  23. import sys
  24. import qubesadmin.tools
  25. import qubesadmin.vm
  26. parser = qubesadmin.tools.QubesArgumentParser(description=__doc__,
  27. vmname_nargs='+')
  28. parser.add_argument("--running", action="store_true", dest="running",
  29. default=False, help="Determine if (any of given) VM is running")
  30. parser.add_argument("--paused", action="store_true", dest="paused",
  31. default=False, help="Determine if (any of given) VM is paused")
  32. parser.add_argument("--template", action="store_true", dest="template",
  33. default=False, help="Determine if (any of given) VM is a template")
  34. def print_msg(domains, what_single, what_plural):
  35. '''Print message in appropriate form about given domain(s)'''
  36. if not domains:
  37. print("None of given VM {!s}".format(what_single))
  38. elif len(domains) == 1:
  39. print("VM {!s} {!s}".format(domains[0], what_single))
  40. else:
  41. txt = ", ".join([vm.name for vm in sorted(domains)])
  42. print("VMs {!s} {!s}".format(txt, what_plural))
  43. def main(args=None, app=None):
  44. '''Main function of qvm-check tool'''
  45. args = parser.parse_args(args, app=app)
  46. domains = args.domains
  47. if args.running:
  48. running = [vm for vm in domains if vm.is_running()]
  49. if args.verbose:
  50. print_msg(running, "is running", "are running")
  51. return 0 if running else 1
  52. elif args.paused:
  53. paused = [vm for vm in domains if vm.is_paused()]
  54. if args.verbose:
  55. print_msg(paused, "is paused", "are paused")
  56. return 0 if paused else 1
  57. elif args.template:
  58. template = [vm for vm in domains if isinstance(vm,
  59. qubesadmin.vm.TemplateVM)]
  60. if args.verbose:
  61. print_msg(template, "is a template", "are templates")
  62. return 0 if template else 1
  63. else:
  64. if args.verbose:
  65. print_msg(domains, "exists", "exist")
  66. return 0 if domains else 1
  67. if __name__ == '__main__':
  68. sys.exit(main())