Corrected calls to SlabDriver in slab-import.
[sfa.git] / sfa / senslab / slab-import.py
1 ###########################################################################
2 #    Copyright (C) 2011 by root                                      
3 #    <root@FlabFedora2>                                                             
4 #
5 # Copyright: See COPYING file that comes with this distribution
6 #
7 ###########################################################################
8 import sys
9 from sfa.senslab.OARrestapi import OARapi
10 from sfa.senslab.LDAPapi import LDAPapi
11 from sfa.senslab.slabdriver import SlabDriver
12 from sfa.util.config import Config
13 from sfa.util.xrn import hrn_to_urn, get_authority
14 from sfa.util.table import SfaTable
15 from sfa.trust.hierarchy import Hierarchy
16
17 AuthHierarchy = Hierarchy()
18 table = SfaTable()
19 if not table.exists():
20     table.create()
21     
22 def create_top_level_auth_records(hrn):
23     """
24     Create top level records (includes root and sub authorities (local/remote)
25     """
26     print>>sys.stderr, "\r\n =========SenslabImport create_top_level_auth_records\r\n"
27     urn = hrn_to_urn(hrn, 'authority')
28     # make sure parent exists
29     parent_hrn = get_authority(hrn)
30     if not parent_hrn:
31         parent_hrn = hrn
32     if not parent_hrn == hrn:
33         create_top_level_auth_records(parent_hrn)
34         
35     
36     # create the authority if it doesnt already exist 
37     if not AuthHierarchy.auth_exists(urn):
38         AuthHierarchy.create_auth(urn)
39     
40     # create the db record if it doesnt already exist    
41     auth_info = AuthHierarchy.get_auth_info(hrn)
42    
43     auth_record = table.find({'type': 'authority', 'hrn': hrn})
44
45     if not auth_record:
46         auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
47         auth_record['authority'] = get_authority(auth_record['hrn'])
48         table.insert(auth_record)
49         print>>sys.stderr, "\r\n ========= \t\t SenslabImport NO AUTH RECORD \r\n" ,auth_record['authority']
50         
51     
52 def import_node(hrn, node):
53
54     # ASN.1 will have problems with hrn's longer than 64 characters
55     if len(hrn) > 64:
56         hrn = hrn[:64]
57
58     node_record = table.find({'type': 'node', 'hrn': hrn})
59     pkey = Keypair(create=True)
60     urn = hrn_to_urn(hrn, 'node')
61     node_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
62     node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
63     node_record['authority'] = get_authority(node_record['hrn'])
64     existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
65     if not existing_records:
66         table.insert(node_record)
67     else:
68         existing_record = existing_records[0]
69         node_record['record_id'] = existing_record['record_id']
70         table.update(node_record)
71
72 # person is already a sfa record 
73 def import_person(person):       
74     existing_records = table.find({'hrn': person['hrn'], 'type': 'user'})
75     if not existing_records:
76         table.insert(person)
77     else:
78         existing_record = existing_records[0]
79         person['record_id'] = existing_record['record_id']
80         table.update(person)      
81         
82         
83         
84 def delete_record( hrn, type):
85     # delete the record
86     record_list = table.find({'type': type, 'hrn': hrn})
87     for record in record_list:
88         table.remove(record)
89                 
90                 
91 def main():
92
93     config = Config()
94     if not config.SFA_REGISTRY_ENABLED:
95         sys.exit(0)
96     root_auth = config.SFA_REGISTRY_ROOT_AUTH
97     interface_hrn = config.SFA_INTERFACE_HRN
98     print interface_hrn, root_auth
99     
100      # initialize registry db table
101     #table = SfaTable()
102     #if not table.exists():
103         #table.create()
104
105     # create root authority 
106     create_top_level_auth_records(root_auth)
107     
108     # create s user record for the slice manager
109     #Do we need this?
110     #SenslabImporter.create_sm_client_record()
111     
112     # create interface records 
113     #Do we need this?
114     #SenslabImporter.logger.info("Import: creating interface records")
115     #SenslabImporter.create_interface_records()
116      # create dict of all existing sfa records
117      
118     existing_records = {}
119     existing_hrns = []
120     key_ids = []
121     person_keys = {} 
122     results = table.find()
123     for result in results:
124         existing_records[(result['hrn'], result['type'])] = result
125         existing_hrns.append(result['hrn'])   
126         
127     #Get Senslab nodes 
128    
129     Driver = SlabDriver()
130     nodes_dict  = Driver.GetNodes()
131     print "\r\n NODES8DICT ",nodes_dict
132     
133     ldap_person_list = Driver.GetPersons()
134     print "\r\n PERSONS_LIST ",ldap_person_list
135
136    
137     
138     #slices_list = SenslabUsers.GetSlices()
139     #print "\r\n SLICES_LIST ",slices_list
140     
141         # Get all Senslab sites
142     #sites_dict  = OARImporter.GetSites()
143     #print "\r\n sSITES_DICT" , sites_dict
144     
145      # start importing 
146     #for site in sites_dict:
147         #site_hrn = interface_hrn + "." + site['login_base']
148         ##sfa_logger().info("Importing site: %s" % site_hrn)
149         #print "HRN %s %s site existing in hrn ? %s" %( site['login_base'],site_hrn, site_hrn in existing_hrns)
150         ## import if hrn is not in list of existing hrns or if the hrn exists
151         ## but its not a site record
152         #if site_hrn not in existing_hrns or \
153             #(site_hrn, 'authority') not in existing_records:
154              #print "SITE HRN UNKNOWN" , site, site_hrn
155              #site_hrn = SenslabImporter.import_site(interface_hrn, site)
156              
157     print "\r\n \r\n ===========IMPORT NODE_RECORDS ==========\r\n site %s \r\n \t nodes_dict %s" %(site,nodes_dict)             
158         # import node records
159         #for node_id in site['node_ids']:
160                 #for[node['node_id'] for node in nodes_dict]:
161                         #print '\r\n \t **NODE_ID %s node %s '%( node_id, node)         
162                         #continue 
163     for node in nodes_dict:
164         print '\r\n \t NODE_ID %s node %s '%( node_id, node)
165         hrn =  hostname_to_hrn(interface_hrn, root_auth, node['hostname'])
166         if hrn not in existing_hrns or \
167         (hrn, 'node') not in existing_records:
168             print "\t\t NODE HRN NOT in existing records!" ,hrn
169             import_node(hrn, node)
170
171    # import persons
172     for person in ldap_person_list:
173         print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^PERSON hrn %s person %s site hrn %s" %(hrn,person)    
174         if person['hrn'] not in existing_hrns or \
175             (person['hrn'], 'user') not in existing_records :
176             import_person( person)      
177 # import slices
178         #for slice_id in site['slice_ids']:
179                 #print >>sys.stderr, "\r\n\r\n \t ^^^^^^^\\\\\\\\\\\\\\\^^^^^^ slice_id  %s  " %(slice_id)              
180                 #for sl in slices_list:
181                         #if slice_id is sl['slice_id']:
182                                 ##hrn = slicename_to_hrn(interface_hrn, sl['name'])
183                                 #hrn = email_to_hrn(site_hrn, sl['name'])
184                                 #print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^SLICE ID hrn %s  site_hrn %s" %(hrn,site_hrn)                                
185                                 #if hrn not in existing_hrns or \
186                                 #(hrn, 'slice') not in existing_records:
187                                         #SenslabImporter.import_slice(site_hrn, sl)     
188
189                                         
190     # remove stale records    
191     system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
192     for (record_hrn, type) in existing_records.keys():
193         if record_hrn in system_records:
194             continue
195         
196         record = existing_records[(record_hrn, type)]
197         if record['peer_authority']:
198             continue                                    
199
200
201
202         found = False
203         
204         if type == 'authority':    
205             #for site in sites_dict:
206                 #print "\t type : authority : ", site
207                 #site_hrn = interface_hrn + "." + site['login_base']
208                 #if site_hrn == record_hrn and site['site_id'] == record['pointer']:
209             found = True
210             print "\t \t Found :", found
211             break
212                 
213         elif type == 'user':
214             for person in persons:
215                 if person['hrn'] == record_hrn:
216                     found = True
217                 break
218             
219         elif type == 'node':
220             login_base = get_leaf(get_authority(record_hrn))
221             nodename = Xrn.unescape(get_leaf(record_hrn))
222             print "type: node :  nodename %s" %(nodename)
223             for node in nodes_dict.values():
224                 if node['hostname'] == nodename :
225                     found = True
226                     break 
227                     
228         else:
229             continue 
230         
231         if not found:
232             record_object = existing_records[(record_hrn, type)]
233             print "\t\t NOT FOUND ! "
234             delete_record(record_hrn, type) 
235     
236 if __name__ == "__main__":
237     main()