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.rspecs.sfa_rspec import SfaRSpec
19 from sfa.util.policy import Policy
20 from sfa.util.prefixTree import prefixTree
21 from sfa.util.sfaticket import *
22 from sfa.trust.credential import Credential
23 from sfa.util.threadmanager import ThreadManager
24 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
25 import sfa.plc.peers as peers
26 from sfa.util.version import version_core
27 from sfa.util.callids import Callids
29 # we have specialized xmlrpclib.ServerProxy to remember the input url
30 # OTOH it's not clear if we're only dealing with XMLRPCServerProxy instances
31 def get_serverproxy_url (server):
35 sfa_logger().warning("GetVersion, falling back to xmlrpclib.ServerProxy internals")
36 return server._ServerProxy__host + server._ServerProxy__handler
39 # peers explicitly in aggregates.xml
40 peers =dict ([ (peername,get_serverproxy_url(v)) for (peername,v) in api.aggregates.iteritems()
41 if peername != api.hrn])
43 sm_version=version_core({'interface':'slicemgr',
44 'hrn' : xrn.get_hrn(),
45 'urn' : xrn.get_urn(),
48 # local aggregate if present needs to have localhost resolved
49 if api.hrn in api.aggregates:
50 local_am_url=get_serverproxy_url(api.aggregates[api.hrn])
51 sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname'])
54 def CreateSliver(api, xrn, creds, rspec, users, call_id):
56 if Callids().already_handled(call_id): return ""
58 hrn, type = urn_to_hrn(xrn)
60 # Validate the RSpec against PlanetLab's schema --disabled for now
61 # The schema used here needs to aggregate the PL and VINI schemas
62 # schema = "/var/www/html/schemas/pl.rng"
66 tree = etree.parse(StringIO(rspec))
67 except etree.XMLSyntaxError:
68 message = str(sys.exc_info()[1])
69 raise InvalidRSpec(message)
71 relaxng_doc = etree.parse(schema)
72 relaxng = etree.RelaxNG(relaxng_doc)
75 error = relaxng.error_log.last_error
76 message = "%s (line %s)" % (error.message, error.line)
77 raise InvalidRSpec(message)
80 valid_cred = api.auth.checkCredentials(creds, 'createsliver', hrn)[0]
81 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
83 # attempt to use delegated credential first
84 credential = api.getDelegatedCredential(creds)
86 credential = api.getCredential()
87 threads = ThreadManager()
88 for aggregate in api.aggregates:
89 # prevent infinite loop. Dont send request back to caller
90 # unless the caller is the aggregate's SM
91 if caller_hrn == aggregate and aggregate != api.hrn:
94 # Just send entire RSpec to each aggregate
95 server = api.aggregates[aggregate]
96 threads.run(server.CreateSliver, xrn, credential, rspec, users, call_id)
98 results = threads.get_results()
100 for result in results:
104 def RenewSliver(api, xrn, creds, expiration_time, call_id):
105 if Callids().already_handled(call_id): return True
107 (hrn, type) = urn_to_hrn(xrn)
108 # get the callers hrn
109 valid_cred = api.auth.checkCredentials(creds, 'renewsliver', hrn)[0]
110 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
112 # attempt to use delegated credential first
113 credential = api.getDelegatedCredential(creds)
115 credential = api.getCredential()
116 threads = ThreadManager()
117 for aggregate in api.aggregates:
118 # prevent infinite loop. Dont send request back to caller
119 # unless the caller is the aggregate's SM
120 if caller_hrn == aggregate and aggregate != api.hrn:
123 server = api.aggregates[aggregate]
124 threads.run(server.RenewSliver, xrn, [credential], expiration_time, call_id)
126 return reduce (lambda x,y: x and y, threads.get_results() , True)
128 def get_ticket(api, xrn, creds, rspec, users):
129 slice_hrn, type = urn_to_hrn(xrn)
130 # get the netspecs contained within the clients rspec
131 aggregate_rspecs = {}
132 tree= etree.parse(StringIO(rspec))
133 elements = tree.findall('./network')
134 for element in elements:
135 aggregate_hrn = element.values()[0]
136 aggregate_rspecs[aggregate_hrn] = rspec
138 # get the callers hrn
139 valid_cred = api.auth.checkCredentials(creds, 'getticket', slice_hrn)[0]
140 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
142 # attempt to use delegated credential first
143 credential = api.getDelegatedCredential(creds)
145 credential = api.getCredential()
146 threads = ThreadManager()
147 for (aggregate, aggregate_rspec) in aggregate_rspecs.iteritems():
148 # prevent infinite loop. Dont send request back to caller
149 # unless the caller is the aggregate's SM
150 if caller_hrn == aggregate and aggregate != api.hrn:
153 if aggregate in api.aggregates:
154 server = api.aggregates[aggregate]
156 net_urn = hrn_to_urn(aggregate, 'authority')
157 # we may have a peer that knows about this aggregate
158 for agg in api.aggregates:
159 target_aggs = api.aggregates[agg].get_aggregates(credential, net_urn)
160 if not target_aggs or not 'hrn' in target_aggs[0]:
162 # send the request to this address
163 url = target_aggs[0]['url']
164 server = xmlrpcprotocol.get_server(url, api.key_file, api.cert_file)
165 # aggregate found, no need to keep looping
169 threads.run(server.GetTicket, xrn, credential, aggregate_rspec, users)
171 results = threads.get_results()
173 # gather information from each ticket
178 for result in results:
179 agg_ticket = SfaTicket(string=result)
180 attrs = agg_ticket.get_attributes()
182 object_gid = agg_ticket.get_gid_object()
183 rspecs.append(agg_ticket.get_rspec())
184 initscripts.extend(attrs.get('initscripts', []))
185 slivers.extend(attrs.get('slivers', []))
188 attributes = {'initscripts': initscripts,
190 merged_rspec = merge_rspecs(rspecs)
192 # create a new ticket
193 ticket = SfaTicket(subject = slice_hrn)
194 ticket.set_gid_caller(api.auth.client_gid)
195 ticket.set_issuer(key=api.key, subject=api.hrn)
196 ticket.set_gid_object(object_gid)
197 ticket.set_pubkey(object_gid.get_pubkey())
198 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
199 ticket.set_attributes(attributes)
200 ticket.set_rspec(merged_rspec)
203 return ticket.save_to_string(save_parents=True)
206 def DeleteSliver(api, xrn, creds, call_id):
207 if Callids().already_handled(call_id): return ""
208 (hrn, type) = urn_to_hrn(xrn)
209 # get the callers hrn
210 valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0]
211 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
213 # attempt to use delegated credential first
214 credential = api.getDelegatedCredential(creds)
216 credential = api.getCredential()
217 threads = ThreadManager()
218 for aggregate in api.aggregates:
219 # prevent infinite loop. Dont send request back to caller
220 # unless the caller is the aggregate's SM
221 if caller_hrn == aggregate and aggregate != api.hrn:
223 server = api.aggregates[aggregate]
224 threads.run(server.DeleteSliver, xrn, credential, call_id)
225 threads.get_results()
228 def start_slice(api, xrn, creds):
229 hrn, type = urn_to_hrn(xrn)
231 # get the callers hrn
232 valid_cred = api.auth.checkCredentials(creds, 'startslice', hrn)[0]
233 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
235 # attempt to use delegated credential first
236 credential = api.getDelegatedCredential(creds)
238 credential = api.getCredential()
239 threads = ThreadManager()
240 for aggregate in api.aggregates:
241 # prevent infinite loop. Dont send request back to caller
242 # unless the caller is the aggregate's SM
243 if caller_hrn == aggregate and aggregate != api.hrn:
245 server = api.aggregates[aggregate]
246 threads.run(server.Start, xrn, credential)
247 threads.get_results()
250 def stop_slice(api, xrn, creds):
251 hrn, type = urn_to_hrn(xrn)
253 # get the callers hrn
254 valid_cred = api.auth.checkCredentials(creds, 'stopslice', hrn)[0]
255 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
257 # attempt to use delegated credential first
258 credential = api.getDelegatedCredential(creds)
260 credential = api.getCredential()
261 threads = ThreadManager()
262 for aggregate in api.aggregates:
263 # prevent infinite loop. Dont send request back to caller
264 # unless the caller is the aggregate's SM
265 if caller_hrn == aggregate and aggregate != api.hrn:
267 server = api.aggregates[aggregate]
268 threads.run(server.Stop, xrn, credential)
269 threads.get_results()
272 def reset_slice(api, xrn):
278 def shutdown(api, xrn, creds):
284 def status(api, xrn, creds):
290 # Thierry : caching at the slicemgr level makes sense to some extent
293 def ListSlices(api, creds, call_id):
295 if Callids().already_handled(call_id): return []
297 # look in cache first
298 if caching and api.cache:
299 slices = api.cache.get('slices')
303 # get the callers hrn
304 valid_cred = api.auth.checkCredentials(creds, 'listslices', None)[0]
305 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
307 # attempt to use delegated credential first
308 credential = api.getDelegatedCredential(creds)
310 credential = api.getCredential()
311 threads = ThreadManager()
312 # fetch from aggregates
313 for aggregate in api.aggregates:
314 # prevent infinite loop. Dont send request back to caller
315 # unless the caller is the aggregate's SM
316 if caller_hrn == aggregate and aggregate != api.hrn:
318 server = api.aggregates[aggregate]
319 threads.run(server.ListSlices, credential, call_id)
322 results = threads.get_results()
324 for result in results:
325 slices.extend(result)
328 if caching and api.cache:
329 api.cache.add('slices', slices)
334 def ListResources(api, creds, options, call_id):
336 if Callids().already_handled(call_id): return ""
338 # get slice's hrn from options
339 xrn = options.get('geni_slice_urn', '')
340 (hrn, type) = urn_to_hrn(xrn)
342 # get hrn of the original caller
343 origin_hrn = options.get('origin_hrn', None)
345 if isinstance(creds, list):
346 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
348 origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
350 # look in cache first
351 if caching and api.cache and not xrn:
352 rspec = api.cache.get('nodes')
356 # get the callers hrn
357 valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
358 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
360 # attempt to use delegated credential first
361 credential = api.getDelegatedCredential(creds)
363 credential = api.getCredential()
364 threads = ThreadManager()
365 for aggregate in api.aggregates:
366 # prevent infinite loop. Dont send request back to caller
367 # unless the caller is the aggregate's SM
368 if caller_hrn == aggregate and aggregate != api.hrn:
370 # get the rspec from the aggregate
371 server = api.aggregates[aggregate]
372 my_opts = copy(options)
373 my_opts['geni_compressed'] = False
374 threads.run(server.ListResources, credential, my_opts, call_id)
375 #threads.run(server.get_resources, cred, xrn, origin_hrn)
377 results = threads.get_results()
379 for result in results:
383 if caching and api.cache and not xrn:
384 api.cache.add('nodes', rspec)
388 # first draft at a merging SliverStatus
389 def SliverStatus(api, slice_xrn, creds, call_id):
390 if Callids().already_handled(call_id): return {}
391 # attempt to use delegated credential first
392 credential = api.getDelegatedCredential(creds)
394 credential = api.getCredential()
395 threads = ThreadManager()
396 for aggregate in api.aggregates:
397 server = api.aggregates[aggregate]
398 threads.run (server.SliverStatus, slice_xrn, credential, call_id)
399 results = threads.get_results()
401 # get rid of any void result - e.g. when call_id was hit where by convention we return {}
402 results = [ result for result in results if result and result['geni_resources']]
404 # do not try to combine if there's no result
405 if not results : return {}
407 # otherwise let's merge stuff
410 # mmh, it is expected that all results carry the same urn
411 overall['geni_urn'] = results[0]['geni_urn']
413 # consolidate geni_status - simple model using max on a total order
414 states = [ 'ready', 'configuring', 'failed', 'unknown' ]
416 shash = dict ( zip ( states, range(len(states)) ) )
417 def combine_status (x,y):
418 return shash [ max (shash(x),shash(y)) ]
419 overall['geni_status'] = reduce (combine_status, [ result['geni_status'] for result in results], 'ready' )
421 # {'ready':0,'configuring':1,'failed':2,'unknown':3}
422 # append all geni_resources
423 overall['geni_resources'] = \
424 reduce (lambda x,y: x+y, [ result['geni_resources'] for result in results] , [])
430 r.parseFile(sys.argv[1])
432 CreateSliver(None,'plc.princeton.tmacktestslice',rspec,'create-slice-tmacktestslice')
434 if __name__ == "__main__":