6 from types import StringTypes
8 from sfa.util.namespace import get_authority, urn_to_hrn, slicename_to_hrn, hrn_to_pl_slicename, hrn_to_urn
9 from sfa.util.rspec import *
10 from sfa.util.specdict import *
11 from sfa.util.faults import *
12 from sfa.util.record import SfaRecord
13 from sfa.util.policy import Policy
14 from sfa.util.record import *
15 from sfa.util.sfaticket import SfaTicket
16 from sfa.plc.slices import Slices
17 from sfa.trust.credential import Credential
18 import sfa.plc.peers as peers
19 from sfa.plc.network import *
20 from sfa.plc.api import SfaAPI
21 from sfa.plc.slices import *
22 from dateutil.parser import parse
25 def __get_registry_objects(slice_xrn, creds, users):
29 hrn, type = urn_to_hrn(slice_xrn)
31 hrn_auth = get_authority(hrn)
33 # Build up objects that an SFA registry would return if SFA
34 # could contact the slice's registry directly
38 # dont allow special characters in the site login base
39 #only_alphanumeric = re.compile('[^a-zA-Z0-9]+')
40 #login_base = only_alphanumeric.sub('', hrn_auth[:20]).lower()
41 slicename = hrn_to_pl_slicename(hrn)
42 login_base = slicename.split('_')[0]
47 site['name'] = 'geni.%s' % login_base
48 site['enabled'] = True
49 site['max_slices'] = 100
52 # Is it okay if this login base is the same as one already at this myplc site?
53 # Do we need uniqueness? Should use hrn_auth instead of just the leaf perhaps?
54 site['login_base'] = login_base
55 site['abbreviated_name'] = login_base
56 site['max_slivers'] = 1000
57 reg_objects['site'] = site
60 slice['expires'] = int(time.mktime(Credential(string=creds[0]).get_expiration().timetuple()))
62 slice['name'] = hrn_to_pl_slicename(hrn)
64 slice['description'] = hrn
66 reg_objects['slice_record'] = slice
68 reg_objects['users'] = {}
71 hrn, _ = urn_to_hrn(user['urn'])
72 user['email'] = hrn_to_pl_slicename(hrn) + "@geni.net"
73 user['first_name'] = hrn
74 user['last_name'] = hrn
75 reg_objects['users'][user['email']] = user
79 def __get_hostnames(nodes):
82 hostnames.append(node.hostname)
87 version['geni_api'] = 1
91 def slice_status(api, slice_xrn, creds):
92 hrn, type = urn_to_hrn(slice_xrn)
93 # find out where this slice is currently running
95 slicename = hrn_to_pl_slicename(hrn)
97 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids','person_ids','name','expires'])
99 raise Exception("Slice %s not found (used %s as slicename internally)" % slice_xrn, slicename)
102 nodes = api.plshell.GetNodes(api.plauth, slice['node_ids'],
103 ['hostname', 'boot_state', 'last_contact'])
104 api.logger.info(slice)
105 api.logger.info(nodes)
108 result['geni_urn'] = slice_xrn
109 result['geni_status'] = 'unknown'
110 result['pl_login'] = slice['name']
111 result['pl_expires'] = slice['expires']
117 res['pl_hostname'] = node['hostname']
118 res['pl_boot_state'] = node['boot_state']
119 res['pl_last_contact'] = node['last_contact']
121 res['geni_status'] = 'unknown'
122 res['geni_error'] = ''
124 resources.append(res)
126 result['geni_resources'] = resources
129 def create_slice(api, slice_xrn, creds, rspec, users):
131 Create the sliver[s] (slice) at this aggregate.
132 Verify HRN and initialize the slice record in PLC if necessary.
135 reg_objects = __get_registry_objects(slice_xrn, creds, users)
137 hrn, type = urn_to_hrn(slice_xrn)
140 peer = slices.get_peer(hrn)
141 sfa_peer = slices.get_sfa_peer(hrn)
142 registry = api.registries[api.hrn]
143 credential = api.getCredential()
144 site_id, remote_site_id = slices.verify_site(registry, credential, hrn,
145 peer, sfa_peer, reg_objects)
147 slice_record = slices.verify_slice(registry, credential, hrn, site_id,
148 remote_site_id, peer, sfa_peer, reg_objects)
150 network = Network(api)
152 slice = network.get_slice(api, hrn)
153 slice.peer_id = slice_record['peer_slice_id']
154 current = __get_hostnames(slice.get_nodes())
156 network.addRSpec(rspec, api.config.SFA_AGGREGATE_RSPEC_SCHEMA)
157 request = __get_hostnames(network.nodesWithSlivers())
159 # remove nodes not in rspec
160 deleted_nodes = list(set(current).difference(request))
162 # add nodes from rspec
163 added_nodes = list(set(request).difference(current))
167 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
169 api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes)
170 api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
172 network.updateSliceTags()
176 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer,
179 # print network.toxml()
184 def renew_slice(api, xrn, creds, expiration_time):
185 hrn, type = urn_to_hrn(xrn)
186 slicename = hrn_to_pl_slicename(hrn)
187 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
189 raise RecordNotFound(hrn)
191 requested_time = parse(expiration_time)
192 record = {'expires': int(time.mktime(requested_time.timetuple()))}
193 api.plshell.UpdateSlice(api.plauth, slice['slice_id'], record)
196 def start_slice(api, xrn, creds):
197 hrn, type = urn_to_hrn(xrn)
198 slicename = hrn_to_pl_slicename(hrn)
199 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
201 raise RecordNotFound(hrn)
202 slice_id = slices[0]['slice_id']
203 slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'}, ['slice_tag_id'])
204 # just remove the tag if it exists
206 api.plshell.DeleteSliceTag(api.plauth, slice_tags[0]['slice_tag_id'])
210 def stop_slice(api, xrn, creds):
211 hrn, type = urn_to_hrn(xrn)
212 slicename = hrn_to_pl_slicename(hrn)
213 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
215 raise RecordNotFound(hrn)
216 slice_id = slices[0]['slice_id']
217 slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'})
219 api.plshell.AddSliceTag(api.plauth, slice_id, 'enabled', '0')
220 elif slice_tags[0]['value'] != "0":
221 tag_id = attributes[0]['slice_tag_id']
222 api.plshell.UpdateSliceTag(api.plauth, tag_id, '0')
225 def reset_slice(api, xrn):
226 # XX not implemented at this interface
229 def delete_slice(api, xrn, creds):
230 hrn, type = urn_to_hrn(xrn)
231 slicename = hrn_to_pl_slicename(hrn)
232 slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
237 # determine if this is a peer slice
238 peer = peers.get_peer(api, hrn)
241 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
242 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
245 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
248 def get_slices(api, creds):
249 # look in cache first
251 slices = api.cache.get('slices')
256 slices = api.plshell.GetSlices(api.plauth, {'peer_id': None}, ['name'])
257 slice_hrns = [slicename_to_hrn(api.hrn, slice['name']) for slice in slices]
258 slice_urns = [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slice_hrns]
262 api.cache.add('slices', slice_urns)
266 def get_rspec(api, creds, options):
267 # get slice's hrn from options
268 xrn = options.get('geni_slice_urn', None)
269 hrn, type = urn_to_hrn(xrn)
271 # look in cache first
272 if api.cache and not xrn:
273 rspec = api.cache.get('nodes')
277 network = Network(api)
279 if network.get_slice(api, hrn):
282 rspec = network.toxml()
285 if api.cache and not xrn:
286 api.cache.add('nodes', rspec)
291 def get_ticket(api, xrn, creds, rspec, users):
293 reg_objects = __get_registry_objects(xrn, creds, users)
295 slice_hrn, type = urn_to_hrn(xrn)
297 peer = slices.get_peer(slice_hrn)
298 sfa_peer = slices.get_sfa_peer(slice_hrn)
300 # get the slice record
301 registry = api.registries[api.hrn]
302 credential = api.getCredential()
303 records = registry.Resolve(xrn, credential)
305 # similar to create_slice, we must verify that the required records exist
306 # at this aggregate before we can issue a ticket
307 site_id, remote_site_id = slices.verify_site(registry, credential, slice_hrn,
308 peer, sfa_peer, reg_objects)
309 slice = slices.verify_slice(registry, credential, slice_hrn, site_id,
310 remote_site_id, peer, sfa_peer, reg_objects)
312 # make sure we get a local slice record
314 for tmp_record in records:
315 if tmp_record['type'] == 'slice' and \
316 not tmp_record['peer_authority']:
317 record = SliceRecord(dict=tmp_record)
319 raise RecordNotFound(slice_hrn)
322 slivers = Slices(api).get_slivers(slice_hrn)
324 raise SliverDoesNotExist(slice_hrn)
329 'timestamp': int(time.time()),
330 'initscripts': initscripts,
335 object_gid = record.get_gid_object()
336 new_ticket = SfaTicket(subject = object_gid.get_subject())
337 new_ticket.set_gid_caller(api.auth.client_gid)
338 new_ticket.set_gid_object(object_gid)
339 new_ticket.set_issuer(key=api.key, subject=api.hrn)
340 new_ticket.set_pubkey(object_gid.get_pubkey())
341 new_ticket.set_attributes(data)
342 new_ticket.set_rspec(rspec)
343 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
347 return new_ticket.save_to_string(save_parents=True)
354 rspec = get_rspec(api, "plc.princeton.sapan", None)
355 #rspec = get_rspec(api, "plc.princeton.coblitz", None)
356 #rspec = get_rspec(api, "plc.pl.sirius", None)
359 f = open(sys.argv[1])
362 create_slice(api, "plc.princeton.sapan", xml)
364 if __name__ == "__main__":