(no commit message)
[sfa.git] / sfa / rspecs / aggregates / rspec_manager_max.py
1 from sfa.util.rspec import Rspec
2 import sys
3 import pdb
4
5 SFA_MAX_CONF_FILE = '/etc/sfa/max_allocations'
6
7 # Topology 
8
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')
15             }
16
17 def link_endpoints(links):
18     nodes=[]
19     for l in links:
20         nodes.extend(topology[l])
21     return l
22
23
24 def lock_state_file():
25     # Noop for demo
26     return True
27
28 def unlock_state_file():
29     return True
30     # Noop for demo
31
32 def read_alloc_dict():
33     alloc_dict={}
34     rows = open(SFA_MAX_CONF_FILE).read().split('\n')
35     for r in rows:
36         columns = r.split(' ')
37         if (len(columns)>2):
38             hrn = columns[0]
39             allocs = columns[1].split(',')
40             alloc_dict[hrn]=allocs
41     return alloc_dict
42
43 def commit_alloc_dict(d):
44     f = open(SFA_MAX_CONF_FILE, 'w')
45     for hrn in d.keys():
46         columns = d[hrn]
47         row = hrn+' '+','.join(columns)+'\n'
48         f.write(row)
49     f.close()
50
51 def collapse_alloc_dict(d):
52     ret = []
53     for k in d.keys():
54         ret.extend(d[k])
55     return ret
56
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
61         slice = {}
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()
69         if not slice:
70             raise RecordNotFound(hrn)   
71
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'])
75         if not slices:
76             parts = slicename.split("_")
77             login_base = parts[0]
78             # if site doesnt exist add it
79             sites = api.plshell.GetSites(api.plauth, [login_base])
80             if not sites:
81                 authority = get_authority(hrn)
82                 site_records = registry.resolve(credential, authority)
83                 site_record = {}
84                 if not site_records:
85                     raise RecordNotFound(authority)
86                 site_record = site_records[0]
87                 site = site_record.as_dict()
88                 
89                  # add the site
90                 site.pop('site_id')
91                 site_id = api.plshell.AddSite(api.plauth, site)
92             else:
93                 site = sites[0]
94             
95             slice_fields = {}
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)
101             slice = slice_fields
102             slice['node_ids'] = 0
103         else:
104             slice = slices[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:
109             person_record = {}
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:
115                 pass
116             person_dict = person_record.as_dict()
117             persons = api.plshell.GetPersons(api.plauth, [person_dict['email']], ['person_id', 'key_ids'])
118
119             # Create the person record 
120             if not persons:
121                 person_id=api.plshell.AddPerson(api.plauth, person_dict)
122
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.
126
127                 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
128                 key_ids = []
129             else:
130                 key_ids = persons[0]['key_ids']
131
132             api.plshell.AddPersonToSlice(api.plauth, person_dict['email'], slicename)        
133
134             # Get this users local keys
135             keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
136             keys = [key['key'] for key in keylist]
137
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)
143
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]
147
148         api.plshell.AddSliceToNodes(self.api.plauth, slicename, added_nodes) 
149         api.plshell.DeleteSliceFromNodes(self.api.plauth, slicename, deleted_nodes)
150
151         return 1
152
153 def alloc_nodes(hrn, links_to_add, links_to_delete):
154     
155     nodes_to_add = link_endpoints(links_to_add)
156     nodes_to_delete = link_endpoints(links_to_delete)
157
158     #bootstrap_slice(api, hrn, nodes_to_add, nodes_to_delete)
159
160     for r in requested_allocations:
161         print "Requesting "+r
162
163 def get_rspec(hrn):
164     # Eg. config line:
165     # plc.princeton.sapan vlan23,vlan45
166
167     allocations = read_alloc_dict()
168     if (hrn):
169         current_allocations = allocations[hrn]
170     else:
171         current_allocations = collapse_alloc_dict(allocations)
172
173     return (allocations_to_rspec_dict(current_allocations))
174
175
176 def create_slice(api, hrn, rspec):
177     # Check if everything in rspec is either allocated by hrn
178     # or not allocated at all.
179
180     lock_state_file()
181
182     allocations = read_alloc_dict()
183     requested_allocations = rspec_to_allocations (rspec)
184     current_allocations = collapse_alloc_dict(allocations)
185     try:
186         current_hrn_allocations=allocations[hrn]
187     except KeyError:
188         current_hrn_allocations=[]
189
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):
193             return False
194     # Request OK
195
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])
201
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)
207
208     unlock_state_file()
209
210     return True
211
212 def rspec_to_allocations(rspec):
213     links = []
214     try:
215         linkspecs = rspec['rspec']['request'][0]['netspec'][0]['linkspec']
216         for l in linkspecs:
217             links.extend([l['name'].replace('tns:','')])
218         
219     except KeyError:
220         # Bad Rspec
221         pass
222     return links
223
224 def main():
225     r = Rspec()
226     rspec_xml = open(sys.argv[1]).read()
227     r.parseString(rspec_xml)
228     rspec = r.toDict()
229     create_slice(None,'plc',rspec)
230     
231 if __name__ == "__main__":
232     main()