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
13 from sface.sliceview import SliceView, SliceModel
15 class SfiRenewer(QObject):
16 def __init__(self, hrn, newExpiration, parent=None):
17 QObject.__init__(self, parent)
19 self.newExpiration = newExpiration
20 self.faultString = None
22 self.renewProcess = SfiProcess(self)
23 self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
24 self.renewProcess.getRecord(hrn=hrn, filename="/tmp/slicerecord")
26 def finishedGetRecord(self):
27 self.faultString = self.renewProcess.getFaultString()
29 self.emitFinished("fault", self.faultString)
32 f = open("/tmp/slicerecord", "r")
36 # find the expiration time
37 exp = re.compile('expires="[^"]*"')
38 if exp.search(data)==None:
40 self.faultString = "failed to find expiration in slice record"
41 self.emitFinished("failure", self.faultString)
44 # change the expiration time
45 delta = 24*60*60 # always extend the slice by one extra day to cover slop for time zone differences
46 data = exp.sub('expires="' + str(calendar.timegm(self.newExpiration.timetuple())+delta) + '"', data)
48 open("/tmp/slicerecord", "w").write(data)
50 self.disconnect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
51 self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedUpdateRecord)
53 self.renewProcess.updateRecord("/tmp/slicerecord")
55 def finishedUpdateRecord(self):
56 self.faultString = self.renewProcess.getFaultString()
58 self.emitFinished("fault", self.faultString)
61 # we have to force sfi.py to download an updated slice credential
62 sliceCredName = config.fullpath("slice_" + self.hrn.split(".")[-1] + ".cred")
63 if os.path.exists(sliceCredName):
64 os.remove(sliceCredName)
66 open("/tmp/expiration", "w").write(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"))
68 # call renewSlivers on the aggregate
69 self.disconnect(self.renewProcess, SIGNAL('finished()'), self.finishedUpdateRecord)
70 self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedRenewSlivers)
71 self.renewProcess.renewSlivers(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"), slice = self.hrn)
73 def finishedRenewSlivers(self):
74 self.faultString = self.renewProcess.getFaultString()
76 self.emitFinished("fault", self.faultString)
79 self.emitFinished("success")
81 def emitFinished(self, status, statusMsg=None):
83 self.statusMsg = statusMsg
84 self.emit(SIGNAL("finished()"))
86 def getFaultString(self):
87 return self.faultString
89 class RenewWindow(QDialog):
90 def __init__(self, batch=False, parent=None):
91 super(RenewWindow, self).__init__(parent)
92 self.setWindowTitle("Renew Slivers")
96 self.renewProcess = None
98 durationLabel = QLabel("Duration:")
99 self.duration = QComboBox()
101 self.expirations = []
103 durations = ( (1, "One Week"), (2, "Two Weeks"), (3, "Three Weeks"), (4, "One Month") )
105 now = datetime.datetime.utcnow()
106 for (weeks, desc) in durations:
107 exp = now + datetime.timedelta(days = weeks * 7)
108 desc = desc + " " + exp.strftime("%Y-%m-%d %H:%M:%S")
109 self.expirations.append(exp)
110 self.duration.addItem(desc)
112 self.duration.setCurrentIndex(0)
115 sliceLabel = QLabel("Slices:")
116 self.sliceView = SliceView()
118 self.status = QLabel("")
119 self.status.setMaximumWidth(640)
121 self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
122 self.buttonBox.button(QDialogButtonBox.Ok).setDefault(True)
125 self.buttonBox.addButton("Batch", QDialogButtonBox.ActionRole)
127 layout = QVBoxLayout()
128 layout.addWidget(durationLabel)
129 layout.addWidget(self.duration)
131 layout.addWidget(sliceLabel)
132 layout.addWidget(self.sliceView)
133 layout.addWidget(self.status)
134 layout.addWidget(self.buttonBox)
135 self.setLayout(layout)
137 self.connect(self.buttonBox, SIGNAL("accepted()"), self, SLOT("accept()"))
138 self.connect(self.buttonBox, SIGNAL("rejected()"), self, SLOT("reject()"))
139 self.connect(self.buttonBox, SIGNAL("clicked(QAbstractButton *)"), self.clicked)
142 self.sliceModel = SliceModel()
143 self.refreshAuthority()
145 def clicked(self, button):
146 if button.text() == "Batch":
147 # close ourself, and reopen the batch renew window
149 dlg = RenewWindow(batch=True, parent=self.parent())
154 self.slicesToRenew = self.sliceModel.getSelectedSlices()
155 if self.slicesToRenew == []:
156 QMessageBox.warning(self, "No Slices", "Please add at least on slice by double-clicking on a slice name")
159 self.slicesToRenew = [config.getSlice()]
161 self.duration.setEnabled(False)
162 self.buttonBox.setEnabled(False)
164 self.sliceView.setEnabled(False)
166 self.renewNextSlice()
168 def setStatus(self, x):
169 self.status.setText(x)
171 def renewNextSlice(self):
172 self.sliceName = self.slicesToRenew.pop(0)
173 self.renewProcess = SfiRenewer(self.sliceName, self.get_new_expiration(), self)
174 self.connect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
175 self.setStatus("<font color='green'>Renewing: %s.</font>" % self.sliceName)
177 def renewFinished(self):
178 self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
180 faultString = self.renewProcess.getFaultString()
182 self.setStatus("<font color='red'>Renew %s Error: %s</font>" % (self.sliceName, faultString))
183 self.buttonBox.setEnabled(True)
185 self.setStatus("<font color='green'>Renew %s Success</font>" % self.sliceName)
187 if (self.slicesToRenew != []):
188 self.renewNextSlice()
190 # give the user the <close> button
191 self.buttonBox.clear()
192 self.buttonBox.addButton(QDialogButtonBox.Close)
193 self.buttonBox.setEnabled(True)
195 def get_new_expiration(self):
196 index = self.duration.currentIndex()
197 return self.expirations[index]
199 def refreshAuthority(self):
200 self.process = SfiProcess(self)
201 self.connect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
203 self.process.listRecords(config.getAuthority(), None)
204 self.setStatus("Refreshing slice list. This will take a moment...")
206 def getAuthorityRecordFinished(self):
207 self.disconnect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
209 faultString = self.process.getFaultString()
211 self.setStatus("<font color='green'>Slice list refreshed.</font>")
212 self.updateSliceView()
214 self.setStatus("<font color='red'>Authority rec refresh error: %s</font>" % (faultString))
216 def updateSliceView(self):
217 self.sliceModel.updateModel()
219 self.sliceView.setModel(self.sliceModel)
220 self.sliceView.resizeColumnToContents(0)