2013-01-20 21:44:13 +01:00
|
|
|
#!/usr/bin/python2
|
2010-04-05 20:58:57 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU 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.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection
|
|
|
|
from qubes.qubes import QubesException
|
2012-07-05 01:30:00 +02:00
|
|
|
from qubes.guihelpers import notify_error_qubes_manager
|
2010-04-05 20:58:57 +02:00
|
|
|
from optparse import OptionParser
|
|
|
|
import subprocess
|
|
|
|
import socket
|
|
|
|
import errno
|
|
|
|
import dbus
|
|
|
|
import time
|
2011-10-07 21:56:58 +02:00
|
|
|
import sys
|
2011-03-07 15:58:04 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
notify_object = None
|
|
|
|
|
|
|
|
# how long (in sec) to wait for VMs to shutdown
|
|
|
|
# before killing them (when used with --wait option)
|
2013-03-15 18:43:09 +01:00
|
|
|
from qubes.qubes import defaults
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def tray_notify(str, label, timeout = 3000):
|
2013-12-15 22:51:42 +01:00
|
|
|
if notify_object:
|
|
|
|
notify_object.Notify("Qubes", 0, label.icon, "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def tray_notify_error(str, timeout = 3000):
|
2013-12-15 22:51:42 +01:00
|
|
|
if notify_object:
|
|
|
|
notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def vm_run_cmd(vm, cmd, options):
|
|
|
|
if options.shutdown:
|
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Shutting down VM: '{0}'...".format(vm.name)
|
2011-10-17 22:15:10 +02:00
|
|
|
try:
|
|
|
|
vm.shutdown(force=options.force)
|
|
|
|
except (QubesException) as err:
|
|
|
|
# "There are other VMs connected to this VM:"
|
|
|
|
print >> sys.stderr, "ERROR: {0}".format(err)
|
|
|
|
if str(err).startswith("There are other VMs connected"):
|
|
|
|
print >> sys.stderr, "Shutdown them first or use --force switch"
|
|
|
|
exit(1)
|
2013-03-25 16:23:26 +01:00
|
|
|
return 0
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2010-07-08 12:41:29 +02:00
|
|
|
if options.pause:
|
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Pausing VM: '{0}'...".format(vm.name)
|
2012-03-08 12:03:49 +01:00
|
|
|
vm.pause()
|
2013-03-25 16:23:26 +01:00
|
|
|
return 0
|
2010-07-08 12:41:29 +02:00
|
|
|
|
|
|
|
if options.unpause:
|
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "UnPausing VM: '{0}'...".format(vm.name)
|
2012-03-08 12:03:49 +01:00
|
|
|
vm.unpause()
|
2013-03-25 16:23:26 +01:00
|
|
|
return 0
|
2010-07-08 12:41:29 +02:00
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Running command on VM: '{0}'...".format(vm.name)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-10-31 13:31:17 +01:00
|
|
|
try:
|
2011-11-02 00:46:57 +01:00
|
|
|
def tray_notify_generic(level, str):
|
|
|
|
if level == "info":
|
|
|
|
tray_notify(str, label=vm.label)
|
|
|
|
elif level == "error":
|
|
|
|
tray_notify_error(str)
|
|
|
|
|
2012-02-10 20:45:43 +01:00
|
|
|
return vm.run(cmd, autostart = options.auto,
|
2011-10-31 13:31:17 +01:00
|
|
|
verbose = options.verbose,
|
2012-11-13 04:28:09 +01:00
|
|
|
user = options.user,
|
2011-10-31 13:31:17 +01:00
|
|
|
notify_function = tray_notify_generic if options.tray else None,
|
2012-06-15 17:37:19 +02:00
|
|
|
passio = options.passio, localcmd = options.localcmd, gui = options.gui)
|
2011-10-31 13:31:17 +01:00
|
|
|
except QubesException as err:
|
2012-04-24 01:40:56 +02:00
|
|
|
if options.tray:
|
|
|
|
tray_notify_error(str(err))
|
2012-07-05 01:30:00 +02:00
|
|
|
notify_error_qubes_manager(vm.name, str(err))
|
2012-08-01 01:01:37 +02:00
|
|
|
print >> sys.stderr, "ERROR(%s): %s" % (str(vm.name), str(err))
|
|
|
|
return 1
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = "usage: %prog [options] [<vm-name>] [<cmd>]"
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
|
|
|
|
parser.add_option ("-a", "--auto", action="store_true", dest="auto", default=False,
|
|
|
|
help="Auto start the VM if not running")
|
2012-06-13 17:16:12 +02:00
|
|
|
parser.add_option ("-u", "--user", action="store", dest="user", default=None,
|
2010-04-05 20:58:57 +02:00
|
|
|
help="Run command in a VM as a specified user")
|
|
|
|
parser.add_option ("--tray", action="store_true", dest="tray", default=False,
|
|
|
|
help="Use tray notifications instead of stdout" )
|
|
|
|
|
|
|
|
parser.add_option ("--all", action="store_true", dest="run_on_all_running", default=False,
|
2010-07-08 12:41:29 +02:00
|
|
|
help="Run command on all currently running VMs (or all paused, in case of --unpause)")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
parser.add_option ("--exclude", action="append", dest="exclude_list",
|
2014-01-23 04:50:14 +01:00
|
|
|
help="When --all is used: exclude this VM name (may be "
|
|
|
|
"repeated)")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
parser.add_option ("--wait", action="store_true", dest="wait_for_shutdown", default=False,
|
|
|
|
help="Wait for the VM(s) to shutdown")
|
|
|
|
|
|
|
|
parser.add_option ("--shutdown", action="store_true", dest="shutdown", default=False,
|
2012-01-16 03:28:26 +01:00
|
|
|
help="(deprecated) Do 'xl shutdown' for the VM(s) (can be combined this with --all and --wait)")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2010-07-08 12:41:29 +02:00
|
|
|
parser.add_option ("--pause", action="store_true", dest="pause", default=False,
|
2011-06-01 23:44:06 +02:00
|
|
|
help="Do 'xl pause' for the VM(s) (can be combined this with --all and --wait)")
|
2010-07-08 12:41:29 +02:00
|
|
|
|
|
|
|
parser.add_option ("--unpause", action="store_true", dest="unpause", default=False,
|
2011-06-01 23:44:06 +02:00
|
|
|
help="Do 'xl unpause' for the VM(s) (can be combined this with --all and --wait)")
|
2010-07-08 12:41:29 +02:00
|
|
|
|
2012-02-01 16:04:52 +01:00
|
|
|
parser.add_option ("-p", "--pass-io", action="store_true", dest="passio", default=False,
|
2013-11-18 01:15:17 +01:00
|
|
|
help="Pass stdin/stdout/stderr from remote program (implies -q)")
|
2011-03-07 15:58:04 +01:00
|
|
|
|
|
|
|
parser.add_option ("--localcmd", action="store", dest="localcmd", default=None,
|
2012-02-01 16:04:52 +01:00
|
|
|
help="With --pass-io, pass stdin/stdout/stderr to the given program")
|
2010-07-08 12:41:29 +02:00
|
|
|
|
2011-04-23 02:32:54 +02:00
|
|
|
parser.add_option ("--force", action="store_true", dest="force", default=False,
|
|
|
|
help="Force operation, even if may damage other VMs (eg shutdown of NetVM)")
|
|
|
|
|
2012-06-15 17:37:19 +02:00
|
|
|
parser.add_option ("--nogui", action="store_false", dest="gui", default=True,
|
|
|
|
help="Run command without gui")
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
|
2013-11-18 01:15:17 +01:00
|
|
|
if options.passio and options.run_on_all_running:
|
|
|
|
parser.error ("Options --all and --pass-io cannot be used together")
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-07-17 13:56:09 +02:00
|
|
|
if options.passio:
|
|
|
|
options.verbose = False
|
|
|
|
|
2012-01-16 03:28:26 +01:00
|
|
|
if options.shutdown:
|
|
|
|
print >>sys.stderr, "WARNING: --shutdown is deprecated. Use qvm-shutdown instead."
|
|
|
|
|
2010-07-08 12:41:29 +02:00
|
|
|
if (options.shutdown or options.pause or options.unpause):
|
|
|
|
takes_cmd_argument = False
|
|
|
|
else:
|
|
|
|
takes_cmd_argument = True
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
if options.run_on_all_running:
|
2010-07-08 12:41:29 +02:00
|
|
|
if len(args) < 1 and takes_cmd_argument:
|
2010-04-05 20:58:57 +02:00
|
|
|
parser.error ("You must provide a command to execute on all the VMs.")
|
2010-07-08 12:41:29 +02:00
|
|
|
if len(args) > 1 or ((not takes_cmd_argument) and len(args) > 0):
|
2010-04-05 20:58:57 +02:00
|
|
|
parser.error ("To many arguments...")
|
2010-07-08 12:41:29 +02:00
|
|
|
cmdstr = args[0] if takes_cmd_argument else None
|
2010-04-05 20:58:57 +02:00
|
|
|
else:
|
2010-07-08 12:41:29 +02:00
|
|
|
if len (args) < 1 and not takes_cmd_argument:
|
|
|
|
parser.error ("You must specify the VM name to shutdown/pause/unpause.")
|
|
|
|
if len (args) < 2 and takes_cmd_argument:
|
2010-04-05 20:58:57 +02:00
|
|
|
parser.error ("You must specify the VM name and the command to execute in the VM.")
|
2010-07-08 12:41:29 +02:00
|
|
|
if len (args) > 2 or ((not takes_cmd_argument) and len(args) > 1):
|
2010-04-05 20:58:57 +02:00
|
|
|
parser.error ("To many arguments...")
|
|
|
|
vmname = args[0]
|
2010-07-08 12:41:29 +02:00
|
|
|
cmdstr = args[1] if takes_cmd_argument else None
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
if options.tray:
|
|
|
|
global notify_object
|
2013-12-15 22:51:42 +01:00
|
|
|
try:
|
|
|
|
notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
|
|
|
|
except dbus.DBusException as ex:
|
|
|
|
print >>sys.stderr, "WARNING: failed connect to tray notification service: %s" % str(ex)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_reading()
|
|
|
|
qvm_collection.load()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
|
|
|
vms_list = []
|
|
|
|
if options.run_on_all_running:
|
|
|
|
all_vms = [vm for vm in qvm_collection.values()]
|
|
|
|
for vm in all_vms:
|
|
|
|
if options.exclude_list is not None and vm.name in options.exclude_list:
|
|
|
|
continue
|
|
|
|
if vm.qid == 0:
|
|
|
|
continue
|
2010-07-08 12:41:29 +02:00
|
|
|
if (options.unpause and vm.is_paused()) or (not options.unpause and vm.is_running()):
|
2010-04-05 20:58:57 +02:00
|
|
|
vms_list.append (vm)
|
2011-10-31 13:31:17 +01:00
|
|
|
# disable options incompatible with --all
|
|
|
|
options.passio = False
|
2010-04-05 20:58:57 +02:00
|
|
|
else:
|
|
|
|
vm = qvm_collection.get_vm_by_name(vmname)
|
|
|
|
if vm is None:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "A VM with the name '{0}' does not exist in the system!".format(vmname)
|
2010-04-05 20:58:57 +02:00
|
|
|
exit(1)
|
|
|
|
vms_list.append(vm)
|
|
|
|
|
2013-03-25 16:23:26 +01:00
|
|
|
retcode = 0
|
2010-04-05 20:58:57 +02:00
|
|
|
for vm in vms_list:
|
2013-03-25 16:23:26 +01:00
|
|
|
r = vm_run_cmd(vm, cmdstr, options)
|
|
|
|
retcode = max(r, retcode)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
if options.wait_for_shutdown:
|
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Waiting for the VM(s) to shutdown..."
|
2010-04-05 20:58:57 +02:00
|
|
|
shutdown_counter = 0
|
|
|
|
|
|
|
|
while len (vms_list):
|
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Waiting for VMs: ", [vm.name for vm in vms_list]
|
2010-04-05 20:58:57 +02:00
|
|
|
for vm in vms_list:
|
|
|
|
if not vm.is_running():
|
|
|
|
vms_list.remove (vm)
|
2013-03-15 18:43:09 +01:00
|
|
|
if shutdown_counter > defaults["shutdown_counter_max"]:
|
2010-04-05 20:58:57 +02:00
|
|
|
# kill the VM
|
|
|
|
if options.verbose:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Killing the (apparently hanging) VM '{0}'...".format(vm.name)
|
2010-04-05 20:58:57 +02:00
|
|
|
vm.force_shutdown()
|
|
|
|
#vms_list.remove(vm)
|
|
|
|
|
|
|
|
shutdown_counter += 1
|
|
|
|
time.sleep (1)
|
|
|
|
exit (0) # there is no point in executing the other daemons in the case of --wait
|
2013-03-25 16:23:26 +01:00
|
|
|
exit(retcode)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
main()
|