6 from types import StringTypes
8 from sfa.util.faults import *
9 from sfa.util.xrn import get_authority, hrn_to_urn, urn_to_hrn, Xrn
10 from sfa.util.plxrn import slicename_to_hrn, hrn_to_pl_slicename, hostname_to_urn
11 from sfa.util.rspec import *
12 from sfa.util.specdict import *
13 from sfa.util.record import SfaRecord
14 from sfa.util.policy import Policy
15 from sfa.util.record import *
16 from sfa.util.sfaticket import SfaTicket
17 from sfa.plc.slices import Slices
18 from sfa.trust.credential import Credential
19 import sfa.plc.peers as peers
20 from sfa.plc.network import *
21 from sfa.plc.api import SfaAPI
22 from sfa.plc.slices import *
23 from sfa.util.version import version_core
24 from sfa.util.sfatime import utcparse
25 from sfa.util.callids import Callids
29 return version_core({'interface':'aggregate',
34 def __get_registry_objects(slice_xrn, creds, users):
38 hrn, type = urn_to_hrn(slice_xrn)
40 hrn_auth = get_authority(hrn)
42 # Build up objects that an SFA registry would return if SFA
43 # could contact the slice's registry directly
47 # dont allow special characters in the site login base
48 #only_alphanumeric = re.compile('[^a-zA-Z0-9]+')
49 #login_base = only_alphanumeric.sub('', hrn_auth[:20]).lower()
50 slicename = hrn_to_pl_slicename(hrn)
51 login_base = slicename.split('_')[0]
55 site['name'] = 'geni.%s' % login_base
56 site['enabled'] = True
57 site['max_slices'] = 100
60 # Is it okay if this login base is the same as one already at this myplc site?
61 # Do we need uniqueness? Should use hrn_auth instead of just the leaf perhaps?
62 site['login_base'] = login_base
63 site['abbreviated_name'] = login_base
64 site['max_slivers'] = 1000
65 reg_objects['site'] = site
69 extime = Credential(string=creds[0]).get_expiration()
70 # If the expiration time is > 60 days from now, set the expiration time to 60 days from now
71 if extime > datetime.datetime.utcnow() + datetime.timedelta(days=60):
72 extime = datetime.datetime.utcnow() + datetime.timedelta(days=60)
73 slice['expires'] = int(time.mktime(extime.timetuple()))
75 slice['name'] = hrn_to_pl_slicename(hrn)
77 slice['description'] = hrn
79 reg_objects['slice_record'] = slice
81 reg_objects['users'] = {}
84 hrn, _ = urn_to_hrn(user['urn'])
85 user['email'] = hrn_to_pl_slicename(hrn) + "@geni.net"
86 user['first_name'] = hrn
87 user['last_name'] = hrn
88 reg_objects['users'][user['email']] = user
92 def __get_hostnames(nodes):
95 hostnames.append(node.hostname)
98 def slice_status(api, slice_xrn, creds):
99 hrn, type = urn_to_hrn(slice_xrn)
100 # find out where this slice is currently running
102 slicename = hrn_to_pl_slicename(hrn)
104 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids','person_ids','name','expires'])
106 raise Exception("Slice %s not found (used %s as slicename internally)" % slice_xrn, slicename)
109 nodes = api.plshell.GetNodes(api.plauth, slice['node_ids'],
110 ['hostname', 'site_id', 'boot_state', 'last_contact'])
111 site_ids = [node['site_id'] for node in nodes]
112 sites = api.plshell.GetSites(api.plauth, site_ids, ['site_id', 'login_base'])
115 sites_dict[site['site_id']] = site['login_base']
118 #api.logger.info(slice_xrn)
119 #api.logger.info(slice)
120 #api.logger.info(nodes)
124 top_level_status = 'unknown'
126 top_level_status = 'ready'
127 result['geni_urn'] = Xrn(slice_xrn, 'slice').get_urn()
128 result['pl_login'] = slice['name']
129 result['pl_expires'] = datetime.datetime.fromtimestamp(slice['expires']).ctime()
134 res['pl_hostname'] = node['hostname']
135 res['pl_boot_state'] = node['boot_state']
136 res['pl_last_contact'] = node['last_contact']
137 if not node['last_contact'] is None:
138 res['pl_last_contact'] = datetime.datetime.fromtimestamp(node['last_contact']).ctime()
139 res['geni_urn'] = hostname_to_urn(api.hrn, sites_dict[node['site_id']], node['hostname'])
140 if node['boot_state'] == 'boot':
141 res['geni_status'] = 'ready'
143 res['geni_status'] = 'failed'
144 top_level_staus = 'failed'
146 res['geni_error'] = ''
148 resources.append(res)
150 result['geni_status'] = top_level_status
151 result['geni_resources'] = resources
153 #api.logger.info(result)
157 def create_slice(api, slice_xrn, creds, rspec, users):
159 Create the sliver[s] (slice) at this aggregate.
160 Verify HRN and initialize the slice record in PLC if necessary.
163 reg_objects = __get_registry_objects(slice_xrn, creds, users)
165 hrn, type = urn_to_hrn(slice_xrn)
168 peer = slices.get_peer(hrn)
169 sfa_peer = slices.get_sfa_peer(hrn)
170 registry = api.registries[api.hrn]
171 credential = api.getCredential()
172 site_id, remote_site_id = slices.verify_site(registry, credential, hrn,
173 peer, sfa_peer, reg_objects)
175 slice_record = slices.verify_slice(registry, credential, hrn, site_id,
176 remote_site_id, peer, sfa_peer, reg_objects)
178 network = Network(api)
180 slice = network.get_slice(api, hrn)
181 slice.peer_id = slice_record['peer_slice_id']
182 current = __get_hostnames(slice.get_nodes())
184 network.addRSpec(rspec, api.config.SFA_AGGREGATE_RSPEC_SCHEMA)
185 request = __get_hostnames(network.nodesWithSlivers())
187 # remove nodes not in rspec
188 deleted_nodes = list(set(current).difference(request))
190 # add nodes from rspec
191 added_nodes = list(set(request).difference(current))
195 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
197 api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes)
198 api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
200 network.updateSliceTags()
204 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer,
207 # print network.toxml()
212 def renew_slice(api, xrn, creds, expiration_time):
213 hrn, type = urn_to_hrn(xrn)
214 slicename = hrn_to_pl_slicename(hrn)
215 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
217 raise RecordNotFound(hrn)
219 requested_time = utcparse(expiration_time)
220 record = {'expires': int(time.mktime(requested_time.timetuple()))}
221 api.plshell.UpdateSlice(api.plauth, slice['slice_id'], record)
224 def start_slice(api, xrn, creds):
225 hrn, type = urn_to_hrn(xrn)
226 slicename = hrn_to_pl_slicename(hrn)
227 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
229 raise RecordNotFound(hrn)
230 slice_id = slices[0]['slice_id']
231 slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'}, ['slice_tag_id'])
232 # just remove the tag if it exists
234 api.plshell.DeleteSliceTag(api.plauth, slice_tags[0]['slice_tag_id'])
238 def stop_slice(api, xrn, creds):
239 hrn, type = urn_to_hrn(xrn)
240 slicename = hrn_to_pl_slicename(hrn)
241 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
243 raise RecordNotFound(hrn)
244 slice_id = slices[0]['slice_id']
245 slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'})
247 api.plshell.AddSliceTag(api.plauth, slice_id, 'enabled', '0')
248 elif slice_tags[0]['value'] != "0":
249 tag_id = attributes[0]['slice_tag_id']
250 api.plshell.UpdateSliceTag(api.plauth, tag_id, '0')
253 def reset_slice(api, xrn):
254 # XX not implemented at this interface
257 def delete_slice(api, xrn, creds):
258 hrn, type = urn_to_hrn(xrn)
259 slicename = hrn_to_pl_slicename(hrn)
260 slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
265 # determine if this is a peer slice
266 peer = peers.get_peer(api, hrn)
269 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
270 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
273 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
276 def get_slices(api, creds):
277 # look in cache first
279 slices = api.cache.get('slices')
284 slices = api.plshell.GetSlices(api.plauth, {'peer_id': None}, ['name'])
285 slice_hrns = [slicename_to_hrn(api.hrn, slice['name']) for slice in slices]
286 slice_urns = [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slice_hrns]
290 api.cache.add('slices', slice_urns)
294 # xxx Thierry : caching at the aggregate level sounds wrong...
296 def get_rspec(api, creds, options,call_id):
297 if not Callids().should_handle_call_id(call_id): return ""
298 # get slice's hrn from options
299 xrn = options.get('geni_slice_urn', '')
300 (hrn, type) = urn_to_hrn(xrn)
302 # look in cache first
303 if caching and api.cache and not xrn:
304 rspec = api.cache.get('nodes')
306 api.logger.info("aggregate.get_rspec: returning cached value for hrn %s"%hrn)
309 network = Network(api)
311 if network.get_slice(api, hrn):
314 rspec = network.toxml()
317 if caching and api.cache and not xrn:
318 api.cache.add('nodes', rspec)
323 def get_ticket(api, xrn, creds, rspec, users):
325 reg_objects = __get_registry_objects(xrn, creds, users)
327 slice_hrn, type = urn_to_hrn(xrn)
329 peer = slices.get_peer(slice_hrn)
330 sfa_peer = slices.get_sfa_peer(slice_hrn)
332 # get the slice record
333 registry = api.registries[api.hrn]
334 credential = api.getCredential()
335 records = registry.Resolve(xrn, credential)
337 # similar to create_slice, we must verify that the required records exist
338 # at this aggregate before we can issue a ticket
339 site_id, remote_site_id = slices.verify_site(registry, credential, slice_hrn,
340 peer, sfa_peer, reg_objects)
341 slice = slices.verify_slice(registry, credential, slice_hrn, site_id,
342 remote_site_id, peer, sfa_peer, reg_objects)
344 # make sure we get a local slice record
346 for tmp_record in records:
347 if tmp_record['type'] == 'slice' and \
348 not tmp_record['peer_authority']:
349 record = SliceRecord(dict=tmp_record)
351 raise RecordNotFound(slice_hrn)
354 slivers = Slices(api).get_slivers(slice_hrn)
356 raise SliverDoesNotExist(slice_hrn)
361 'timestamp': int(time.time()),
362 'initscripts': initscripts,
367 object_gid = record.get_gid_object()
368 new_ticket = SfaTicket(subject = object_gid.get_subject())
369 new_ticket.set_gid_caller(api.auth.client_gid)
370 new_ticket.set_gid_object(object_gid)
371 new_ticket.set_issuer(key=api.key, subject=api.hrn)
372 new_ticket.set_pubkey(object_gid.get_pubkey())
373 new_ticket.set_attributes(data)
374 new_ticket.set_rspec(rspec)
375 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
379 return new_ticket.save_to_string(save_parents=True)
386 rspec = get_rspec(api, "plc.princeton.sapan", None)
387 #rspec = get_rspec(api, "plc.princeton.coblitz", None)
388 #rspec = get_rspec(api, "plc.pl.sirius", None)
391 f = open(sys.argv[1])
394 create_slice(api, "plc.princeton.sapan", xml)
396 if __name__ == "__main__":