clone_vm.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2020 Marta Marczykowska-Górecka
  6. # <marmarta@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. #
  21. #
  22. import os
  23. import sys
  24. import subprocess
  25. from PyQt5 import QtCore, QtWidgets, QtGui # pylint: disable=import-error
  26. import qubesadmin
  27. import qubesadmin.tools
  28. import qubesadmin.exc
  29. from . import common_threads
  30. from . import utils
  31. from .ui_clonevmdlg import Ui_CloneVMDlg # pylint: disable=import-error
  32. class CloneVMDlg(QtWidgets.QDialog, Ui_CloneVMDlg):
  33. def __init__(self, qtapp, app, parent=None, src_vm=None):
  34. super().__init__(parent)
  35. self.setupUi(self)
  36. self.qtapp = qtapp
  37. self.app = app
  38. self.thread = None
  39. self.progress = None
  40. utils.initialize_widget_with_vms(
  41. widget=self.src_vm,
  42. qubes_app=self.app,
  43. filter_function=(lambda vm: vm.klass != 'AdminVM'))
  44. if src_vm and self.src_vm.findText(src_vm.name) > -1:
  45. self.src_vm.setCurrentIndex(self.src_vm.findText(src_vm.name))
  46. utils.initialize_widget_with_labels(widget=self.label,
  47. qubes_app=self.app)
  48. self.update_label()
  49. try:
  50. utils.initialize_widget_with_default(
  51. widget=self.storage_pool,
  52. choices=[(str(pool), pool) for pool in self.app.pools.values()],
  53. add_qubes_default=True,
  54. mark_existing_as_default=True,
  55. default_value=self.app.default_pool)
  56. except qubesadmin.exc.QubesDaemonAccessError:
  57. self.storage_pool.clear()
  58. self.storage_pool.addItem("(default)", qubesadmin.DEFAULT)
  59. self.set_clone_name()
  60. self.name.setValidator(QtGui.QRegExpValidator(
  61. QtCore.QRegExp("[a-zA-Z0-9_-]*", QtCore.Qt.CaseInsensitive), None))
  62. self.name.selectAll()
  63. self.name.setFocus()
  64. if src_vm:
  65. self.src_vm.setEnabled(False)
  66. else:
  67. self.src_vm.currentIndexChanged.connect(self.set_clone_name)
  68. self.src_vm.currentIndexChanged.connect(self.update_label)
  69. def reject(self):
  70. self.done(0)
  71. def accept(self):
  72. name = self.name.text()
  73. if name in self.app.domains:
  74. QtWidgets.QMessageBox.warning(
  75. self,
  76. self.tr('Incorrect qube name!'),
  77. self.tr('A qube with the name <b>{}</b> already exists in the '
  78. 'system!').format(self.name.text()))
  79. return
  80. label = self.label.currentData()
  81. pool = self.storage_pool.currentData()
  82. if pool is qubesadmin.DEFAULT:
  83. pool = None
  84. src_vm = self.src_vm.currentData()
  85. self.thread = common_threads.CloneVMThread(
  86. src_vm, name, pool=pool, label=label)
  87. self.thread.finished.connect(self.clone_finished)
  88. self.thread.start()
  89. self.progress = QtWidgets.QProgressDialog(
  90. self.tr("Cloning qube <b>{0}</b>...").format(name), "", 0, 0)
  91. self.progress.setCancelButton(None)
  92. self.progress.setModal(True)
  93. self.progress.show()
  94. def set_clone_name(self):
  95. vm_name = self.src_vm.currentText()
  96. name_number = 1
  97. name_format = vm_name + '-clone-%d'
  98. while name_format % name_number in self.app.domains.keys():
  99. name_number += 1
  100. self.name.setText(name_format % name_number)
  101. def update_label(self):
  102. vm_label = self.src_vm.currentData().label
  103. label_idx = self.label.findText(str(vm_label))
  104. if label_idx > -1:
  105. self.label.setCurrentIndex(label_idx)
  106. def clone_finished(self):
  107. self.progress.hide()
  108. if not self.thread.msg_is_success:
  109. QtWidgets.QMessageBox.warning(
  110. self,
  111. self.tr("Error cloning the qube!"),
  112. self.tr("ERROR: {0}").format(self.thread.msg))
  113. else:
  114. (title, msg) = self.thread.msg
  115. QtWidgets.QMessageBox.warning(
  116. self,
  117. title,
  118. msg)
  119. self.done(0)
  120. if self.thread.msg_is_success:
  121. if self.launch_settings.isChecked():
  122. subprocess.check_call(['qubes-vm-settings',
  123. str(self.name.text())])
  124. parser = qubesadmin.tools.QubesArgumentParser(vmname_nargs='?')
  125. def main(args=None):
  126. args = parser.parse_args(args)
  127. if args.domains:
  128. src_vm = args.domains.pop()
  129. else:
  130. src_vm = None
  131. qtapp = QtWidgets.QApplication(sys.argv)
  132. translator = QtCore.QTranslator(qtapp)
  133. locale = QtCore.QLocale.system().name()
  134. i18n_dir = os.path.join(
  135. os.path.dirname(os.path.realpath(__file__)),
  136. 'i18n')
  137. translator.load("qubesmanager_{!s}.qm".format(locale), i18n_dir)
  138. qtapp.installTranslator(translator)
  139. QtCore.QCoreApplication.installTranslator(translator)
  140. qtapp.setOrganizationName('Invisible Things Lab')
  141. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  142. qtapp.setApplicationName(QtCore.QCoreApplication.translate(
  143. "appname", 'Clone qube'))
  144. dialog = CloneVMDlg(qtapp, args.app, src_vm=src_vm)
  145. dialog.exec_()