2017-03-08 21:53:46 +01:00
|
|
|
# encoding=utf-8
|
2017-03-01 15:21:55 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2011-2016 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2016 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
2017-03-08 21:53:46 +01:00
|
|
|
# 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
|
2017-03-01 15:21:55 +01:00
|
|
|
# (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
|
2017-03-08 21:53:46 +01:00
|
|
|
# GNU Lesser General Public License for more details.
|
2017-03-01 15:21:55 +01:00
|
|
|
#
|
2017-03-08 21:53:46 +01:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
''' Shutdown a qube '''
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2017-06-25 21:03:06 +02:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
try:
|
|
|
|
import qubesadmin.events.utils
|
|
|
|
have_events = True
|
|
|
|
except ImportError:
|
|
|
|
have_events = False
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.tools
|
|
|
|
import qubesadmin.exc
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
parser = qubesadmin.tools.QubesArgumentParser(
|
2017-03-01 15:21:55 +01:00
|
|
|
description=__doc__, vmname_nargs='+')
|
|
|
|
|
|
|
|
parser.add_argument('--wait',
|
|
|
|
action='store_true', default=False,
|
|
|
|
help='wait for the VMs to shut down')
|
|
|
|
|
|
|
|
parser.add_argument('--timeout',
|
|
|
|
action='store', type=float,
|
|
|
|
default=60,
|
|
|
|
help='timeout after which domains are killed when using --wait'
|
2017-07-27 19:04:22 +02:00
|
|
|
' (default: %(default)d)')
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
|
2017-03-08 23:41:39 +01:00
|
|
|
def main(args=None, app=None): # pylint: disable=missing-docstring
|
|
|
|
args = parser.parse_args(args, app=app)
|
2017-03-01 15:21:55 +01:00
|
|
|
|
2020-01-21 22:20:36 +01:00
|
|
|
force = bool(args.all_domains)
|
|
|
|
|
2017-06-25 21:03:06 +02:00
|
|
|
if have_events:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
remaining_domains = args.domains
|
|
|
|
for _ in range(len(args.domains)):
|
|
|
|
this_round_domains = set(remaining_domains)
|
|
|
|
if not this_round_domains:
|
|
|
|
break
|
|
|
|
remaining_domains = set()
|
|
|
|
for vm in this_round_domains:
|
|
|
|
try:
|
2020-01-21 22:20:36 +01:00
|
|
|
vm.shutdown(force=force)
|
2017-06-25 21:03:06 +02:00
|
|
|
except qubesadmin.exc.QubesVMNotRunningError:
|
|
|
|
pass
|
|
|
|
except qubesadmin.exc.QubesException as e:
|
|
|
|
if not args.wait:
|
|
|
|
vm.log.error('Shutdown error: {}'.format(e))
|
|
|
|
else:
|
|
|
|
remaining_domains.add(vm)
|
|
|
|
if not args.wait:
|
2020-01-16 12:31:55 +01:00
|
|
|
if remaining_domains:
|
|
|
|
parser.error_runtime(
|
|
|
|
'Failed to shut down: ' +
|
|
|
|
', '.join(vm.name for vm in remaining_domains),
|
|
|
|
len(remaining_domains))
|
|
|
|
return
|
2017-06-25 21:03:06 +02:00
|
|
|
this_round_domains.difference_update(remaining_domains)
|
|
|
|
if not this_round_domains:
|
|
|
|
# no VM shutdown request succeed, no sense to try again
|
|
|
|
break
|
|
|
|
if have_events:
|
|
|
|
try:
|
|
|
|
# pylint: disable=no-member
|
|
|
|
loop.run_until_complete(asyncio.wait_for(
|
|
|
|
qubesadmin.events.utils.wait_for_domain_shutdown(
|
2017-12-10 22:52:29 +01:00
|
|
|
this_round_domains),
|
2017-06-25 21:03:06 +02:00
|
|
|
args.timeout))
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
for vm in this_round_domains:
|
2017-10-28 22:40:24 +02:00
|
|
|
try:
|
|
|
|
vm.kill()
|
|
|
|
except qubesadmin.exc.QubesVMNotStartedError:
|
|
|
|
# already shut down
|
|
|
|
pass
|
2018-03-18 21:38:49 +01:00
|
|
|
except qubesadmin.exc.QubesException as e:
|
|
|
|
parser.error_runtime(e)
|
2017-06-25 21:03:06 +02:00
|
|
|
else:
|
|
|
|
timeout = args.timeout
|
|
|
|
current_vms = list(sorted(this_round_domains))
|
|
|
|
while timeout >= 0:
|
|
|
|
current_vms = [vm for vm in current_vms
|
|
|
|
if vm.get_power_state() != 'Halted']
|
|
|
|
if not current_vms:
|
|
|
|
break
|
|
|
|
args.app.log.info('Waiting for shutdown ({}): {}'.format(
|
|
|
|
timeout, ', '.join([str(vm) for vm in current_vms])))
|
|
|
|
time.sleep(1)
|
|
|
|
timeout -= 1
|
|
|
|
if current_vms:
|
|
|
|
args.app.log.info(
|
|
|
|
'Killing remaining qubes: {}'
|
|
|
|
.format(', '.join([str(vm) for vm in current_vms])))
|
|
|
|
for vm in current_vms:
|
2017-10-28 22:40:24 +02:00
|
|
|
try:
|
|
|
|
vm.kill()
|
|
|
|
except qubesadmin.exc.QubesVMNotStartedError:
|
|
|
|
# already shut down
|
|
|
|
pass
|
2018-03-18 21:38:49 +01:00
|
|
|
except qubesadmin.exc.QubesException as e:
|
|
|
|
parser.error_runtime(e)
|
2017-06-25 21:03:06 +02:00
|
|
|
|
|
|
|
if args.wait:
|
|
|
|
if have_events:
|
|
|
|
loop.close()
|
2020-01-16 12:31:55 +01:00
|
|
|
failed = []
|
|
|
|
for vm in args.domains:
|
|
|
|
power_state = vm.get_power_state()
|
|
|
|
# DispVM might have been deleted before we check them,
|
|
|
|
# so NA is acceptable.
|
|
|
|
if not (power_state == 'Halted' or
|
|
|
|
(vm.klass == 'DispVM' and power_state == 'NA')):
|
|
|
|
failed.append(vm)
|
|
|
|
if failed:
|
|
|
|
parser.error_runtime(
|
|
|
|
'Failed to shut down: ' +
|
|
|
|
', '.join(vm.name for vm in failed),
|
|
|
|
len(failed))
|
|
|
|
|
2017-03-01 15:21:55 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|