create_new_vm.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/python2
  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 <marmarek@mimuw.edu.pl>
  7. # Copyright (C) 2017 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. #
  24. import os
  25. import sys
  26. import threading
  27. import time
  28. from PyQt4.QtCore import *
  29. from PyQt4.QtGui import *
  30. import qubesadmin
  31. import qubesadmin.tools
  32. import qubesmanager.resources_rc
  33. from . import utils
  34. from .ui_newappvmdlg import Ui_NewVMDlg
  35. from .thread_monitor import ThreadMonitor
  36. class NewVmDlg(QDialog, Ui_NewVMDlg):
  37. def __init__(self, qtapp, app, parent = None):
  38. super(NewVmDlg, self).__init__(parent)
  39. self.setupUi(self)
  40. self.qtapp = qtapp
  41. self.app = app
  42. # Theoretically we should be locking for writing here and unlock
  43. # only after the VM creation finished. But the code would be more messy...
  44. # Instead we lock for writing in the actual worker thread
  45. self.label_list, self.label_idx = utils.prepare_label_choice(
  46. self.label,
  47. self.app, None,
  48. None,
  49. allow_default=False)
  50. self.template_list, self.template_idx = utils.prepare_vm_choice(
  51. self.template_vm,
  52. self.app, None,
  53. self.app.default_template,
  54. (lambda vm: isinstance(vm, qubesadmin.vm.TemplateVM)),
  55. allow_internal=False, allow_default=True, allow_none=False)
  56. self.netvm_list, self.netvm_idx = utils.prepare_vm_choice(
  57. self.netvm,
  58. self.app, None,
  59. self.app.default_netvm,
  60. (lambda vm: vm.provides_network),
  61. allow_internal=False, allow_default=True, allow_none=True)
  62. self.name.setValidator(QRegExpValidator(
  63. QRegExp("[a-zA-Z0-9-]*", Qt.CaseInsensitive), None))
  64. self.name.selectAll()
  65. self.name.setFocus()
  66. if len(self.template_list) == 0:
  67. QMessageBox.warning(None,
  68. self.tr('No template available!'),
  69. self.tr('Cannot create a qube when no template exists.'))
  70. def reject(self):
  71. self.done(0)
  72. def accept(self):
  73. vmclass = ('StandaloneVM' if self.standalone.isChecked() else 'AppVM')
  74. name = str(self.name.text())
  75. try:
  76. self.app.domains[name]
  77. except LookupError:
  78. pass
  79. else:
  80. QMessageBox.warning(None,
  81. self.tr('Incorrect qube name!'),
  82. self.tr('A qube with the name <b>{}</b> already exists in the '
  83. 'system!').format(name))
  84. return
  85. label = self.label_list[self.label.currentIndex()]
  86. template = self.template_list[self.template_vm.currentIndex()]
  87. properties = {}
  88. properties['provides_network'] = self.provides_network.isChecked()
  89. properties['virt_mode'] = 'hvm' if self.hvm.isChecked() else 'pv'
  90. properties['netvm'] = self.netvm_list[self.netvm.currentIndex()]
  91. thread_monitor = ThreadMonitor()
  92. thread = threading.Thread(target=self.do_create_vm,
  93. args=(self.app, vmclass, name, label, template, properties,
  94. thread_monitor))
  95. thread.daemon = True
  96. thread.start()
  97. progress = QProgressDialog(
  98. self.tr("Creating new qube <b>{}</b>...").format(name), "", 0, 0)
  99. progress.setCancelButton(None)
  100. progress.setModal(True)
  101. progress.show()
  102. while not thread_monitor.is_finished():
  103. self.qtapp.processEvents()
  104. time.sleep (0.1)
  105. progress.hide()
  106. if not thread_monitor.success:
  107. QMessageBox.warning(None,
  108. self.tr("Error creating the qube!"),
  109. self.tr("ERROR: {}").format(thread_monitor.error_msg))
  110. self.done(0)
  111. @staticmethod
  112. def do_create_vm(app, vmclass, name, label, template, properties,
  113. thread_monitor):
  114. try:
  115. vm = app.add_new_vm(vmclass,
  116. name=name, label=label, template=template)
  117. for k, v in properties.items():
  118. setattr(vm, k, v)
  119. except Exception as ex:
  120. thread_monitor.set_error_msg(str(ex))
  121. thread_monitor.set_finished()
  122. parser = qubesadmin.tools.QubesArgumentParser()
  123. def main(args=None):
  124. args = parser.parse_args(args)
  125. qtapp = QApplication(sys.argv)
  126. qtapp.setOrganizationName('Invisible Things Lab')
  127. qtapp.setOrganizationDomain('https://www.qubes-os.org/')
  128. qtapp.setApplicationName('Create qube')
  129. dialog = NewVmDlg(qtapp, args.app)
  130. dialog.exec_()