1 from sfa.util.faults import *
2 from sfa.util.namespace import *
3 from sfa.server.registry import Registries
4 from sfa.plc.nodes import *
5 from sfa.plc.api import *
6 from sfa.managers.vini.utils import *
10 Copied from create_slice_aggregate() in sfa.plc.slices
12 def create_slice_vini_aggregate(api, hrn, nodes):
13 # Get the slice record
15 registries = Registries(api)
16 registry = registries[api.hrn]
17 credential = api.getCredential()
18 records = registry.resolve(credential, hrn)
19 for record in records:
20 if record['type'] in ['slice']:
23 raise RecordNotFound(hrn)
25 # Make sure slice exists at plc, if it doesnt add it
26 slicename = hrn_to_pl_slicename(hrn)
27 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
29 parts = slicename.split("_")
31 # if site doesnt exist add it
32 sites = api.plshell.GetSites(api.plauth, [login_base])
34 authority = get_authority(hrn)
35 site_records = registry.resolve(credential, authority)
38 raise RecordNotFound(authority)
39 site = site_records[0]
43 site_id = api.plshell.AddSite(api.plauth, site)
48 slice_keys = ['name', 'url', 'description']
49 for key in slice_keys:
50 if key in slice and slice[key]:
51 slice_fields[key] = slice[key]
52 api.plshell.AddSlice(api.plauth, slice_fields)
58 # get the list of valid slice users from the registry and make
59 # they are added to the slice
60 researchers = record.get('researcher', [])
61 for researcher in researchers:
63 person_records = registry.resolve(credential, researcher)
64 for record in person_records:
65 if record['type'] in ['user']:
66 person_record = record
69 person_dict = person_record
70 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']],
71 ['person_id', 'key_ids'])
73 # Create the person record
75 person_id=api.plshell.AddPerson(api.plauth, person_dict)
77 # The line below enables the user account on the remote aggregate
78 # soon after it is created.
79 # without this the user key is not transfered to the slice
80 # (as GetSlivers returns key of only enabled users),
81 # which prevents the user from login to the slice.
82 # We may do additional checks before enabling the user.
84 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
87 key_ids = persons[0]['key_ids']
89 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'],
92 # Get this users local keys
93 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
94 keys = [key['key'] for key in keylist]
96 # add keys that arent already there
97 for personkey in person_dict['keys']:
98 if personkey not in keys:
99 key = {'key_type': 'ssh', 'key': personkey}
100 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
102 # find out where this slice is currently running
103 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'],
105 hostnames = [node['hostname'] for node in nodelist]
107 # remove nodes not in rspec
108 deleted_nodes = list(set(hostnames).difference(nodes))
109 # add nodes from rspec
110 added_nodes = list(set(nodes).difference(hostnames))
113 print >> sys.stderr, "Slice on nodes:"
115 print >> sys.stderr, n
116 print >> sys.stderr, "Wants nodes:"
118 print >> sys.stderr, n
119 print >> sys.stderr, "Deleting nodes:"
120 for n in deleted_nodes:
121 print >> sys.stderr, n
122 print >> sys.stderr, "Adding nodes:"
123 for n in added_nodes:
124 print >> sys.stderr, n
127 api.plshell.AddSliceToNodes(api.plauth, slicename, added_nodes)
128 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, deleted_nodes)
132 def get_rspec(api, xrn, origin_hrn):
133 hrn = urn_to_hrn(xrn)[0]
136 slicename = hrn_to_pl_slicename(hrn)
137 slice = get_slice(api, slicename)
140 topo.nodeTopoFromSliceTags(slice)
142 # call the default sfa.plc.nodes.get_rspec() method
143 return Nodes(api).get_rspec(hrn)
145 return topo.toxml(hrn)
150 Hook called via 'sfi.py create'
152 def create_slice(api, xrn, xml):
153 hrn = urn_to_hrn(xrn)[0]
155 topo.nodeTopoFromRSpec(xml)
157 # Check request against current allocations
158 topo.verifyNodeTopo(hrn, topo)
160 nodes = topo.nodesInTopo()
163 hostnames.append(node.hostname)
164 create_slice_vini_aggregate(api, hrn, hostnames)
166 slicename = hrn_to_pl_slicename(hrn)
167 slice = get_slice(api, slicename)
169 topo.updateSliceTags(slice)
171 # print topo.toxml(hrn)
176 Returns the request context required by sfatables. At some point, this mechanism should be changed
177 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
178 return the basic information needed in a dict.
180 def fetch_context(slice_hrn, user_hrn, contexts):
181 base_context = {'sfa':{'user':{'hrn':user_hrn},
182 'slice':{'hrn':slice_hrn}}}
187 #rspec = get_rspec(api, "plc.princeton.iias", None)
189 f = open(sys.argv[1])
192 create_slice(api, "plc.princeton.iias", xml)
194 if __name__ == "__main__":