Addded http proxy server support.
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 # ------------------------------------------------------------
41 from PyQt4 import QtCore
42 from PyQt4 import QtGui
44 from ui_FormatDriveDialog import Ui_FormatDriveDialog
45 from about_dialog import AboutDialog
47 # ------------------------------------------------------------
51 class FormatDriveDialog(QtGui.QDialog):
53 """A dialog for letting the user pass on a password/keyfile combo"""
55 def __init__(self, ip):
57 QtGui.QDialog.__init__(self)
60 <b>Attention!</b><br/>
61 You are going to wipe all data stored<br/>
62 at device attached to IP <b>%s</b>.<br/>
64 <b>This is irreversible.</b><br/>
66 Please provide an appropriate password or keyfile to proceed:
69 # setup the user interface
70 self.ui = Ui_FormatDriveDialog()
74 self._about_dialog = AboutDialog()
75 self._open_file_dialog = QtGui.QFileDialog(self, 'Open Keyfile', QtCore.QDir.homePath(), 'All Files (*.*);;')
76 self._open_file_dialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)
77 self._open_file_dialog.setFileMode(QtGui.QFileDialog.ExistingFile)
80 self.ui.lblText.setText(user_text)
81 self.ui.btnAbout.clicked.connect(self.clicked_about)
82 self.ui.btnCancel.clicked.connect(self.reject)
83 self.ui.btnOk.clicked.connect(self.clicked_ok)
84 self.ui.btnBrowse.clicked.connect(self.clicked_browse)
86 # positionate ourself central
87 screen = QtGui.QDesktopWidget().screenGeometry()
88 size = self.geometry()
89 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
92 def clicked_about(self):
94 """About button has been clicked."""
95 self._about_dialog.show()
98 def clicked_browse(self):
100 """Browse button has been clicked."""
101 if self._open_file_dialog.exec_() == QtGui.QDialog.Accepted:
102 self.ui.edtKeyfile.setText(self._open_file_dialog.selectedFiles()[0])
105 def clicked_ok(self):
107 """Ok button has been clicked."""
112 init_data['password'] = str(self.ui.edtPassword.text())
113 if len(init_data['password']) == 0:
114 QtGui.QMessageBox.critical(self, 'Format error', 'Please specify a password.')
117 # read the content of the keyfile
120 if len(self.ui.edtKeyfile.text()) > 0:
121 keyfile = open(self.ui.edtKeyfile.text(), 'r')
122 keyfile_content = keyfile.read()
123 except Exception as e:
124 sys.stderr.write('failed to read keyfile content:\n' + str(e) + '\n')
125 QtGui.QMessageBox.critical(self, 'Failed to read keyfile', str(e))
129 if len(keyfile_content) > 0:
130 keyfile_content_base64 = base64.b64encode(keyfile_content)
131 init_data['keyfile'] = keyfile_content_base64
135 req_data = urllib.urlencode(init_data)
136 req = urllib2.Request('http://' + ip + ':58081/init', req_data)
137 res = urllib2.urlopen(req)
139 print('EXCEPTION ' + res)
145 def set_user_text(self, user_text):
147 """Set a text to explain which password we need."""
148 self.ui.lblText.setText(user_text)
151 if __name__ == "__main__":
157 ip = sys.argv[-1:][0]
161 a = QtGui.QApplication(sys.argv)
162 d = FormatDriveDialog(ip)