Cleaning up end for the slab-import script.
[sfa.git] / sfa / senslab / slab-import.py
index 351ecd5..57cfd30 100644 (file)
@@ -10,9 +10,13 @@ from sfa.senslab.OARrestapi import OARapi
 from sfa.senslab.LDAPapi import LDAPapi
 from sfa.senslab.slabdriver import SlabDriver
 from sfa.util.config import Config
-from sfa.util.xrn import hrn_to_urn, get_authority
+from sfa.util.xrn import hrn_to_urn, get_authority,Xrn,get_leaf
 from sfa.util.table import SfaTable
+from sfa.util.record import SfaRecord
 from sfa.trust.hierarchy import Hierarchy
+from sfa.trust.certificate import Keypair
+from sfa.trust.gid import create_uuid
+
 
 AuthHierarchy = Hierarchy()
 table = SfaTable()
@@ -45,6 +49,7 @@ def create_top_level_auth_records(hrn):
     if not auth_record:
         auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
         auth_record['authority'] = get_authority(auth_record['hrn'])
+        print sys.stderr, " \r\n \t slab-import : auth record %s inserted" %(auth_record['hrn'])
         table.insert(auth_record)
         print>>sys.stderr, "\r\n ========= \t\t SenslabImport NO AUTH RECORD \r\n" ,auth_record['authority']
         
@@ -63,6 +68,7 @@ def import_node(hrn, node):
     node_record['authority'] = get_authority(node_record['hrn'])
     existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
     if not existing_records:
+        print>>sys.stderr, " \r\n \t slab-import : node record %s inserted" %(node_record['hrn'])
         table.insert(node_record)
     else:
         existing_record = existing_records[0]
@@ -73,6 +79,7 @@ def import_node(hrn, node):
 def import_person(person):       
     existing_records = table.find({'hrn': person['hrn'], 'type': 'user'})
     if not existing_records:
+        print>>sys.stderr, " \r\n \t slab-import : person record %s inserted" %(person['hrn'])
         table.insert(person)
     else:
         existing_record = existing_records[0]
@@ -85,9 +92,16 @@ def delete_record( hrn, type):
     # delete the record
     record_list = table.find({'type': type, 'hrn': hrn})
     for record in record_list:
+        print>>sys.stderr, " \r\n \t slab-import : record %s deleted" %(record['hrn'])
         table.remove(record)
                 
-                
+def hostname_to_hrn(root_auth,hostname):
+    # keep only the first part of the DNS name
+    #hrn='.'.join( [auth,hostname.split(".")[0] ] )
+    # escape the '.' in the hostname
+    hrn='.'.join( [root_auth,Xrn.escape(hostname)] )
+    return hrn_to_urn(hrn,'node')
+    
 def main():
 
     config = Config()
@@ -128,13 +142,11 @@ def main():
    
     Driver = SlabDriver(config)
     nodes_dict  = Driver.GetNodes()
-    print "\r\n NODES8DICT ",nodes_dict
+    #print "\r\n NODES8DICT ",nodes_dict
     
     ldap_person_list = Driver.GetPersons()
-    print "\r\n PERSONS_LIST ",ldap_person_list
-
-   
     
+
     #slices_list = SenslabUsers.GetSlices()
     #print "\r\n SLICES_LIST ",slices_list
     
@@ -155,15 +167,10 @@ def main():
              #site_hrn = SenslabImporter.import_site(interface_hrn, site)
    
         # import node records
-       #for node_id in site['node_ids']:
-               #for[node['node_id'] for node in nodes_dict]:
-                       #print '\r\n \t **NODE_ID %s node %s '%( node_id, node)         
-                       #continue 
     for node in nodes_dict:
-        hrn =  hostname_to_hrn(interface_hrn, root_auth, node['hostname'])
+        hrn =  hostname_to_hrn( root_auth, node['hostname'])
         if hrn not in existing_hrns or \
         (hrn, 'node') not in existing_records:
-            print "\t\t NODE HRN NOT in existing records!" ,hrn
             import_node(hrn, node)
 
    # import persons
@@ -186,6 +193,7 @@ def main():
                                        
     # remove stale records    
     system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
+
     for (record_hrn, type) in existing_records.keys():
         if record_hrn in system_records:
             continue
@@ -208,16 +216,15 @@ def main():
             break
                 
         elif type == 'user':
-            for person in persons:
+            for person in ldap_person_list:
                 if person['hrn'] == record_hrn:
                     found = True
-                break
+                    break
             
         elif type == 'node':
             login_base = get_leaf(get_authority(record_hrn))
             nodename = Xrn.unescape(get_leaf(record_hrn))
-            print "type: node :  nodename %s" %(nodename)
-            for node in nodes_dict.values():
+            for node in nodes_dict:
                 if node['hostname'] == nodename :
                     found = True
                     break 
@@ -227,8 +234,8 @@ def main():
         
         if not found:
             record_object = existing_records[(record_hrn, type)]
-            print "\t\t NOT FOUND ! "
+            print "\t\t  NOT FOUND ! ", record_hrn
             delete_record(record_hrn, type) 
     
 if __name__ == "__main__":
-    main()    
\ No newline at end of file
+    main()