new slide credential file names
[sface.git] / sface / sfirenew.py
index 682d09a..f2cab6b 100644 (file)
@@ -10,6 +10,7 @@ from PyQt4.QtGui import *
 from sface.config import config
 from sface.sfiprocess import SfiProcess
 #from sface.sfithread import SfiThread
+from sface.sliceview import SliceView, SliceModel
 
 class SfiRenewer(QObject):
     def __init__(self, hrn, newExpiration, parent=None):
@@ -20,12 +21,12 @@ class SfiRenewer(QObject):
 
         self.renewProcess = SfiProcess(self)
         self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
-        self.renewProcess.getRecord(hrn=config.getSlice(), filename="/tmp/slicerecord")
+        self.renewProcess.getRecord(hrn=hrn, filename="/tmp/slicerecord")
 
     def finishedGetRecord(self):
-        faultString = self.renewProcess.getFaultString()
-        if faultString:
-            self.emitFinished("fault", faultString)
+        self.faultString = self.renewProcess.getFaultString()
+        if self.faultString:
+            self.emitFinished("fault", self.faultString)
             return
 
         f = open("/tmp/slicerecord", "r")
@@ -36,7 +37,8 @@ class SfiRenewer(QObject):
         exp = re.compile('expires="[^"]*"')
         if exp.search(data)==None:
             # didn't find it
-            self.emitFinished("failure", "failed to find expiration in slice record")
+            self.faultString = "failed to find expiration in slice record"
+            self.emitFinished("failure", self.faultString)
             return
 
         # change the expiration time
@@ -51,13 +53,18 @@ class SfiRenewer(QObject):
         self.renewProcess.updateRecord("/tmp/slicerecord")
 
     def finishedUpdateRecord(self):
-        faultString = self.renewProcess.getFaultString()
-        if faultString:
-            self.emitFinished("fault", faultString)
+        self.faultString = self.renewProcess.getFaultString()
+        if self.faultString:
+            self.emitFinished("fault", self.faultString)
             return
 
         # we have to force sfi.py to download an updated slice credential
-        sliceCredName = os.path.expanduser("~/.sfi/slice_" + self.hrn.split(".")[-1] + ".cred")
+        sliceCredName = config.fullpath("slice_" + self.hrn.split(".")[-1] + ".cred")
+        if os.path.exists(sliceCredName):
+            os.remove(sliceCredName)
+
+        # newer SFA has a slightly different slice credential name
+        sliceCredName = config.fullpath(self.hrn + ".slice.cred")
         if os.path.exists(sliceCredName):
             os.remove(sliceCredName)
 
@@ -66,12 +73,12 @@ class SfiRenewer(QObject):
         # call renewSlivers on the aggregate
         self.disconnect(self.renewProcess, SIGNAL('finished()'), self.finishedUpdateRecord)
         self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedRenewSlivers)
