3 from sfa.util.rspec import Rspec
7 SFA_MAX_CONF_FILE = '/etc/sfa/max_allocations'
11 topology = {'pl23':('planetlab2.dragon.maxgigapop.net','planetlab3.dragon.maxgigapop.net'),
12 'pl24':('planetlab2.dragon.maxgigapop.net','planetlab4.dragon.maxgigapop.net'),
13 'pl25':('planetlab2.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net'),
14 'pl34':('planetlab3.dragon.maxgigapop.net','planetlab4.dragon.maxgigapop.net'),
15 'pl35':('planetlab3.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net'),
16 'pl45':('planetlab4.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net')
19 def link_endpoints(links):
22 nodes.extend(topology[l])
25 def lock_state_file():
29 def unlock_state_file():
33 def read_alloc_dict():
35 rows = open(SFA_MAX_CONF_FILE).read().split('\n')
37 columns = r.split(' ')
40 allocs = columns[1].split(',')
41 alloc_dict[hrn]=allocs
44 def commit_alloc_dict(d):
45 f = open(SFA_MAX_CONF_FILE, 'w')
48 row = hrn+' '+','.join(columns)+'\n'
52 def collapse_alloc_dict(d):
59 def alloc_links(api, links_to_add, links_to_drop, foo):
62 def alloc_nodes(api,hrn, requested_links, links_to_delete):
64 nodes_to_add = link_endpoints(links_to_add)
65 nodes_to_delete = link_endpoints(links_to_delete)
68 create_slice_max_aggregate(api, hrn, nodes_to_add, nodes_to_delete)
70 # Taken from slices.py
72 def create_slice_max_aggregate(api, hrn, nodes):
73 # Get the slice record from geni
75 registries = Registries(api)
76 registry = registries[api.hrn]
77 credential = api.getCredential()
78 records = registry.resolve(credential, hrn)
79 for record in records:
80 if record.get_type() in ['slice']:
81 slice = record.as_dict()
83 raise RecordNotFound(hrn)
85 # Make sure slice exists at plc, if it doesnt add it
86 slicename = hrn_to_pl_slicename(hrn)
87 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
89 parts = slicename.split("_")
91 # if site doesnt exist add it
92 sites = api.plshell.GetSites(api.plauth, [login_base])
94 authority = get_authority(hrn)
95 site_records = registry.resolve(credential, authority)
98 raise RecordNotFound(authority)
99 site_record = site_records[0]
100 site = site_record.as_dict()
104 site_id = api.plshell.AddSite(api.plauth, site)
109 slice_keys = ['name', 'url', 'description']
110 for key in slice_keys:
111 if key in slice and slice[key]:
112 slice_fields[key] = slice[key]
113 api.plshell.AddSlice(api.plauth, slice_fields)
115 slice['node_ids'] = 0
119 # get the list of valid slice users from the registry and make
120 # they are added to the slice
121 researchers = record.get('researcher', [])
122 for researcher in researchers:
124 person_records = registry.resolve(credential, researcher)
125 for record in person_records:
126 if record.get_type() in ['user']:
127 person_record = record
128 if not person_record:
130 person_dict = person_record.as_dict()
131 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']],
132 ['person_id', 'key_ids'])
134 # Create the person record
136 person_id=api.plshell.AddPerson(api.plauth, person_dict)
138 # The line below enables the user account on the remote aggregate
139 # soon after it is created.
140 # without this the user key is not transfered to the slice
141 # (as GetSlivers returns key of only enabled users),
142 # which prevents the user from login to the slice.
143 # We may do additional checks before enabling the user.
145 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
148 key_ids = persons[0]['key_ids']
150 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'],
153 # Get this users local keys
154 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
155 keys = [key['key'] for key in keylist]
157 # add keys that arent already there
158 for personkey in person_dict['keys']:
159 if personkey not in keys:
160 key = {'key_type': 'ssh', 'key': personkey}
161 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
163 # find out where this slice is currently running
164 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'],
166 hostnames = [node['hostname'] for node in nodelist]
168 # remove nodes not in rspec
169 deleted_nodes = list(set(hostnames).difference(nodes))
170 # add nodes from rspec
171 added_nodes = list(set(nodes).difference(hostnames))
173 api.plshell.AddSliceToNodes(api.plauth, slicename, added_nodes)
174 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, deleted_nodes)
181 # plc.princeton.sapan vlan23,vlan45
183 allocations = read_alloc_dict()
185 current_allocations = allocations[hrn]
187 current_allocations = collapse_alloc_dict(allocations)
189 return (allocations_to_rspec_dict(current_allocations))
192 def create_slice(api, hrn, rspec):
193 # Check if everything in rspec is either allocated by hrn
194 # or not allocated at all.
198 allocations = read_alloc_dict()
199 requested_allocations = rspec_to_allocations (rspec)
200 current_allocations = collapse_alloc_dict(allocations)
202 current_hrn_allocations=allocations[hrn]
204 current_hrn_allocations=[]
206 # Check request against current allocations
207 for a in requested_allocations:
208 if (a not in current_hrn_allocations and a in current_allocations):
212 # Allocations to delete
213 allocations_to_delete = []
214 for a in current_hrn_allocations:
215 if (a not in requested_allocations):
216 allocations_to_delete.extend([a])
218 # Ok, let's do our thing
219 alloc_nodes(api, hrn, requested_allocations, allocations_to_delete)
220 alloc_links(api, hrn, requested_allocations, allocations_to_delete)
221 allocations[hrn] = requested_allocations
222 commit_alloc_dict(allocations)
228 def rspec_to_allocations(rspec):
231 linkspecs = rspec['rspec']['request'][0]['netspec'][0]['linkspec']
233 links.extend([l['name'].replace('tns:','')])
242 rspec_xml = open(sys.argv[1]).read()
243 r.parseString(rspec_xml)
245 create_slice(None,'plc',rspec)
247 if __name__ == "__main__":