123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- # pylint: disable=too-few-public-methods
- #
- # The Qubes OS Project, http://www.qubes-os.org
- #
- # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
- # Copyright (C) 2019 Frédéric Pierret <frederic.pierret@qubes-os.org>
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU Lesser General Public License as published by
- # the Free Software Foundation; either version 2.1 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Lesser General Public License for more details.
- #
- # You should have received a copy of the GNU Lesser General Public License along
- # with this program; if not, write to the Free Software Foundation, Inc.,
- # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- #
- """ Exits sucessfull if the provided domains exists, else returns failure """
- import sys
- import qubesadmin.tools
- import qubesadmin.vm
- parser = qubesadmin.tools.QubesArgumentParser(description=__doc__,
- vmname_nargs='+')
- parser.add_argument("--running", action="store_true", dest="running",
- default=False,
- help="Determine if (any of given) VM is running")
- parser.add_argument("--paused", action="store_true", dest="paused",
- default=False,
- help="Determine if (any of given) VM is paused")
- parser.add_argument("--template", action="store_true", dest="template",
- default=False,
- help="Determine if (any of given) VM is a template")
- parser.add_argument("--networked", action="store_true", dest="networked",
- default=False,
- help="Determine if (any of given) VM can reach network")
- def print_msg(log, domains, status):
- """Print message in appropriate form about given domain(s)"""
- if not domains:
- log.info("None of qubes: {!s}".format(', '.join(status)))
- else:
- for vm in sorted(domains):
- log.info("{!s}: {!s}".format(vm.name, ', '.join(status)))
- def get_filters(args):
- """Get status and check functions"""
- filters = []
- if args.running:
- filters.append({'status': 'running', 'check': lambda x: x.is_running()})
- if args.paused:
- filters.append({'status': 'paused', 'check': lambda x: x.is_paused()})
- if args.template:
- filters.append(
- {'status': 'template', 'check': lambda x: x.klass == 'TemplateVM'})
- if args.networked:
- filters.append(
- {'status': 'networked', 'check': lambda x: x.is_networked()})
- return filters
- def main(args=None, app=None):
- """Main function of qvm-check tool"""
- args = parser.parse_args(args, app=app)
- domains = args.domains
- return_code = 0
- log = args.app.log
- log.name = "qvm-check"
- status = []
- filters = get_filters(args)
- filtered_domains = set(domains)
- if filters:
- for filt in filters:
- status.append(filt['status'])
- check = filt['check']
- filtered_domains = filtered_domains.intersection(
- [vm for vm in domains if check(vm)])
- filtered_domains = list(filtered_domains)
- if set(domains) & set(filtered_domains) != set(domains):
- if not filtered_domains:
- return_code = 1
- else:
- return_code = 3
- if args.verbose:
- print_msg(log, filtered_domains, status)
- else:
- if not domains:
- return_code = 1
- elif args.verbose:
- print_msg(log, domains, ["exists"])
- return return_code
- if __name__ == '__main__':
- sys.exit(main())
|