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
27 # XX FIX ME: should merge result from multiple aggregates instead of
28 # calling aggregate implementation
29 from sfa.managers.aggregate_manager_pl import slice_status
32 peers =dict ([ (peername,v._ServerProxy__host) for (peername,v) in api.aggregates.items()
33 if peername != api.hrn])
35 return version_core({'interface':'slicemgr',
36 'hrn' : xrn.get_hrn(),
37 'urn' : xrn.get_urn(),
41 def create_slice(api, xrn, creds, rspec, users):
42 hrn, type = urn_to_hrn(xrn)
44 # Validate the RSpec against PlanetLab's schema --disabled for now
45 # The schema used here needs to aggregate the PL and VINI schemas
46 # schema = "/var/www/html/schemas/pl.rng"
50 tree = etree.parse(StringIO(rspec))
51 except etree.XMLSyntaxError:
52 message = str(sys.exc_info()[1])
53 raise InvalidRSpec(message)
55 relaxng_doc = etree.parse(schema)
56 relaxng = etree.RelaxNG(relaxng_doc)
59 error = relaxng.error_log.last_error
60 message = "%s (line %s)" % (error.message, error.line)
61 raise InvalidRSpec(message)
64 valid_cred = api.auth.checkCredentials(creds, 'createsliver', hrn)[0]
65 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
67 # attempt to use delegated credential first
68 credential = api.getDelegatedCredential(creds)
70 credential = api.getCredential()
71 threads = ThreadManager()
72 for aggregate in api.aggregates:
73 # prevent infinite loop. Dont send request back to caller
74 # unless the caller is the aggregate's SM
75 if caller_hrn == aggregate and aggregate != api.hrn:
78 # Just send entire RSpec to each aggregate
79 server = api.aggregates[aggregate]
80 threads.run(server.CreateSliver, xrn, credential, rspec, users)
82 results = threads.get_results()
83 merged_rspec = merge_rspecs(results)
86 def renew_slice(api, xrn, creds, expiration_time):
87 hrn, type = urn_to_hrn(xrn)
90 valid_cred = api.auth.checkCredentials(creds, 'renewsliver', hrn)[0]
91 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
93 # attempt to use delegated credential first
94 credential = api.getDelegatedCredential(creds)
96 credential = api.getCredential()
97 threads = ThreadManager()
98 for aggregate in api.aggregates:
99 # prevent infinite loop. Dont send request back to caller
100 # unless the caller is the aggregate's SM
101 if caller_hrn == aggregate and aggregate != api.hrn:
104 server = api.aggregates[aggregate]
105 threads.run(server.RenewSliver, xrn, [credential], expiration_time)
106 threads.get_results()
109 def get_ticket(api, xrn, creds, rspec, users):
110 slice_hrn, type = urn_to_hrn(xrn)
111 # get the netspecs contained within the clients rspec
112 aggregate_rspecs = {}
113 tree= etree.parse(StringIO(rspec))
114 elements = tree.findall('./network')
115 for element in elements:
116 aggregate_hrn = element.values()[0]
117 aggregate_rspecs[aggregate_hrn] = rspec
119 # get the callers hrn
120 valid_cred = api.auth.checkCredentials(creds, 'getticket', slice_hrn)[0]
121 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
123 # attempt to use delegated credential first
124 credential = api.getDelegatedCredential(creds)
126 credential = api.getCredential()
127 threads = ThreadManager()
128 for aggregate, aggregate_rspec in aggregate_rspecs.items():
129 # prevent infinite loop. Dont send request back to caller
130 # unless the caller is the aggregate's SM
131 if caller_hrn == aggregate and aggregate != api.hrn:
134 if aggregate in api.aggregates:
135 server = api.aggregates[aggregate]
137 net_urn = hrn_to_urn(aggregate, 'authority')
138 # we may have a peer that knows about this aggregate
139 for agg in api.aggregates:
140 target_aggs = api.aggregates[agg].get_aggregates(credential, net_urn)
141 if not target_aggs or not 'hrn' in target_aggs[0]:
143 # send the request to this address
144 url = target_aggs[0]['url']
145 server = xmlrpcprotocol.get_server(url, api.key_file, api.cert_file)
146 # aggregate found, no need to keep looping
150 threads.run(server.GetTicket, xrn, credential, aggregate_rspec, users)
152 results = threads.get_results()
154 # gather information from each ticket
159 for result in results:
160 agg_ticket = SfaTicket(string=result)
161 attrs = agg_ticket.get_attributes()
163 object_gid = agg_ticket.get_gid_object()
164 rspecs.append(agg_ticket.get_rspec())
165 initscripts.extend(attrs.get('initscripts', []))
166 slivers.extend(attrs.get('slivers', []))
169 attributes = {'initscripts': initscripts,
171 merged_rspec = merge_rspecs(rspecs)
173 # create a new ticket
174 ticket = SfaTicket(subject = slice_hrn)
175 ticket.set_gid_caller(api.auth.client_gid)
176 ticket.set_issuer(key=api.key, subject=api.hrn)
177 ticket.set_gid_object(object_gid)
178 ticket.set_pubkey(object_gid.get_pubkey())
179 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
180 ticket.set_attributes(attributes)
181 ticket.set_rspec(merged_rspec)
184 return ticket.save_to_string(save_parents=True)
187 def delete_slice(api, xrn, creds):
188 hrn, type = urn_to_hrn(xrn)
190 # get the callers hrn
191 valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0]
192 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
194 # attempt to use delegated credential first
195 credential = api.getDelegatedCredential(creds)
197 credential = api.getCredential()
198 threads = ThreadManager()
199 for aggregate in api.aggregates:
200 # prevent infinite loop. Dont send request back to caller
201 # unless the caller is the aggregate's SM
202 if caller_hrn == aggregate and aggregate != api.hrn:
204 server = api.aggregates[aggregate]
205 threads.run(server.DeleteSliver, xrn, credential)
206 threads.get_results()
209 def start_slice(api, xrn, creds):
210 hrn, type = urn_to_hrn(xrn)
212 # get the callers hrn
213 valid_cred = api.auth.checkCredentials(creds, 'startslice', hrn)[0]
214 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
216 # attempt to use delegated credential first
217 credential = api.getDelegatedCredential(creds)
219 credential = api.getCredential()
220 threads = ThreadManager()
221 for aggregate in api.aggregates:
222 # prevent infinite loop. Dont send request back to caller
223 # unless the caller is the aggregate's SM
224 if caller_hrn == aggregate and aggregate != api.hrn:
226 server = api.aggregates[aggregate]
227 threads.run(server.Start, xrn, credential)
228 threads.get_results()
231 def stop_slice(api, xrn, creds):
232 hrn, type = urn_to_hrn(xrn)
234 # get the callers hrn
235 valid_cred = api.auth.checkCredentials(creds, 'stopslice', hrn)[0]
236 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
238 # attempt to use delegated credential first
239 credential = api.getDelegatedCredential(creds)
241 credential = api.getCredential()
242 threads = ThreadManager()
243 for aggregate in api.aggregates:
244 # prevent infinite loop. Dont send request back to caller
245 # unless the caller is the aggregate's SM
246 if caller_hrn == aggregate and aggregate != api.hrn:
248 server = api.aggregates[aggregate]
249 threads.run(server.Stop, xrn, credential)
250 threads.get_results()
253 def reset_slice(api, xrn):
259 def shutdown(api, xrn, creds):
265 def status(api, xrn, creds):
271 def get_slices(api, creds):
273 # look in cache first
275 slices = api.cache.get('slices')
279 # get the callers hrn
280 valid_cred = api.auth.checkCredentials(creds, 'listslices', None)[0]
281 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
283 # attempt to use delegated credential first
284 credential = api.getDelegatedCredential(creds)
286 credential = api.getCredential()
287 threads = ThreadManager()
288 # fetch from aggregates
289 for aggregate in api.aggregates:
290 # prevent infinite loop. Dont send request back to caller
291 # unless the caller is the aggregate's SM
292 if caller_hrn == aggregate and aggregate != api.hrn:
294 server = api.aggregates[aggregate]
295 threads.run(server.ListSlices, credential)
298 results = threads.get_results()
300 for result in results:
301 slices.extend(result)
305 api.cache.add('slices', slices)
309 def get_rspec(api, creds, options):
311 # get slice's hrn from options
312 xrn = options.get('geni_slice_urn', '')
313 hrn, type = urn_to_hrn(xrn)
315 # get hrn of the original caller
316 origin_hrn = options.get('origin_hrn', None)
318 if isinstance(creds, list):
319 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
321 origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
323 # look in cache first
324 if api.cache and not xrn:
325 rspec = api.cache.get('nodes')
329 hrn, type = urn_to_hrn(xrn)
331 # get the callers hrn
332 valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
333 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
335 # attempt to use delegated credential first
336 credential = api.getDelegatedCredential(creds)
338 credential = api.getCredential()
339 threads = ThreadManager()
340 for aggregate in api.aggregates:
341 # prevent infinite loop. Dont send request back to caller
342 # unless the caller is the aggregate's SM
343 if caller_hrn == aggregate and aggregate != api.hrn:
345 # get the rspec from the aggregate
346 server = api.aggregates[aggregate]
347 my_opts = copy(options)
348 my_opts['geni_compressed'] = False
349 threads.run(server.ListResources, credential, my_opts)
350 #threads.run(server.get_resources, cred, xrn, origin_hrn)
352 results = threads.get_results()
353 merged_rspec = merge_rspecs(results)
356 if api.cache and not xrn:
357 api.cache.add('nodes', merged_rspec)
363 r.parseFile(sys.argv[1])
365 create_slice(None,'plc.princeton.tmacktestslice',rspec)
367 if __name__ == "__main__":