8 from PyQt4.QtCore import *
9 from PyQt4.QtGui import *
10 from sface.config import config
11 from sface.sfiprocess import SfiProcess
12 #from sface.sfithread import SfiThread
14 class SfiRenewer(QObject):
15 def __init__(self, hrn, newExpiration, parent=None):
16 QObject.__init__(self, parent)
18 self.newExpiration = newExpiration
20 self.renewProcess = SfiProcess(self)
21 self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
22 self.renewProcess.getRecord(hrn=config.getSlice(), filename="/tmp/slicerecord")
24 def finishedGetRecord(self):
25 f = open("/tmp/slicerecord", "r")
29 # find the expiration time
30 exp = re.compile('expires="[^"]*"')
31 if exp.search(data)==None:
33 self.emitFinished("failure", "failed to find expiration in slice record")
36 # change the expiration time
37 delta = 24*60*60 # always extend the slice by one extra day to cover slop for time zone differences
38 data = exp.sub('expires="' + str(calendar.timegm(self.newExpiration.timetuple())+delta) + '"', data)
40 open("/tmp/slicerecord", "w").write(data)
42 self.disconnect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
43 self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedUpdateRecord)
45 self.renewProcess.updateRecord("/tmp/slicerecord")
47 def finishedUpdateRecord(self):
48 # we have to force sfi.py to download an updated slice credential
49 sliceCredName = os.path.expanduser("~/.sfi/slice_" + self.hrn.split(".")[-1] + ".cred")
50 if os.path.exists(sliceCredName):
51 os.remove(sliceCredName)
53 open("/tmp/expiration", "w").write(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"))
55 # call renewSlivers on the aggregate
56 self.disconnect(self.renewProcess, SIGNAL('finished()'), self.finishedUpdateRecord)
57 self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedRenewSlivers)
58 self.renewProcess.renewSlivers(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"))
60 def finishedRenewSlivers(self):
61 self.emitFinished("success")
63 def emitFinished(self, status, statusMsg=None):
65 self.statusMsg = statusMsg
66 self.emit(SIGNAL("finished()"))
68 class RenewWindow(QDialog):
69 def __init__(self, parent=None):
70 super(RenewWindow, self).__init__(parent)
71 self.setWindowTitle("Renew Slivers")
73 self.renewProcess = None
75 self.duration = QComboBox()
79 durations = ( (1, "One Week"), (2, "Two Weeks"), (3, "Three Weeks"), (4, "One Month") )
81 now = datetime.datetime.utcnow()
82 for (weeks, desc) in durations:
83 exp = now + datetime.timedelta(days = weeks * 7)
84 desc = desc + " " + exp.strftime("%Y-%m-%d %H:%M:%S")
85 self.expirations.append(exp)
86 self.duration.addItem(desc)
88 self.duration.setCurrentIndex(0)
90 self.status = QLabel("")
92 self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
93 self.buttonBox.button(QDialogButtonBox.Ok).setDefault(True)
95 layout = QVBoxLayout()
96 layout.addWidget(self.duration)
97 layout.addWidget(self.status)
98 layout.addWidget(self.buttonBox)
99 self.setLayout(layout)
103 self.connect(self.buttonBox, SIGNAL("accepted()"), self, SLOT("accept()"))
104 self.connect(self.buttonBox, SIGNAL("rejected()"), self, SLOT("reject()"))
107 self.setStatus("Renewing Slice...")
109 self.renewProcess = SfiRenewer(config.getSlice(), self.get_new_expiration(), self)
110 self.connect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
112 self.duration.setEnabled(False)
113 self.buttonBox.setEnabled(False)
115 def setStatus(self, x):
116 self.status.setText(x)
118 def renewFinished(self):
119 if self.renewProcess.statusMsg:
120 self.setStatus("Renew " + self.renewProcess.status + ": " + self.renewProcess.statusMsg)
122 self.setStatus("Renew " + self.renewProcess.status)
123 self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
124 self.renewProcess = None
126 self.buttonBox.setEnabled(True)
127 self.buttonBox.clear()
128 self.buttonBox.addButton(QDialogButtonBox.Close)
130 def get_new_expiration(self):
131 index = self.duration.currentIndex()
132 return self.expirations[index]