make renew a self-contained dialog
[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
14 class SfiRenewer(QObject):
15     def __init__(self, hrn, newExpiration, parent=None):
16         QObject.__init__(self, parent)
17         self.hrn = hrn
18         self.newExpiration = newExpiration
19
20         self.renewProcess = SfiProcess(self)
21         self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
22         self.renewProcess.getRecord(hrn=config.getSlice(), filename="/tmp/slicerecord")
23
24     def finishedGetRecord(self):
25         f = open("/tmp/slicerecord", "r")
26         data = f.read()
27         f.close()
28
29         # find the expiration time
30         exp = re.compile('expires="[^"]*"')
31         if exp.search(data)==None:
32             # didn't find it
33             self.emitFinished("failure", "failed to find expiration in slice record")
34             return
35
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)
39
40         open("/tmp/slicerecord", "w").write(data)
41
42         self.disconnect(self.renewProcess, SIGNAL('finished()'), self.finishedGetRecord)
43         self.connect(self.renewProcess, SIGNAL('finished()'), self.finishedUpdateRecord)
44
45         self.renewProcess.updateRecord("/tmp/slicerecord")
46
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)
52
53         open("/tmp/expiration", "w").write(self.newExpiration.strftime("%Y-%m-%dT%H:%M:%SZ"))
54
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"))
59
60     def finishedRenewSlivers(self):
61         self.emitFinished("success")
62
63     def emitFinished(self, status, statusMsg=None):
64         self.status = status
65         self.statusMsg = statusMsg
66         self.emit(SIGNAL("finished()"))
67
68 class RenewWindow(QDialog):
69     def __init__(self, parent=None):
70         super(RenewWindow, self).__init__(parent)
71         self.setWindowTitle("Renew Slivers")
72
73         self.renewProcess = None
74
75         self.duration = QComboBox()
76
77         self.expirations = []
78
79         durations = ( (1, "One Week"), (2, "Two Weeks"), (3, "Three Weeks"), (4, "One Month") )
80
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)
87
88         self.duration.setCurrentIndex(0)
89
90         self.status = QLabel("")
91
92         self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
93         self.buttonBox.button(QDialogButtonBox.Ok).setDefault(True)
94
95         layout = QVBoxLayout()
96         layout.addWidget(self.duration)
97         layout.addWidget(self.status)
98         layout.addWidget(self.buttonBox)
99         self.setLayout(layout)
100
101         #self.status.hide()
102
103         self.connect(self.buttonBox, SIGNAL("accepted()"), self, SLOT("accept()"))
104         self.connect(self.buttonBox, SIGNAL("rejected()"), self, SLOT("reject()"))
105
106     def accept(self):
107         self.setStatus("Renewing Slice...")
108
109         self.renewProcess = SfiRenewer(config.getSlice(), self.get_new_expiration(), self)
110         self.connect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
111
112         self.duration.setEnabled(False)
113         self.buttonBox.setEnabled(False)
114
115     def setStatus(self, x):
116         self.status.setText(x)
117
118     def renewFinished(self):
119         if self.renewProcess.statusMsg:
120             self.setStatus("Renew " + self.renewProcess.status + ": " + self.renewProcess.statusMsg)
121         else:
122             self.setStatus("Renew " + self.renewProcess.status)
123         self.disconnect(self.renewProcess, SIGNAL('finished()'), self.renewFinished)
124         self.renewProcess = None
125
126         self.buttonBox.setEnabled(True)
127         self.buttonBox.clear()
128         self.buttonBox.addButton(QDialogButtonBox.Close)
129
130     def get_new_expiration(self):
131         index = self.duration.currentIndex()
132         return self.expirations[index]