2018-11-09 15:44:14 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Marta Marczykowska-Górecka
|
|
|
|
# <marmarta@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 Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from qubesadmin import exc
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
from PyQt5 import QtWidgets, QtGui, QtCore # pylint: disable=import-error
|
2018-11-09 15:44:14 +01:00
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
from . import ui_templatemanager # pylint: disable=no-name-in-module
|
2019-09-26 22:31:39 +02:00
|
|
|
from . import utils
|
2018-11-09 15:44:14 +01:00
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
column_names = ['State', 'Qube', 'Current template', 'New template']
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TemplateManagerWindow(
|
2019-05-22 23:10:09 +02:00
|
|
|
ui_templatemanager.Ui_MainWindow, QtWidgets.QMainWindow):
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
def __init__(self, qt_app, qubes_app, dispatcher, parent=None):
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
super(TemplateManagerWindow, self).__init__()
|
|
|
|
self.setupUi(self)
|
|
|
|
|
|
|
|
self.qubes_app = qubes_app
|
|
|
|
self.qt_app = qt_app
|
|
|
|
self.dispatcher = dispatcher
|
|
|
|
|
|
|
|
self.rows_in_table = {}
|
|
|
|
self.templates = []
|
|
|
|
self.timers = []
|
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
self.prepare_lists()
|
2018-11-09 15:44:14 +01:00
|
|
|
self.initialize_table_events()
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).clicked.connect(
|
2018-11-09 15:44:14 +01:00
|
|
|
self.apply)
|
2019-05-22 23:10:09 +02:00
|
|
|
self.buttonBox.button(
|
|
|
|
QtWidgets.QDialogButtonBox.Cancel).clicked.connect(self.cancel)
|
|
|
|
self.buttonBox.button(QtWidgets.QDialogButtonBox.Reset).clicked.connect(
|
2018-11-09 15:44:14 +01:00
|
|
|
self.reset)
|
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
self.change_all_combobox.currentIndexChanged.connect(
|
|
|
|
self.change_all_changed)
|
|
|
|
self.clear_selection_button.clicked.connect(self.clear_selection)
|
|
|
|
|
2018-11-09 15:44:14 +01:00
|
|
|
self.vm_list.show()
|
|
|
|
|
2019-11-08 23:35:26 +01:00
|
|
|
def setup_application(self):
|
|
|
|
self.qt_app.setApplicationName(self.tr("Template Manager"))
|
|
|
|
self.qt_app.setWindowIcon(QtGui.QIcon.fromTheme("qubes-manager"))
|
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
def prepare_lists(self):
|
2018-11-09 15:44:14 +01:00
|
|
|
self.templates = [vm.name for vm in self.qubes_app.domains
|
2018-12-04 00:56:24 +01:00
|
|
|
if vm.klass == 'TemplateVM']
|
|
|
|
|
2019-10-23 18:18:39 +02:00
|
|
|
self.change_all_combobox.addItem(self.tr('(select template)'))
|
2018-12-04 00:56:24 +01:00
|
|
|
for template in self.templates:
|
|
|
|
self.change_all_combobox.addItem(template)
|
|
|
|
|
2018-11-09 15:44:14 +01:00
|
|
|
vms_with_templates = [vm for vm in self.qubes_app.domains
|
2019-01-14 14:57:00 +01:00
|
|
|
if getattr(vm, 'template', None) and
|
|
|
|
vm.klass != 'DispVM']
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
self.vm_list.setColumnCount(len(column_names))
|
|
|
|
self.vm_list.setRowCount(len(vms_with_templates))
|
|
|
|
|
|
|
|
row_count = 0
|
|
|
|
for vm in vms_with_templates:
|
|
|
|
row = VMRow(vm, row_count, self.vm_list, column_names,
|
|
|
|
self.templates)
|
|
|
|
self.rows_in_table[vm.name] = row
|
|
|
|
row_count += 1
|
|
|
|
|
2019-10-23 18:18:39 +02:00
|
|
|
self.vm_list.setHorizontalHeaderLabels(
|
|
|
|
['', self.tr('Qube'), self.tr('Current'), self.tr('New')])
|
2018-11-09 15:44:14 +01:00
|
|
|
self.vm_list.resizeColumnsToContents()
|
|
|
|
|
|
|
|
def initialize_table_events(self):
|
|
|
|
self.vm_list.cellDoubleClicked.connect(self.table_double_click)
|
2018-12-04 00:56:24 +01:00
|
|
|
self.vm_list.cellClicked.connect(self.table_click)
|
|
|
|
|
2018-11-09 15:44:14 +01:00
|
|
|
self.vm_list.horizontalHeader().sortIndicatorChanged.connect(
|
|
|
|
self.sorting_changed)
|
|
|
|
|
|
|
|
self.dispatcher.add_handler('domain-pre-start', self.vm_state_changed)
|
|
|
|
self.dispatcher.add_handler('domain-start-failed',
|
|
|
|
self.vm_state_changed)
|
|
|
|
self.dispatcher.add_handler('domain-stopped', self.vm_state_changed)
|
|
|
|
self.dispatcher.add_handler('domain-shutdown', self.vm_state_changed)
|
|
|
|
|
|
|
|
self.dispatcher.add_handler('domain-add', self.vm_added)
|
|
|
|
self.dispatcher.add_handler('domain-delete', self.vm_removed)
|
|
|
|
|
|
|
|
def vm_added(self, _submitter, _event, vm, **_kwargs):
|
|
|
|
# unfortunately, a VM just in the moment of creation may not have
|
|
|
|
# a template it will have in a second - e.g., when cloning
|
2019-05-22 23:10:09 +02:00
|
|
|
timer = QtCore.QTimer()
|
2018-11-09 15:44:14 +01:00
|
|
|
timer.setSingleShot(True)
|
|
|
|
timer.timeout.connect(lambda: self._vm_added(vm, timer))
|
|
|
|
self.timers.append(timer)
|
|
|
|
timer.start(1000) # 1s
|
|
|
|
|
|
|
|
def _vm_added(self, vm_name, timer):
|
|
|
|
self.timers.remove(timer)
|
|
|
|
try:
|
|
|
|
vm = self.qubes_app.domains[vm_name]
|
2019-01-14 14:57:00 +01:00
|
|
|
if not getattr(vm, 'template', None) or vm.klass == 'DispVM':
|
2018-11-09 15:44:14 +01:00
|
|
|
return
|
|
|
|
except (exc.QubesException, KeyError):
|
|
|
|
return # it was a dispVM that crashed on start
|
|
|
|
|
|
|
|
row_no = self.vm_list.rowCount()
|
|
|
|
self.vm_list.setRowCount(self.vm_list.rowCount() + 1)
|
|
|
|
row = VMRow(vm, row_no, self.vm_list, column_names,
|
|
|
|
self.templates)
|
|
|
|
self.rows_in_table[vm.name] = row
|
|
|
|
self.vm_list.show()
|
|
|
|
|
|
|
|
def vm_removed(self, _submitter, _event, **kwargs):
|
|
|
|
if kwargs['vm'] not in self.rows_in_table:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.vm_list.removeRow(self.rows_in_table[kwargs['vm']].name_item.row())
|
|
|
|
|
|
|
|
def vm_state_changed(self, vm, event, **_kwargs):
|
|
|
|
try:
|
|
|
|
if vm.name not in self.rows_in_table:
|
|
|
|
return
|
|
|
|
except exc.QubesException:
|
|
|
|
return # it was a crashing DispVM or closed DispVM
|
|
|
|
|
|
|
|
if event == 'domain-pre-start':
|
|
|
|
self.rows_in_table[vm.name].vm_state_change(is_running=True)
|
|
|
|
elif event == 'domain-start-failed':
|
|
|
|
self.rows_in_table[vm.name].vm_state_change(is_running=False)
|
|
|
|
elif event == 'domain-stopped':
|
|
|
|
self.rows_in_table[vm.name].vm_state_change(is_running=False)
|
|
|
|
elif event == 'domain-shutdown':
|
|
|
|
self.rows_in_table[vm.name].vm_state_change(is_running=False)
|
|
|
|
|
|
|
|
def sorting_changed(self, index, _order):
|
|
|
|
# this is very much not perfect, but QTableWidget does not
|
|
|
|
# want to be sorted on custom widgets
|
2018-12-04 00:56:24 +01:00
|
|
|
if index == column_names.index('New template') or \
|
|
|
|
index == column_names.index('State'):
|
2018-11-09 15:44:14 +01:00
|
|
|
self.vm_list.horizontalHeader().setSortIndicator(
|
|
|
|
-1, QtCore.Qt.AscendingOrder)
|
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
def clear_selection(self):
|
|
|
|
for row in self.rows_in_table.values():
|
2018-12-07 18:07:45 +01:00
|
|
|
if row.checkbox:
|
|
|
|
row.checkbox.setChecked(False)
|
2018-12-04 00:56:24 +01:00
|
|
|
|
|
|
|
def change_all_changed(self):
|
|
|
|
if self.change_all_combobox.currentIndex() == 0:
|
|
|
|
return
|
|
|
|
selected_template = self.change_all_combobox.currentText()
|
|
|
|
|
|
|
|
for row in self.rows_in_table.values():
|
2018-12-07 18:07:45 +01:00
|
|
|
if row.checkbox and row.checkbox.isChecked():
|
2018-12-04 00:56:24 +01:00
|
|
|
row.new_item.setCurrentIndex(
|
|
|
|
row.new_item.findText(selected_template))
|
|
|
|
|
|
|
|
self.change_all_combobox.setCurrentIndex(0)
|
|
|
|
|
2018-11-09 15:44:14 +01:00
|
|
|
def table_double_click(self, row, column):
|
|
|
|
template_column = column_names.index('Current template')
|
|
|
|
|
|
|
|
if column != template_column:
|
|
|
|
return
|
|
|
|
|
|
|
|
template_name = self.vm_list.item(row, column).text()
|
|
|
|
|
|
|
|
for row_number in range(0, self.vm_list.rowCount()):
|
|
|
|
if self.vm_list.item(
|
|
|
|
row_number, template_column).text() == template_name:
|
2018-12-04 00:56:24 +01:00
|
|
|
checkbox = self.vm_list.cellWidget(
|
|
|
|
row_number, column_names.index('State'))
|
|
|
|
if checkbox:
|
|
|
|
if row_number == row:
|
|
|
|
# this is because double click registers as a
|
|
|
|
# single click and a double click
|
|
|
|
checkbox.setChecked(False)
|
|
|
|
else:
|
|
|
|
checkbox.setChecked(True)
|
|
|
|
|
|
|
|
def table_click(self, row, column):
|
|
|
|
if column == column_names.index('New template'):
|
|
|
|
return
|
|
|
|
|
|
|
|
checkbox = self.vm_list.cellWidget(row, column_names.index('State'))
|
|
|
|
if not checkbox:
|
|
|
|
return
|
|
|
|
|
|
|
|
checkbox.setChecked(not checkbox.isChecked())
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
for row in self.rows_in_table.values():
|
2018-12-07 18:07:45 +01:00
|
|
|
if row.new_item:
|
|
|
|
row.new_item.reset_choice()
|
|
|
|
if row.checkbox:
|
|
|
|
row.checkbox.setChecked(False)
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
def cancel(self):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def apply(self):
|
|
|
|
errors = {}
|
|
|
|
for vm, row in self.rows_in_table.items():
|
2018-12-07 18:07:45 +01:00
|
|
|
if row.new_item and row.new_item.changed:
|
2018-11-09 15:44:14 +01:00
|
|
|
try:
|
|
|
|
setattr(self.qubes_app.domains[vm],
|
|
|
|
'template', row.new_item.currentText())
|
|
|
|
except Exception as ex: # pylint: disable=broad-except
|
|
|
|
errors[vm] = str(ex)
|
|
|
|
if errors:
|
|
|
|
error_messages = [vm + ": " + errors[vm] for vm in errors]
|
2019-05-22 23:10:09 +02:00
|
|
|
QtWidgets.QMessageBox.warning(
|
2018-11-09 15:44:14 +01:00
|
|
|
self,
|
|
|
|
self.tr("Errors encountered!"),
|
|
|
|
self.tr(
|
|
|
|
"Errors encountered on template change in the following "
|
|
|
|
"qubes: <br> {}.").format("<br> ".join(error_messages)))
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
class VMNameItem(QtWidgets.QTableWidgetItem):
|
2018-11-09 15:51:22 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2018-11-09 15:44:14 +01:00
|
|
|
def __init__(self, vm):
|
|
|
|
super(VMNameItem, self).__init__()
|
|
|
|
self.vm = vm
|
|
|
|
|
|
|
|
self.setText(self.vm.name)
|
|
|
|
self.setIcon(QtGui.QIcon.fromTheme(vm.label.icon))
|
|
|
|
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
class StatusItem(QtWidgets.QTableWidgetItem):
|
2018-11-09 15:44:14 +01:00
|
|
|
def __init__(self, vm):
|
|
|
|
super(StatusItem, self).__init__()
|
|
|
|
self.vm = vm
|
|
|
|
|
|
|
|
self.state = None
|
|
|
|
|
|
|
|
def set_state(self, is_running):
|
|
|
|
self.state = is_running
|
|
|
|
|
|
|
|
if self.state:
|
|
|
|
self.setIcon(QtGui.QIcon.fromTheme('dialog-warning'))
|
2019-11-08 23:35:26 +01:00
|
|
|
self.setToolTip(QtCore.QCoreApplication.translate(
|
|
|
|
"template-manager", "Cannot change template on a running VM."))
|
2018-11-09 15:44:14 +01:00
|
|
|
else:
|
|
|
|
self.setIcon(QtGui.QIcon())
|
|
|
|
self.setToolTip("")
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if self.state == other.state:
|
|
|
|
return self.vm.name < other.vm.name
|
|
|
|
return self.state < other.state
|
|
|
|
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
class CurrentTemplateItem(QtWidgets.QTableWidgetItem):
|
2018-11-09 15:51:22 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2018-11-09 15:44:14 +01:00
|
|
|
def __init__(self, vm):
|
|
|
|
super(CurrentTemplateItem, self).__init__()
|
|
|
|
self.vm = vm
|
|
|
|
|
|
|
|
self.setText(self.vm.template.name)
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if self.text() == other.text():
|
|
|
|
return self.vm.name < other.vm.name
|
|
|
|
return self.text() < other.text()
|
|
|
|
|
|
|
|
|
2019-05-22 23:10:09 +02:00
|
|
|
class NewTemplateItem(QtWidgets.QComboBox):
|
2018-11-09 15:44:14 +01:00
|
|
|
def __init__(self, vm, templates, table_widget):
|
|
|
|
super(NewTemplateItem, self).__init__()
|
|
|
|
self.vm = vm
|
|
|
|
self.table_widget = table_widget
|
|
|
|
self.changed = False
|
|
|
|
|
2018-11-09 15:51:22 +01:00
|
|
|
for template in templates:
|
|
|
|
self.addItem(template)
|
2018-11-09 15:44:14 +01:00
|
|
|
self.setCurrentIndex(self.findText(vm.template.name))
|
|
|
|
self.start_value = self.currentText()
|
|
|
|
|
|
|
|
self.currentIndexChanged.connect(self.choice_changed)
|
|
|
|
|
|
|
|
def choice_changed(self):
|
|
|
|
if self.currentText() != self.start_value:
|
|
|
|
self.changed = True
|
|
|
|
self.setStyleSheet('font-weight: bold')
|
|
|
|
else:
|
|
|
|
self.changed = False
|
|
|
|
self.setStyleSheet('font-weight: normal')
|
|
|
|
|
|
|
|
def reset_choice(self):
|
|
|
|
self.setCurrentIndex(self.findText(self.start_value))
|
|
|
|
|
|
|
|
|
|
|
|
class VMRow:
|
2018-11-09 15:51:22 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2018-11-09 15:44:14 +01:00
|
|
|
def __init__(self, vm, row_no, table_widget, columns, templates):
|
|
|
|
self.vm = vm
|
2018-12-04 00:56:24 +01:00
|
|
|
self.table_widget = table_widget
|
|
|
|
self.templates = templates
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
# state
|
|
|
|
self.state_item = StatusItem(self.vm)
|
|
|
|
table_widget.setItem(row_no, columns.index('State'), self.state_item)
|
2019-05-22 23:10:09 +02:00
|
|
|
self.checkbox = QtWidgets.QCheckBox()
|
2018-12-04 00:56:24 +01:00
|
|
|
|
|
|
|
# icon and name
|
|
|
|
self.name_item = VMNameItem(self.vm)
|
|
|
|
table_widget.setItem(row_no, columns.index('Qube'), self.name_item)
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
# current template
|
|
|
|
self.current_item = CurrentTemplateItem(self.vm)
|
|
|
|
table_widget.setItem(row_no, columns.index('Current template'),
|
|
|
|
self.current_item)
|
|
|
|
|
|
|
|
# new template
|
2019-10-23 18:18:39 +02:00
|
|
|
self.dummy_new_item = QtWidgets.QTableWidgetItem(
|
|
|
|
QtCore.QCoreApplication.translate("TemplateManager",
|
|
|
|
"qube is running"))
|
2018-11-09 15:44:14 +01:00
|
|
|
self.new_item = NewTemplateItem(self.vm, templates, table_widget)
|
|
|
|
|
|
|
|
table_widget.setItem(row_no, columns.index('New template'),
|
|
|
|
self.dummy_new_item)
|
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
self.vm_state_change(self.vm.is_running(), row_no)
|
2018-11-09 15:44:14 +01:00
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
def vm_state_change(self, is_running, row=None):
|
2018-11-09 15:44:14 +01:00
|
|
|
self.state_item.set_state(is_running)
|
|
|
|
|
2018-12-04 00:56:24 +01:00
|
|
|
if not row:
|
|
|
|
row = 0
|
|
|
|
while row < self.table_widget.rowCount():
|
|
|
|
if self.table_widget.item(
|
|
|
|
row, column_names.index('Qube')).text() == \
|
|
|
|
self.name_item.text():
|
|
|
|
break
|
|
|
|
row += 1
|
|
|
|
|
|
|
|
# hiding cellWidgets does not work in a qTableWidget
|
|
|
|
if not is_running:
|
|
|
|
self.new_item = NewTemplateItem(self.vm, self.templates,
|
|
|
|
self.table_widget)
|
2019-05-22 23:10:09 +02:00
|
|
|
self.checkbox = QtWidgets.QCheckBox()
|
2018-12-04 00:56:24 +01:00
|
|
|
|
|
|
|
self.table_widget.setCellWidget(
|
|
|
|
row, column_names.index('New template'), self.new_item)
|
|
|
|
self.table_widget.setCellWidget(
|
|
|
|
row, column_names.index('State'), self.checkbox)
|
|
|
|
else:
|
|
|
|
new_template = self.table_widget.cellWidget(
|
|
|
|
row, column_names.index('New template'))
|
|
|
|
if new_template:
|
|
|
|
self.table_widget.removeCellWidget(
|
|
|
|
row, column_names.index('New template'))
|
2018-12-07 18:07:45 +01:00
|
|
|
self.new_item = None
|
2018-12-04 00:56:24 +01:00
|
|
|
|
|
|
|
checkbox = self.table_widget.cellWidget(
|
|
|
|
row, column_names.index('State'))
|
|
|
|
if checkbox:
|
|
|
|
self.table_widget.removeCellWidget(
|
|
|
|
row, column_names.index('State'))
|
2018-12-07 18:07:45 +01:00
|
|
|
self.checkbox = None
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-11-08 23:35:26 +01:00
|
|
|
utils.run_asynchronous(TemplateManagerWindow)
|
2018-11-09 15:44:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|