1 from sfa.util.rspec import Rspec
5 SFA_VINI_DEFAULT_RSPEC = '/etc/sfa/vini.rspec'
8 Copied from create_slice_aggregate() in sfa.plc.slices.
10 def create_slice_vini_aggregate(api, hrn, nodes):
11 # Get the slice record from geni
13 registries = Registries(api)
14 registry = registries[api.hrn]
15 credential = api.getCredential()
16 records = registry.resolve(credential, hrn)
17 for record in records:
18 if record.get_type() in ['slice']:
19 slice = record.as_dict()
21 raise RecordNotFound(hrn)
23 # Make sure slice exists at plc, if it doesnt add it
24 slicename = hrn_to_pl_slicename(hrn)
25 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
27 parts = slicename.split("_")
29 # if site doesnt exist add it
30 sites = api.plshell.GetSites(api.plauth, [login_base])
32 authority = get_authority(hrn)
33 site_records = registry.resolve(credential, authority)
36 raise RecordNotFound(authority)
37 site_record = site_records[0]
38 site = site_record.as_dict()
42 site_id = api.plshell.AddSite(api.plauth, site)
47 slice_keys = ['name', 'url', 'description']
48 for key in slice_keys:
49 if key in slice and slice[key]:
50 slice_fields[key] = slice[key]
51 api.plshell.AddSlice(api.plauth, slice_fields)
56 # get the list of valid slice users from the registry and make
57 # they are added to the slice
58 researchers = record.get('researcher', [])
59 for researcher in researchers:
61 person_records = registry.resolve(credential, researcher)
62 for record in person_records:
63 if record.get_type() in ['user']:
64 person_record = record
67 person_dict = person_record.as_dict()
68 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']], ['person_id', 'key_ids'])
70 # Create the person record
72 person_id=api.plshell.AddPerson(api.plauth, person_dict)
74 # The line below enables the user account on the remote aggregate soon after it is created.
75 # without this the user key is not transfered to the slice (as GetSlivers returns key of only enabled users),
76 # which prevents the user from login to the slice. We may do additional checks before enabling the user.
78 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
81 key_ids = persons[0]['key_ids']
83 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'], slicename)
85 # Get this users local keys
86 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
87 keys = [key['key'] for key in keylist]
89 # add keys that arent already there
90 for personkey in person_dict['keys']:
91 if personkey not in keys:
92 key = {'key_type': 'ssh', 'key': personkey}
93 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
95 # find out where this slice is currently running
96 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'], ['hostname'])
97 hostnames = [node['hostname'] for node in nodelist]
99 # remove nodes not in rspec
100 deleted_nodes = list(set(hostnames).difference(nodes))
101 # add nodes from rspec
102 added_nodes = list(set(nodes).difference(hostnames))
104 api.plshell.AddSliceToNodes(api.plauth, slicename, added_nodes)
105 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, deleted_nodes)
110 def get_rspec(api, hrn):
114 # Convert HRN to slice name
115 # Get SliceTags for the slice
117 # Construct LinkSpecs from the topo_rspec SliceTags
118 # The first field is the NodeId of the remote node.
119 # So the endpoints are the SliceTag node and the remote node.
122 # - avoid duplicates?
123 # - verify both ends of the link?
126 # Return canned response for now...
128 r.parseFile(SFA_VINI_DEFAULT_RSPEC)
129 rspec = r.toGenDict()
134 def create_slice(api, hrn, rspec):
135 # Check request against current allocations
138 nodes = rspec_to_nodeset(rspec)
139 create_slice_vini_aggregate(api, hrn, nodes)
141 # Add VINI-specific topology attributes to slice here
145 def rspec_to_nodeset(rspec):
149 sitespecs = rspec['Rspec'][0]['Capacity'][0]['NetSpec'][0]['SiteSpec']
151 for node in s['NodeSpec']:
152 nodedict[node['name'][0]] = node['hostname'][0]
154 linkspecs = rspec['Rspec'][0]['Request'][0]['NetSpec'][0]['LinkSpec']
156 for e in l['endpoint']:
157 nodes.add(nodedict[e])
167 r.parseFile(sys.argv[1])
168 rspec = r.toGenDict()
169 create_slice(None,'plc',rspec)
171 if __name__ == "__main__":