clone_vm.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(CloneVMDlg, self).__init__(parent)
  35. self.setupUi(self)
  36. self.qtapp = qtapp
  37. self.app = app
  38. self.thread = None
  39. self.progress = None
  40. self.vm_list, self.vm_idx = utils.prepare_vm_choice(
  41. self.src_vm,
  42. self.app, None,
  43. None,
  44. (lambda vm: vm.klass != 'AdminVM'),
  45. allow_internal=False
  46. )
  47. if src_vm and self.src_vm.findText(src_vm.name) > -1:
  48. self.src_vm.setCurrentIndex(self.src_vm.findText(src_vm.name))
  49. self.label_list, self.label_idx = utils.prepare_label_choice(
  50. self.label,
  51. self.app, None,
  52. None,
  53. allow_default=False
  54. )
  55. self.update_label()
  56. self.pool_list, self.pool_idx = utils.prepare_choice(
  57. widget=self.storage_pool,
  58. holder=None,
  59. propname=None,
  60. choice=self.app.pools.values(),
  61. default=self.app.default_pool,
  62. allow_default=True,
  63. allow_none=False
  64. )
  65. self.set_clone_name()
  66. self.name.setValidator(QtGui.QRegExpValidator(
  67. QtCore.QRegExp("[a-zA-Z0-9_-]*", QtCore.Qt.CaseInsensitive), None))
  68. self.name.selectAll()
  69. self.name.setFocus()
  70. if src_vm:
  71. self.src_vm.setEnabled(False)
  72. else:
  73. self.src_vm.currentIndexChanged.connect(self.set_clone_name)
  74. self.src_vm.currentIndexChanged.connect(self.update_label)
  75. def reject(self):
  76. self.done(0)
  77. def accept(self):
  78. name = self.name.text()
  79. if name in self.app.domains:
  80. QtWidgets.QMessageBox.warning(
  81. self,
  82. self.tr('Incorrect qube name!'),
  83. self.tr('A qube with the name <b>{}</b> already exists in the '
  84. 'system!').format(self.name.text()))
  85. return
  86. label = self.label_list[self.label.currentIndex()]
  87. if self.pool_list[self.storage_pool.currentIndex()] is not \
  88. qubesadmin.DEFAULT:
  89. pool = self.pool_list[self.storage_pool.currentIndex()]
  90. else:
  91. pool = None
  92. src_vm = self.vm_list[self.src_vm.currentIndex()]
  93. self.thread = common_threads.CloneVMThread(
  94. src_vm, name, pool=pool, label=label)
  95. self.thread.finished.connect(self.clone_finished)
  96. self.thread.start()
  97. self.progress = QtWidgets.QProgressDialog(
  98. self.tr("Cloning qube <b>{0}</b>...").format(name), "", 0, 0)
  99. self.progress.setCancelButton(None)
  100. self.progress.setModal(True)
  101. self.progress.show()
  102. def set_clone_name(self):
  103. vm_name = self.src_vm.currentText()
  104. name_number = 1
  105. name_format = vm_name + '-clone-%d'
  106. while name_format % name_number in self.app.domains.keys():
  107. name_number += 1
  108. self.name.setText(name_format % name_number)
  109. def update_label(self):
  110. vm_label = self.vm_list[self.src_vm.currentIndex()].label
  111. label_idx = self.label.findText(str(vm_label))
  112. if label_idx > -1:
  113. self.label.setCurrentIndex(label_idx)
  114. def clone_finished(self):
  115. self.progress.hide()
  116. if not self.thread.msg_is_success:
  117. QtWidgets.QMessageBox.warning(
  118. self,
  119. self.tr("Error cloning the qube!"),
  120. self.tr("ERROR: {0}").format(self.thread.msg))
  121. self.done(0)
  122. if self.thread.msg_is_success:
  123. if self.launch_settings.isChecked():
  124. subprocess.check_call(['qubes-vm-settings',
  125. str(self.name.text())])
  126. parser = qubesadmin.tools.QubesArgumentParser(vmname_nargs='?')
  127. def main(args=None):
  128. args = parser.parse_args(args)
  129. if args.domains:
  130. src_vm = args.domains.pop()
  131. else:
  132. src_vm = None
  133. qtapp = QtWidgets.QApplication(sys.argv)
  134. translator = QtCore.QTranslator(qtapp)
  135. locale = QtCore.QLocale.system().name()
  136. i18n_dir = os.path.join(
  137. os.path.dirname(os.path.realpath(__file__)),
  138. 'i18n')
  139. translator.load("qubesmanager_{!s}.qm".format(locale), i18n_dir)
  140. qtapp.installTranslator(translator)
  141. QtCore.QCoreApplication.installTranslator(translator)
  142. qtapp.setOrganizationName('Invisible Things Lab')
  143. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  144. qtapp.setApplicationName(QtCore.QCoreApplication.translate(
  145. "appname", 'Clone qube'))
  146. dialog = CloneVMDlg(qtapp, args.app, src_vm=src_vm)
  147. dialog.exec_()