-        self.renewProcess.renewSlivers(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"))
+        self.renewProcess.renewSlivers(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"), slice = self.hrn)
 
     def finishedRenewSlivers(self):
-        faultString = self.renewProcess.getFaultString()
-        if faultString:
-            self.emitFinished("fault", faultString)
+        self.faultString = self.renewProcess.getFaultString()
+        if self.faultString:
+            self.emitFinished("fault", self.faultString)
             return
 
         self.emitFinished("success")
@@ -81,13 +88,19 @@ class SfiRenewer(QObject):
         self.statusMsg = statusMsg
         self.emit(SIGNAL("finished()"))
 
+    def getFaultString(self):
+        return self.faultString
+
 class RenewWindow(QDialog):
-    def __init__(self, parent=None):
+    def __init__(self, batch=False, parent=None):
         super(RenewWindow, self).__init__(parent)
         self.setWindowTitle("Renew Slivers")
 
+        self.batch = batch
+
         self.renewProcess = None
 
+        durationLabel = QLabel("Duration:")
         self.duration = QComboBox()
 
         self.expirations = []
@@ -103,53 +116,111 @@ class RenewWindow(QDialog):
 
         self.duration.setCurrentIndex(0)
 
+        if self.batch:
+            sliceLabel = QLabel("Slices:")
+            self.sliceView = SliceView()
+
         self.status = QLabel("")
+        self.status.setMaximumWidth(640)
 
         self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
         self.buttonBox.button(QDialogButtonBox.Ok).setDefault(True)
 
+        if not self.batch:
+            self.buttonBox.addButton("Batch", QDialogButtonBox.ActionRole)
+
         layout = QVBoxLayout()
+        layout.addWidget(durationLabel)
         layout.addWidget(self.duration)
+        if self.batch:
+            layout.addWidget(sliceLabel)
+            layout.addWidget(self.sliceView)
         layout.addWidget(self.status)
         layout.addWidget(self.buttonBox)
         self.setLayout(layout)
 
-        #self.status.hide()
-
         self.connect(self.buttonBox, SIGNAL("accepted()"), self, SLOT("accept()"))
         self.connect(self.buttonBox, SIGNAL("rejected()"), self, SLOT("reject()"))
+        self.connect(self.buttonBox, SIGNAL("clicked(QAbstractButton *)"), self.clicked)
 
-    def accept(self):
-        self.setStatus("Renewing Slice...")
+        if self.batch:
+            self.sliceModel = SliceModel()
+            self.refreshAuthority()
 
-        self.renewProcess = SfiRenewer(config.getSlice(), self.get_new_expiration(), self)
-        self.connect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
+    def clicked(self, button):
+        if button.text() == "Batch":
+            # close ourself, and reopen the batch renew window
+            self.close()
+            dlg = RenewWindow(batch=True, parent=self.parent())
+            dlg.exec_()
+
+    def accept(self):
+        if self.batch:
+            self.slicesToRenew = self.sliceModel.getSelectedSlices()
+            if self.slicesToRenew == []:
+                QMessageBox.warning(self, "No Slices", "Please add at least on slice by double-clicking on a slice name")
+                return
+        else:
+            self.slicesToRenew = [config.getSlice()]
 
         self.duration.setEnabled(False)
         self.buttonBox.setEnabled(False)
+        if self.batch:
+            self.sliceView.setEnabled(False)
+
+        self.renewNextSlice()
 
     def setStatus(self, x):
         self.status.setText(x)
 
+    def renewNextSlice(self):
+        self.sliceName = self.slicesToRenew.pop(0)
+        self.renewProcess = SfiRenewer(self.sliceName, self.get_new_expiration(), self)
+        self.connect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
+        self.setStatus("<font color='green'>Renewing: %s.</font>" % self.sliceName)
+
     def renewFinished(self):
-        if self.renewProcess.status == "success":
-            color = "green"
-            # give the user the <close> button
-            self.buttonBox.clear()
-            self.buttonBox.addButton(QDialogButtonBox.Close)
-        else:
-            color = "red"
+        self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
 
-        if self.renewProcess.statusMsg:
-            self.setStatus("<font color='%s'>Renew %s: %s</font>" % (color, self.renewProcess.status, self.renewProcess.statusMsg))
+        faultString = self.renewProcess.getFaultString()
+        if faultString:
+            self.setStatus("<font color='red'>Renew %s Error: %s</font>" % (self.sliceName, faultString))
+            self.buttonBox.setEnabled(True)
         else:
-            self.setStatus("<font color='%s'>Renew %s</font>" % (color, self.renewProcess.status))
+            self.setStatus("<font color='green'>Renew %s Success</font>" % self.sliceName)
 
-        self.buttonBox.setEnabled(True)
-
-        self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
-        self.renewProcess = None
+            if (self.slicesToRenew != []):
+                self.renewNextSlice()
+            else:
+                # give the user the <close> button
+                self.buttonBox.clear()
+                self.buttonBox.addButton(QDialogButtonBox.Close)
+                self.buttonBox.setEnabled(True)
 
     def get_new_expiration(self):
         index = self.duration.currentIndex()
         return self.expirations[index]
+
+    def refreshAuthority(self):
+        self.process = SfiProcess(self)
+        self.connect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
+
+        self.process.listRecords(config.getAuthority(), None)
+        self.setStatus("Refreshing slice list. This will take a moment...")
+
+    def getAuthorityRecordFinished(self):
+        self.disconnect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
+
+        faultString = self.process.getFaultString()
+        if not faultString:
+            self.setStatus("<font color='green'>Slice list refreshed.</font>")
+            self.updateSliceView()
+        else:
+            self.setStatus("<font color='red'>Authority rec refresh error: %s</font>" % (faultString))
+
+    def updateSliceView(self):
+        self.sliceModel.updateModel()
+
+        self.sliceView.setModel(self.sliceModel)
+        self.sliceView.resizeColumnToContents(0)
+