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 *
28 version['geni_api'] = 1
31 def slice_status(api, slice_xrn, creds):
33 result['geni_urn'] = slice_xrn
34 result['geni_status'] = 'unknown'
35 result['geni_resources'] = {}
38 def __get_hostnames(nodes):
41 hostnames.append(node.hostname)
44 def create_slice(api, slice_xrn, creds, rspec, users):
46 Create the sliver[s] (slice) at this aggregate.
47 Verify HRN and initialize the slice record in PLC if necessary.
50 reg_objects = __get_registry_objects(slice_xrn, creds, users)
52 hrn, type = urn_to_hrn(slice_xrn)
55 peer = slices.get_peer(hrn)
56 sfa_peer = slices.get_sfa_peer(hrn)
57 registry = api.registries[api.hrn]
58 credential = api.getCredential()
59 site_id, remote_site_id = slices.verify_site(registry, credential, hrn,
60 peer, sfa_peer, reg_objects)
62 slice = slices.verify_slice(registry, credential, hrn, site_id,
63 remote_site_id, peer, sfa_peer, reg_objects)
65 network = Network(api)
67 slice = network.get_slice(api, hrn)
68 current = __get_hostnames(slice.get_nodes())
70 network.addRSpec(rspec, api.config.SFA_AGGREGATE_RSPEC_SCHEMA)
71 request = __get_hostnames(network.nodesWithSlivers())
73 # remove nodes not in rspec
74 deleted_nodes = list(set(current).difference(request))
76 # add nodes from rspec
77 added_nodes = list(set(request).difference(current))
80 api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice.id, peer)
82 api.plshell.AddSliceToNodes(api.plauth, slice.name, added_nodes)
83 api.plshell.DeleteSliceFromNodes(api.plauth, slice.name, deleted_nodes)
85 network.updateSliceTags()
88 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice.id, peer,
91 # print network.toxml()
96 def __get_registry_objects(slice_xrn, creds, users):
100 hrn, type = urn_to_hrn(slice_xrn)
102 hrn_auth = get_authority(hrn)
104 # Build up objects that an SFA registry would return if SFA
105 # could contact the slice's registry directly
113 site['name'] = 'geni.%s' % hrn_auth
114 site['enabled'] = True
115 site['max_slices'] = 100
118 # Is it okay if this login base is the same as one already at this myplc site?
119 # Do we need uniqueness? Should use hrn_auth instead of just the leaf perhaps?
120 site['login_base'] = get_leaf(hrn_auth)
121 site['abbreviated_name'] = hrn
122 site['max_slivers'] = 1000
123 reg_objects['site'] = site
126 slice['expires'] = int(mktime(Credential(string=creds[0]).get_lifetime().timetuple()))
128 slice['name'] = site['login_base'] + "_" + get_leaf(hrn)
130 slice['description'] = hrn
132 reg_objects['slice_record'] = slice
134 reg_objects['users'] = {}
137 hrn, _ = urn_to_hrn(user['urn'])
138 user['email'] = hrn + "@geni.net"
139 user['first_name'] = hrn
140 user['last_name'] = hrn
141 reg_objects['users'][user['email']] = user
145 def get_ticket(api, xrn, rspec, origin_hrn=None, reg_objects=None):
147 slice_hrn, type = urn_to_hrn(xrn)
149 peer = slices.get_peer(slice_hrn)
150 sfa_peer = slices.get_sfa_peer(slice_hrn)
152 # get the slice record
153 registry = api.registries[api.hrn]
154 credential = api.getCredential()
155 records = registry.resolve(credential, xrn)
157 # similar to create_slice, we must verify that the required records exist
158 # at this aggregate before we can issue a ticket
159 site_id, remote_site_id = slices.verify_site(registry, credential, slice_hrn,
160 peer, sfa_peer, reg_objects)
161 slice = slices.verify_slice(registry, credential, slice_hrn, site_id,
162 remote_site_id, peer, sfa_peer, reg_objects)
164 # make sure we get a local slice record
166 for tmp_record in records:
167 if tmp_record['type'] == 'slice' and \
168 not tmp_record['peer_authority']:
169 record = SliceRecord(dict=tmp_record)
171 raise RecordNotFound(slice_hrn)
174 slivers = Slices(api).get_slivers(slice_hrn)
176 raise SliverDoesNotExist(slice_hrn)
181 'timestamp': int(time.time()),
182 'initscripts': initscripts,
187 object_gid = record.get_gid_object()
188 new_ticket = SfaTicket(subject = object_gid.get_subject())
189 new_ticket.set_gid_caller(api.auth.client_gid)
190 new_ticket.set_gid_object(object_gid)
191 new_ticket.set_issuer(key=api.key, subject=api.hrn)
192 new_ticket.set_pubkey(object_gid.get_pubkey())
193 new_ticket.set_attributes(data)
194 new_ticket.set_rspec(rspec)
195 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
199 return new_ticket.save_to_string(save_parents=True)
201 def start_slice(api, xrn):
202 hrn, type = urn_to_hrn(xrn)
203 slicename = hrn_to_pl_slicename(hrn)
204 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
206 raise RecordNotFound(hrn)
208 attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
209 attribute_id = attributes[0]['slice_attribute_id']
210 api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
214 def stop_slice(api, xrn, creds):
215 hrn, type = urn_to_hrn(xrn)
216 slicename = hrn_to_pl_slicename(hrn)
217 slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
219 raise RecordNotFound(hrn)
220 slice_id = slices[0]['slice_id']
221 attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
222 attribute_id = attributes[0]['slice_attribute_id']
223 api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
226 def reset_slice(api, xrn):
227 # XX not implemented at this interface
230 def delete_slice(api, xrn, creds):
231 hrn, type = urn_to_hrn(xrn)
232 slicename = hrn_to_pl_slicename(hrn)
233 slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
238 # determine if this is a peer slice
239 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'])
244 api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
248 # look in cache first
250 slices = api.cache.get('slices')
255 slices = api.plshell.GetSlices(api.plauth, {'peer_id': None}, ['name'])
256 slice_hrns = [slicename_to_hrn(api.hrn, slice['name']) for slice in slices]
257 slice_urns = [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slice_hrns]
261 api.cache.add('slices', slice_urns)
265 def get_rspec(api, creds, options):
266 # get slice's hrn from options
267 xrn = options.get('geni_slice_urn', None)
268 hrn, type = urn_to_hrn(xrn)
270 # get hrn of the original caller
271 origin_hrn = options.get('origin_hrn', None)
273 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
275 # look in cache first
276 if api.cache and not xrn:
277 rspec = api.cache.get('nodes')
281 network = Network(api)
283 if network.get_slice(api, hrn):
286 rspec = network.toxml()
289 if api.cache and not xrn:
290 api.cache.add('nodes', rspec)
298 rspec = get_rspec(api, "plc.princeton.sapan", None)
299 #rspec = get_rspec(api, "plc.princeton.coblitz", None)
300 #rspec = get_rspec(api, "plc.pl.sirius", None)
303 f = open(sys.argv[1])
306 create_slice(api, "plc.princeton.sapan", xml)
308 if __name__ == "__main__":