2013-01-23 15:55:14 +01:00
|
|
|
#!/usr/bin/python2
|
2012-05-11 22:52:27 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Agnieszka Kostrzewa <agnieszka.kostrzewa@gmail.com>
|
|
|
|
# Copyright (C) 2012 Marek Marczykowski <marmarek@mimuw.edu.pl>
|
2017-07-12 14:10:15 +02:00
|
|
|
# Copyright (C) 2017 Wojtek Porczyk <woju@invisiblethingslab.com>
|
2012-05-11 22:52:27 +02:00
|
|
|
#
|
|
|
|
# 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 General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
2017-07-12 14:10:15 +02:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2012-05-11 22:52:27 +02:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
import qubesadmin
|
|
|
|
import qubesadmin.tools
|
2012-05-11 22:52:27 +02:00
|
|
|
|
|
|
|
import qubesmanager.resources_rc
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
from . import utils
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
from .ui_newappvmdlg import Ui_NewVMDlg
|
|
|
|
from .thread_monitor import ThreadMonitor
|
2012-05-11 22:52:27 +02:00
|
|
|
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
class NewVmDlg(QDialog, Ui_NewVMDlg):
|
|
|
|
def __init__(self, qtapp, app, parent = None):
|
|
|
|
super(NewVmDlg, self).__init__(parent)
|
2012-05-11 22:52:27 +02:00
|
|
|
self.setupUi(self)
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
self.qtapp = qtapp
|
2012-05-11 22:52:27 +02:00
|
|
|
self.app = app
|
|
|
|
|
|
|
|
# Theoretically we should be locking for writing here and unlock
|
|
|
|
# only after the VM creation finished. But the code would be more messy...
|
|
|
|
# Instead we lock for writing in the actual worker thread
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
self.label_list, self.label_idx = utils.prepare_label_choice(
|
|
|
|
self.label,
|
|
|
|
self.app, None,
|
|
|
|
None,
|
|
|
|
allow_default=False)
|
|
|
|
|
|
|
|
self.template_list, self.template_idx = utils.prepare_vm_choice(
|
|
|
|
self.template_vm,
|
|
|
|
self.app, None,
|
|
|
|
self.app.default_template,
|
|
|
|
(lambda vm: isinstance(vm, qubesadmin.vm.TemplateVM)),
|
|
|
|
allow_internal=False, allow_default=True, allow_none=False)
|
|
|
|
|
|
|
|
self.netvm_list, self.netvm_idx = utils.prepare_vm_choice(
|
|
|
|
self.netvm,
|
|
|
|
self.app, None,
|
|
|
|
self.app.default_netvm,
|
|
|
|
(lambda vm: vm.provides_network),
|
|
|
|
allow_internal=False, allow_default=True, allow_none=True)
|
|
|
|
|
|
|
|
self.name.setValidator(QRegExpValidator(
|
|
|
|
QRegExp("[a-zA-Z0-9-]*", Qt.CaseInsensitive), None))
|
|
|
|
self.name.selectAll()
|
|
|
|
self.name.setFocus()
|
|
|
|
|
|
|
|
if len(self.template_list) == 0:
|
|
|
|
QMessageBox.warning(None,
|
|
|
|
self.tr('No template available!'),
|
|
|
|
self.tr('Cannot create a qube when no template exists.'))
|
2013-11-21 04:19:41 +01:00
|
|
|
|
2012-05-11 22:52:27 +02:00
|
|
|
def reject(self):
|
|
|
|
self.done(0)
|
|
|
|
|
|
|
|
def accept(self):
|
2017-07-12 14:10:15 +02:00
|
|
|
vmclass = ('StandaloneVM' if self.standalone.isChecked() else 'AppVM')
|
|
|
|
|
|
|
|
name = str(self.name.text())
|
|
|
|
try:
|
|
|
|
self.app.domains[name]
|
|
|
|
except LookupError:
|
|
|
|
pass
|
|
|
|
else:
|
2017-01-22 05:42:38 +01:00
|
|
|
QMessageBox.warning(None,
|
2017-07-12 14:10:15 +02:00
|
|
|
self.tr('Incorrect qube name!'),
|
|
|
|
self.tr('A qube with the name <b>{}</b> already exists in the '
|
|
|
|
'system!').format(name))
|
2012-05-11 22:52:27 +02:00
|
|
|
return
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
label = self.label_list[self.label.currentIndex()]
|
|
|
|
template = self.template_list[self.template_vm.currentIndex()]
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
properties = {}
|
|
|
|
properties['provides_network'] = self.provides_network.isChecked()
|
|
|
|
properties['hvm'] = self.hvm.isChecked()
|
|
|
|
properties['netvm'] = self.netvm_list[self.netvm.currentIndex()]
|
2012-05-11 22:52:27 +02:00
|
|
|
|
|
|
|
thread_monitor = ThreadMonitor()
|
2017-07-12 14:10:15 +02:00
|
|
|
thread = threading.Thread(target=self.do_create_vm,
|
|
|
|
args=(self.app, vmclass, name, label, template, properties,
|
|
|
|
thread_monitor))
|
2012-05-11 22:52:27 +02:00
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
|
|
|
|
2017-01-22 05:42:38 +01:00
|
|
|
progress = QProgressDialog(
|
2017-07-12 14:10:15 +02:00
|
|
|
self.tr("Creating new qube <b>{}</b>...").format(name), "", 0, 0)
|
2012-05-11 22:52:27 +02:00
|
|
|
progress.setCancelButton(None)
|
|
|
|
progress.setModal(True)
|
|
|
|
progress.show()
|
|
|
|
|
|
|
|
while not thread_monitor.is_finished():
|
2017-07-12 14:10:15 +02:00
|
|
|
self.qtapp.processEvents()
|
2012-05-11 22:52:27 +02:00
|
|
|
time.sleep (0.1)
|
|
|
|
|
|
|
|
progress.hide()
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
if not thread_monitor.success:
|
2017-01-22 05:42:38 +01:00
|
|
|
QMessageBox.warning(None,
|
2017-07-12 14:10:15 +02:00
|
|
|
self.tr("Error creating the qube!"),
|
|
|
|
self.tr("ERROR: {}").format(thread_monitor.error_msg))
|
2012-05-11 22:52:27 +02:00
|
|
|
|
|
|
|
self.done(0)
|
|
|
|
|
2015-05-04 01:25:55 +02:00
|
|
|
@staticmethod
|
2017-07-12 14:10:15 +02:00
|
|
|
def do_create_vm(app, vmclass, name, label, template, properties,
|
|
|
|
thread_monitor):
|
2012-05-11 22:52:27 +02:00
|
|
|
try:
|
2017-07-12 14:10:15 +02:00
|
|
|
vm = app.add_new_vm(vmclass,
|
|
|
|
name=name, label=label, template=template)
|
|
|
|
for k, v in properties.items():
|
|
|
|
setattr(vm, k, v)
|
|
|
|
|
2012-05-11 22:52:27 +02:00
|
|
|
except Exception as ex:
|
2015-05-04 01:25:55 +02:00
|
|
|
thread_monitor.set_error_msg(str(ex))
|
2012-05-11 22:52:27 +02:00
|
|
|
|
|
|
|
thread_monitor.set_finished()
|
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
parser = qubesadmin.tools.QubesArgumentParser()
|
|
|
|
|
|
|
|
def main(args=None):
|
|
|
|
args = parser.parse_args(args)
|
|
|
|
|
|
|
|
qtapp = QApplication(sys.argv)
|
|
|
|
qtapp.setOrganizationName('Invisible Things Lab')
|
|
|
|
qtapp.setOrganizationDomain('https://www.qubes-os.org/')
|
|
|
|
qtapp.setApplicationName('Create qube')
|
2012-05-11 22:52:27 +02:00
|
|
|
|
2017-07-12 14:10:15 +02:00
|
|
|
dialog = NewVmDlg(qtapp, args.app)
|
|
|
|
dialog.exec_()
|