add sliver_type column, sorting in mainscreen
[sface.git] / sface / sfirenew.py
1 import calendar
2 import datetime
3 import os
4 import re
5 import sys
6 import time
7
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
14
15 class SfiRenewer(QObject):
16     def __init__(self, hrn, newExpiration, parent=None):
17         QObject.__init__(self, parent)
18         self.hrn = hrn
19         self.newExpiration = newExpiration
20         self.faultString = None
21
22         self.renewProcess = SfiProcess(self)
23         self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
24         self.renewProcess.getRecord(hrn=hrn, filename="/tmp/slicerecord")
25
26     def finishedGetRecord(self):
27         self.faultString = self.renewProcess.getFaultString()
28         if self.faultString:
29             self.emitFinished("fault", self.faultString)
30             return
31
32         f = open("/tmp/slicerecord", "r")
33         data = f.read()
34         f.close()
35
36         # find the expiration time
37         exp = re.compile('expires="[^"]*"')
38         if exp.search(data)==None:
39             # didn't find it
40             self.faultString = "failed to find expiration in slice record"
41             self.emitFinished("failure", self.faultString)
42             return
43
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)
47
48         open("/tmp/slicerecord", "w").write(data)
49
50         self.disconnect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
51         self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedUpdateRecord)
52
53         self.renewProcess.updateRecord("/tmp/slicerecord")
54
55     def finishedUpdateRecord(self):
56         self.faultString = self.renewProcess.getFaultString()
57         if self.faultString:
58             self.emitFinished("fault", self.faultString)
59             return
60
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)
65
66         open("/tmp/expiration", "w").write(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"))
67
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)
72
73     def finishedRenewSlivers(self):
74         self.faultString = self.renewProcess.getFaultString()
75         if self.faultString:
76             self.emitFinished("fault", self.faultString)
77             return
78
79         self.emitFinished("success")
80
81     def emitFinished(self, status, statusMsg=None):
82         self.status = status
83         self.statusMsg = statusMsg
84         self.emit(SIGNAL("finished()"))
85
86     def getFaultString(self):
87         return self.faultString
88
89 class RenewWindow(QDialog):
90     def __init__(self, batch=False, parent=None):
91         super(RenewWindow, self).__init__(parent)
92         self.setWindowTitle("Renew Slivers")
93
94         self.batch = batch
95
96         self.renewProcess = None
97
98         durationLabel = QLabel("Duration:")
99         self.duration = QComboBox()
100
101         self.expirations = []
102
103         durations = ( (1, "One Week"), (2, "Two Weeks"), (3, "Three Weeks"), (4, "One Month") )
104
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)
111
112         self.duration.setCurrentIndex(0)
113
114         if self.batch:
115             sliceLabel = QLabel("Slices:")
116             self.sliceView = SliceView()
117
118         self.status = QLabel("")
119         self.status.setMaximumWidth(640)
120
121         self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
122         self.buttonBox.button(QDialogButtonBox.Ok).setDefault(True)
123
124         if not self.batch:
125             self.buttonBox.addButton("Batch", QDialogButtonBox.ActionRole)
126
127         layout = QVBoxLayout()
128         layout.addWidget(durationLabel)
129         layout.addWidget(self.duration)
130         if self.batch:
131             layout.addWidget(sliceLabel)
132             layout.addWidget(self.sliceView)
133         layout.addWidget(self.status)
134         layout.addWidget(self.buttonBox)
135         self.setLayout(layout)
136
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)
140
141         if self.batch:
142             self.sliceModel = SliceModel()
143             self.refreshAuthority()
144
145     def clicked(self, button):
146         if button.text() == "Batch":
147             # close ourself, and reopen the batch renew window
148             self.close()
149             dlg = RenewWindow(batch=True, parent=self.parent())
150             dlg.exec_()
151
152     def accept(self):
153         if self.batch:
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")
157                 return
158         else:
159             self.slicesToRenew = [config.getSlice()]
160
161         self.duration.setEnabled(False)
162         self.buttonBox.setEnabled(False)
163         if self.batch:
164             self.sliceView.setEnabled(False)
165
166         self.renewNextSlice()
167
168     def setStatus(self, x):
169         self.status.setText(x)
170
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)
176
177     def renewFinished(self):
178         self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
179
180         faultString = self.renewProcess.getFaultString()
181         if faultString:
182             self.setStatus("<font color='red'>Renew %s Error: %s</font>" % (self.sliceName, faultString))
183             self.buttonBox.setEnabled(True)
184         else:
185             self.setStatus("<font color='green'>Renew %s Success</font>" % self.sliceName)
186
187             if (self.slicesToRenew != []):
188                 self.renewNextSlice()
189             else:
190                 # give the user the <close> button
191                 self.buttonBox.clear()
192                 self.buttonBox.addButton(QDialogButtonBox.Close)
193                 self.buttonBox.setEnabled(True)
194
195     def get_new_expiration(self):
196         index = self.duration.currentIndex()
197         return self.expirations[index]
198
199     def refreshAuthority(self):
200         self.process = SfiProcess(self)
201         self.connect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
202
203         self.process.listRecords(config.getAuthority(), None)
204         self.setStatus("Refreshing slice list. This will take a moment...")
205
206     def getAuthorityRecordFinished(self):
207         self.disconnect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
208
209         faultString = self.process.getFaultString()
210         if not faultString:
211             self.setStatus("<font color='green'>Slice list refreshed.</font>")
212             self.updateSliceView()
213         else:
214             self.setStatus("<font color='red'>Authority rec refresh error: %s</font>" % (faultString))
215
216     def updateSliceView(self):
217         self.sliceModel.updateModel()
218
219         self.sliceView.setModel(self.sliceModel)
220         self.sliceView.resizeColumnToContents(0)
221