qube_manager.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
  6. # Copyright (C) 2012 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. # Copyright (C) 2017 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License
  12. # as published by the Free Software Foundation; either version 2
  13. # of the License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License along
  21. # with this program; if not, see <http://www.gnu.org/licenses/>.
  22. #
  23. #
  24. import os
  25. import os.path
  26. import subprocess
  27. from datetime import datetime, timedelta
  28. from qubesadmin import exc
  29. from qubesadmin import utils
  30. from PyQt5 import QtWidgets, QtCore, QtGui # pylint: disable=import-error
  31. from qubesmanager.about import AboutDialog
  32. from . import ui_qubemanager # pylint: disable=no-name-in-module
  33. from . import table_widgets
  34. from . import settings
  35. from . import global_settings
  36. from . import restore
  37. from . import backup
  38. from . import create_new_vm
  39. from . import log_dialog
  40. from . import utils as manager_utils
  41. from . import common_threads
  42. class SearchBox(QtWidgets.QLineEdit):
  43. def __init__(self, parent=None):
  44. super(SearchBox, self).__init__(parent)
  45. self.focusing = False
  46. def focusInEvent(self, e): # pylint: disable=invalid-name
  47. super(SearchBox, self).focusInEvent(e)
  48. self.selectAll()
  49. self.focusing = True
  50. def mousePressEvent(self, e): # pylint: disable=invalid-name
  51. super(SearchBox, self).mousePressEvent(e)
  52. if self.focusing:
  53. self.selectAll()
  54. self.focusing = False
  55. class VmRowInTable:
  56. # pylint: disable=too-few-public-methods,too-many-instance-attributes
  57. def __init__(self, vm, row_no, table):
  58. self.vm = vm
  59. table_widgets.row_height = VmManagerWindow.row_height
  60. table.setRowHeight(row_no, VmManagerWindow.row_height)
  61. self.type_widget = table_widgets.VmTypeWidget(vm)
  62. table.setCellWidget(row_no, VmManagerWindow.columns_indices['Type'],
  63. self.type_widget)
  64. table.setItem(row_no, VmManagerWindow.columns_indices['Type'],
  65. self.type_widget.table_item)
  66. self.label_widget = table_widgets.VmLabelWidget(vm)
  67. table.setCellWidget(row_no, VmManagerWindow.columns_indices['Label'],
  68. self.label_widget)
  69. table.setItem(row_no, VmManagerWindow.columns_indices['Label'],
  70. self.label_widget.table_item)
  71. self.name_widget = table_widgets.VMPropertyItem(vm, "name")
  72. table.setItem(row_no, VmManagerWindow.columns_indices['Name'],
  73. self.name_widget)
  74. self.info_widget = table_widgets.VmInfoWidget(vm)
  75. table.setCellWidget(row_no, VmManagerWindow.columns_indices['State'],
  76. self.info_widget)
  77. table.setItem(row_no, VmManagerWindow.columns_indices['State'],
  78. self.info_widget.table_item)
  79. self.template_widget = table_widgets.VmTemplateItem(vm)
  80. table.setItem(row_no, VmManagerWindow.columns_indices['Template'],
  81. self.template_widget)
  82. self.netvm_widget = table_widgets.VMPropertyItem(vm, "netvm",
  83. check_default=True)
  84. table.setItem(row_no, VmManagerWindow.columns_indices['NetVM'],
  85. self.netvm_widget)
  86. self.size_widget = table_widgets.VmSizeOnDiskItem(vm)
  87. table.setItem(row_no, VmManagerWindow.columns_indices['Size'],
  88. self.size_widget)
  89. self.internal_widget = table_widgets.VmInternalItem(vm)
  90. table.setItem(row_no, VmManagerWindow.columns_indices['Internal'],
  91. self.internal_widget)
  92. self.ip_widget = table_widgets.VMPropertyItem(vm, "ip")
  93. table.setItem(row_no, VmManagerWindow.columns_indices['IP'],
  94. self.ip_widget)
  95. self.include_in_backups_widget = table_widgets.VMPropertyItem(
  96. vm, "include_in_backups",
  97. empty_function=(lambda x: not bool(x)))
  98. table.setItem(row_no, VmManagerWindow.columns_indices[
  99. 'Include in backups'], self.include_in_backups_widget)
  100. self.last_backup_widget = table_widgets.VmLastBackupItem(
  101. vm, "backup_timestamp")
  102. table.setItem(row_no, VmManagerWindow.columns_indices[
  103. 'Last backup'], self.last_backup_widget)
  104. self.dvm_template_widget = table_widgets.VMPropertyItem(
  105. vm, "default_dispvm", check_default=True)
  106. table.setItem(row_no, VmManagerWindow.columns_indices['Default DispVM'],
  107. self.dvm_template_widget)
  108. self.is_dispvm_template_widget = table_widgets.VMPropertyItem(
  109. vm, "template_for_dispvms", empty_function=(lambda x: not x))
  110. table.setItem(
  111. row_no, VmManagerWindow.columns_indices['Is DVM Template'],
  112. self.is_dispvm_template_widget)
  113. self.virt_mode_widget = table_widgets.VMPropertyItem(vm, 'virt_mode')
  114. table.setItem(row_no, VmManagerWindow.columns_indices[
  115. 'Virtualization Mode'], self.virt_mode_widget)
  116. self.table = table
  117. def update(self, update_size_on_disk=False, event=None):
  118. """
  119. Update info in a single VM row
  120. :param update_size_on_disk: should disk utilization be updated? the
  121. widget will extract the data from VM object
  122. :param event: name of the event that caused the update, to avoid
  123. updating unnecessary properties; if event is none, update everything
  124. :return: None
  125. """
  126. try:
  127. self.info_widget.update_vm_state()
  128. if not event or event.endswith(':label'):
  129. self.label_widget.update()
  130. if not event or event.endswith(':template'):
  131. self.template_widget.update()
  132. if not event or event.endswith(':netvm'):
  133. self.netvm_widget.update()
  134. if not event or event.endswith(':internal'):
  135. # this is a feature, not a property; TODO: fix event handling
  136. self.internal_widget.update()
  137. if not event or event.endswith(':ip'):
  138. self.ip_widget.update()
  139. if not event or event.endswith(':include_in_backups'):
  140. self.include_in_backups_widget.update()
  141. if not event or event.endswith(':backup_timestamp'):
  142. self.last_backup_widget.update()
  143. if not event or event.endswith(':default_dispvm'):
  144. self.dvm_template_widget.update()
  145. if not event or event.endswith(':template_for_dispvms'):
  146. self.is_dispvm_template_widget.update()
  147. if not event or event.endswith(':virt_mode'):
  148. self.virt_mode_widget.update()
  149. if update_size_on_disk:
  150. self.size_widget.update()
  151. except exc.QubesPropertyAccessError:
  152. pass
  153. except exc.QubesDaemonNoResponseError:
  154. # TODO: this will be fixed by a rewrite moving the event system to
  155. # AdminAPI
  156. pass
  157. # force re-sorting
  158. self.table.setSortingEnabled(True)
  159. vm_shutdown_timeout = 20000 # in msec
  160. vm_restart_check_timeout = 1000 # in msec
  161. class VmShutdownMonitor(QtCore.QObject):
  162. def __init__(self, vm, shutdown_time=vm_shutdown_timeout,
  163. check_time=vm_restart_check_timeout,
  164. and_restart=False, caller=None):
  165. QtCore.QObject.__init__(self)
  166. self.vm = vm
  167. self.shutdown_time = shutdown_time
  168. self.check_time = check_time
  169. self.and_restart = and_restart
  170. self.shutdown_started = datetime.now()
  171. self.caller = caller
  172. def restart_vm_if_needed(self):
  173. if self.and_restart and self.caller:
  174. self.caller.start_vm(self.vm)
  175. def check_again_later(self):
  176. # noinspection PyTypeChecker,PyCallByClass
  177. QtCore.QTimer.singleShot(self.check_time, self.check_if_vm_has_shutdown)
  178. def timeout_reached(self):
  179. actual = datetime.now() - self.shutdown_started
  180. allowed = timedelta(milliseconds=self.shutdown_time)
  181. return actual > allowed
  182. def check_if_vm_has_shutdown(self):
  183. vm = self.vm
  184. vm_is_running = vm.is_running()
  185. try:
  186. vm_start_time = datetime.fromtimestamp(float(vm.start_time))
  187. except (AttributeError, TypeError, ValueError):
  188. vm_start_time = None
  189. if vm_is_running and vm_start_time \
  190. and vm_start_time < self.shutdown_started:
  191. if self.timeout_reached():
  192. msgbox = QtWidgets.QMessageBox(self.caller)
  193. msgbox.setIcon(QtWidgets.QMessageBox.Question)
  194. msgbox.setWindowTitle(self.tr("Qube Shutdown"))
  195. msgbox.setText(self.tr(
  196. "The Qube <b>'{0}'</b> hasn't shutdown within the last "
  197. "{1} seconds, do you want to kill it?<br>").format(
  198. vm.name, self.shutdown_time / 1000))
  199. kill_button = msgbox.addButton(
  200. self.tr("Kill it!"), QtWidgets.QMessageBox.YesRole)
  201. wait_button = msgbox.addButton(
  202. self.tr("Wait another {0} seconds...").format(
  203. self.shutdown_time / 1000),
  204. QtWidgets.QMessageBox.NoRole)
  205. msgbox.setDefaultButton(wait_button)
  206. msgbox.exec_()
  207. msgbox.deleteLater()
  208. if msgbox.clickedButton() is kill_button:
  209. try:
  210. vm.kill()
  211. except exc.QubesVMNotStartedError:
  212. # the VM shut down while the user was thinking about
  213. # shutting it down
  214. pass
  215. self.restart_vm_if_needed()
  216. else:
  217. self.shutdown_started = datetime.now()
  218. self.check_again_later()
  219. else:
  220. self.check_again_later()
  221. else:
  222. if vm_is_running:
  223. # Due to unknown reasons, Xen sometimes reports that a domain
  224. # is running even though its start-up timestamp is not valid.
  225. # Make sure that "restart_vm_if_needed" is not called until
  226. # the domain has been completely shut down according to Xen.
  227. self.check_again_later()
  228. return
  229. self.restart_vm_if_needed()
  230. # pylint: disable=too-few-public-methods
  231. class StartVMThread(common_threads.QubesThread):
  232. def run(self):
  233. try:
  234. self.vm.start()
  235. except exc.QubesException as ex:
  236. self.msg = ("Error starting Qube!", str(ex))
  237. # pylint: disable=too-few-public-methods
  238. class UpdateVMThread(common_threads.QubesThread):
  239. def run(self):
  240. try:
  241. if self.vm.qid == 0:
  242. subprocess.check_call(
  243. ["/usr/bin/qubes-dom0-update", "--clean", "--gui"])
  244. else:
  245. if not self.vm.is_running():
  246. self.vm.start()
  247. # apply DSA-4371
  248. with open('/usr/libexec/qubes-manager/dsa-4371-update', 'rb') \
  249. as dsa4371update:
  250. stdout, stderr = self.vm.run_service_for_stdio(
  251. "qubes.VMShell",
  252. user="root",
  253. input=dsa4371update.read())
  254. if stdout == b'changed=yes\n':
  255. subprocess.call(
  256. ['notify-send', '-i', 'dialog-information',
  257. self.tr('Debian DSA-4371 fix installed in {}').format(
  258. self.vm.name)])
  259. elif stdout == b'changed=no\n':
  260. pass
  261. else:
  262. raise exc.QubesException(
  263. self.tr("Failed to apply DSA-4371 fix: {}").format(
  264. stderr.decode('ascii')))
  265. self.vm.run_service("qubes.InstallUpdatesGUI",
  266. user="root", wait=False)
  267. except (ChildProcessError, exc.QubesException) as ex:
  268. self.msg = (self.tr("Error on qube update!"), str(ex))
  269. # pylint: disable=too-few-public-methods
  270. class RunCommandThread(common_threads.QubesThread):
  271. def __init__(self, vm, command_to_run):
  272. super(RunCommandThread, self).__init__(vm)
  273. self.command_to_run = command_to_run
  274. def run(self):
  275. try:
  276. self.vm.run(self.command_to_run)
  277. except (ChildProcessError, exc.QubesException) as ex:
  278. self.msg = (self.tr("Error while running command!"), str(ex))
  279. class VmManagerWindow(ui_qubemanager.Ui_VmManagerWindow, QtWidgets.QMainWindow):
  280. # pylint: disable=too-many-instance-attributes
  281. row_height = 30
  282. column_width = 200
  283. search = ""
  284. # suppress saving settings while initializing widgets
  285. settings_loaded = False
  286. columns_indices = {"Type": 0,
  287. "Label": 1,
  288. "Name": 2,
  289. "State": 3,
  290. "Template": 4,
  291. "NetVM": 5,
  292. "Size": 6,
  293. "Internal": 7,
  294. "IP": 8,
  295. "Include in backups": 9,
  296. "Last backup": 10,
  297. "Default DispVM": 11,
  298. "Is DVM Template": 12,
  299. "Virtualization Mode": 13
  300. }
  301. def __init__(self, qt_app, qubes_app, dispatcher, parent=None):
  302. # pylint: disable=unused-argument
  303. super(VmManagerWindow, self).__init__()
  304. self.setupUi(self)
  305. self.manager_settings = QtCore.QSettings(self)
  306. self.qubes_app = qubes_app
  307. self.qt_app = qt_app
  308. self.searchbox = SearchBox()
  309. self.searchbox.setValidator(QtGui.QRegExpValidator(
  310. QtCore.QRegExp("[a-zA-Z0-9_-]*", QtCore.Qt.CaseInsensitive), None))
  311. self.searchContainer.addWidget(self.searchbox)
  312. self.table.itemSelectionChanged.connect(self.table_selection_changed)
  313. self.table.setColumnWidth(0, self.column_width)
  314. self.sort_by_column = "Type"
  315. self.sort_order = QtCore.Qt.AscendingOrder
  316. self.vms_list = []
  317. self.vms_in_table = {}
  318. self.frame_width = 0
  319. self.frame_height = 0
  320. self.columns_actions = {
  321. self.columns_indices["Type"]: self.action_vm_type,
  322. self.columns_indices["Label"]: self.action_label,
  323. self.columns_indices["Name"]: self.action_name,
  324. self.columns_indices["State"]: self.action_state,
  325. self.columns_indices["Template"]: self.action_template,
  326. self.columns_indices["NetVM"]: self.action_netvm,
  327. self.columns_indices["Size"]: self.action_size_on_disk,
  328. self.columns_indices["Internal"]: self.action_internal,
  329. self.columns_indices["IP"]: self.action_ip,
  330. self.columns_indices["Include in backups"]: self.action_backups,
  331. self.columns_indices["Last backup"]: self.action_last_backup,
  332. self.columns_indices["Default DispVM"]: self.action_dispvm_template,
  333. self.columns_indices["Is DVM Template"]:
  334. self.action_is_dvm_template,
  335. self.columns_indices["Virtualization Mode"]: self.action_virt_mode
  336. }
  337. self.visible_columns_count = len(self.columns_indices)
  338. # Other columns get sensible default sizes, but those have only
  339. # icon content, and thus PyQt makes them too wide
  340. self.table.setColumnWidth(self.columns_indices["State"], 80)
  341. self.table.setColumnWidth(self.columns_indices["Label"], 40)
  342. self.table.setColumnWidth(self.columns_indices["Type"], 40)
  343. self.table.horizontalHeader().setSectionResizeMode(
  344. QtWidgets.QHeaderView.Interactive)
  345. self.table.horizontalHeader().setStretchLastSection(True)
  346. self.table.horizontalHeader().setMinimumSectionSize(40)
  347. self.context_menu = QtWidgets.QMenu(self)
  348. self.context_menu.addAction(self.action_settings)
  349. self.context_menu.addAction(self.action_editfwrules)
  350. self.context_menu.addAction(self.action_appmenus)
  351. self.context_menu.addAction(self.action_set_keyboard_layout)
  352. self.context_menu.addSeparator()
  353. self.context_menu.addAction(self.action_updatevm)
  354. self.context_menu.addAction(self.action_run_command_in_vm)
  355. self.context_menu.addAction(self.action_open_console)
  356. self.context_menu.addAction(self.action_resumevm)
  357. self.context_menu.addAction(self.action_startvm_tools_install)
  358. self.context_menu.addAction(self.action_pausevm)
  359. self.context_menu.addAction(self.action_shutdownvm)
  360. self.context_menu.addAction(self.action_restartvm)
  361. self.context_menu.addAction(self.action_killvm)
  362. self.context_menu.addSeparator()
  363. self.context_menu.addAction(self.action_clonevm)
  364. self.context_menu.addAction(self.action_removevm)
  365. self.context_menu.addSeparator()
  366. self.context_menu.addMenu(self.logs_menu)
  367. self.context_menu.addSeparator()
  368. self.tools_context_menu = QtWidgets.QMenu(self)
  369. self.tools_context_menu.addAction(self.action_toolbar)
  370. self.tools_context_menu.addAction(self.action_menubar)
  371. self.dom0_context_menu = QtWidgets.QMenu(self)
  372. self.dom0_context_menu.addAction(self.action_global_settings)
  373. self.dom0_context_menu.addAction(self.action_updatevm)
  374. self.dom0_context_menu.addSeparator()
  375. self.dom0_context_menu.addMenu(self.logs_menu)
  376. self.dom0_context_menu.addSeparator()
  377. self.table.horizontalHeader().sortIndicatorChanged.connect(
  378. self.sort_indicator_changed)
  379. self.table.customContextMenuRequested.connect(self.open_context_menu)
  380. self.menubar.customContextMenuRequested.connect(
  381. lambda pos: self.open_tools_context_menu(self.menubar, pos))
  382. self.toolbar.customContextMenuRequested.connect(
  383. lambda pos: self.open_tools_context_menu(self.toolbar, pos))
  384. self.logs_menu.triggered.connect(self.show_log)
  385. self.searchbox.textChanged.connect(self.do_search)
  386. self.table.setContentsMargins(0, 0, 0, 0)
  387. self.centralwidget.layout().setContentsMargins(0, 0, 0, 0)
  388. self.layout().setContentsMargins(0, 0, 0, 0)
  389. self.action_menubar.toggled.connect(self.showhide_menubar)
  390. self.action_toolbar.toggled.connect(self.showhide_toolbar)
  391. try:
  392. self.load_manager_settings()
  393. except Exception as ex: # pylint: disable=broad-except
  394. QtWidgets.QMessageBox.warning(
  395. self,
  396. self.tr("Manager settings unreadable"),
  397. self.tr("Qube Manager settings cannot be parsed. Previously "
  398. "saved display settings may not be restored "
  399. "correctly.\nError: {}".format(str(ex))))
  400. self.settings_loaded = True
  401. self.fill_table()
  402. self.table.resizeColumnsToContents()
  403. self.update_size_on_disk = False
  404. self.shutdown_monitor = {}
  405. # Connect events
  406. self.dispatcher = dispatcher
  407. dispatcher.add_handler('domain-pre-start',
  408. self.on_domain_status_changed)
  409. dispatcher.add_handler('domain-start', self.on_domain_status_changed)
  410. dispatcher.add_handler('domain-start-failed',
  411. self.on_domain_status_changed)
  412. dispatcher.add_handler('domain-stopped', self.on_domain_status_changed)
  413. dispatcher.add_handler('domain-pre-shutdown',
  414. self.on_domain_status_changed)
  415. dispatcher.add_handler('domain-shutdown', self.on_domain_status_changed)
  416. dispatcher.add_handler('domain-paused', self.on_domain_status_changed)
  417. dispatcher.add_handler('domain-unpaused', self.on_domain_status_changed)
  418. dispatcher.add_handler('domain-add', self.on_domain_added)
  419. dispatcher.add_handler('domain-delete', self.on_domain_removed)
  420. dispatcher.add_handler('property-set:*',
  421. self.on_domain_changed)
  422. dispatcher.add_handler('property-del:*',
  423. self.on_domain_changed)
  424. dispatcher.add_handler('property-load',
  425. self.on_domain_changed)
  426. dispatcher.add_handler('domain-feature-set:updates-available',
  427. self.on_domain_updates_available)
  428. dispatcher.add_handler('domain-feature-delete:updates-available',
  429. self.on_domain_updates_available)
  430. # It needs to store threads until they finish
  431. self.threads_list = []
  432. self.progress = None
  433. self.check_updates()
  434. # select the first row of the table to make sure menu actions are
  435. # correctly initialized
  436. self.table.selectRow(0)
  437. def setup_application(self):
  438. self.qt_app.setApplicationName(self.tr("Qube Manager"))
  439. self.qt_app.setWindowIcon(QtGui.QIcon.fromTheme("qubes-manager"))
  440. def keyPressEvent(self, event): # pylint: disable=invalid-name
  441. if event.key() == QtCore.Qt.Key_Escape:
  442. self.searchbox.clear()
  443. super(VmManagerWindow, self).keyPressEvent(event)
  444. def clear_threads(self):
  445. for thread in self.threads_list:
  446. if thread.isFinished():
  447. if self.progress:
  448. self.progress.hide()
  449. self.progress = None
  450. if thread.msg:
  451. (title, msg) = thread.msg
  452. if thread.msg_is_success:
  453. QtWidgets.QMessageBox.information(
  454. self,
  455. title,
  456. msg)
  457. else:
  458. QtWidgets.QMessageBox.warning(
  459. self,
  460. title,
  461. msg)
  462. self.threads_list.remove(thread)
  463. return
  464. raise RuntimeError(self.tr('No finished thread found'))
  465. def closeEvent(self, event):
  466. # pylint: disable=invalid-name
  467. # save window size at close
  468. self.manager_settings.setValue("window_size", self.size())
  469. event.accept()
  470. def check_updates(self, vm=None):
  471. if vm is None:
  472. for vm_iter in self.qubes_app.domains:
  473. self.check_updates(vm_iter)
  474. return
  475. if vm.klass in {'TemplateVM', 'StandaloneVM'}:
  476. try:
  477. self.vms_in_table[vm.qid].info_widget.update_vm_state()
  478. except (exc.QubesException, KeyError):
  479. # the VM might have vanished in the meantime or
  480. # the signal might have been handled in the wrong order
  481. pass
  482. def on_domain_added(self, _submitter, _event, vm, **_kwargs):
  483. row_no = 0
  484. self.table.setSortingEnabled(False)
  485. try:
  486. domain = self.qubes_app.domains[vm]
  487. row_no = self.table.rowCount()
  488. self.table.setRowCount(row_no + 1)
  489. vm_row = VmRowInTable(domain, row_no, self.table)
  490. self.vms_in_table[domain.qid] = vm_row
  491. except (exc.QubesException, KeyError):
  492. if row_no != 0:
  493. self.table.removeRow(row_no)
  494. self.table.setSortingEnabled(True)
  495. self.showhide_vms()
  496. def on_domain_removed(self, _submitter, _event, **kwargs):
  497. row_to_delete = None
  498. qid_to_delete = None
  499. for qid, row in self.vms_in_table.items():
  500. if row.vm.name == kwargs['vm']:
  501. row_to_delete = row
  502. qid_to_delete = qid
  503. if not row_to_delete:
  504. return # for some reason, the VM was removed in some other way
  505. del self.vms_in_table[qid_to_delete]
  506. self.table.removeRow(row_to_delete.name_widget.row())
  507. def on_domain_status_changed(self, vm, _event, **_kwargs):
  508. try:
  509. self.vms_in_table[vm.qid].info_widget.update_vm_state()
  510. except exc.QubesPropertyAccessError:
  511. return # the VM was deleted before its status could be updated
  512. except KeyError: # adding the VM failed for some reason
  513. self.on_domain_added(None, None, vm)
  514. if vm == self.get_selected_vm():
  515. self.table_selection_changed()
  516. if vm.klass == 'TemplateVM':
  517. for row in self.vms_in_table.values():
  518. if getattr(row.vm, 'template', None) == vm:
  519. row.info_widget.update_vm_state()
  520. def on_domain_updates_available(self, vm, _event, **_kwargs):
  521. self.check_updates(vm)
  522. def on_domain_changed(self, vm, event, **_kwargs):
  523. if not vm: # change of global properties occured
  524. if event.endswith(':default_netvm'):
  525. for vm_row in self.vms_in_table.values():
  526. vm_row.update(event='property-set:netvm')
  527. if event.endswith(':default_dispvm'):
  528. for vm_row in self.vms_in_table.values():
  529. vm_row.update(event='property-set:default_dispvm')
  530. return
  531. try:
  532. self.vms_in_table[vm.qid].update(event=event)
  533. except exc.QubesPropertyAccessError:
  534. return # the VM was deleted before its status could be updated
  535. def load_manager_settings(self):
  536. for col in self.columns_indices:
  537. col_no = self.columns_indices[col]
  538. if col == 'Name':
  539. # 'Name' column should be always visible
  540. self.columns_actions[col_no].setChecked(True)
  541. else:
  542. visible = self.manager_settings.value(
  543. 'columns/%s' % col,
  544. defaultValue="true")
  545. self.columns_actions[col_no].setChecked(visible == "true")
  546. self.sort_by_column = str(
  547. self.manager_settings.value("view/sort_column",
  548. defaultValue=self.sort_by_column))
  549. self.sort_order = QtCore.Qt.SortOrder(
  550. self.manager_settings.value("view/sort_order",
  551. defaultValue=self.sort_order))
  552. try:
  553. self.table.sortItems(self.columns_indices[self.sort_by_column],
  554. self.sort_order)
  555. except KeyError:
  556. # the manager was sorted on a column that does not exist in the
  557. # current version; possible only with downgrades
  558. self.table.sortItems(self.columns_indices["Name"],
  559. self.sort_order)
  560. if not self.manager_settings.value("view/menubar_visible",
  561. defaultValue=True):
  562. self.action_menubar.setChecked(False)
  563. if not self.manager_settings.value("view/toolbar_visible",
  564. defaultValue=True):
  565. self.action_toolbar.setChecked(False)
  566. # load last window size
  567. self.resize(self.manager_settings.value("window_size",
  568. QtCore.QSize(1100, 600)))
  569. def get_vms_list(self):
  570. return list(self.qubes_app.domains)
  571. def fill_table(self):
  572. self.table.setSortingEnabled(False)
  573. vms_list = self.get_vms_list()
  574. vms_in_table = {}
  575. self.table.setRowCount(len(vms_list))
  576. progress = QtWidgets.QProgressDialog(
  577. self.tr(
  578. "Loading Qube Manager..."), "", 0, len(vms_list))
  579. progress.setWindowTitle(self.tr("Qube Manager"))
  580. progress.setMinimumDuration(1000)
  581. progress.setCancelButton(None)
  582. row_no = 0
  583. for vm in vms_list:
  584. progress.setValue(row_no)
  585. vm_row = VmRowInTable(vm, row_no, self.table)
  586. vms_in_table[vm.qid] = vm_row
  587. row_no += 1
  588. progress.setValue(row_no)
  589. self.vms_list = vms_list
  590. self.vms_in_table = vms_in_table
  591. self.table.setSortingEnabled(True)
  592. def showhide_vms(self):
  593. if not self.search:
  594. for row_no in range(self.table.rowCount()):
  595. self.table.setRowHidden(row_no, False)
  596. else:
  597. for row_no in range(self.table.rowCount()):
  598. widget = self.table.cellWidget(row_no,
  599. self.columns_indices["State"])
  600. show = (self.search in widget.vm.name)
  601. self.table.setRowHidden(row_no, not show)
  602. @QtCore.pyqtSlot(str)
  603. def do_search(self, search):
  604. self.search = str(search)
  605. self.showhide_vms()
  606. # noinspection PyArgumentList
  607. @QtCore.pyqtSlot(name='on_action_search_triggered')
  608. def action_search_triggered(self):
  609. self.searchbox.setFocus()
  610. # noinspection PyPep8Naming
  611. def sort_indicator_changed(self, column, order):
  612. self.sort_by_column = [name for name in self.columns_indices if
  613. self.columns_indices[name] == column][0]
  614. self.sort_order = order
  615. if self.settings_loaded:
  616. self.manager_settings.setValue('view/sort_column',
  617. self.sort_by_column)
  618. self.manager_settings.setValue('view/sort_order', self.sort_order)
  619. self.manager_settings.sync()
  620. def table_selection_changed(self):
  621. vm = self.get_selected_vm()
  622. if vm is not None and vm in self.qubes_app.domains:
  623. # TODO: add boot from device to menu and add windows tools there
  624. # Update available actions:
  625. self.action_settings.setEnabled(vm.klass != 'AdminVM')
  626. self.action_removevm.setEnabled(
  627. vm.klass != 'AdminVM' and not vm.is_running())
  628. self.action_clonevm.setEnabled(vm.klass != 'AdminVM')
  629. self.action_resumevm.setEnabled(
  630. not vm.is_running() or vm.get_power_state() == "Paused")
  631. self.action_pausevm.setEnabled(
  632. vm.is_running() and vm.get_power_state() != "Paused"
  633. and vm.klass != 'AdminVM')
  634. self.action_shutdownvm.setEnabled(
  635. vm.is_running() and vm.get_power_state() != "Paused"
  636. and vm.klass != 'AdminVM')
  637. self.action_restartvm.setEnabled(
  638. vm.is_running() and vm.get_power_state() != "Paused"
  639. and vm.klass != 'AdminVM'
  640. and (vm.klass != 'DispVM' or not vm.auto_cleanup))
  641. self.action_killvm.setEnabled(
  642. (vm.get_power_state() == "Paused" or vm.is_running())
  643. and vm.klass != 'AdminVM')
  644. self.action_appmenus.setEnabled(
  645. vm.klass != 'AdminVM' and vm.klass != 'DispVM'
  646. and not vm.features.get('internal', False))
  647. self.action_editfwrules.setEnabled(vm.klass != 'AdminVM')
  648. self.action_updatevm.setEnabled(getattr(vm, 'updateable', False)
  649. or vm.qid == 0)
  650. self.action_run_command_in_vm.setEnabled(
  651. not vm.get_power_state() == "Paused" and vm.qid != 0)
  652. self.action_open_console.setEnabled(
  653. not vm.get_power_state() == "Paused" and vm.qid != 0)
  654. self.action_set_keyboard_layout.setEnabled(
  655. vm.qid != 0 and
  656. vm.get_power_state() != "Paused" and vm.is_running())
  657. else:
  658. self.action_settings.setEnabled(False)
  659. self.action_removevm.setEnabled(False)
  660. self.action_clonevm.setEnabled(False)
  661. self.action_resumevm.setEnabled(False)
  662. self.action_pausevm.setEnabled(False)
  663. self.action_shutdownvm.setEnabled(False)
  664. self.action_restartvm.setEnabled(False)
  665. self.action_killvm.setEnabled(False)
  666. self.action_appmenus.setEnabled(False)
  667. self.action_editfwrules.setEnabled(False)
  668. self.action_updatevm.setEnabled(False)
  669. self.action_run_command_in_vm.setEnabled(False)
  670. self.action_open_console.setEnabled(False)
  671. self.action_set_keyboard_layout.setEnabled(False)
  672. self.update_logs_menu()
  673. # noinspection PyArgumentList
  674. @QtCore.pyqtSlot(name='on_action_createvm_triggered')
  675. def action_createvm_triggered(self): # pylint: disable=no-self-use
  676. with common_threads.busy_cursor():
  677. create_window = create_new_vm.NewVmDlg(self.qt_app, self.qubes_app)
  678. create_window.exec_()
  679. def get_selected_vm(self):
  680. # vm selection relies on the VmInfo widget's value used
  681. # for sorting by VM name
  682. row_index = self.table.currentRow()
  683. if row_index != -1:
  684. vm_item = self.table.item(row_index, self.columns_indices["Name"])
  685. # here is possible race with update_table timer so check
  686. # if really got the item
  687. if vm_item is None:
  688. return None
  689. qid = vm_item.qid
  690. assert self.vms_in_table[qid] is not None
  691. vm = self.vms_in_table[qid].vm
  692. return vm
  693. return None
  694. # noinspection PyArgumentList
  695. @QtCore.pyqtSlot(name='on_action_removevm_triggered')
  696. def action_removevm_triggered(self):
  697. # pylint: disable=no-else-return
  698. vm = self.get_selected_vm()
  699. dependencies = utils.vm_dependencies(self.qubes_app, vm)
  700. if dependencies:
  701. list_text = "<br>" + \
  702. manager_utils.format_dependencies_list(dependencies) + \
  703. "<br>"
  704. info_dialog = QtWidgets.QMessageBox(self)
  705. info_dialog.setWindowTitle(self.tr("Warning!"))
  706. info_dialog.setText(
  707. self.tr("This qube cannot be removed. It is used as:"
  708. " <br> {} <small>If you want to remove this qube, "
  709. "you should remove or change settings of each qube "
  710. "or setting that uses it.</small>").format(list_text))
  711. info_dialog.setModal(False)
  712. info_dialog.show()
  713. return
  714. (requested_name, ok) = QtWidgets.QInputDialog.getText(
  715. self, self.tr("Qube Removal Confirmation"),
  716. self.tr("Are you sure you want to remove the Qube <b>'{0}'</b>"
  717. "?<br> All data on this Qube's private storage will be "
  718. "lost!<br><br>Type the name of the Qube (<b>{1}</b>) below "
  719. "to confirm:").format(vm.name, vm.name))
  720. if not ok:
  721. # user clicked cancel
  722. return
  723. if requested_name != vm.name:
  724. # name did not match
  725. QtWidgets.QMessageBox.warning(
  726. self,
  727. self.tr("Qube removal confirmation failed"),
  728. self.tr(
  729. "Entered name did not match! Not removing "
  730. "{0}.").format(vm.name))
  731. return
  732. else:
  733. # remove the VM
  734. thread = common_threads.RemoveVMThread(vm)
  735. self.threads_list.append(thread)
  736. thread.finished.connect(self.clear_threads)
  737. thread.start()
  738. # noinspection PyArgumentList
  739. @QtCore.pyqtSlot(name='on_action_clonevm_triggered')
  740. def action_clonevm_triggered(self):
  741. vm = self.get_selected_vm()
  742. name_number = 1
  743. name_format = vm.name + '-clone-%d'
  744. while name_format % name_number in self.qubes_app.domains.keys():
  745. name_number += 1
  746. (clone_name, ok) = QtWidgets.QInputDialog.getText(
  747. self, self.tr('Qubes clone Qube'),
  748. self.tr('Enter name for Qube <b>{}</b> clone:').format(vm.name),
  749. text=(name_format % name_number))
  750. if not ok or clone_name == "":
  751. return
  752. name_in_use = clone_name in self.qubes_app.domains
  753. if name_in_use:
  754. QtWidgets.QMessageBox.warning(
  755. self, self.tr("Name already in use!"),
  756. self.tr("There already exists a qube called '{}'. "
  757. "Cloning aborted.").format(clone_name))
  758. return
  759. self.progress = QtWidgets.QProgressDialog(
  760. self.tr(
  761. "Cloning Qube..."), "", 0, 0)
  762. self.progress.setCancelButton(None)
  763. self.progress.setModal(True)
  764. self.progress.setWindowTitle(self.tr("Cloning qube..."))
  765. self.progress.show()
  766. thread = common_threads.CloneVMThread(vm, clone_name)
  767. thread.finished.connect(self.clear_threads)
  768. self.threads_list.append(thread)
  769. thread.start()
  770. # noinspection PyArgumentList
  771. @QtCore.pyqtSlot(name='on_action_resumevm_triggered')
  772. def action_resumevm_triggered(self):
  773. vm = self.get_selected_vm()
  774. if vm.get_power_state() in ["Paused", "Suspended"]:
  775. try:
  776. vm.unpause()
  777. except exc.QubesException as ex:
  778. QtWidgets.QMessageBox.warning(
  779. self, self.tr("Error unpausing Qube!"),
  780. self.tr("ERROR: {0}").format(ex))
  781. return
  782. self.start_vm(vm)
  783. def start_vm(self, vm):
  784. if vm.is_running():
  785. return
  786. thread = StartVMThread(vm)
  787. self.threads_list.append(thread)
  788. thread.finished.connect(self.clear_threads)
  789. thread.start()
  790. # noinspection PyArgumentList
  791. @QtCore.pyqtSlot(name='on_action_startvm_tools_install_triggered')
  792. # TODO: replace with boot from device
  793. def action_startvm_tools_install_triggered(self):
  794. # pylint: disable=invalid-name
  795. pass
  796. @QtCore.pyqtSlot(name='on_action_pausevm_triggered')
  797. def action_pausevm_triggered(self):
  798. vm = self.get_selected_vm()
  799. try:
  800. vm.pause()
  801. except exc.QubesException as ex:
  802. QtWidgets.QMessageBox.warning(
  803. self,
  804. self.tr("Error pausing Qube!"),
  805. self.tr("ERROR: {0}").format(ex))
  806. return
  807. # noinspection PyArgumentList
  808. @QtCore.pyqtSlot(name='on_action_shutdownvm_triggered')
  809. def action_shutdownvm_triggered(self):
  810. vm = self.get_selected_vm()
  811. reply = QtWidgets.QMessageBox.question(
  812. self, self.tr("Qube Shutdown Confirmation"),
  813. self.tr("Are you sure you want to power down the Qube"
  814. " <b>'{0}'</b>?<br><small>This will shutdown all the "
  815. "running applications within this Qube.</small>").format(
  816. vm.name),
  817. QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel)
  818. if reply == QtWidgets.QMessageBox.Yes:
  819. self.shutdown_vm(vm)
  820. def shutdown_vm(self, vm, shutdown_time=vm_shutdown_timeout,
  821. check_time=vm_restart_check_timeout, and_restart=False):
  822. try:
  823. vm.shutdown()
  824. except exc.QubesException as ex:
  825. QtWidgets.QMessageBox.warning(
  826. self,
  827. self.tr("Error shutting down Qube!"),
  828. self.tr("ERROR: {0}").format(ex))
  829. return
  830. self.shutdown_monitor[vm.qid] = VmShutdownMonitor(vm, shutdown_time,
  831. check_time,
  832. and_restart, self)
  833. # noinspection PyCallByClass,PyTypeChecker
  834. QtCore.QTimer.singleShot(check_time, self.shutdown_monitor[
  835. vm.qid].check_if_vm_has_shutdown)
  836. # noinspection PyArgumentList
  837. @QtCore.pyqtSlot(name='on_action_restartvm_triggered')
  838. def action_restartvm_triggered(self):
  839. vm = self.get_selected_vm()
  840. reply = QtWidgets.QMessageBox.question(
  841. self, self.tr("Qube Restart Confirmation"),
  842. self.tr("Are you sure you want to restart the Qube <b>'{0}'</b>?"
  843. "<br><small>This will shutdown all the running "
  844. "applications within this Qube.</small>").format(vm.name),
  845. QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel)
  846. if reply == QtWidgets.QMessageBox.Yes:
  847. # in case the user shut down the VM in the meantime
  848. if vm.is_running():
  849. self.shutdown_vm(vm, and_restart=True)
  850. else:
  851. self.start_vm(vm)
  852. # noinspection PyArgumentList
  853. @QtCore.pyqtSlot(name='on_action_killvm_triggered')
  854. def action_killvm_triggered(self):
  855. vm = self.get_selected_vm()
  856. if not (vm.is_running() or vm.is_paused()):
  857. info = self.tr("Qube <b>'{0}'</b> is not running. Are you "
  858. "absolutely sure you want to try to kill it?<br>"
  859. "<small>This will end <b>(not shutdown!)</b> all "
  860. "the running applications within this "
  861. "Qube.</small>").format(vm.name)
  862. else:
  863. info = self.tr("Are you sure you want to kill the Qube "
  864. "<b>'{0}'</b>?<br><small>This will end <b>(not "
  865. "shutdown!)</b> all the running applications within "
  866. "this Qube.</small>").format(vm.name)
  867. reply = QtWidgets.QMessageBox.question(
  868. self, self.tr("Qube Kill Confirmation"), info,
  869. QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel,
  870. QtWidgets.QMessageBox.Cancel)
  871. if reply == QtWidgets.QMessageBox.Yes:
  872. try:
  873. vm.kill()
  874. except exc.QubesException as ex:
  875. QtWidgets.QMessageBox.critical(
  876. self, self.tr("Error while killing Qube!"),
  877. self.tr(
  878. "<b>An exception ocurred while killing {0}.</b><br>"
  879. "ERROR: {1}").format(vm.name, ex))
  880. return
  881. def open_settings(self, vm, tab='basic'):
  882. try:
  883. with common_threads.busy_cursor():
  884. settings_window = settings.VMSettingsWindow(
  885. vm, self.qt_app, tab)
  886. settings_window.exec_()
  887. except exc.QubesException as ex:
  888. QtWidgets.QMessageBox.warning(
  889. self,
  890. self.tr("Qube settings unavailable"),
  891. self.tr(
  892. "Qube settings cannot be opened. The qube might have "
  893. "been removed or unavailable due to policy settings."
  894. "\nError: {}".format(str(ex))))
  895. return
  896. vm_deleted = False
  897. try:
  898. # the VM might not exist after running Settings - it might
  899. # have been cloned or removed
  900. self.vms_in_table[vm.qid].update()
  901. except exc.QubesException:
  902. vm_deleted = True
  903. if vm_deleted:
  904. for row in self.vms_in_table:
  905. try:
  906. self.vms_in_table[row].update()
  907. except exc.QubesException:
  908. pass
  909. # noinspection PyArgumentList
  910. @QtCore.pyqtSlot(name='on_action_settings_triggered')
  911. def action_settings_triggered(self):
  912. vm = self.get_selected_vm()
  913. if vm:
  914. self.open_settings(vm, "basic")
  915. # noinspection PyArgumentList
  916. @QtCore.pyqtSlot(name='on_action_appmenus_triggered')
  917. def action_appmenus_triggered(self):
  918. vm = self.get_selected_vm()
  919. if vm:
  920. self.open_settings(vm, "applications")
  921. # noinspection PyArgumentList
  922. @QtCore.pyqtSlot(name='on_action_updatevm_triggered')
  923. def action_updatevm_triggered(self):
  924. vm = self.get_selected_vm()
  925. if not vm.is_running():
  926. reply = QtWidgets.QMessageBox.question(
  927. self, self.tr("Qube Update Confirmation"),
  928. self.tr(
  929. "<b>{0}</b><br>The Qube has to be running to be updated."
  930. "<br>Do you want to start it?<br>").format(vm.name),
  931. QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel)
  932. if reply != QtWidgets.QMessageBox.Yes:
  933. return
  934. thread = UpdateVMThread(vm)
  935. self.threads_list.append(thread)
  936. thread.finished.connect(self.clear_threads)
  937. thread.start()
  938. # noinspection PyArgumentList
  939. @QtCore.pyqtSlot(name='on_action_run_command_in_vm_triggered')
  940. def action_run_command_in_vm_triggered(self):
  941. # pylint: disable=invalid-name
  942. vm = self.get_selected_vm()
  943. (command_to_run, ok) = QtWidgets.QInputDialog.getText(
  944. self, self.tr('Qubes command entry'),
  945. self.tr('Run command in <b>{}</b>:').format(vm.name))
  946. if not ok or command_to_run == "":
  947. return
  948. thread = RunCommandThread(vm, command_to_run)
  949. self.threads_list.append(thread)
  950. thread.finished.connect(self.clear_threads)
  951. thread.start()
  952. # noinspection PyArgumentList
  953. @QtCore.pyqtSlot(name='on_action_open_console_triggered')
  954. def action_open_console_triggered(self):
  955. # pylint: disable=invalid-name
  956. vm = self.get_selected_vm()
  957. subprocess.Popen(['qvm-console-dispvm', vm.name],
  958. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  959. # noinspection PyArgumentList
  960. @QtCore.pyqtSlot(name='on_action_set_keyboard_layout_triggered')
  961. def action_set_keyboard_layout_triggered(self):
  962. # pylint: disable=invalid-name
  963. vm = self.get_selected_vm()
  964. vm.run('qubes-change-keyboard-layout')
  965. # noinspection PyArgumentList
  966. @QtCore.pyqtSlot(name='on_action_editfwrules_triggered')
  967. def action_editfwrules_triggered(self):
  968. vm = self.get_selected_vm()
  969. if vm:
  970. self.open_settings(vm, "firewall")
  971. # noinspection PyArgumentList
  972. @QtCore.pyqtSlot(name='on_action_global_settings_triggered')
  973. def action_global_settings_triggered(self): # pylint: disable=invalid-name
  974. with common_threads.busy_cursor():
  975. global_settings_window = global_settings.GlobalSettingsWindow(
  976. self.qt_app,
  977. self.qubes_app)
  978. global_settings_window.exec_()
  979. # noinspection PyArgumentList
  980. @QtCore.pyqtSlot(name='on_action_manage_templates_triggered')
  981. def action_manage_templates_triggered(self):
  982. # pylint: disable=invalid-name, no-self-use
  983. subprocess.check_call('qubes-template-manager')
  984. # noinspection PyArgumentList
  985. @QtCore.pyqtSlot(name='on_action_show_network_triggered')
  986. def action_show_network_triggered(self):
  987. pass
  988. # TODO: revive for 4.1
  989. # network_notes_dialog = NetworkNotesDialog()
  990. # network_notes_dialog.exec_()
  991. # noinspection PyArgumentList
  992. @QtCore.pyqtSlot(name='on_action_restore_triggered')
  993. def action_restore_triggered(self):
  994. with common_threads.busy_cursor():
  995. restore_window = restore.RestoreVMsWindow(self.qt_app,
  996. self.qubes_app)
  997. restore_window.exec_()
  998. # noinspection PyArgumentList
  999. @QtCore.pyqtSlot(name='on_action_backup_triggered')
  1000. def action_backup_triggered(self):
  1001. with common_threads.busy_cursor():
  1002. backup_window = backup.BackupVMsWindow(
  1003. self.qt_app, self.qubes_app, self.dispatcher, self)
  1004. backup_window.show()
  1005. # noinspection PyArgumentList
  1006. @QtCore.pyqtSlot(name='on_action_exit_triggered')
  1007. def action_exit_triggered(self):
  1008. self.close()
  1009. def showhide_menubar(self, checked):
  1010. self.menubar.setVisible(checked)
  1011. if not checked:
  1012. self.context_menu.addAction(self.action_menubar)
  1013. else:
  1014. self.context_menu.removeAction(self.action_menubar)
  1015. if self.settings_loaded:
  1016. self.manager_settings.setValue('view/menubar_visible', checked)
  1017. self.manager_settings.sync()
  1018. def showhide_toolbar(self, checked):
  1019. self.toolbar.setVisible(checked)
  1020. if not checked:
  1021. self.context_menu.addAction(self.action_toolbar)
  1022. else:
  1023. self.context_menu.removeAction(self.action_toolbar)
  1024. if self.settings_loaded:
  1025. self.manager_settings.setValue('view/toolbar_visible', checked)
  1026. self.manager_settings.sync()
  1027. def showhide_column(self, col_num, show):
  1028. self.table.setColumnHidden(col_num, not show)
  1029. if self.settings_loaded:
  1030. col_name = [name for name in self.columns_indices if
  1031. self.columns_indices[name] == col_num][0]
  1032. self.manager_settings.setValue('columns/%s' % col_name, show)
  1033. self.manager_settings.sync()
  1034. def on_action_vm_type_toggled(self, checked):
  1035. self.showhide_column(self.columns_indices['Type'], checked)
  1036. def on_action_label_toggled(self, checked):
  1037. self.showhide_column(self.columns_indices['Label'], checked)
  1038. def on_action_name_toggled(self, checked):
  1039. self.showhide_column(self.columns_indices['Name'], checked)
  1040. def on_action_state_toggled(self, checked):
  1041. self.showhide_column(self.columns_indices['State'], checked)
  1042. def on_action_internal_toggled(self, checked):
  1043. self.showhide_column(self.columns_indices['Internal'], checked)
  1044. def on_action_ip_toggled(self, checked):
  1045. self.showhide_column(self.columns_indices['IP'], checked)
  1046. def on_action_backups_toggled(self, checked):
  1047. self.showhide_column(
  1048. self.columns_indices['Include in backups'], checked)
  1049. def on_action_last_backup_toggled(self, checked):
  1050. self.showhide_column(self.columns_indices['Last backup'], checked)
  1051. def on_action_template_toggled(self, checked):
  1052. self.showhide_column(self.columns_indices['Template'], checked)
  1053. def on_action_netvm_toggled(self, checked):
  1054. self.showhide_column(self.columns_indices['NetVM'], checked)
  1055. def on_action_size_on_disk_toggled(self, checked):
  1056. self.showhide_column(self.columns_indices['Size'], checked)
  1057. def on_action_virt_mode_toggled(self, checked):
  1058. self.showhide_column(self.columns_indices['Virtualization Mode'],
  1059. checked)
  1060. # pylint: disable=invalid-name
  1061. def on_action_dispvm_template_toggled(self, checked):
  1062. self.showhide_column(self.columns_indices['Default DispVM'], checked)
  1063. # pylint: disable=invalid-name
  1064. def on_action_is_dvm_template_toggled(self, checked):
  1065. self.showhide_column(self.columns_indices['Is DVM Template'], checked)
  1066. # noinspection PyArgumentList
  1067. @QtCore.pyqtSlot(name='on_action_about_qubes_triggered')
  1068. def action_about_qubes_triggered(self): # pylint: disable=no-self-use
  1069. about = AboutDialog()
  1070. about.exec_()
  1071. def createPopupMenu(self): # pylint: disable=invalid-name
  1072. menu = QtWidgets.QMenu()
  1073. menu.addAction(self.action_toolbar)
  1074. menu.addAction(self.action_menubar)
  1075. return menu
  1076. def open_tools_context_menu(self, widget, point):
  1077. self.tools_context_menu.exec_(widget.mapToGlobal(point))
  1078. def update_logs_menu(self):
  1079. try:
  1080. vm = self.get_selected_vm()
  1081. # logs menu
  1082. self.logs_menu.clear()
  1083. if vm.qid == 0:
  1084. logfiles = ["/var/log/xen/console/hypervisor.log"]
  1085. else:
  1086. logfiles = [
  1087. "/var/log/xen/console/guest-" + vm.name + ".log",
  1088. "/var/log/xen/console/guest-" + vm.name + "-dm.log",
  1089. "/var/log/qubes/guid." + vm.name + ".log",
  1090. "/var/log/qubes/qrexec." + vm.name + ".log",
  1091. ]
  1092. menu_empty = True
  1093. for logfile in logfiles:
  1094. if os.path.exists(logfile):
  1095. action = self.logs_menu.addAction(QtGui.QIcon(":/log.png"),
  1096. logfile)
  1097. action.setData(logfile)
  1098. menu_empty = False
  1099. self.logs_menu.setEnabled(not menu_empty)
  1100. except exc.QubesPropertyAccessError:
  1101. pass
  1102. @QtCore.pyqtSlot('const QPoint&')
  1103. def open_context_menu(self, point):
  1104. vm = self.get_selected_vm()
  1105. if vm.qid == 0:
  1106. self.dom0_context_menu.exec_(self.table.mapToGlobal(
  1107. point + QtCore.QPoint(10, 0)))
  1108. else:
  1109. self.context_menu.exec_(self.table.mapToGlobal(
  1110. point + QtCore.QPoint(10, 0)))
  1111. @QtCore.pyqtSlot('QAction *')
  1112. def show_log(self, action):
  1113. log = str(action.data())
  1114. log_dlg = log_dialog.LogDialog(self.qt_app, log)
  1115. log_dlg.exec_()
  1116. def main():
  1117. manager_utils.run_asynchronous(VmManagerWindow)
  1118. if __name__ == "__main__":
  1119. main()