3 from sfa.util.rspec import Rspec
6 from sfa.util.misc import *
7 from sfa.util.rspec import *
8 from sfa.util.specdict import *
9 from sfa.util.faults import *
10 from sfa.util.storage import *
11 from sfa.util.policy import Policy
12 from sfa.util.debug import log
13 from sfa.server.aggregate import Aggregates
14 from sfa.server.registry import Registries
15 SFA_MAX_CONF_FILE = '/etc/sfa/max_allocations'
19 topology = {'pl23':('planetlab2.dragon.maxgigapop.net','planetlab3.dragon.maxgigapop.net'),
20 'pl24':('planetlab2.dragon.maxgigapop.net','planetlab4.dragon.maxgigapop.net'),
21 'pl25':('planetlab2.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net'),
22 'pl34':('planetlab3.dragon.maxgigapop.net','planetlab4.dragon.maxgigapop.net'),
23 'pl35':('planetlab3.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net'),
24 'pl45':('planetlab4.dragon.maxgigapop.net','planetlab5.dragon.maxgigapop.net')
27 def link_endpoints(links):
30 nodes.extend(topology[l])
33 def lock_state_file():
37 def unlock_state_file():
41 def read_alloc_dict():
43 rows = open(SFA_MAX_CONF_FILE).read().split('\n')
45 columns = r.split(' ')
48 allocs = columns[1].split(',')
49 alloc_dict[hrn]=allocs
52 def commit_alloc_dict(d):
53 f = open(SFA_MAX_CONF_FILE, 'w')
56 row = hrn+' '+','.join(columns)+'\n'
60 def collapse_alloc_dict(d):
67 def alloc_links(api, links_to_add, links_to_drop, foo):
70 def alloc_nodes(api,hrn, requested_links):
72 requested_nodes = link_endpoints(requested_links)
75 create_slice_max_aggregate(api, hrn, requested_nodes)
77 # Taken from slices.py
79 def create_slice_max_aggregate(api, hrn, nodes):
80 # Get the slice record from geni
82 registries = Registries(api)
83 registry = registries[api.hrn]
84 credential = api.getCredential()
85 records = registry.resolve(credential, hrn)
86 for record in records:
87 if record.get_type() in ['slice']:
88 slice = record.as_dict()
90 raise RecordNotFound(hrn)
92 # Make sure slice exists at plc, if it doesnt add it
93 slicename = hrn_to_pl_slicename(hrn)
94 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
96 parts = slicename.split("_")
98 # if site doesnt exist add it
99 sites = api.plshell.GetSites(api.plauth, [login_base])
101 authority = get_authority(hrn)
102 site_records = registry.resolve(credential, authority)
105 raise RecordNotFound(authority)
106 site_record = site_records[0]
107 site = site_record.as_dict()
111 site_id = api.plshell.AddSite(api.plauth, site)
116 slice_keys = ['name', 'url', 'description']
117 for key in slice_keys:
118 if key in slice and slice[key]:
119 slice_fields[key] = slice[key]
120 api.plshell.AddSlice(api.plauth, slice_fields)
122 slice['node_ids'] = 0
126 # get the list of valid slice users from the registry and make
127 # they are added to the slice
128 researchers = record.get('researcher', [])
129 for researcher in researchers:
131 person_records = registry.resolve(credential, researcher)
132 for record in person_records:
133 if record.get_type() in ['user']:
134 person_record = record
135 if not person_record:
137 person_dict = person_record.as_dict()
138 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']],
139 ['person_id', 'key_ids'])
141 # Create the person record
143 person_id=api.plshell.AddPerson(api.plauth, person_dict)
145 # The line below enables the user account on the remote aggregate
146 # soon after it is created.
147 # without this the user key is not transfered to the slice
148 # (as GetSlivers returns key of only enabled users),
149 # which prevents the user from login to the slice.
150 # We may do additional checks before enabling the user.
152 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
155 key_ids = persons[0]['key_ids']
157 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'],
160 # Get this users local keys
161 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
162 keys = [key['key'] for key in keylist]
164 # add keys that arent already there
165 for personkey in person_dict['keys']:
166 if personkey not in keys:
167 key = {'key_type': 'ssh', 'key': personkey}
168 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
170 # find out where this slice is currently running
171 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'],
173 hostnames = [node['hostname'] for node in nodelist]
175 # remove nodes not in rspec
176 deleted_nodes = list(set(hostnames).difference(nodes))
177 # add nodes from rspec
178 added_nodes = list(set(nodes).difference(hostnames))
180 api.plshell.AddSliceToNodes(api.plauth, slicename, added_nodes)
181 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, deleted_nodes)
188 # plc.princeton.sapan vlan23,vlan45
190 allocations = read_alloc_dict()
192 current_allocations = allocations[hrn]
194 current_allocations = collapse_alloc_dict(allocations)
196 return (allocations_to_rspec_dict(current_allocations))
199 def create_slice(api, hrn, rspec_xml):
200 # Check if everything in rspec is either allocated by hrn
201 # or not allocated at all.
204 r.parseString(rspec_xml)
209 allocations = read_alloc_dict()
210 requested_allocations = rspec_to_allocations (rspec)
211 current_allocations = collapse_alloc_dict(allocations)
213 current_hrn_allocations=allocations[hrn]
215 current_hrn_allocations=[]
217 # Check request against current allocations
218 for a in requested_allocations:
219 if (a not in current_hrn_allocations and a in current_allocations):
223 # Allocations to delete
224 allocations_to_delete = []
225 for a in current_hrn_allocations:
226 if (a not in requested_allocations):
227 allocations_to_delete.extend([a])
229 # Ok, let's do our thing
230 alloc_nodes(api, hrn, requested_allocations)
231 alloc_links(api, hrn, requested_allocations, allocations_to_delete)
232 allocations[hrn] = requested_allocations
233 commit_alloc_dict(allocations)
239 def rspec_to_allocations(rspec):
242 linkspecs = rspec['rspec']['request'][0]['netspec'][0]['linkspec']
244 links.extend([l['name'].replace('tns:','')])
253 rspec_xml = open(sys.argv[1]).read()
254 r.parseString(rspec_xml)
256 create_slice(None,'plc',rspec)
258 if __name__ == "__main__":