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 2013-2014 X-Net and AIT Austrian Institute of Technology
18 # https://www.x-net.at
20 # AIT Austrian Institute of Technology
21 # Donau City Strasse 1
24 # http://www.ait.ac.at
27 # Licensed under the Apache License, Version 2.0 (the "License");
28 # you may not use this file except in compliance with the License.
29 # You may obtain a copy of the License at
31 # http://www.apache.org/licenses/LICENSE-2.0
33 # Unless required by applicable law or agreed to in writing, software
34 # distributed under the License is distributed on an "AS IS" BASIS,
35 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 # See the License for the specific language governing permissions and
37 # limitations under the License.
38 # ------------------------------------------------------------
41 # ------------------------------------------------------------
50 from PyQt4 import QtCore
51 from PyQt4 import QtGui
53 from ui_FormatDriveDialog import Ui_FormatDriveDialog
54 from about_dialog import AboutDialog
56 # ------------------------------------------------------------
60 class FormatDriveDialog(QtGui.QDialog):
62 """A dialog for letting the user pass on a password/keyfile combo"""
64 def __init__(self, ip):
66 QtGui.QDialog.__init__(self)
69 <b>Attention!</b><br/>
70 You are going to wipe all data stored<br/>
71 at device attached to IP <b>%s</b>.<br/>
73 <b>This is irreversible.</b><br/>
75 Please provide an appropriate password or keyfile to proceed:
78 # setup the user interface
79 self.ui = Ui_FormatDriveDialog()
83 self._about_dialog = AboutDialog()
84 self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
85 self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
86 self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
89 self.ui.lblText.setText(user_text)
90 self.ui.btnAbout.clicked.connect(self.clicked_about)
91 self.ui.btnCancel.clicked.connect(self.reject)
92 self.ui.btnOk.clicked.connect(self.clicked_ok)
93 self.ui.btnBrowse.clicked.connect(self.clicked_browse)
95 # positionate ourself central
96 screen = QtGui.QDesktopWidget().screenGeometry()
97 size = self.geometry()
98 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
101 def clicked_about(self):
103 """About button has been clicked."""
104 self._about_dialog.show()
107 def clicked_browse(self):
109 """Browse button has been clicked."""
110 if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
111 self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
114 def clicked_ok(self):
116 """Ok button has been clicked."""
121 init_data['password'] = str(self.ui.edtPassword.text())
122 if len(init_data['password']) == 0:
123 QtGui.QMessageBox.critical(self, 'Format error', 'Please specify a password.')
126 # read the content of the keyfile
129 if len(self.ui.edtKeyfile.text()) > 0:
130 keyfile = open(self.ui.edtKeyfile.text(), 'r')
131 keyfile_content = keyfile.read()
132 except Exception as e:
133 sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
134 QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
138 if len(keyfile_content) > 0:
139 keyfile_content_base64 = base64.b64encode(keyfile_content)
140 init_data['keyfile'] = keyfile_content_base64
144 req_data = urllib.urlencode(init_data)
145 req = urllib2.Request('http://' + ip + ':58081/init', req_data)
146 res = urllib2.urlopen(req)
148 print('EXCEPTION ' + res)
154 def set_user_text(self, user_text):
156 """Set a text to explain which password we need."""
157 self.ui.lblText.setText(user_text)
160 if __name__ == "__main__":
166 ip = sys.argv[-1:][0]
170 a = QtGui.QApplication(sys.argv)
171 d = FormatDriveDialog(ip)