#!/usr/bin/python3 # # The Qubes OS Project, http://www.qubes-os.org # # Copyright (C) 2012 Agnieszka Kostrzewa # Copyright (C) 2012 Marek Marczykowski-Górecki # # Copyright (C) 2017 Wojtek Porczyk # # 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 Lesser General Public License along # with this program; if not, see . # # import sys import os import os.path import signal import subprocess import time from datetime import datetime, timedelta import traceback from qubesadmin import Qubes from PyQt4 import QtGui from PyQt4 import QtCore from . import ui_vtmanager from . import thread_monitor from . import table_widgets from . import settings from . import global_settings from . import restore from . import backup import threading from qubesmanager.about import AboutDialog # TODO: probably unneeded class QMVmState: ErrorMsg = 1 AudioRecAvailable = 2 AudioRecAllowed = 3 class SearchBox(QtGui.QLineEdit): def __init__(self, parent=None): super(SearchBox, self).__init__(parent) self.focusing = False def focusInEvent(self, e): super(SearchBox, self).focusInEvent(e) self.selectAll() self.focusing = True def mousePressEvent(self, e): super(SearchBox, self).mousePressEvent(e) if self.focusing: self.selectAll() self.focusing = False class VmRowInTable(object): def __init__(self, vm, row_no, table): self.vm = vm self.row_no = row_no # TODO: replace a million different widgets with a more generic # VmFeatureWidget or VMPropertyWidget table_widgets.row_height = VmManagerWindow.row_height table.setRowHeight(row_no, VmManagerWindow.row_height) self.type_widget = table_widgets.VmTypeWidget(vm) table.setCellWidget(row_no, VmManagerWindow.columns_indices['Type'], self.type_widget) table.setItem(row_no, VmManagerWindow.columns_indices['Type'], self.type_widget.tableItem) self.label_widget = table_widgets.VmLabelWidget(vm) table.setCellWidget(row_no, VmManagerWindow.columns_indices['Label'], self.label_widget) table.setItem(row_no, VmManagerWindow.columns_indices['Label'], self.label_widget.tableItem) self.name_widget = table_widgets.VmNameItem(vm) table.setItem(row_no, VmManagerWindow.columns_indices['Name'], self.name_widget) self.info_widget = table_widgets.VmInfoWidget(vm) table.setCellWidget(row_no, VmManagerWindow.columns_indices['State'], self.info_widget) table.setItem(row_no, VmManagerWindow.columns_indices['State'], self.info_widget.tableItem) self.template_widget = table_widgets.VmTemplateItem(vm) table.setItem(row_no, VmManagerWindow.columns_indices['Template'], self.template_widget) self.netvm_widget = table_widgets.VmNetvmItem(vm) table.setItem(row_no, VmManagerWindow.columns_indices['NetVM'], self.netvm_widget) self.size_widget = table_widgets.VmSizeOnDiskItem(vm) table.setItem(row_no, VmManagerWindow.columns_indices['Size'], self.size_widget) self.internal_widget = table_widgets.VmInternalItem(vm) table.setItem(row_no, VmManagerWindow.columns_indices['Internal'], self.internal_widget) self.ip_widget = table_widgets.VmIPItem(vm) table.setItem(row_no, VmManagerWindow.columns_indices['IP'], self.ip_widget) self.include_in_backups_widget = \ table_widgets.VmIncludeInBackupsItem(vm) table.setItem(row_no, VmManagerWindow.columns_indices[ 'Backups'], self.include_in_backups_widget) self.last_backup_widget = table_widgets.VmLastBackupItem(vm) table.setItem(row_no, VmManagerWindow.columns_indices[ 'Last backup'], self.last_backup_widget) def update(self, update_size_on_disk=False): """ Update info in a single VM row :param update_size_on_disk: should disk utilization be updated? the widget will extract the data from VM object :return: None """ self.info_widget.update_vm_state(self.vm) if update_size_on_disk: self.size_widget.update() vm_shutdown_timeout = 20000 # in msec vm_restart_check_timeout = 1000 # in msec class VmShutdownMonitor(QtCore.QObject): def __init__(self, vm, shutdown_time=vm_shutdown_timeout, check_time=vm_restart_check_timeout, and_restart=False, caller=None): QtCore.QObject.__init__(self) self.vm = vm self.shutdown_time = shutdown_time self.check_time = check_time self.and_restart = and_restart self.shutdown_started = datetime.now() self.caller = caller def restart_vm_if_needed(self): if self.and_restart and self.caller: self.caller.start_vm(self.vm) # TODO: can i kill running vm def check_again_later(self): # noinspection PyTypeChecker,PyCallByClass QtCore.QTimer.singleShot(self.check_time, self.check_if_vm_has_shutdown) def timeout_reached(self): actual = datetime.now() - self.shutdown_started allowed = timedelta(milliseconds=self.shutdown_time) return actual > allowed def check_if_vm_has_shutdown(self): vm = self.vm vm_is_running = vm.is_running() vm_start_time = vm.get_start_time() if vm_is_running and vm_start_time \ and vm_start_time < self.shutdown_started: if self.timeout_reached(): reply = QtGui.QMessageBox.question( None, self.tr("VM Shutdown"), self.tr( "The VM '{0}' hasn't shutdown within the last " "{1} seconds, do you want to kill it?
").format( vm.name, self.shutdown_time / 1000), self.tr("Kill it!"), self.tr("Wait another {0} seconds...").format( self.shutdown_time / 1000)) if reply == 0: vm.force_shutdown() self.restart_vm_if_needed() else: self.shutdown_started = datetime.now() self.check_again_later() else: self.check_again_later() else: if vm_is_running: # Due to unknown reasons, Xen sometimes reports that a domain # is running even though its start-up timestamp is not valid. # Make sure that "restart_vm_if_needed" is not called until # the domain has been completely shut down according to Xen. self.check_again_later() return self.restart_vm_if_needed() class VmManagerWindow(ui_vtmanager.Ui_VmManagerWindow, QtGui.QMainWindow): row_height = 30 column_width = 200 min_visible_rows = 10 search = "" # suppress saving settings while initializing widgets settings_loaded = False columns_indices = {"Type": 0, "Label": 1, "Name": 2, "State": 3, "Template": 4, "NetVM": 5, "Size": 6, "Internal": 7, "IP": 8, "Backups": 9, "Last backup": 10, } def __init__(self, qvm_collection, parent=None): super(VmManagerWindow, self).__init__() self.setupUi(self) self.toolbar = self.toolBar self.manager_settings = QtCore.QSettings(self) self.qvm_collection = qvm_collection self.searchbox = SearchBox() # TODO check if this works self.searchbox.setValidator(QtGui.QRegExpValidator( QtCore.QRegExp("[a-zA-Z0-9-]*", QtCore.Qt.CaseInsensitive), None)) self.searchContainer.addWidget(self.searchbox) self.connect(self.table, QtCore.SIGNAL("itemSelectionChanged()"), self.table_selection_changed) self.table.setColumnWidth(0, self.column_width) self.sort_by_column = "Type" self.sort_order = QtCore.Qt.AscendingOrder self.screen_number = -1 self.screen_changed = False self.vms_list = [] self.vms_in_table = {} self.reload_table = False self.vm_errors = {} self.vm_rec = {} self.frame_width = 0 self.frame_height = 0 self.move(self.x(), 0) self.columns_actions = { self.columns_indices["Type"]: self.action_vm_type, self.columns_indices["Label"]: self.action_label, self.columns_indices["Name"]: self.action_name, self.columns_indices["State"]: self.action_state, self.columns_indices["Template"]: self.action_template, self.columns_indices["NetVM"]: self.action_netvm, self.columns_indices["Size"]: self.action_size_on_disk, self.columns_indices["Internal"]: self.action_internal, self.columns_indices["IP"]: self .action_ip, self.columns_indices["Backups"]: self .action_backups, self.columns_indices["Last backup"]: self .action_last_backup } # TODO: make refresh button self.visible_columns_count = len(self.columns_indices) self.table.setColumnHidden(self.columns_indices["Size"], True) self.action_size_on_disk.setChecked(False) self.table.setColumnHidden(self.columns_indices["Internal"], True) self.action_internal.setChecked(False) self.table.setColumnHidden(self.columns_indices["IP"], True) self.action_ip.setChecked(False) self.table.setColumnHidden(self.columns_indices["Backups"], True) self.action_backups.setChecked(False) self.table.setColumnHidden(self.columns_indices["Last backup"], True) self.action_last_backup.setChecked(False) self.table.setColumnWidth(self.columns_indices["State"], 80) self.table.setColumnWidth(self.columns_indices["Name"], 150) self.table.setColumnWidth(self.columns_indices["Label"], 40) self.table.setColumnWidth(self.columns_indices["Type"], 40) self.table.setColumnWidth(self.columns_indices["Size"], 100) self.table.setColumnWidth(self.columns_indices["Internal"], 60) self.table.setColumnWidth(self.columns_indices["IP"], 100) self.table.setColumnWidth(self.columns_indices["Backups"], 60) self.table.setColumnWidth(self.columns_indices["Last backup"], 90) self.table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Fixed) self.table.sortItems(self.columns_indices[self.sort_by_column], self.sort_order) self.context_menu = QtGui.QMenu(self) # TODO: check if this works, check all options self.context_menu.addAction(self.action_settings) self.context_menu.addAction(self.action_editfwrules) self.context_menu.addAction(self.action_appmenus) self.context_menu.addAction(self.action_set_keyboard_layout) self.context_menu.addAction(self.action_toggle_audio_input) self.context_menu.addSeparator() self.context_menu.addAction(self.action_updatevm) self.context_menu.addAction(self.action_run_command_in_vm) self.context_menu.addAction(self.action_resumevm) self.context_menu.addAction(self.action_startvm_tools_install) self.context_menu.addAction(self.action_pausevm) self.context_menu.addAction(self.action_shutdownvm) self.context_menu.addAction(self.action_restartvm) self.context_menu.addAction(self.action_killvm) self.context_menu.addSeparator() self.context_menu.addAction(self.action_clonevm) self.context_menu.addAction(self.action_removevm) self.context_menu.addSeparator() self.context_menu.addMenu(self.logs_menu) self.context_menu.addSeparator() self.tools_context_menu = QtGui.QMenu(self) self.tools_context_menu.addAction(self.action_toolbar) self.tools_context_menu.addAction(self.action_menubar) self.table_selection_changed() self.connect( self.table.horizontalHeader(), QtCore.SIGNAL("sortIndicatorChanged(int, Qt::SortOrder)"), self.sortIndicatorChanged) self.connect(self.table, QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"), self.open_context_menu) self.connect(self.menubar, QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"), lambda pos: self.open_tools_context_menu(self.menubar, pos)) self.connect(self.toolBar, QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"), lambda pos: self.open_tools_context_menu(self.toolBar, pos)) self.connect(self.logs_menu, QtCore.SIGNAL("triggered(QAction *)"), self.show_log) self.connect(self.searchbox, QtCore.SIGNAL("textChanged(const QString&)"), self.do_search) self.table.setContentsMargins(0, 0, 0, 0) self.centralwidget.layout().setContentsMargins(0, 0, 0, 0) self.layout().setContentsMargins(0, 0, 0, 0) self.connect(self.action_menubar, QtCore.SIGNAL("toggled(bool)"), self.showhide_menubar) self.connect(self.action_toolbar, QtCore.SIGNAL("toggled(bool)"), self.showhide_toolbar) self.load_manager_settings() self.fill_table() self.counter = 0 self.update_size_on_disk = False self.shutdown_monitor = {} self.last_measure_results = {} self.last_measure_time = time.time() def load_manager_settings(self): # visible columns # self.manager_settings.beginGroup("columns") for col in self.columns_indices.keys(): col_no = self.columns_indices[col] visible = self.manager_settings.value( 'columns/%s' % col, defaultValue=not self.table.isColumnHidden(col_no)) self.columns_actions[col_no].setChecked(visible == "true") # self.manager_settings.endGroup() self.sort_by_column = str( self.manager_settings.value("view/sort_column", defaultValue=self.sort_by_column)) self.sort_order = QtCore.Qt.SortOrder( self.manager_settings.value("view/sort_order", defaultValue=self.sort_order)[ 0]) self.table.sortItems(self.columns_indices[self.sort_by_column], self.sort_order) if not self.manager_settings.value("view/menubar_visible", defaultValue=True): self.action_menubar.setChecked(False) if not self.manager_settings.value("view/toolbar_visible", defaultValue=True): self.action_toolbar.setChecked(False) self.settings_loaded = True def show(self): super(VmManagerWindow, self).show() self.screen_number = app.desktop().screenNumber(self) def domain_state_changed_callback(self, name=None, uuid=None): if name is not None: vm = self.qvm_collection.domains[name] if vm: vm.refresh() def get_vms_list(self): return [vm for vm in self.qvm_collection.domains] def fill_table(self): # save current selection row_index = self.table.currentRow() selected_qid = -1 if row_index != -1: vm_item = self.table.item(row_index, self.columns_indices["Name"]) if vm_item: selected_qid = vm_item.qid self.table.setSortingEnabled(False) self.table.clearContents() vms_list = self.get_vms_list() self.table.setRowCount(len(vms_list)) vms_in_table = {} row_no = 0 for vm in vms_list: # if vm.internal: # continue vm_row = VmRowInTable(vm, row_no, self.table) vms_in_table[vm.qid] = vm_row row_no += 1 self.table.setRowCount(row_no) self.vms_list = vms_list self.vms_in_table = vms_in_table self.reload_table = False if selected_qid in vms_in_table.keys(): self.table.setCurrentItem( self.vms_in_table[selected_qid].name_widget) self.table.setSortingEnabled(True) self.showhide_vms() def showhide_vms(self): # TODO: just show all all the time? if not self.search: for row_no in range(self.table.rowCount()): self.table.setRowHidden(row_no, False) else: for row_no in range(self.table.rowCount()): widget = self.table.cellWidget(row_no, self.columns_indices["State"]) show = (self.search in widget.vm.name or not self.search) self.table.setRowHidden(row_no, not show) @QtCore.pyqtSlot(str) def do_search(self, search): self.search = str(search) self.showhide_vms() # self.set_table_geom_size() @QtCore.pyqtSlot(name='on_action_search_triggered') def action_search_triggered(self): self.searchbox.setFocus() def mark_table_for_update(self): self.reload_table = True # When calling update_table() directly, always use out_of_schedule=True! def update_table(self, out_of_schedule=False): reload_table = self.reload_table if manager_window.isVisible(): # some_vms_have_changed_power_state = False # for vm in self.vms_list: # state = vm.get_power_state() # if vm.last_power_state != state: # if state == "Running" and \ # self.vm_errors.get(vm.qid, "") \ # .startswith("Error starting VM:"): # self.clear_error(vm.qid) # prev_running = vm.last_running # vm.last_power_state = state # vm.last_running = vm.is_running() # self.update_audio_rec_info(vm) # if not prev_running and vm.last_running: # self.running_vms_count += 1 # some_vms_have_changed_power_state = True # # Clear error state when VM just started # self.clear_error(vm.qid) # elif prev_running and not vm.last_running: # # FIXME: remove when recAllowed state will be preserved # if self.vm_rec.has_key(vm.name): # self.vm_rec.pop(vm.name) # self.running_vms_count -= 1 # some_vms_have_changed_power_state = True # else: # # pulseaudio agent register itself some time after VM # # startup # if state == "Running" and not vm.qubes_manager_state[ # QMVmState.AudioRecAvailable]: # self.update_audio_rec_info(vm) # if self.vm_errors.get(vm.qid, "") == \ # "Error starting VM: Cannot execute qrexec-daemon!" \ # and vm.is_qrexec_running(): # self.clear_error(vm.qid) pass if self.screen_changed: reload_table = True self.screen_changed = False if reload_table: self.fill_table() update_devs = True # if self.sort_by_column == \ # "State" and some_vms_have_changed_power_state: # self.table.sortItems(self.columns_indices[self.sort_by_column], # self.sort_order) if (not self.table.isColumnHidden(self.columns_indices['Size'])) \ and self.counter % 60 == 0 or out_of_schedule: self.update_size_on_disk = True for vm_row in self.vms_in_table.values(): vm_row.update(update_size_on_disk=self.update_size_on_disk) # TODO: fix these for saner opts TODO2: is it fixed? if self.sort_by_column in ["CPU", "State", "Size", "Internal"]: # "State": needed to sort after reload (fill_table sorts items # with setSortingEnabled, but by that time the widgets values # are not correct yet). self.table.sortItems(self.columns_indices[self.sort_by_column], self.sort_order) self.table_selection_changed() self.update_size_on_disk = False # noinspection PyPep8Naming @QtCore.pyqtSlot(bool, str) def recAllowedChanged(self, state, vmname): self.vm_rec[str(vmname)] = bool(state) # noinspection PyPep8Naming def sortIndicatorChanged(self, column, order): self.sort_by_column = [name for name in self.columns_indices.keys() if self.columns_indices[name] == column][0] self.sort_order = order if self.settings_loaded: self.manager_settings.setValue('view/sort_column', self.sort_by_column) self.manager_settings.setValue('view/sort_order', self.sort_order) self.manager_settings.sync() def table_selection_changed(self): # TODO: and this should actually work, fixit vm = self.get_selected_vm() if vm is not None: # Update available actions: self.action_settings.setEnabled(vm.qid != 0) self.action_removevm.setEnabled(vm.klass != 'AdminVM' and not vm.is_running()) # TODO: think about this self.action_clonevm.setEnabled(vm.klass != 'AdminVM') self.action_resumevm.setEnabled(vm.get_power_state() == "Paused") # TODO: check try: pass # self.action_startvm_tools_install.setVisible( # isinstance(vm, QubesHVm)) except NameError: # ignore non existing QubesHVm pass self.action_startvm_tools_install.setEnabled( getattr(vm, 'updateable', False)) # TODO: add qvm boot from device to this and add there windows tools self.action_pausevm.setEnabled(vm.get_power_state() != "Paused" and vm.qid != 0) self.action_shutdownvm.setEnabled(vm.get_power_state() != "Paused" and vm.qid != 0) self.action_restartvm.setEnabled(vm.get_power_state() != "Paused" and vm.qid != 0 and vm.klass != 'DisposableVM') self.action_killvm.setEnabled(vm.get_power_state() == "Paused" and vm.qid != 0) # TODO: check conditions self.action_appmenus.setEnabled( vm.klass != 'AdminVM' and vm.klass != 'DisposableMV' and not vm.features.get('internal', False)) self.action_editfwrules.setEnabled(True) # TODO: remove this and make sure the option is enabled in designer # TODO: this should work # self.action_updatevm.setEnabled(vm.is_updateable() or vm.qid == 0) # TODO: this should work # self.action_toggle_audio_input.setEnabled( # vm.qubes_manager_state[QMVmState.AudioRecAvailable]) self.action_run_command_in_vm.setEnabled( not vm.get_power_state() == "Paused" and vm.qid != 0) self.action_set_keyboard_layout.setEnabled( vm.qid != 0 and vm.get_power_state() != "Paused") else: self.action_settings.setEnabled(False) self.action_removevm.setEnabled(False) self.action_startvm_tools_install.setVisible(False) self.action_startvm_tools_install.setEnabled(False) self.action_clonevm.setEnabled(False) self.action_resumevm.setEnabled(False) self.action_pausevm.setEnabled(False) self.action_shutdownvm.setEnabled(False) self.action_restartvm.setEnabled(False) self.action_killvm.setEnabled(False) self.action_appmenus.setEnabled(False) self.action_editfwrules.setEnabled(False) self.action_updatevm.setEnabled(False) self.action_toggle_audio_input.setEnabled(False) self.action_run_command_in_vm.setEnabled(False) self.action_set_keyboard_layout.setEnabled(False) def set_error(self, qid, message): for vm in self.vms_list: if vm.qid == qid: vm.qubes_manager_state[QMVmState.ErrorMsg] = message # Store error in separate dict to make it immune to VM list reload self.vm_errors[qid] = str(message) def clear_error(self, qid): self.vm_errors.pop(qid, None) for vm in self.vms_list: if vm.qid == qid: vm.qubes_manager_state[QMVmState.ErrorMsg] = None def clear_error_exact(self, qid, message): for vm in self.vms_list: if vm.qid == qid: if vm.qubes_manager_state[QMVmState.ErrorMsg] == message: vm.qubes_manager_state[QMVmState.ErrorMsg] = None self.vm_errors.pop(qid, None) @QtCore.pyqtSlot(name='on_action_createvm_triggered') def action_createvm_triggered(self): # TODO: this should work subprocess.check_call('qubes-vm-create') def get_selected_vm(self): # vm selection relies on the VmInfo widget's value used # for sorting by VM name row_index = self.table.currentRow() if row_index != -1: vm_item = self.table.item(row_index, self.columns_indices["Name"]) # here is possible race with update_table timer so check # if really got the item if vm_item is None: return None qid = vm_item.qid assert self.vms_in_table[qid] is not None vm = self.vms_in_table[qid].vm return vm else: return None @QtCore.pyqtSlot(name='on_action_removevm_triggered') def action_removevm_triggered(self): # TODO: this should work vm = self.get_selected_vm() assert not vm.is_running() assert not vm.installed_by_rpm vm = self.qvm_collection[vm.qid] if vm.is_template(): dependent_vms = 0 for single_vm in self.qvm_collection.domains: if getattr(single_vm, 'template', None) == vm: dependent_vms += 1 if dependent_vms > 0: QtGui.QMessageBox.warning( None, self.tr("Warning!"), self.tr("This Template VM cannot be removed, " "because there is at least one AppVM that is based " "on it.
If you want to remove this " "Template VM and all the AppVMs based on it, you " "should first remove each individual AppVM that " "uses this template.")) return (requested_name, ok) = QtGui.QInputDialog.getText( None, self.tr("VM Removal Confirmation"), self.tr("Are you sure you want to remove the VM '{0}'?
" "All data on this VM's private storage will be lost!" "

