qvm_check.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. # Copyright (C) 2019 Frédéric Pierret <frederic.pierret@qubes-os.org>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. """ Exits sucessfull if the provided domains exists, else returns failure """
  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,
  30. help="Determine if (any of given) VM is running")
  31. parser.add_argument("--paused", action="store_true", dest="paused",
  32. default=False,
  33. help="Determine if (any of given) VM is paused")
  34. parser.add_argument("--template", action="store_true", dest="template",
  35. default=False,
  36. help="Determine if (any of given) VM is a template")
  37. parser.add_argument("--networked", action="store_true", dest="networked",
  38. default=False,
  39. help="Determine if (any of given) VM can reach network")
  40. def print_msg(log, domains, status):
  41. """Print message in appropriate form about given domain(s)"""
  42. if not domains:
  43. log.info("None of qubes: {!s}".format(', '.join(status)))
  44. else:
  45. for vm in sorted(domains):
  46. log.info("{!s}: {!s}".format(vm.name, ', '.join(status)))
  47. def get_filters(args):
  48. """Get status and check functions"""
  49. filters = []
  50. if args.running:
  51. filters.append({'status': 'running', 'check': lambda x: x.is_running()})
  52. if args.paused:
  53. filters.append({'status': 'paused', 'check': lambda x: x.is_paused()})
  54. if args.template:
  55. filters.append(
  56. {'status': 'template', 'check': lambda x: x.klass == 'TemplateVM'})
  57. if args.networked:
  58. filters.append(
  59. {'status': 'networked', 'check': lambda x: x.is_networked()})
  60. return filters
  61. def main(args=None, app=None):
  62. """Main function of qvm-check tool"""
  63. args = parser.parse_args(args, app=app)
  64. domains = args.domains
  65. return_code = 0
  66. log = args.app.log
  67. log.name = "qvm-check"
  68. status = []
  69. filters = get_filters(args)
  70. filtered_domains = set(domains)
  71. if filters:
  72. for filt in filters:
  73. status.append(filt['status'])
  74. check = filt['check']
  75. filtered_domains = filtered_domains.intersection(
  76. [vm for vm in domains if check(vm)])
  77. filtered_domains = list(filtered_domains)
  78. if set(domains) & set(filtered_domains) != set(domains):
  79. if not filtered_domains:
  80. return_code = 1
  81. else:
  82. return_code = 3
  83. if args.verbose:
  84. print_msg(log, filtered_domains, status)
  85. else:
  86. if not domains:
  87. return_code = 1
  88. elif args.verbose:
  89. print_msg(log, domains, ["exists"])
  90. return return_code
  91. if __name__ == '__main__':
  92. sys.exit(main())