2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
5 # format_drive_dialog.pyw
7 # letting the user format a drive attached to a certain VM
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
11 # Copyright (C) 2014 AIT Austrian Institute of Technology
12 # AIT Austrian Institute of Technology GmbH
13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
14 # http://www.ait.ac.at
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation version 2.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 # Boston, MA 02110-1301, USA.
29 # ------------------------------------------------------------
32 # ------------------------------------------------------------
38 from PyQt4 import QtCore
39 from PyQt4 import QtGui
41 from ui_FormatDriveDialog import Ui_FormatDriveDialog
42 from about_dialog import AboutDialog
45 # ------------------------------------------------------------
49 class FormatDriveDialog(QtGui.QDialog):
51 """A dialog for letting the user pass on a password/keyfile combo"""
53 def __init__(self, ip):
55 QtGui.QDialog.__init__(self)
58 <b>Attention!</b><br/>
59 You are going to wipe all data stored<br/>
60 at device attached to IP <b>%s</b>.<br/>
62 <b>This is irreversible.</b><br/>
64 Please provide an approbitate password and keyfile to proceed:
67 # setup the user interface
68 self.ui = Ui_FormatDriveDialog()
72 self._about_dialog = AboutDialog()
73 self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
74 self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
75 self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
78 self.ui.lblText.setText(user_text)
79 self.ui.btnAbout.clicked.connect(self.clicked_about)
80 self.ui.btnCancel.clicked.connect(self.reject)
81 self.ui.btnOk.clicked.connect(self.clicked_ok)
82 self.ui.btnBrowse.clicked.connect(self.clicked_browse)
84 # positionate ourself central
85 screen = QtGui.QDesktopWidget().screenGeometry()
86 size = self.geometry()
87 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
90 def clicked_about(self):
92 """About button has been clicked."""
93 self._about_dialog.show()
96 def clicked_browse(self):
98 """Browse button has been clicked."""
99 if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
100 self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
103 def clicked_ok(self):
105 """Ok button has been clicked."""
110 init_data['password'] = self.ui.edtPassword.text()
111 if len(init_data['password']) == 0:
112 QtGui.QMessageBox.critical(self, 'Format error', 'Please specify a password.')
115 # read the content of the keyfile
118 if len(self.ui.edtKeyfile.text()) > 0:
119 keyfile = open(self.ui.edtKeyfile.text(), 'r')
120 keyfile_content = keyfile.read()
121 except Exception as e:
122 sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
123 QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
127 if len(keyfile_content) > 0:
128 keyfile_content_base64 = base64.b64encode(keyfile_content)
129 init_data['keyfile'] = keyfile_content_base64
132 req = urllib2.Request('http://' + ip + ':58081/init', urllib.urlencode(init_data))
133 res = urllib2.urlopen(req)
141 def set_user_text(user_text):
143 """Set a text to explain which password we need."""
144 self.ui.lblText.setText(user_text)
147 if __name__ == "__main__":
153 ip = sys.argv[-1:][0]
157 a = QtGui.QApplication(sys.argv)
158 d = FormatDriveDialog(ip)