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])
24 def lock_state_file():
28 def unlock_state_file():
32 def read_alloc_dict():
34 rows = open(SFA_MAX_CONF_FILE).read().split('\n')
36 columns = r.split(' ')
39 allocs = columns[1].split(',')
40 alloc_dict[hrn]=allocs
43 def commit_alloc_dict(d):
44 f = open(SFA_MAX_CONF_FILE, 'w')
47 row = hrn+' '+','.join(columns)+'\n'
51 def collapse_alloc_dict(d):
57 def bootstrap_slice(api, hrn, added_nodes, deleted_nodes):
58 # This code is taken from slices.py
59 # To clean up after 21 July
60 # Get the slice record from geni
62 registries = Registries(api)
63 registry = registries[self.api.hrn]
64 credential = self.api.getCredential()
65 records = registry.resolve(credential, hrn)
66 for record in records:
67 if record.get_type() in ['slice']:
68 slice = record.as_dict()
70 raise RecordNotFound(hrn)
72 # Make sure slice exists at plc, if it doesnt add it
73 slicename = hrn_to_pl_slicename(hrn)
74 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
76 parts = slicename.split("_")
78 # if site doesnt exist add it
79 sites = api.plshell.GetSites(api.plauth, [login_base])
81 authority = get_authority(hrn)
82 site_records = registry.resolve(credential, authority)
85 raise RecordNotFound(authority)
86 site_record = site_records[0]
87 site = site_record.as_dict()
91 site_id = api.plshell.AddSite(api.plauth, site)
96 slice_keys = ['name', 'url', 'description']
97 for key in slice_keys:
98 if key in slice and slice[key]:
99 slice_fields[key] = slice[key]
100 api.plshell.AddSlice(api.plauth, slice_fields)
102 slice['node_ids'] = 0
105 # get the list of valid slice users from the registry and make
106 # they are added to the slice
107 researchers = record.get('researcher', [])
108 for researcher in researchers:
110 person_records = registry.resolve(credential, researcher)
111 for record in person_records:
112 if record.get_type() in ['user']:
113 person_record = record
114 if not person_record:
116 person_dict = person_record.as_dict()
117 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']], ['person_id', 'key_ids'])
119 # Create the person record
121 person_id=api.plshell.AddPerson(api.plauth, person_dict)
123 # The line below enables the user account on the remote aggregate soon after it is created.
124 # without this the user key is not transfered to the slice (as GetSlivers returns key of only enabled users),
125 # which prevents the user from login to the slice. We may do additional checks before enabling the user.
127 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
130 key_ids = persons[0]['key_ids']
132 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'], slicename)
134 # Get this users local keys
135 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
136 keys = [key['key'] for key in keylist]
138 # add keys that arent already there
139 for personkey in person_dict['keys']:
140 if personkey not in keys:
141 key = {'key_type': 'ssh', 'key': personkey}
142 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
144 # find out where this slice is currently running
145 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'], ['hostname'])
146 hostnames = [node['hostname'] for node in nodelist]
148 api.plshell.AddSliceToNodes(self.api.plauth, slicename, added_nodes)
149 api.plshell.DeleteSliceFromNodes(self.api.plauth, slicename, deleted_nodes)
153 def alloc_nodes(hrn, links_to_add, links_to_delete):
155 nodes_to_add = link_endpoints(links_to_add)
156 nodes_to_delete = link_endpoints(links_to_delete)
158 #bootstrap_slice(api, hrn, nodes_to_add, nodes_to_delete)
160 for r in requested_allocations:
161 print "Requesting "+r
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__":