added renew slice mechanism
[sface.git] / sface / sfirenew.py
diff --git a/sface/sfirenew.py b/sface/sfirenew.py
new file mode 100644 (file)
index 0000000..863b253
--- /dev/null
@@ -0,0 +1,65 @@
+import calendar
+import datetime
+import os
+import re
+import sys
+import time
+
+from PyQt4.QtCore import *
+from sface.config import config
+from sface.sfiprocess import SfiProcess
+
+class SfiRenewer(QObject):
+    def __init__(self, hrn, newExpiration, parent=None):
+        QObject.__init__(self, parent)
+        self.hrn = hrn
+        self.newExpiration = newExpiration
+
+        self.renewProcess = SfiProcess(self)
+        self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
+        self.renewProcess.getRecord(hrn=config.getSlice(), filename="/tmp/slicerecord")
+
+    def finishedGetRecord(self):
+        f = open("/tmp/slicerecord", "r")
+        data = f.read()
+        f.close()
+
+        # find the expiration time
+        exp = re.compile('expires="[^"]*"')
+        if exp.search(data)==None:
+            # didn't find it
+            self.emitFinished("failure", "failed to find expiration in slice record")
+            return
+
+        # change the expiration time
+        delta = 24*60*60 # always extend the slice by one extra day to cover slop for time zone differences
+        data = exp.sub('expires="' + str(calendar.timegm(self.newExpiration.timetuple())+delta) + '"', data)
+
+        open("/tmp/slicerecord", "w").write(data)
+
+        self.disconnect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
+        self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedUpdateRecord)
+
+        self.renewProcess.updateRecord("/tmp/slicerecord")
+
+    def finishedUpdateRecord(self):
+        # we have to force sfi.py to download an updated slice credential
+        sliceCredName = os.path.expanduser("~/.sfi/slice_" + self.hrn.split(".")[-1] + ".cred")
+        if os.path.exists(sliceCredName):
+            os.remove(sliceCredName)
+
+        open("/tmp/expiration", "w").write(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"))
+
+        # 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"))
+
+    def finishedRenewSlivers(self):
+        self.emitFinished("success")
+
+    def emitFinished(self, status, statusMsg=None):
+        self.status = status
+        self.statusMsg = statusMsg
+        self.emit(SIGNAL("finished()"))
+