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.rspecs.rspec_version import RSpecVersion
28 from sfa.util.callids import Callids
30 # we have specialized xmlrpclib.ServerProxy to remember the input url
31 # OTOH it's not clear if we're only dealing with XMLRPCServerProxy instances
32 def get_serverproxy_url (server):
36 sfa_logger().warning("GetVersion, falling back to xmlrpclib.ServerProxy internals")
37 return server._ServerProxy__host + server._ServerProxy__handler
40 # peers explicitly in aggregates.xml
41 peers =dict ([ (peername,get_serverproxy_url(v)) for (peername,v) in api.aggregates.iteritems()
42 if peername != api.hrn])
44 sm_version=version_core({'interface':'slicemgr',
45 'hrn' : xrn.get_hrn(),
46 'urn' : xrn.get_urn(),
49 # local aggregate if present needs to have localhost resolved
50 if api.hrn in api.aggregates:
51 local_am_url=get_serverproxy_url(api.aggregates[api.hrn])
52 sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname'])
55 def CreateSliver(api, xrn, creds, rspec, users, call_id):
57 if Callids().already_handled(call_id): return ""
59 hrn, type = urn_to_hrn(xrn)
61 # Validate the RSpec against PlanetLab's schema --disabled for now
62 # The schema used here needs to aggregate the PL and VINI schemas
63 # schema = "/var/www/html/schemas/pl.rng"
67 tree = etree.parse(StringIO(rspec))
68 except etree.XMLSyntaxError:
69 message = str(sys.exc_info()[1])
70 raise InvalidRSpec(message)
72 relaxng_doc = etree.parse(schema)
73 relaxng = etree.RelaxNG(relaxng_doc)
76 error = relaxng.error_log.last_error
77 message = "%s (line %s)" % (error.message, error.line)
78 raise InvalidRSpec(message)
81 valid_cred = api.auth.checkCredentials(creds, 'createsliver', hrn)[0]
82 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
84 # attempt to use delegated credential first
85 credential = api.getDelegatedCredential(creds)
87 credential = api.getCredential()
88 threads = ThreadManager()
89 for aggregate in api.aggregates:
90 # prevent infinite loop. Dont send request back to caller
91 # unless the caller is the aggregate's SM
92 if caller_hrn == aggregate and aggregate != api.hrn:
95 # Just send entire RSpec to each aggregate
96 server = api.aggregates[aggregate]
97 threads.run(server.CreateSliver, xrn, credential, rspec, users, call_id)
99 results = threads.get_results()
101 for result in 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 the rspec's return format from options
344 rspec_version = RSpecVersion(options.get('rspec_version', 'SFA 1'))
345 version_string = "rspec_%s_%s" % (rspec_version.format, rspec_version.version)
347 # get hrn of the original caller
348 origin_hrn = options.get('origin_hrn', None)
350 if isinstance(creds, list):
351 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
353 origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
355 # look in cache first
356 if caching and api.cache and not xrn:
357 rspec = api.cache.get(version_string)
361 # get the callers hrn
362 valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
363 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
365 # attempt to use delegated credential first
366 credential = api.getDelegatedCredential(creds)
368 credential = api.getCredential()
369 threads = ThreadManager()
370 for aggregate in api.aggregates:
371 # prevent infinite loop. Dont send request back to caller
372 # unless the caller is the aggregate's SM
373 if caller_hrn == aggregate and aggregate != api.hrn:
375 # get the rspec from the aggregate
376 server = api.aggregates[aggregate]
377 my_opts = copy(options)
378 my_opts['geni_compressed'] = False
379 threads.run(server.ListResources, credential, my_opts, call_id)
380 #threads.run(server.get_resources, cred, xrn, origin_hrn)
382 results = threads.get_results()
384 for result in results:
388 if caching and api.cache and not xrn:
389 api.cache.add(version_string, rspec)
393 # first draft at a merging SliverStatus
394 def SliverStatus(api, slice_xrn, creds, call_id):
395 if Callids().already_handled(call_id): return {}
396 # attempt to use delegated credential first
397 credential = api.getDelegatedCredential(creds)
399 credential = api.getCredential()
400 threads = ThreadManager()
401 for aggregate in api.aggregates:
402 server = api.aggregates[aggregate]
403 threads.run (server.SliverStatus, slice_xrn, credential, call_id)
404 results = threads.get_results()
406 # get rid of any void result - e.g. when call_id was hit where by convention we return {}
407 results = [ result for result in results if result and result['geni_resources']]
409 # do not try to combine if there's no result
410 if not results : return {}
412 # otherwise let's merge stuff
415 # mmh, it is expected that all results carry the same urn
416 overall['geni_urn'] = results[0]['geni_urn']
418 # consolidate geni_status - simple model using max on a total order
419 states = [ 'ready', 'configuring', 'failed', 'unknown' ]
421 shash = dict ( zip ( states, range(len(states)) ) )
422 def combine_status (x,y):
423 return shash [ max (shash(x),shash(y)) ]
424 overall['geni_status'] = reduce (combine_status, [ result['geni_status'] for result in results], 'ready' )
426 # {'ready':0,'configuring':1,'failed':2,'unknown':3}
427 # append all geni_resources
428 overall['geni_resources'] = \
429 reduce (lambda x,y: x+y, [ result['geni_resources'] for result in results] , [])
435 r.parseFile(sys.argv[1])
437 CreateSliver(None,'plc.princeton.tmacktestslice',rspec,'create-slice-tmacktestslice')
439 if __name__ == "__main__":