import repository from arizona
[raven.git] / apps / gacksui / gackshelper.py
1 import os
2
3 import calendar
4 import cPickle
5 import tempfile
6 import time
7
8 from sfa.trust.certificate import Keypair, Certificate
9 from sfa.trust.gid import GID
10 from sfa.trust.credential import Credential
11 #import sfa.client.sfaprotocol as sfaprotocol
12 from sfa.client.sfaserverproxy import SfaServerProxy
13
14 from gackshandle import *
15 from gacksjob import *
16 from gacksclient import GacksClient
17 from gacksresource import *
18 from gacksauth import GacksAuthToken
19
20 class Gacks:
21     def __init__(self, reg_url=None):
22         if not reg_url:
23             reg_url = "http://www.planet-lab.org:12345/"
24         self.reg_url = reg_url
25         self._registry = None
26         self._client = None
27         self._timezone = -(time.timezone/3600)
28         self._grm_gid = None
29         self._gid = None
30         self._key_file = None
31         #self._cred_file = None
32         self.resources_hash = ""
33         self.resources_checked = False
34         self.resources = None
35         self._resources_file = os.path.expanduser("~/.sfi/gacks_cached_resources")
36         #self._cred_file = os.path.expanduser("~/.sfi/gacks_identity_cred")
37         #self._user_cred_file = os.path.expanduser("~/.sfi/gacks_user_cred")
38         self.load_cached_resources()
39
40     def load_cached_resources(self):
41         if not os.path.exists(self._resources_file):
42             return
43         resources_str = file(self._resources_file,"r").read()
44         self.resources = cPickle.loads(resources_str)
45         self.resources_hash = hashlib.sha1(resources_str).hexdigest()
46         print "Gacks: loaded resources with hash", self.resources_hash
47
48     def set_resources(self, resources_str):
49         dir = os.path.dirname(self._resources_file)
50         if not os.path.exists(dir):
51            os.makedirs(dir)
52         file(self._resources_file,"w").write(resources_str)
53         self.resources = cPickle.loads(resources_str)
54         self.resources_hash = hashlib.sha1(resources_str).hexdigest()
55         print "Gacks: saved resources with hash", self.resources_hash
56
57     def get_registry(self):
58         if self._registry is not None:
59            return self._registry
60
61         (tmph, cert_file) = tempfile.mkstemp("cert")
62
63         # this is littering temp files
64         os.write(tmph, self._cert.save_to_string(save_parents=True))
65         os.close(tmph)
66
67         print "Gacks: connecting to registry at", self.reg_url
68
69         self._registry = SfaServerProxy(self.reg_url, self._key_file, cert_file)
70
71         return self._registry
72
73     def get_cert(self, key_file, hrn):
74         k = Keypair(filename=key_file)
75         cert = Certificate(subject=hrn)
76         cert.set_pubkey(k)
77         cert.set_issuer(k, hrn)
78         cert.sign()
79         return cert
80
81     def get_cred(self, type, hrn):
82         # attempt to load a cached credential
83         cred = None
84         if not cred:
85             registry = self.get_registry()
86
87             if type in ['user']:
88                 print "Gacks: downloading user self credential for", hrn
89                 cert_string = self._cert.save_to_string(save_parents=True)
90                 cred_str = registry.GetSelfCredential(cert_string, hrn, "user")
91             else:
92                 # bootstrap slice credential from user credential
93                 user_cred = self.get_cred("user", self.user_hrn)
94                 print "Gacks: downloading", type, "credential for", hrn
95                 cred_str = registry.GetCredential(user_cred.save_to_string(save_parents=True), hrn, type)
96
97             cred = Credential(string=cred_str)
98
99             print "Gacks: successfully retrieved credential for", hrn
100
101         return cred
102
103     def set_identity(self, key_file, user_hrn, identity_hrn): #cred_file):
104         self._key_file = key_file
105         self._key = Keypair(filename = self._key_file)
106         self._cert = self.get_cert(key_file, user_hrn)
107
108         self.user_hrn = user_hrn
109         self.identity_hrn = identity_hrn
110
111         if (user_hrn == identity_hrn):
112             self._cred = self.get_cred("user", identity_hrn)
113         else:
114             self._cred = self.get_cred("slice", identity_hrn)
115
116         # make sure these get reloaded if they are already set
117         self._client = None
118
119         self._gid = self._cred.get_gid_object()
120
121     def identity_okay(self):
122         return (self._gid != None)
123
124     def client(self):
125         if not self._client:\r
126             self._client = GacksClient("https://vicci.org/GACKSAPI/", self._key_file)\r
127             #self._client = GacksClient("https://198.0.0.136/GACKSAPI/", self._key_file)\r
128         return self._client\r
129 \r
130     def time_to_str(self, t):\r
131         if not t:
132             return "None"
133         if t==INFINITY:
134             return "Infinity"
135
136         t = t + self.get_timezone() * 60 *60
137         gmt = time.gmtime(int(t))
138         return "%04d-%02d-%02d %02d:%02d:%02d" % gmt[0:6]\r
139 \r
140     def get_timezone(self):\r
141         return self._timezone\r
142 \r
143     def ymd_to_time(self, y=None, m=None, d=None, hour=None):\r
144         if (y==None) or (m==None) or (d==None):
145             gmt = time.gmtime(time.time() + int(self.get_timezone()*60*60))
146             y=gmt[0]
147             m=gmt[1]
148             d=gmt[2]
149
150         y = int(y)
151         m = int(m)
152         d = int(d)
153
154         if hour:
155             h = int(hour)
156         else:
157             h = 0
158
159         return self.timetuple_to_time((y, m, d, h, 0, 0, 0, 0, 0))
160 \r
161     def timetuple_to_time(self, timetuple, plus_h=0):\r
162         return calendar.timegm(timetuple) + plus_h*60*60\r
163 \r
164     def get_authtoken(self):\r
165         return GacksAuthToken(cred=self._cred, pkey=self._key)\r
166 \r
167     def get_gid(self):\r
168         return self._gid\r
169 \r
170     def get_hrn(self):\r
171         return self.get_gid().get_hrn()\r
172 \r
173     #def get_grm_gid(self):\r
174     #    if not self._grm_gid:\r
175     #        gid_file = "/home/smbaker/projects/stork/trunk/tools/sfastuff/keys/grm.gid"\r
176     #        self._grm_gid = GID(filename=gid_file)\r
177     #    return self._grm_gid\r
178 \r
179     def get_grm_hrn(self):\r
180         return "plc.arizona.grm"\r
181         #return self.get_grm_gid().get_hrn()\r
182 \r
183     def get_matching_reclist(self, rec):\r
184         recList = self.client().query_exact(id = rec.id, timeStart=rec.timeStart, timeStop=rec.timeStop, hasAllocator = rec.allocatorHRNs[-1])\r
185
186         # make sure we only get ones with the exact same allocatorHRNs and consumerHRN
187         recList2 = []
188         for candidate in recList:
189             if (candidate.get_agg_key() == rec.get_agg_key()):
190                 recList2.append(rec)\r
191 \r
192         return recList2\r