qvm_check.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. ''' Exits sucessfull if the provided domains exists, else returns failure '''
  24. from __future__ import print_function
  25. import sys
  26. import qubes.tools
  27. import qubes.vm.templatevm
  28. parser = qubes.tools.QubesArgumentParser(description=__doc__, vmname_nargs='+')
  29. parser.add_argument("--running", action="store_true", dest="running",
  30. default=False, help="Determine if (any of given) VM is running")
  31. parser.add_argument("--paused", action="store_true", dest="paused",
  32. default=False, help="Determine if (any of given) VM is paused")
  33. parser.add_argument("--template", action="store_true", dest="template",
  34. default=False, help="Determine if (any of given) VM is a template")
  35. def print_msg(domains, what_single, what_plural):
  36. if len(domains) == 0:
  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 domains])
  42. print("VMs {!s} {!s}".format(txt, what_plural))
  43. def main(args=None):
  44. args = parser.parse_args(args)
  45. domains = args.domains
  46. if args.running:
  47. running = [vm for vm in domains if vm.is_running()]
  48. if args.verbose:
  49. print_msg(running, "is running", "are running")
  50. return 0 if running else 1
  51. elif args.paused:
  52. paused = [vm for vm in domains if vm.is_paused()]
  53. if args.verbose:
  54. print_msg(paused, "is paused", "are paused")
  55. return 0 if paused else 1
  56. elif args.template:
  57. template = [vm for vm in domains if isinstance(vm,
  58. qubes.vm.templatevm.TemplateVM)]
  59. if args.verbose:
  60. print_msg(template, "is a template", "are templates")
  61. return 0 if template else 1
  62. else:
  63. if args.verbose:
  64. print_msg(domains, "exists", "exist")
  65. return 0 if domains else 1
  66. if __name__ == '__main__':
  67. sys.exit(main())