qvm_check.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 General Public License as published by
  9. # the Free Software Foundation; either version 2 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 General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU 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 qubes.tools
  25. import qubes.vm.templatevm
  26. parser = qubes.tools.QubesArgumentParser(description=__doc__, vmname_nargs='+')
  27. parser.add_argument("--running", action="store_true", dest="running",
  28. default=False, help="Determine if (any of given) VM is running")
  29. parser.add_argument("--paused", action="store_true", dest="paused",
  30. default=False, help="Determine if (any of given) VM is paused")
  31. parser.add_argument("--template", action="store_true", dest="template",
  32. default=False, help="Determine if (any of given) VM is a template")
  33. def print_msg(domains, what_single, what_plural):
  34. if len(domains) == 0:
  35. print("None of given VM {!s}".format(what_single))
  36. elif len(domains) == 1:
  37. print("VM {!s} {!s}".format(domains[0], what_single))
  38. else:
  39. txt = ", ".join([vm.name for vm in domains])
  40. print("VMs {!s} {!s}".format(txt, what_plural))
  41. def main(args=None):
  42. args = parser.parse_args(args)
  43. domains = args.domains
  44. if args.running:
  45. running = [vm for vm in domains if vm.is_running()]
  46. if args.verbose:
  47. print_msg(running, "is running", "are running")
  48. return 0 if running else 1
  49. elif args.paused:
  50. paused = [vm for vm in domains if vm.is_paused()]
  51. if args.verbose:
  52. print_msg(paused, "is paused", "are paused")
  53. return 0 if paused else 1
  54. elif args.template:
  55. template = [vm for vm in domains if isinstance(vm,
  56. qubes.vm.templatevm.TemplateVM)]
  57. if args.verbose:
  58. print_msg(template, "is a template", "are templates")
  59. return 0 if template else 1
  60. else:
  61. if args.verbose:
  62. print_msg(domains, "exists", "exist")
  63. return 0 if domains else 1
  64. if __name__ == '__main__':
  65. sys.exit(main())