Commented out the caching attributes in aggregate_slab
[sfa.git] / sfa / senslab / senslab-import.py
1 #!/usr/bin/python
2 #
3 ##
4 # Import PLC records into the SFA database. It is indended that this tool be
5 # run once to create SFA records that reflect the current state of the
6 # planetlab database.
7 #
8 # The import tool assumes that the existing PLC hierarchy should all be part
9 # of "planetlab.us" (see the root_auth and level1_auth variables below).
10 #
11 # Public keys are extracted from the users' SSH keys automatically and used to
12 # create GIDs. This is relatively experimental as a custom tool had to be
13 # written to perform conversion from SSH to OpenSSL format. It only supports
14 # RSA keys at this time, not DSA keys.
15 ##
16
17 import getopt
18 import sys
19 import tempfile
20
21
22 from sfa.util.record import *
23 from sfa.util.table import SfaTable
24 from sfa.util.xrn import get_leaf, get_authority
25 from sfa.util.plxrn import hostname_to_hrn, slicename_to_hrn, email_to_hrn, hrn_to_pl_slicename
26 from sfa.util.config import Config
27 from sfa.trust.certificate import convert_public_key, Keypair
28 from sfa.trust.trustedroots import *
29 from sfa.trust.hierarchy import *
30 from sfa.util.xrn import Xrn
31 from sfa.trust.gid import create_uuid
32
33
34 from sfa.senslab.SenslabImportUsers import *
35 from sfa.senslab.OARrestapi import *
36
37 from sfa.senslab.SenslabImport import SenslabImport
38
39
40
41
42
43 oarserver = {}
44 oarserver['ip'] = '10.127.255.254'
45 oarserver['port'] = 80
46 oarserver['uri'] = '/oarapi/resources/full.json'
47
48
49 def process_options():
50
51    (options, args) = getopt.getopt(sys.argv[1:], '', [])
52    for opt in options:
53        name = opt[0]
54        val = opt[1]
55
56
57 def load_keys(filename):
58     keys = {}
59     tmp_dict = {}
60     try:
61         execfile(filename, tmp_dict)
62         if 'keys' in tmp_dict:
63             keys = tmp_dict['keys']
64         return keys
65     except:
66         return keys
67
68 def save_keys(filename, keys):
69     f = open(filename, 'w')
70     f.write("keys = %s" % str(keys))
71     f.close()
72
73 def main():
74
75     process_options()
76     config = Config()
77     if not config.SFA_REGISTRY_ENABLED:
78         sys.exit(0)
79     root_auth = config.SFA_REGISTRY_ROOT_AUTH
80     interface_hrn = config.SFA_INTERFACE_HRN
81     print interface_hrn, root_auth
82     keys_filename = config.config_path + os.sep + 'person_keys.py' 
83
84     SenslabImporter = SenslabImport()
85     SenslabUsers = SenslabImportUsers()
86     
87     OARImporter = OARapi()
88     #print '\r\n =====OAR Importer list===== '
89     #for node in OARImporter.OARserver.GetNodes().keys():
90         #print node, OARImporter.OARserver.GetNodes[node]
91
92
93     #if config.SFA_API_DEBUG: SenslabImporter.logger.setLevelDebug()
94     #shell = sfaImporter.shell
95     #plc_auth = sfaImporter.plc_auth 
96     #print plc_auth 
97
98     # initialize registry db table
99     table = SfaTable()
100     if not table.exists():
101         table.create()
102
103     # create root authority 
104     SenslabImporter.create_top_level_auth_records(root_auth)
105     if not root_auth == interface_hrn:
106        SenslabImporter.create_top_level_auth_records(interface_hrn)
107     
108     # create s user record for the slice manager
109     SenslabImporter.create_sm_client_record()
110        
111     # create interface records ADDED 12 JUILLET 2011 
112     SenslabImporter.logger.info("Import: creating interface records")
113     SenslabImporter.create_interface_records()
114
115     # add local root authority's cert  to trusted list ADDED 12 JUILLET 2011 
116     SenslabImporter.logger.info("Import: adding " + interface_hrn + " to trusted list")
117     authority = SenslabImporter.AuthHierarchy.get_auth_info(interface_hrn)
118     SenslabImporter.TrustedRoots.add_gid(authority.get_gid_object())
119     
120     
121     print "\r\n \r\n create dict of all existing sfa records"
122  # create dict of all existing sfa records
123     existing_records = {}
124     existing_hrns = []
125     key_ids = []
126     person_keys = {} 
127     results = table.find()
128     for result in results:
129         existing_records[(result['hrn'], result['type'])] = result
130         existing_hrns.append(result['hrn'])   
131      
132
133
134
135     #Get Senslab nodes 
136     nodes_dict  = OARImporter.GetNodes()
137     print "\r\n NODES8DICT ",nodes_dict
138     
139     persons_list = SenslabUsers.GetPersons()
140     print "\r\n PERSONS_LIST ",persons_list
141
142     keys_list = SenslabUsers.GetKeys()
143     print "\r\n KEYSS_LIST ",keys_list
144     
145     slices_list = SenslabUsers.GetSlices()
146     print "\r\n SLICES_LIST ",slices_list
147     
148         # Get all Senslab sites
149     sites_dict  = OARImporter.GetSites()
150     print "\r\n sSITES_DICT" , sites_dict
151     
152  # start importing 
153     for site in sites_dict:
154         site_hrn = interface_hrn + "." + site['login_base']
155         #sfa_logger().info("Importing site: %s" % site_hrn)
156         print "HRN %s %s site existing in hrn ? %s" %( site['login_base'],site_hrn, site_hrn in existing_hrns)
157         # import if hrn is not in list of existing hrns or if the hrn exists
158         # but its not a site record
159         if site_hrn not in existing_hrns or \
160             (site_hrn, 'authority') not in existing_records:
161              print "SITE HRN UNKNOWN" , site, site_hrn
162              site_hrn = SenslabImporter.import_site(interface_hrn, site)
163              
164         print "\r\n \r\n ===========IMPORT NODE_RECORDS ==========\r\n site %s \r\n \t nodes_dict %s" %(site,nodes_dict)                 
165         # import node records
166         for node_id in site['node_ids']:
167                 #for[node['node_id'] for node in nodes_dict]:
168                         #print '\r\n \t **NODE_ID %s node %s '%( node_id, node)         
169                         #continue 
170                 for node in nodes_dict:
171                         if node_id is node['node_id']:  
172                                 #node = nodes_dict[node_id]
173                                 print '\r\n \t NODE_ID %s node %s '%( node_id, node)
174                                 hrn =  hostname_to_hrn(interface_hrn, site['login_base'], node['hostname'])
175                                 break   
176
177                 if hrn not in existing_hrns or \
178                 (hrn, 'node') not in existing_records:
179                         print "\t\t NODE HRN NOT in existing records!" ,hrn
180                         SenslabImporter.import_node(hrn, node)
181
182    # import persons
183         for person in persons_list:
184                 hrn = email_to_hrn(site_hrn, person['email'])
185                 print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^PERSON hrn %s person %s site hrn %s" %(hrn,person,site_hrn)    
186                 SenslabImporter.import_person( site_hrn, person,keys_list)
187                 
188 # import slices
189         for slice_id in site['slice_ids']:
190                 print >>sys.stderr, "\r\n\r\n \t ^^^^^^^\\\\\\\\\\\\\\\^^^^^^ slice_id  %s  " %(slice_id)               
191                 for sl in slices_list:
192                         if slice_id is sl['slice_id']:
193                                 #hrn = slicename_to_hrn(interface_hrn, sl['name'])
194                                 hrn = email_to_hrn(site_hrn, sl['name'])
195                                 print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^SLICE ID hrn %s  site_hrn %s" %(hrn,site_hrn)                                 
196                                 if hrn not in existing_hrns or \
197                                 (hrn, 'slice') not in existing_records:
198                                         SenslabImporter.import_slice(site_hrn, sl)      
199
200                                         
201     # remove stale records    
202     system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
203     for (record_hrn, type) in existing_records.keys():
204         if record_hrn in system_records:
205             continue
206         
207         record = existing_records[(record_hrn, type)]
208         if record['peer_authority']:
209             continue                                    
210  ## remove stale records    
211     #for (record_hrn, type) in existing_records.keys():
212         #record = existing_records[(record_hrn, type)]
213         #print" \r\n ****record hrn %s \t\t TYPE %s " %(record_hrn,type)
214         ## if this is the interface name dont do anything
215         #if record_hrn == interface_hrn or \
216            #record_hrn == root_auth or \
217            #record['peer_authority']:
218             #continue
219
220
221         found = False
222         
223         if type == 'authority':    
224             for site in sites_dict:
225                 print "\t type : authority : ", site
226                 site_hrn = interface_hrn + "." + site['login_base']
227                 if site_hrn == record_hrn and site['site_id'] == record['pointer']:
228                     found = True
229                     print "\t \t Found :", found
230                     break
231  
232         elif type == 'node':
233             login_base = get_leaf(get_authority(record_hrn))
234
235             nodename = Xrn.unescape(get_leaf(record_hrn))
236             print "type: node : login_base %s nodename %s" %(login_base, nodename)
237             if login_base in sites_dict:
238                 site = sites_dict[login_base]
239                 print "\t type node : login base %s site %s" %(login_base, site)
240                 for node in nodes_dict.values():
241                     tmp_nodename = node['hostname']
242                     if tmp_nodename == nodename and \
243                        node['site_id'] == site['site_id'] and \
244                        node['node_id'] == record['pointer']:
245                         found = True
246                         print "\t Nodename: %s site id %s node id %s record %s" %( nodename,  node['site_id'], node['node_id'],record['pointer'])
247                         break  
248         else:
249             continue 
250         
251         if not found:
252             record_object = existing_records[(record_hrn, type)]
253             print "\t\t NOT FOUND ! "
254             SenslabImporter.delete_record(record_hrn, type) 
255                                    
256     # save pub keys
257     SenslabImporter.logger.info('Import: saving current pub keys')
258     save_keys(keys_filename, person_keys)                
259
260  
261   
262 if __name__ == "__main__":
263     main()