4 from StringIO import StringIO
5 from types import StringTypes
6 from copy import deepcopy
10 from sfa.util.sfalogging import sfa_logger
11 from sfa.util.rspecHelper import merge_rspecs
12 from sfa.util.xrn import Xrn, urn_to_hrn, hrn_to_urn
13 from sfa.util.plxrn import hrn_to_pl_slicename
14 from sfa.util.rspec import *
15 from sfa.util.specdict import *
16 from sfa.util.faults import *
17 from sfa.util.record import SfaRecord
18 from sfa.util.policy import Policy
19 from sfa.util.prefixTree import prefixTree
20 from sfa.util.sfaticket import *
21 from sfa.trust.credential import Credential
22 from sfa.util.threadmanager import ThreadManager
23 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
24 import sfa.plc.peers as peers
25 from sfa.util.version import version_core
26 from sfa.util.callids import Callids
28 # XX FIX ME: should merge result from multiple aggregates instead of
29 # calling aggregate implementation
30 from sfa.managers.aggregate_manager_pl import slice_status
32 # we have specialized xmlrpclib.ServerProxy to remember the input url
33 # OTOH it's not clear if we're only dealing with XMLRPCServerProxy instances
34 def get_serverproxy_url (server):
38 sfa_logger().warning("GetVersion, falling back to xmlrpclib.ServerProxy internals")
39 return server._ServerProxy__host + server._ServerProxy__handler
42 # peers explicitly in aggregates.xml
43 peers =dict ([ (peername,get_serverproxy_url(v)) for (peername,v) in api.aggregates.iteritems()
44 if peername != api.hrn])
46 sm_version=version_core({'interface':'slicemgr',
47 'hrn' : xrn.get_hrn(),
48 'urn' : xrn.get_urn(),
51 # local aggregate if present needs to have localhost resolved
52 if api.hrn in api.aggregates:
53 local_am_url=get_serverproxy_url(api.aggregates[api.hrn])
54 sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname'])
57 def CreateSliver(api, xrn, creds, rspec, users, call_id):
59 if Callids().already_handled(call_id): return ""
61 hrn, type = urn_to_hrn(xrn)
63 # Validate the RSpec against PlanetLab's schema --disabled for now
64 # The schema used here needs to aggregate the PL and VINI schemas
65 # schema = "/var/www/html/schemas/pl.rng"
69 tree = etree.parse(StringIO(rspec))
70 except etree.XMLSyntaxError:
71 message = str(sys.exc_info()[1])
72 raise InvalidRSpec(message)
74 relaxng_doc = etree.parse(schema)
75 relaxng = etree.RelaxNG(relaxng_doc)
78 error = relaxng.error_log.last_error
79 message = "%s (line %s)" % (error.message, error.line)
80 raise InvalidRSpec(message)
83 valid_cred = api.auth.checkCredentials(creds, 'createsliver', hrn)[0]
84 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
86 # attempt to use delegated credential first
87 credential = api.getDelegatedCredential(creds)
89 credential = api.getCredential()
90 threads = ThreadManager()
91 for aggregate in api.aggregates:
92 # prevent infinite loop. Dont send request back to caller
93 # unless the caller is the aggregate's SM
94 if caller_hrn == aggregate and aggregate != api.hrn:
97 # Just send entire RSpec to each aggregate
98 server = api.aggregates[aggregate]
99 threads.run(server.CreateSliver, xrn, credential, rspec, users, call_id)
101 results = threads.get_results()
102 merged_rspec = merge_rspecs(results)
105 def RenewSliver(api, xrn, creds, expiration_time, call_id):
106 if Callids().already_handled(call_id): return True
108 (hrn, type) = urn_to_hrn(xrn)
109 # get the callers hrn
110 valid_cred = api.auth.checkCredentials(creds, 'renewsliver', hrn)[0]
111 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
113 # attempt to use delegated credential first
114 credential = api.getDelegatedCredential(creds)
116 credential = api.getCredential()
117 threads = ThreadManager()
118 for aggregate in api.aggregates:
119 # prevent infinite loop. Dont send request back to caller
120 # unless the caller is the aggregate's SM
121 if caller_hrn == aggregate and aggregate != api.hrn:
124 server = api.aggregates[aggregate]
125 threads.run(server.RenewSliver, xrn, [credential], expiration_time, call_id)
127 return reduce (lambda x,y: x and y, threads.get_results() , True)
129 def get_ticket(api, xrn, creds, rspec, users):
130 slice_hrn, type = urn_to_hrn(xrn)
131 # get the netspecs contained within the clients rspec
132 aggregate_rspecs = {}
133 tree= etree.parse(StringIO(rspec))
134 elements = tree.findall('./network')
135 for element in elements:
136 aggregate_hrn = element.values()[0]
137 aggregate_rspecs[aggregate_hrn] = rspec
139 # get the callers hrn
140 valid_cred = api.auth.checkCredentials(creds, 'getticket', slice_hrn)[0]
141 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
143 # attempt to use delegated credential first
144 credential = api.getDelegatedCredential(creds)
146 credential = api.getCredential()
147 threads = ThreadManager()
148 for (aggregate, aggregate_rspec) in aggregate_rspecs.iteritems():
149 # prevent infinite loop. Dont send request back to caller
150 # unless the caller is the aggregate's SM
151 if caller_hrn == aggregate and aggregate != api.hrn:
154 if aggregate in api.aggregates:
155 server = api.aggregates[aggregate]
157 net_urn = hrn_to_urn(aggregate, 'authority')
158 # we may have a peer that knows about this aggregate
159 for agg in api.aggregates:
160 target_aggs = api.aggregates[agg].get_aggregates(credential, net_urn)
161 if not target_aggs or not 'hrn' in target_aggs[0]:
163 # send the request to this address
164 url = target_aggs[0]['url']
165 server = xmlrpcprotocol.get_server(url, api.key_file, api.cert_file)
166 # aggregate found, no need to keep looping
170 threads.run(server.GetTicket, xrn, credential, aggregate_rspec, users)
172 results = threads.get_results()
174 # gather information from each ticket
179 for result in results:
180 agg_ticket = SfaTicket(string=result)
181 attrs = agg_ticket.get_attributes()
183 object_gid = agg_ticket.get_gid_object()
184 rspecs.append(agg_ticket.get_rspec())
185 initscripts.extend(attrs.get('initscripts', []))
186 slivers.extend(attrs.get('slivers', []))
189 attributes = {'initscripts': initscripts,
191 merged_rspec = merge_rspecs(rspecs)
193 # create a new ticket
194 ticket = SfaTicket(subject = slice_hrn)
195 ticket.set_gid_caller(api.auth.client_gid)
196 ticket.set_issuer(key=api.key, subject=api.hrn)
197 ticket.set_gid_object(object_gid)
198 ticket.set_pubkey(object_gid.get_pubkey())
199 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
200 ticket.set_attributes(attributes)
201 ticket.set_rspec(merged_rspec)
204 return ticket.save_to_string(save_parents=True)
207 def DeleteSliver(api, xrn, creds, call_id):
208 if Callids().already_handled(call_id): return ""
209 (hrn, type) = urn_to_hrn(xrn)
210 # get the callers hrn
211 valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0]
212 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
214 # attempt to use delegated credential first
215 credential = api.getDelegatedCredential(creds)
217 credential = api.getCredential()
218 threads = ThreadManager()
219 for aggregate in api.aggregates:
220 # prevent infinite loop. Dont send request back to caller
221 # unless the caller is the aggregate's SM
222 if caller_hrn == aggregate and aggregate != api.hrn:
224 server = api.aggregates[aggregate]
225 threads.run(server.DeleteSliver, xrn, credential, call_id)
226 threads.get_results()
229 def start_slice(api, xrn, creds):
230 hrn, type = urn_to_hrn(xrn)
232 # get the callers hrn
233 valid_cred = api.auth.checkCredentials(creds, 'startslice', hrn)[0]
234 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
236 # attempt to use delegated credential first
237 credential = api.getDelegatedCredential(creds)
239 credential = api.getCredential()
240 threads = ThreadManager()
241 for aggregate in api.aggregates:
242 # prevent infinite loop. Dont send request back to caller
243 # unless the caller is the aggregate's SM
244 if caller_hrn == aggregate and aggregate != api.hrn:
246 server = api.aggregates[aggregate]
247 threads.run(server.Start, xrn, credential)
248 threads.get_results()
251 def stop_slice(api, xrn, creds):
252 hrn, type = urn_to_hrn(xrn)
254 # get the callers hrn
255 valid_cred = api.auth.checkCredentials(creds, 'stopslice', hrn)[0]
256 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
258 # attempt to use delegated credential first
259 credential = api.getDelegatedCredential(creds)
261 credential = api.getCredential()
262 threads = ThreadManager()
263 for aggregate in api.aggregates:
264 # prevent infinite loop. Dont send request back to caller
265 # unless the caller is the aggregate's SM
266 if caller_hrn == aggregate and aggregate != api.hrn:
268 server = api.aggregates[aggregate]
269 threads.run(server.Stop, xrn, credential)
270 threads.get_results()
273 def reset_slice(api, xrn):
279 def shutdown(api, xrn, creds):
285 def status(api, xrn, creds):
291 # Thierry : caching at the slicemgr level makes sense to some extent
294 def ListSlices(api, creds, call_id):
296 if Callids().already_handled(call_id): return []
298 # look in cache first
299 if caching and api.cache:
300 slices = api.cache.get('slices')
304 # get the callers hrn
305 valid_cred = api.auth.checkCredentials(creds, 'listslices', None)[0]
306 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
308 # attempt to use delegated credential first
309 credential = api.getDelegatedCredential(creds)
311 credential = api.getCredential()
312 threads = ThreadManager()
313 # fetch from aggregates
314 for aggregate in api.aggregates:
315 # prevent infinite loop. Dont send request back to caller
316 # unless the caller is the aggregate's SM
317 if caller_hrn == aggregate and aggregate != api.hrn:
319 server = api.aggregates[aggregate]
320 threads.run(server.ListSlices, credential, call_id)
323 results = threads.get_results()
325 for result in results:
326 slices.extend(result)
329 if caching and api.cache:
330 api.cache.add('slices', slices)
335 def ListResources(api, creds, options, call_id):
337 if Callids().already_handled(call_id): return ""
339 # get slice's hrn from options
340 xrn = options.get('geni_slice_urn', '')
341 (hrn, type) = urn_to_hrn(xrn)
343 # get hrn of the original caller
344 origin_hrn = options.get('origin_hrn', None)
346 if isinstance(creds, list):
347 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
349 origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
351 # look in cache first
352 if caching and api.cache and not xrn:
353 rspec = api.cache.get('nodes')
357 # get the callers hrn
358 valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
359 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
361 # attempt to use delegated credential first
362 credential = api.getDelegatedCredential(creds)
364 credential = api.getCredential()
365 threads = ThreadManager()
366 for aggregate in api.aggregates:
367 # prevent infinite loop. Dont send request back to caller
368 # unless the caller is the aggregate's SM
369 if caller_hrn == aggregate and aggregate != api.hrn:
371 # get the rspec from the aggregate
372 server = api.aggregates[aggregate]
373 my_opts = copy(options)
374 my_opts['geni_compressed'] = False
375 threads.run(server.ListResources, credential, my_opts, call_id)
376 #threads.run(server.get_resources, cred, xrn, origin_hrn)
378 results = threads.get_results()
379 merged_rspec = merge_rspecs(results)
382 if caching and api.cache and not xrn:
383 api.cache.add('nodes', merged_rspec)
389 r.parseFile(sys.argv[1])
391 CreateSliver(None,'plc.princeton.tmacktestslice',rspec,'create-slice-tmacktestslice')
393 if __name__ == "__main__":