Type the name of the VM ({1}) below to " "confirm:").format(vm.name, vm.name)) if not ok: # user clicked cancel return elif requested_name != vm.name: # name did not match QtGui.QMessageBox.warning( None, self.tr("VM removal confirmation failed"), self.tr( "Entered name did not match! Not removing " "{0}.").format(vm.name)) return else: # remove the VM t_monitor = thread_monitor.ThreadMonitor() thread = threading.Thread(target=self.do_remove_vm, args=(vm, self.qvm_collection, t_monitor)) thread.daemon = True thread.start() progress = QtGui.QProgressDialog( self.tr("Removing VM: {0}...").format(vm.name), "", 0, 0) progress.setCancelButton(None) progress.setModal(True) progress.show() while not t_monitor.is_finished(): app.processEvents() time.sleep(0.1) progress.hide() if t_monitor.success: pass else: QtGui.QMessageBox.warning(None, self.tr("Error removing VM!"), self.tr("ERROR: {0}").format( t_monitor.error_msg)) @staticmethod def do_remove_vm(vm, qvm_collection, t_monitor): try: del qvm_collection.domains[vm.name] except Exception as ex: t_monitor.set_error_msg(str(ex)) t_monitor.set_finished() @QtCore.pyqtSlot(name='on_action_clonevm_triggered') def action_clonevm_triggered(self): # TODO: this should work vm = self.get_selected_vm() name_number = 1 name_format = vm.name + '-clone-%d' while self.qvm_collection.domains[name_format % name_number]: name_number += 1 (clone_name, ok) = QtGui.QInputDialog.getText( self, self.tr('Qubes clone VM'), self.tr('Enter name for VM {} clone:').format(vm.name), text=(name_format % name_number)) if not ok or clone_name == "": return t_monitor = thread_monitor.ThreadMonitor() thread = threading.Thread(target=self.do_clone_vm, args=(vm, self.qvm_collection, clone_name, t_monitor)) thread.daemon = True thread.start() progress = QtGui.QProgressDialog( self.tr("Cloning VM {0} to {1}...").format( vm.name, clone_name), "", 0, 0) progress.setCancelButton(None) progress.setModal(True) progress.show() while not t_monitor.is_finished(): app.processEvents() time.sleep(0.2) progress.hide() if not t_monitor.success: QtGui.QMessageBox.warning( None, self.tr("Error while cloning VM"), self.tr("Exception while cloning:
{0}").format( t_monitor.error_msg)) @staticmethod def do_clone_vm(src_vm, qvm_collection, dst_name, t_monitor): dst_vm = None try: dst_vm = qvm_collection.clone_vm(src_vm, dst_name) except Exception as ex: if dst_vm: pass # TODO: should I remove any remnants? t_monitor.set_error_msg(str(ex)) t_monitor.set_finished() @QtCore.pyqtSlot(name='on_action_resumevm_triggered') def action_resumevm_triggered(self): vm = self.get_selected_vm() if vm.get_power_state() in ["Paused", "Suspended"]: try: vm.unpause() except Exception as ex: QtGui.QMessageBox.warning(None, self.tr("Error unpausing VM!"), self.tr("ERROR: {0}").format(ex)) return self.start_vm(vm) def start_vm(self, vm): # TODO: this should work assert not vm.is_running() t_monitor = thread_monitor.ThreadMonitor() thread = threading.Thread(target=self.do_start_vm, args=(vm, t_monitor)) thread.daemon = True thread.start() while not t_monitor.is_finished(): app.processEvents() time.sleep(0.1) if not t_monitor.success: self.set_error( vm.qid, self.tr("Error starting VM: %s") % t_monitor.error_msg) @staticmethod def do_start_vm(vm, t_monitor): try: vm.start() except Exception as ex: t_monitor.set_error_msg(str(ex)) t_monitor.set_finished() return t_monitor.set_finished() @QtCore.pyqtSlot(name='on_action_startvm_tools_install_triggered') # TODO: replace with boot from device def action_startvm_tools_install_triggered(self): vm = self.get_selected_vm() assert not vm.is_running() windows_tools_installed = \ os.path.exists('/usr/lib/qubes/qubes-windows-tools.iso') if not windows_tools_installed: msg = QtGui.QMessageBox() msg.warning(self, self.tr("Error starting VM!"), self.tr("You need to install 'qubes-windows-tools' " "package to use this option")) return t_monitor = thread_monitor.ThreadMonitor() thread = threading.Thread(target=self.do_start_vm_tools_install, args=(vm, t_monitor)) thread.daemon = True thread.start() while not t_monitor.is_finished(): app.processEvents() time.sleep(0.1) if not t_monitor.success: self.set_error( vm.qid, self.tr("Error starting VM: %s") % t_monitor.error_msg) # noinspection PyMethodMayBeStatic def do_start_vm_tools_install(self, vm, t_monitor): # TODO: should this work? prev_drive = vm.drive try: vm.drive = 'cdrom:dom0:/usr/lib/qubes/qubes-windows-tools.iso' vm.start() except Exception as ex: t_monitor.set_error_msg(str(ex)) t_monitor.set_finished() return finally: vm.drive = prev_drive t_monitor.set_finished() @QtCore.pyqtSlot(name='on_action_pausevm_triggered') def action_pausevm_triggered(self): vm = self.get_selected_vm() assert vm.is_running() try: vm.pause() except Exception as ex: QtGui.QMessageBox.warning( None, self.tr("Error pausing VM!"), self.tr("ERROR: {0}").format(ex)) return @QtCore.pyqtSlot(name='on_action_shutdownvm_triggered') def action_shutdownvm_triggered(self): vm = self.get_selected_vm() assert vm.is_running() reply = QtGui.QMessageBox.question( None, self.tr("VM Shutdown Confirmation"), self.tr("Are you sure you want to power down the VM" " '{0}'?
This will shutdown all the " "running applications within this VM.").format( vm.name), QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel) app.processEvents() # TODO: is this needed?? if reply == QtGui.QMessageBox.Yes: self.shutdown_vm(vm) def shutdown_vm(self, vm, shutdown_time=vm_shutdown_timeout, check_time=vm_restart_check_timeout, and_restart=False): try: vm.shutdown() except Exception as ex: QtGui.QMessageBox.warning( None, self.tr("Error shutting down VM!"), self.tr("ERROR: {0}").format(ex)) return self.shutdown_monitor[vm.qid] = VmShutdownMonitor(vm, shutdown_time, check_time, and_restart, self) # noinspection PyCallByClass,PyTypeChecker QtCore.QTimer.singleShot(check_time, self.shutdown_monitor[ vm.qid].check_if_vm_has_shutdown) @QtCore.pyqtSlot(name='on_action_restartvm_triggered') def action_restartvm_triggered(self): vm = self.get_selected_vm() assert vm.is_running() reply = QtGui.QMessageBox.question( None, self.tr("VM Restart Confirmation"), self.tr("Are you sure you want to restart the VM '{0}'?
" "This will shutdown all the running applications " "within this VM.").format(vm.name), QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel) app.processEvents() if reply == QtGui.QMessageBox.Yes: self.shutdown_vm(vm, and_restart=True) @QtCore.pyqtSlot(name='on_action_killvm_triggered') def action_killvm_triggered(self): vm = self.get_selected_vm() assert vm.is_running() or vm.is_paused() reply = QtGui.QMessageBox.question( None, self.tr("VM Kill Confirmation"), self.tr("Are you sure you want to kill the VM '{0}'?
" "This will end (not shutdown!) all the " "running applications within this VM.").format( vm.name), QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) app.processEvents() if reply == QtGui.QMessageBox.Yes: try: vm.force_shutdown() except Exception as ex: QtGui.QMessageBox.critical( None, self.tr("Error while killing VM!"), self.tr( "An exception ocurred while killing {0}.
" "ERROR: {1}").format(vm.name, ex)) return @QtCore.pyqtSlot(name='on_action_settings_triggered') def action_settings_triggered(self): vm = self.get_selected_vm() settings_window = settings.VMSettingsWindow(vm, app, "basic") settings_window.exec_() @QtCore.pyqtSlot(name='on_action_appmenus_triggered') def action_appmenus_triggered(self): pass # TODO this should work, actually, but please not now # vm = self.get_selected_vm() # settings_window = VMSettingsWindow(vm, app, self.qvm_collection, # "applications") # settings_window.exec_() @QtCore.pyqtSlot(name='on_action_updatevm_triggered') def action_updatevm_triggered(self): vm = self.get_selected_vm() if not vm.is_running(): reply = QtGui.QMessageBox.question( None, self.tr("VM Update Confirmation"), self.tr("{0}
The VM has to be running to be updated.
" "Do you want to start it?
").format(vm.name), QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel) if reply != QtGui.QMessageBox.Yes: return app.processEvents() t_monitor = thread_monitor.ThreadMonitor() thread = threading.Thread(target=self.do_update_vm, args=(vm, t_monitor)) thread.daemon = True thread.start() progress = QtGui.QProgressDialog( self.tr("{0}
Please wait for the updater to " "launch...").format(vm.name), "", 0, 0) progress.setCancelButton(None) progress.setModal(True) progress.show() while not t_monitor.is_finished(): app.processEvents() time.sleep(0.2) progress.hide() if vm.qid != 0: if not t_monitor.success: QtGui.QMessageBox.warning(None, self.tr("Error VM update!"), self.tr("ERROR: {0}").format( t_monitor.error_msg)) @staticmethod def do_update_vm(vm, thread_monitor): #TODO: fixme try: if vm.qid == 0: subprocess.check_call( ["/usr/bin/qubes-dom0-update", "--clean", "--gui"]) else: if not vm.is_running(): vm.start() vm.run_service("qubes.InstallUpdatesGUI", gui=True, user="root", wait=False) except Exception as ex: thread_monitor.set_error_msg(str(ex)) thread_monitor.set_finished() return thread_monitor.set_finished() @QtCore.pyqtSlot(name='on_action_run_command_in_vm_triggered') def action_run_command_in_vm_triggered(self): vm = self.get_selected_vm() (command_to_run, ok) = QtGui.QInputDialog.getText( self, self.tr('Qubes command entry'), self.tr('Run command in {}:').format(vm.name)) if not ok or command_to_run == "": return t_monitor = thread_monitor.ThreadMonitor() thread = threading.Thread(target=self.do_run_command_in_vm, args=( vm, command_to_run, t_monitor)) thread.daemon = True thread.start() while not t_monitor.is_finished(): app.processEvents() time.sleep(0.2) if not t_monitor.success: QtGui.QMessageBox.warning(None, self.tr("Error while running command"), self.tr("Exception while running command:
{0}").format( t_monitor.error_msg)) @staticmethod def do_run_command_in_vm(vm, command_to_run, t_monitor): try: vm.run(command_to_run, verbose=False, autostart=True) except Exception as ex: t_monitor.set_error_msg(str(ex)) t_monitor.set_finished() @QtCore.pyqtSlot(name='on_action_set_keyboard_layout_triggered') def action_set_keyboard_layout_triggered(self): vm = self.get_selected_vm() vm.run('qubes-change-keyboard-layout', verbose=False) #TODO: remove showallvms / show inactive vms / show internal vms @QtCore.pyqtSlot(name='on_action_editfwrules_triggered') def action_editfwrules_triggered(self): vm = self.get_selected_vm() settings_window = settings.VMSettingsWindow(vm, app, "firewall") settings_window.exec_() @QtCore.pyqtSlot(name='on_action_global_settings_triggered') def action_global_settings_triggered(self): global_settings_window = global_settings.GlobalSettingsWindow( app, self.qvm_collection) global_settings_window.exec_() @QtCore.pyqtSlot(name='on_action_show_network_triggered') def action_show_network_triggered(self): pass #TODO: revive TODO: what is this thing?? # network_notes_dialog = NetworkNotesDialog() # network_notes_dialog.exec_() @QtCore.pyqtSlot(name='on_action_restore_triggered') def action_restore_triggered(self): restore_window = restore.RestoreVMsWindow(app, self.qvm_collection) restore_window.exec_() @QtCore.pyqtSlot(name='on_action_backup_triggered') def action_backup_triggered(self): backup_window = backup.BackupVMsWindow(app, self.qvm_collection) backup_window.exec_() def showhide_menubar(self, checked): self.menubar.setVisible(checked) # self.set_table_geom_size() if not checked: self.context_menu.addAction(self.action_menubar) else: self.context_menu.removeAction(self.action_menubar) if self.settings_loaded: self.manager_settings.setValue('view/menubar_visible', checked) self.manager_settings.sync() def showhide_toolbar(self, checked): self.toolbar.setVisible(checked) # self.set_table_geom_size() if not checked: self.context_menu.addAction(self.action_toolbar) else: self.context_menu.removeAction(self.action_toolbar) if self.settings_loaded: self.manager_settings.setValue('view/toolbar_visible', checked) self.manager_settings.sync() def showhide_column(self, col_num, show): self.table.setColumnHidden(col_num, not show) val = 1 if show else -1 self.visible_columns_count += val if self.visible_columns_count == 1: # TODO: is this working at all?? # disable hiding the last one for c in self.columns_actions: if self.columns_actions[c].isChecked(): self.columns_actions[c].setEnabled(False) break elif self.visible_columns_count == 2 and val == 1: # TODO: likewise?? # enable hiding previously disabled column for c in self.columns_actions: if not self.columns_actions[c].isEnabled(): self.columns_actions[c].setEnabled(True) break if self.settings_loaded: col_name = [name for name in self.columns_indices.keys() if self.columns_indices[name] == col_num][0] self.manager_settings.setValue('columns/%s' % col_name, show) self.manager_settings.sync() def on_action_vm_type_toggled(self, checked): self.showhide_column(self.columns_indices['Type'], checked) def on_action_label_toggled(self, checked): self.showhide_column(self.columns_indices['Label'], checked) def on_action_name_toggled(self, checked): self.showhide_column(self.columns_indices['Name'], checked) def on_action_state_toggled(self, checked): self.showhide_column(self.columns_indices['State'], checked) def on_action_internal_toggled(self, checked): self.showhide_column(self.columns_indices['Internal'], checked) def on_action_ip_toggled(self, checked): self.showhide_column(self.columns_indices['IP'], checked) def on_action_backups_toggled(self, checked): self.showhide_column(self.columns_indices['Backups'], checked) def on_action_last_backup_toggled(self, checked): self.showhide_column(self.columns_indices['Last backup'], checked) def on_action_template_toggled(self, checked): self.showhide_column(self.columns_indices['Template'], checked) def on_action_netvm_toggled(self, checked): self.showhide_column(self.columns_indices['NetVM'], checked) def on_action_size_on_disk_toggled(self, checked): self.showhide_column(self.columns_indices['Size'], checked) @QtCore.pyqtSlot(name='on_action_about_qubes_triggered') def action_about_qubes_triggered(self): about = AboutDialog() about.exec_() def createPopupMenu(self): menu = QtGui.QMenu() menu.addAction(self.action_toolbar) menu.addAction(self.action_menubar) return menu def open_tools_context_menu(self, widget, point): self.tools_context_menu.exec_(widget.mapToGlobal(point)) @QtCore.pyqtSlot('const QPoint&') def open_context_menu(self, point): vm = self.get_selected_vm() running = vm.is_running() # logs menu self.logs_menu.clear() if vm.qid == 0: logfiles = ["/var/log/xen/console/hypervisor.log"] else: logfiles = [ "/var/log/xen/console/guest-" + vm.name + ".log", "/var/log/xen/console/guest-" + vm.name + "-dm.log", "/var/log/qubes/guid." + vm.name + ".log", "/var/log/qubes/qrexec." + vm.name + ".log", ] menu_empty = True for logfile in logfiles: if os.path.exists(logfile): action = self.logs_menu.addAction(QtGui.QIcon(":/log.png"), logfile) action.setData(QtCore.QVariant(logfile)) menu_empty = False self.logs_menu.setEnabled(not menu_empty) self.context_menu.exec_(self.table.mapToGlobal(point)) @QtCore.pyqtSlot('QAction *') def show_log(self, action): pass #TODO fixme # log = str(action.data()) # log_dialog = LogDialog(app, log) # log_dialog.exec_() def show_manager(): manager_window.show() manager_window.repaint() manager_window.update_table(out_of_schedule=True) app.processEvents() def exit_app(): app.exit() # Bases on the original code by: # Copyright (c) 2002-2007 Pascal Varet def handle_exception(exc_type, exc_value, exc_traceback): for f in traceback.extract_stack(exc_traceback): print(f[0], f[1]) filename, line, dummy, dummy = traceback.extract_tb(exc_traceback).pop() filename = os.path.basename(filename) error = "%s: %s" % (exc_type.__name__, exc_value) QtGui.QMessageBox.critical( None, "Houston, we have a problem...", "Whoops. A critical error has occured. This is most likely a bug " "in Volume and Template Manager application.

