1 from sfa.util.rspec import Rspec
5 SFA_MAX_CONF_FILE = '/etc/sfa/max_allocations'
9 topology = {'pl23':('planetlab2.dragon.maxgigapop.net','planetlab3.dragon.maxgigapop.net'),
10 'pl24':('planetlab2.dragon.maxgigapop.net','planetlab4.dragon.maxgigapop.net'),
11 'pl25':('planetlab2.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net'),
12 'pl34':('planetlab3.dragon.maxgigapop.net','planetlab4.dragon.maxgigapop.net'),
13 'pl35':('planetlab3.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net'),
14 'pl45':('planetlab4.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net')
17 def link_endpoints(links):
20 nodes.extend(topology[l])
23 def lock_state_file():
27 def unlock_state_file():
31 def read_alloc_dict():
33 rows = open(SFA_MAX_CONF_FILE).read().split('\n')
35 columns = r.split(' ')
38 allocs = columns[1].split(',')
39 alloc_dict[hrn]=allocs
42 def commit_alloc_dict(d):
43 f = open(SFA_MAX_CONF_FILE, 'w')
46 row = hrn+' '+','.join(columns)+'\n'
50 def collapse_alloc_dict(d):
56 def bootstrap_slice(api, hrn, added_nodes, deleted_nodes):
57 # This code is taken from slices.py
58 # To clean up after 21 July
59 # Get the slice record from geni
61 registries = Registries(api)
62 registry = registries[self.api.hrn]
63 credential = self.api.getCredential()
64 records = registry.resolve(credential, hrn)
65 for record in records:
66 if record.get_type() in ['slice']:
67 slice = record.as_dict()
69 raise RecordNotFound(hrn)
71 # Make sure slice exists at plc, if it doesnt add it
72 slicename = hrn_to_pl_slicename(hrn)
73 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
75 parts = slicename.split("_")
77 # if site doesnt exist add it
78 sites = api.plshell.GetSites(api.plauth, [login_base])
80 authority = get_authority(hrn)
81 site_records = registry.resolve(credential, authority)
84 raise RecordNotFound(authority)
85 site_record = site_records[0]
86 site = site_record.as_dict()
90 site_id = api.plshell.AddSite(api.plauth, site)
95 slice_keys = ['name', 'url', 'description']
96 for key in slice_keys:
97 if key in slice and slice[key]:
98 slice_fields[key] = slice[key]
99 api.plshell.AddSlice(api.plauth, slice_fields)
101 slice['node_ids'] = 0
104 # get the list of valid slice users from the registry and make
105 # they are added to the slice
106 researchers = record.get('researcher', [])
107 for researcher in researchers:
109 person_records = registry.resolve(credential, researcher)
110 for record in person_records:
111 if record.get_type() in ['user']:
112 person_record = record
113 if not person_record:
115 person_dict = person_record.as_dict()
116 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']], ['person_id', 'key_ids'])
118 # Create the person record
120 person_id=api.plshell.AddPerson(api.plauth, person_dict)
122 # The line below enables the user account on the remote aggregate soon after it is created.
123 # without this the user key is not transfered to the slice (as GetSlivers returns key of only enabled users),
124 # which prevents the user from login to the slice. We may do additional checks before enabling the user.
126 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
129 key_ids = persons[0]['key_ids']
131 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'], slicename)
133 # Get this users local keys
134 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
135 keys = [key['key'] for key in keylist]
137 # add keys that arent already there
138 for personkey in person_dict['keys']:
139 if personkey not in keys:
140 key = {'key_type': 'ssh', 'key': personkey}
141 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
143 # find out where this slice is currently running
144 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'], ['hostname'])
145 hostnames = [node['hostname'] for node in nodelist]
147 api.plshell.AddSliceToNodes(self.api.plauth, slicename, added_nodes)
148 api.plshell.DeleteSliceFromNodes(self.api.plauth, slicename, deleted_nodes)
152 def alloc_links(api, links_to_add, links_to_drop, foo):
154 def alloc_nodes(api,hrn, links_to_add, links_to_delete):
156 nodes_to_add = link_endpoints(links_to_add)
157 nodes_to_delete = link_endpoints(links_to_delete)
160 bootstrap_slice(api, hrn, nodes_to_add, nodes_to_delete)
165 # plc.princeton.sapan vlan23,vlan45
167 allocations = read_alloc_dict()
169 current_allocations = allocations[hrn]
171 current_allocations = collapse_alloc_dict(allocations)
173 return (allocations_to_rspec_dict(current_allocations))
176 def create_slice(api, hrn, rspec):
177 # Check if everything in rspec is either allocated by hrn
178 # or not allocated at all.
182 allocations = read_alloc_dict()
183 requested_allocations = rspec_to_allocations (rspec)
184 current_allocations = collapse_alloc_dict(allocations)
186 current_hrn_allocations=allocations[hrn]
188 current_hrn_allocations=[]
190 # Check request against current allocations
191 for a in requested_allocations:
192 if (a not in current_hrn_allocations and a in current_allocations):
196 # Allocations to delete
197 allocations_to_delete = []
198 for a in current_hrn_allocations:
199 if (a not in requested_allocations):
200 allocations_to_delete.extend([a])
202 # Ok, let's do our thing
203 alloc_nodes(api, hrn, requested_allocations, allocations_to_delete)
204 alloc_links(api, hrn, requested_allocations, allocations_to_delete)
205 allocations[hrn] = requested_allocations
206 commit_alloc_dict(allocations)
212 def rspec_to_allocations(rspec):
215 linkspecs = rspec['rspec']['request'][0]['netspec'][0]['linkspec']
217 links.extend([l['name'].replace('tns:','')])
226 rspec_xml = open(sys.argv[1]).read()
227 r.parseString(rspec_xml)
229 create_slice(None,'plc',rspec)
231 if __name__ == "__main__":