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 # we have specialized xmlrpclib.ServerProxy to remember the input url
29 # OTOH it's not clear if we're only dealing with XMLRPCServerProxy instances
30 def get_serverproxy_url (server):
34 sfa_logger().warning("GetVersion, falling back to xmlrpclib.ServerProxy internals")
35 return server._ServerProxy__host + server._ServerProxy__handler
38 # peers explicitly in aggregates.xml
39 peers =dict ([ (peername,get_serverproxy_url(v)) for (peername,v) in api.aggregates.iteritems()
40 if peername != api.hrn])
42 sm_version=version_core({'interface':'slicemgr',
43 'hrn' : xrn.get_hrn(),
44 'urn' : xrn.get_urn(),
47 # local aggregate if present needs to have localhost resolved
48 if api.hrn in api.aggregates:
49 local_am_url=get_serverproxy_url(api.aggregates[api.hrn])
50 sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname'])
53 def CreateSliver(api, xrn, creds, rspec, users, call_id):
55 if Callids().already_handled(call_id): return ""
57 hrn, type = urn_to_hrn(xrn)
59 # Validate the RSpec against PlanetLab's schema --disabled for now
60 # The schema used here needs to aggregate the PL and VINI schemas
61 # schema = "/var/www/html/schemas/pl.rng"
65 tree = etree.parse(StringIO(rspec))
66 except etree.XMLSyntaxError:
67 message = str(sys.exc_info()[1])
68 raise InvalidRSpec(message)
70 relaxng_doc = etree.parse(schema)
71 relaxng = etree.RelaxNG(relaxng_doc)
74 error = relaxng.error_log.last_error
75 message = "%s (line %s)" % (error.message, error.line)
76 raise InvalidRSpec(message)
79 valid_cred = api.auth.checkCredentials(creds, 'createsliver', hrn)[0]
80 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
82 # attempt to use delegated credential first
83 credential = api.getDelegatedCredential(creds)
85 credential = api.getCredential()
86 threads = ThreadManager()
87 for aggregate in api.aggregates:
88 # prevent infinite loop. Dont send request back to caller
89 # unless the caller is the aggregate's SM
90 if caller_hrn == aggregate and aggregate != api.hrn:
93 # Just send entire RSpec to each aggregate
94 server = api.aggregates[aggregate]
95 threads.run(server.CreateSliver, xrn, credential, rspec, users, call_id)
97 results = threads.get_results()
98 merged_rspec = merge_rspecs(results)
101 def RenewSliver(api, xrn, creds, expiration_time, call_id):
102 if Callids().already_handled(call_id): return True
104 (hrn, type) = urn_to_hrn(xrn)
105 # get the callers hrn
106 valid_cred = api.auth.checkCredentials(creds, 'renewsliver', hrn)[0]
107 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
109 # attempt to use delegated credential first
110 credential = api.getDelegatedCredential(creds)
112 credential = api.getCredential()
113 threads = ThreadManager()
114 for aggregate in api.aggregates:
115 # prevent infinite loop. Dont send request back to caller
116 # unless the caller is the aggregate's SM
117 if caller_hrn == aggregate and aggregate != api.hrn:
120 server = api.aggregates[aggregate]
121 threads.run(server.RenewSliver, xrn, [credential], expiration_time, call_id)
123 return reduce (lambda x,y: x and y, threads.get_results() , True)
125 def get_ticket(api, xrn, creds, rspec, users):
126 slice_hrn, type = urn_to_hrn(xrn)
127 # get the netspecs contained within the clients rspec
128 aggregate_rspecs = {}
129 tree= etree.parse(StringIO(rspec))
130 elements = tree.findall('./network')
131 for element in elements:
132 aggregate_hrn = element.values()[0]
133 aggregate_rspecs[aggregate_hrn] = rspec
135 # get the callers hrn
136 valid_cred = api.auth.checkCredentials(creds, 'getticket', slice_hrn)[0]
137 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
139 # attempt to use delegated credential first
140 credential = api.getDelegatedCredential(creds)
142 credential = api.getCredential()
143 threads = ThreadManager()
144 for (aggregate, aggregate_rspec) in aggregate_rspecs.iteritems():
145 # prevent infinite loop. Dont send request back to caller
146 # unless the caller is the aggregate's SM
147 if caller_hrn == aggregate and aggregate != api.hrn:
150 if aggregate in api.aggregates:
151 server = api.aggregates[aggregate]
153 net_urn = hrn_to_urn(aggregate, 'authority')
154 # we may have a peer that knows about this aggregate
155 for agg in api.aggregates:
156 target_aggs = api.aggregates[agg].get_aggregates(credential, net_urn)
157 if not target_aggs or not 'hrn' in target_aggs[0]:
159 # send the request to this address
160 url = target_aggs[0]['url']
161 server = xmlrpcprotocol.get_server(url, api.key_file, api.cert_file)
162 # aggregate found, no need to keep looping
166 threads.run(server.GetTicket, xrn, credential, aggregate_rspec, users)
168 results = threads.get_results()
170 # gather information from each ticket
175 for result in results:
176 agg_ticket = SfaTicket(string=result)
177 attrs = agg_ticket.get_attributes()
179 object_gid = agg_ticket.get_gid_object()
180 rspecs.append(agg_ticket.get_rspec())
181 initscripts.extend(attrs.get('initscripts', []))
182 slivers.extend(attrs.get('slivers', []))
185 attributes = {'initscripts': initscripts,
187 merged_rspec = merge_rspecs(rspecs)
189 # create a new ticket
190 ticket = SfaTicket(subject = slice_hrn)
191 ticket.set_gid_caller(api.auth.client_gid)
192 ticket.set_issuer(key=api.key, subject=api.hrn)
193 ticket.set_gid_object(object_gid)
194 ticket.set_pubkey(object_gid.get_pubkey())
195 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
196 ticket.set_attributes(attributes)
197 ticket.set_rspec(merged_rspec)
200 return ticket.save_to_string(save_parents=True)
203 def DeleteSliver(api, xrn, creds, call_id):
204 if Callids().already_handled(call_id): return ""
205 (hrn, type) = urn_to_hrn(xrn)
206 # get the callers hrn
207 valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0]
208 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
210 # attempt to use delegated credential first
211 credential = api.getDelegatedCredential(creds)
213 credential = api.getCredential()
214 threads = ThreadManager()
215 for aggregate in api.aggregates:
216 # prevent infinite loop. Dont send request back to caller
217 # unless the caller is the aggregate's SM
218 if caller_hrn == aggregate and aggregate != api.hrn:
220 server = api.aggregates[aggregate]
221 threads.run(server.DeleteSliver, xrn, credential, call_id)
222 threads.get_results()
225 def start_slice(api, xrn, creds):
226 hrn, type = urn_to_hrn(xrn)
228 # get the callers hrn
229 valid_cred = api.auth.checkCredentials(creds, 'startslice', hrn)[0]
230 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
232 # attempt to use delegated credential first
233 credential = api.getDelegatedCredential(creds)
235 credential = api.getCredential()
236 threads = ThreadManager()
237 for aggregate in api.aggregates:
238 # prevent infinite loop. Dont send request back to caller
239 # unless the caller is the aggregate's SM
240 if caller_hrn == aggregate and aggregate != api.hrn:
242 server = api.aggregates[aggregate]
243 threads.run(server.Start, xrn, credential)
244 threads.get_results()
247 def stop_slice(api, xrn, creds):
248 hrn, type = urn_to_hrn(xrn)
250 # get the callers hrn
251 valid_cred = api.auth.checkCredentials(creds, 'stopslice', hrn)[0]
252 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
254 # attempt to use delegated credential first
255 credential = api.getDelegatedCredential(creds)
257 credential = api.getCredential()
258 threads = ThreadManager()
259 for aggregate in api.aggregates:
260 # prevent infinite loop. Dont send request back to caller
261 # unless the caller is the aggregate's SM
262 if caller_hrn == aggregate and aggregate != api.hrn:
264 server = api.aggregates[aggregate]
265 threads.run(server.Stop, xrn, credential)
266 threads.get_results()
269 def reset_slice(api, xrn):
275 def shutdown(api, xrn, creds):
281 def status(api, xrn, creds):
287 # Thierry : caching at the slicemgr level makes sense to some extent
290 def ListSlices(api, creds, call_id):
292 if Callids().already_handled(call_id): return []
294 # look in cache first
295 if caching and api.cache:
296 slices = api.cache.get('slices')
300 # get the callers hrn
301 valid_cred = api.auth.checkCredentials(creds, 'listslices', None)[0]
302 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
304 # attempt to use delegated credential first
305 credential = api.getDelegatedCredential(creds)
307 credential = api.getCredential()
308 threads = ThreadManager()
309 # fetch from aggregates
310 for aggregate in api.aggregates:
311 # prevent infinite loop. Dont send request back to caller
312 # unless the caller is the aggregate's SM
313 if caller_hrn == aggregate and aggregate != api.hrn:
315 server = api.aggregates[aggregate]
316 threads.run(server.ListSlices, credential, call_id)
319 results = threads.get_results()
321 for result in results:
322 slices.extend(result)
325 if caching and api.cache:
326 api.cache.add('slices', slices)
331 def ListResources(api, creds, options, call_id):
333 if Callids().already_handled(call_id): return ""
335 # get slice's hrn from options
336 xrn = options.get('geni_slice_urn', '')
337 (hrn, type) = urn_to_hrn(xrn)
339 # get hrn of the original caller
340 origin_hrn = options.get('origin_hrn', None)
342 if isinstance(creds, list):
343 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
345 origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
347 # look in cache first
348 if caching and api.cache and not xrn:
349 rspec = api.cache.get('nodes')
353 # get the callers hrn
354 valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
355 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
357 # attempt to use delegated credential first
358 credential = api.getDelegatedCredential(creds)
360 credential = api.getCredential()
361 threads = ThreadManager()
362 for aggregate in api.aggregates:
363 # prevent infinite loop. Dont send request back to caller
364 # unless the caller is the aggregate's SM
365 if caller_hrn == aggregate and aggregate != api.hrn:
367 # get the rspec from the aggregate
368 server = api.aggregates[aggregate]
369 my_opts = copy(options)
370 my_opts['geni_compressed'] = False
371 threads.run(server.ListResources, credential, my_opts, call_id)
372 #threads.run(server.get_resources, cred, xrn, origin_hrn)
374 results = threads.get_results()
375 merged_rspec = merge_rspecs(results)
378 if caching and api.cache and not xrn:
379 api.cache.add('nodes', merged_rspec)
383 # first draft at a merging SliverStatus
384 def SliverStatus(api, slice_xrn, creds, call_id):
385 if Callids().already_handled(call_id): return {}
386 # attempt to use delegated credential first
387 credential = api.getDelegatedCredential(creds)
389 credential = api.getCredential()
390 threads = ThreadManager()
391 for aggregate in api.aggregates:
392 server = api.aggregates[aggregate]
393 threads.run (server.SliverStatus, slice_xrn, credential, call_id)
394 results = threads.get_results()
396 # get rid of any void result - e.g. when call_id was hit where by convention we return {}
397 results = [ result for result in results if result and result['geni_resources']]
399 # do not try to combine if there's no result
400 if not results : return {}
402 # otherwise let's merge stuff
405 # mmh, it is expected that all results carry the same urn
406 overall['geni_urn'] = results[0]['geni_urn']
408 # consolidate geni_status - simple model using max on a total order
409 states = [ 'ready', 'configuring', 'failed', 'unknown' ]
411 shash = dict ( zip ( states, range(len(states)) ) )
412 def combine_status (x,y):
413 return shash [ max (shash(x),shash(y)) ]
414 overall['geni_status'] = reduce (combine_status, [ result['geni_status'] for result in results], 'ready' )
416 # {'ready':0,'configuring':1,'failed':2,'unknown':3}
417 # append all geni_resources
418 overall['geni_resources'] = \
419 reduce (lambda x,y: x+y, [ result['geni_resources'] for result in results] , [])
425 r.parseFile(sys.argv[1])
427 CreateSliver(None,'plc.princeton.tmacktestslice',rspec,'create-slice-tmacktestslice')
429 if __name__ == "__main__":