2017-04-21 03:18:07 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
|
2019-07-31 10:37:15 +02:00
|
|
|
# Copyright (C) 2019 Frédéric Pierret <frederic.pierret@qubes-os.org>
|
2017-04-21 03:18:07 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2019-07-31 10:37:15 +02:00
|
|
|
""" Exits sucessfull if the provided domains exists, else returns failure """
|
2017-04-21 03:18:07 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.tools
|
|
|
|
import qubesadmin.vm
|
2017-04-21 03:18:07 +02:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
parser = qubesadmin.tools.QubesArgumentParser(description=__doc__,
|
2019-07-31 10:37:15 +02:00
|
|
|
vmname_nargs='+')
|
2017-04-21 03:18:07 +02:00
|
|
|
parser.add_argument("--running", action="store_true", dest="running",
|
2019-07-31 10:37:15 +02:00
|
|
|
default=False,
|
|
|
|
help="Determine if (any of given) VM is running")
|
2017-04-21 03:18:07 +02:00
|
|
|
parser.add_argument("--paused", action="store_true", dest="paused",
|
2019-07-31 10:37:15 +02:00
|
|
|
default=False,
|
|
|
|
help="Determine if (any of given) VM is paused")
|
2017-04-21 03:18:07 +02:00
|
|
|
parser.add_argument("--template", action="store_true", dest="template",
|
2019-07-31 10:37:15 +02:00
|
|
|
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")
|
2017-04-21 03:18:07 +02:00
|
|
|
|
|
|
|
|
2019-07-31 10:37:15 +02:00
|
|
|
def print_msg(log, domains, status):
|
|
|
|
"""Print message in appropriate form about given domain(s)"""
|
2017-04-28 02:14:55 +02:00
|
|
|
if not domains:
|
2019-07-31 10:37:15 +02:00
|
|
|
log.info("None of qubes: {!s}".format(', '.join(status)))
|
2017-04-21 03:18:07 +02:00
|
|
|
else:
|
2019-07-31 10:37:15 +02:00
|
|
|
for vm in sorted(domains):
|
|
|
|
log.info("{!s}: {!s}".format(vm.name, ', '.join(status)))
|
2017-04-21 03:18:07 +02:00
|
|
|
|
|
|
|
|
2019-07-31 10:37:15 +02:00
|
|
|
def get_filters(args):
|
|
|
|
"""Get status and check functions"""
|
|
|
|
filters = []
|
|
|
|
|
2017-04-21 03:18:07 +02:00
|
|
|
if args.running:
|
2019-07-31 10:37:15 +02:00
|
|
|
filters.append({'status': 'running', 'check': lambda x: x.is_running()})
|
2018-07-16 02:25:25 +02:00
|
|
|
if args.paused:
|
2019-07-31 10:37:15 +02:00
|
|
|
filters.append({'status': 'paused', 'check': lambda x: x.is_paused()})
|
2018-07-16 02:25:25 +02:00
|
|
|
if args.template:
|
2019-07-31 10:37:15 +02:00
|
|
|
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
|
|
|
|
|
2017-04-21 03:18:07 +02:00
|
|
|
if args.verbose:
|
2019-07-31 10:37:15 +02:00
|
|
|
print_msg(log, filtered_domains, status)
|
|
|
|
else:
|
|
|
|
if not domains:
|
|
|
|
return_code = 1
|
|
|
|
elif args.verbose:
|
|
|
|
print_msg(log, domains, ["exists"])
|
|
|
|
|
|
|
|
return return_code
|
|
|
|
|
2017-04-21 03:18:07 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|