%s" % error + "at line %d of file %s.

" % (line, filename)) def sighup_handler(signum, frame): os.execl("/usr/bin/qubes-manager", "qubes-manager") def main(): signal.signal(signal.SIGHUP, sighup_handler) global app app = QtGui.QApplication(sys.argv) app.setOrganizationName("The Qubes Project") app.setOrganizationDomain("http://qubes-os.org") app.setApplicationName("Qubes VM Manager") app.setWindowIcon(QtGui.QIcon.fromTheme("qubes-manager")) sys.excepthook = handle_exception qvm_collection = Qubes() global manager_window manager_window = VmManagerWindow(qvm_collection) # global wm # wm = WatchManager() # global notifier # # notifier = ThreadedNotifier(wm, QubesManagerFileWatcher( # # manager_window.mark_table_for_update)) # notifier.start() # wm.add_watch(system_path["qubes_store_filename"], # EventsCodes.OP_FLAGS.get('IN_MODIFY')) # wm.add_watch(os.path.dirname(system_path["qubes_store_filename"]), # EventsCodes.OP_FLAGS.get('IN_MOVED_TO')) # if os.path.exists(qubes_clipboard_info_file): # wm.add_watch(qubes_clipboard_info_file, # EventsCodes.OP_FLAGS.get('IN_CLOSE_WRITE')) # wm.add_watch(os.path.dirname(qubes_clipboard_info_file), # EventsCodes.OP_FLAGS.get('IN_CREATE')) # wm.add_watch(os.path.dirname(table_widgets.qubes_dom0_updates_stat_file), # EventsCodes.OP_FLAGS.get('IN_CREATE')) threading.currentThread().setName("QtMainThread") show_manager() app.exec_() if __name__ == "__main__": main() # TODO: change file name to something better