1 ### $Id: slices.py 15842 2009-11-22 09:56:13Z anil $
2 ### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/plc/slices.py $
9 from types import StringTypes
10 from sfa.util.namespace import *
11 from sfa.util.rspec import *
12 from sfa.util.specdict import *
13 from sfa.util.faults import *
14 from sfa.util.record import SfaRecord
15 from sfa.util.policy import Policy
16 from sfa.util.record import *
17 from sfa.util.sfaticket import SfaTicket
18 from sfa.util.debug import log
19 from sfa.plc.slices import Slices
20 from sfa.trust.credential import Credential
21 import sfa.plc.peers as peers
22 from sfa.plc.network import *
23 from sfa.plc.api import SfaAPI
24 from sfa.plc.slices import *
27 def __get_registry_objects(slice_xrn, creds, users):
31 hrn, type = urn_to_hrn(slice_xrn)
33 hrn_auth = get_authority(hrn)
35 # Build up objects that an SFA registry would return if SFA
36 # could contact the slice's registry directly
40 # dont allow special characters in the site login base
41 only_alphanumeric = re.compile('[^a-zA-Z0-9]+')
42 login_base = only_alphanumeric.sub('', hrn_auth[:20]).lower()
48 site['name'] = 'geni.%s' % login_base
49 site['enabled'] = True
50 site['max_slices'] = 100
53 # Is it okay if this login base is the same as one already at this myplc site?
54 # Do we need uniqueness? Should use hrn_auth instead of just the leaf perhaps?
55 site['login_base'] = login_base
56 site['abbreviated_name'] = login_base
57 site['max_slivers'] = 1000
58 reg_objects['site'] = site
61 slice['expires'] = int(time.mktime(Credential(string=creds[0]).get_lifetime().timetuple()))
63 slice['name'] = site['login_base'] + "_" + get_leaf(hrn)
65 slice['description'] = hrn
67 reg_objects['slice_record'] = slice
69 reg_objects['users'] = {}
72 hrn, _ = urn_to_hrn(user['urn'])
73 user['email'] = hrn + "@geni.net"
74 user['first_name'] = hrn
75 user['last_name'] = hrn
76 reg_objects['users'][user['email']] = user
80 def __get_hostnames(nodes):
83 hostnames.append(node.hostname)
88 version['geni_api'] = 1
92 def slice_status(api, slice_xrn, creds):
94 result['geni_urn'] = slice_xrn
95 result['geni_status'] = 'unknown'
96 result['geni_resources'] = {}
99 def create_slice(api, slice_xrn, creds, rspec, users):
101 Create the sliver[s] (slice) at this aggregate.
102 Verify HRN and initialize the slice record in PLC if necessary.
105 reg_objects = __get_registry_objects(slice_xrn, creds, users)
107 hrn, type = urn_to_hrn(slice_xrn)
110 peer = slices.get_peer(hrn)
111 sfa_peer = slices.get_sfa_peer(hrn)
112 registry = api.registries[api.hrn]
113 credential = api.getCredential()
114 site_id, remote_site_id = slices.verify_site(registry, credential, hrn,
115 peer, sfa_peer, reg_objects)
117 slice_record = slices.verify_slice(registry, credential, hrn, site_id,
118 remote_site_id, peer, sfa_peer, reg_objects)
120 network = Network(api)
122 slice = network.get_slice(api, hrn)
123 slice.peer_id = slice_record['peer_slice_id']
124 current = __get_hostnames(slice.get_nodes())
126 network.addRSpec(rspec, api.config.SFA_AGGREGATE_RSPEC_SCHEMA)
127 request = __get_hostnames(network.nodesWithSlivers())
129 # remove nodes not in rspec
130 deleted_nodes = list(set(current).difference(request))
132 # add nodes from rspec
133 added_nodes = list(set(request).difference(current))
137 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
139 api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes)
140 api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
142 network.updateSliceTags()
146 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer,
149 # print network.toxml()
154 def renew_slice(api, xrn, creds, exipration_time):
155 hrn, type = urn_to_hrn(xrn)
156 slicename = hrn_to_pl_slicename(hrn)
157 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
159 raise RecordNotFound(hrn)
161 slice['expires'] = expiration_time
162 api.plshell.UpdateSlice(api.plauth, slice['slice_id'], slice)
165 def start_slice(api, xrn, creds):
166 hrn, type = urn_to_hrn(xrn)
167 slicename = hrn_to_pl_slicename(hrn)
168 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
170 raise RecordNotFound(hrn)
171 slice_id = slices[0]['slice_id']
172 slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'}, ['slice_tag_id'])
173 # just remove the tag if it exists
175 api.plshell.DeleteSliceTag(api.plauth, slice_tags[0]['slice_tag_id'])
179 def stop_slice(api, xrn, creds):
180 hrn, type = urn_to_hrn(xrn)
181 slicename = hrn_to_pl_slicename(hrn)
182 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
184 raise RecordNotFound(hrn)
185 slice_id = slices[0]['slice_id']
186 slice_tags = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'tagname': 'enabled'})
188 api.plshell.AddSliceTag(api.plauth, slice_id, 'enabled', '0')
189 elif slice_tags[0]['value'] != "0":
190 tag_id = attributes[0]['slice_tag_id']
191 api.plshell.UpdateSliceTag(api.plauth, tag_id, '0')
194 def reset_slice(api, xrn):
195 # XX not implemented at this interface
198 def delete_slice(api, xrn, creds):
199 hrn, type = urn_to_hrn(xrn)
200 slicename = hrn_to_pl_slicename(hrn)
201 slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
206 # determine if this is a peer slice
207 peer = peers.get_peer(api, hrn)
210 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
211 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
214 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
217 def get_slices(api, creds):
218 # look in cache first
220 slices = api.cache.get('slices')
225 slices = api.plshell.GetSlices(api.plauth, {'peer_id': None}, ['name'])
226 slice_hrns = [slicename_to_hrn(api.hrn, slice['name']) for slice in slices]
227 slice_urns = [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slice_hrns]
231 api.cache.add('slices', slice_urns)
235 def get_rspec(api, creds, options):
236 # get slice's hrn from options
237 xrn = options.get('geni_slice_urn', None)
238 hrn, type = urn_to_hrn(xrn)
240 # look in cache first
241 if api.cache and not xrn:
242 rspec = api.cache.get('nodes')
246 network = Network(api)
248 if network.get_slice(api, hrn):
251 rspec = network.toxml()
254 if api.cache and not xrn:
255 api.cache.add('nodes', rspec)
260 def get_ticket(api, xrn, creds, rspec, users):
262 reg_objects = __get_registry_objects(xrn, creds, users)
264 slice_hrn, type = urn_to_hrn(xrn)
266 peer = slices.get_peer(slice_hrn)
267 sfa_peer = slices.get_sfa_peer(slice_hrn)
269 # get the slice record
270 registry = api.registries[api.hrn]
271 credential = api.getCredential()
272 records = registry.Resolve(xrn, credential)
274 # similar to create_slice, we must verify that the required records exist
275 # at this aggregate before we can issue a ticket
276 site_id, remote_site_id = slices.verify_site(registry, credential, slice_hrn,
277 peer, sfa_peer, reg_objects)
278 slice = slices.verify_slice(registry, credential, slice_hrn, site_id,
279 remote_site_id, peer, sfa_peer, reg_objects)
281 # make sure we get a local slice record
283 for tmp_record in records:
284 if tmp_record['type'] == 'slice' and \
285 not tmp_record['peer_authority']:
286 record = SliceRecord(dict=tmp_record)
288 raise RecordNotFound(slice_hrn)
291 slivers = Slices(api).get_slivers(slice_hrn)
293 raise SliverDoesNotExist(slice_hrn)
298 'timestamp': int(time.time()),
299 'initscripts': initscripts,
304 object_gid = record.get_gid_object()
305 new_ticket = SfaTicket(subject = object_gid.get_subject())
306 new_ticket.set_gid_caller(api.auth.client_gid)
307 new_ticket.set_gid_object(object_gid)
308 new_ticket.set_issuer(key=api.key, subject=api.hrn)
309 new_ticket.set_pubkey(object_gid.get_pubkey())
310 new_ticket.set_attributes(data)
311 new_ticket.set_rspec(rspec)
312 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
316 return new_ticket.save_to_string(save_parents=True)
323 rspec = get_rspec(api, "plc.princeton.sapan", None)
324 #rspec = get_rspec(api, "plc.princeton.coblitz", None)
325 #rspec = get_rspec(api, "plc.pl.sirius", None)
328 f = open(sys.argv[1])
331 create_slice(api, "plc.princeton.sapan", xml)
333 if __name__ == "__main__":