remove stale records from sfa db
authorTony Mack <tmack@cs.princeton.edu>
Tue, 15 Sep 2009 19:09:01 +0000 (19:09 +0000)
committerTony Mack <tmack@cs.princeton.edu>
Tue, 15 Sep 2009 19:09:01 +0000 (19:09 +0000)
sfa/plc/sfa-import-plc.py
sfa/plc/sfaImport.py

index 9b69749..ae6d4d6 100755 (executable)
@@ -115,10 +115,9 @@ def main():
         # import if hrn is not in list of existing hrns or if the hrn exists
         # but its not a site record
         if site_hrn not in existing_hrns or \
-           (site_hrn, 'authority') not in existing_records:   
+           (site_hrn, 'authority') not in existing_records:
             sfaImporter.import_site(import_auth, site)
-
-        
+             
         # import node records
         for node_id in site['node_ids']:
             if node_id not in nodes_dict:
@@ -148,6 +147,53 @@ def main():
             if hrn not in existing_hrns or \
                (hrn, 'user') not in existing_records:
                 sfaImporter.import_person(site_hrn, person)
+
+        
+        # remove any record in existing_hrns that does not 
+        # have a plc record
+        site_existing_records_only = lambda (r_hrn, r_type): r_hrn.startswith(site_hrn)
+        site_existing_records = filter(site_existing_records_only, existing_records.keys())
+        for (record_hrn, type) in site_existing_records:
+            found = False
+            if type == 'user':
+                for person in persons_dict.values():
+                    tmp_hrn = email_to_hrn(site_hrn, person['email'])
+                    if record_hrn == tmp_hrn:
+                        found = True
+
+            elif type == 'node':
+                for node in nodes_dict.values():
+                    tmp_hrn = hostname_to_hrn(import_auth, site['login_base'], node['hostname'])
+                    if record_hrn == tmp_hrn:
+                        found = True
+            elif type == 'slice':
+                for slice in slices_dict.values():
+                    tmp_hrn = slicename_to_hrn(import_auth, slice['name'])
+                    if record_hrn == tmp_hrn:
+                        found = True
+            else:
+                continue
+            if not found:
+                trace("Import: Removing %s %s" % (type, record_hrn)) 
+                record_object = existing_records[(record_hrn, type)]
+                sfaImporter.delete_record(record_hrn, type)
+    
+    # remove stale site_records    
+    site_records_only = lambda(r_hrn, r_type): r_type == 'authority' and r_hrn != import_auth
+    site_records = filter(site_records_only, existing_records.keys())
+    for (record_hrn, type) in site_records:
+        found = False
+        for site in sites:
+            site_hrn = import_auth + "." + site['login_base']
+            if site_hrn == record_hrn:
+                found = True
+        if not found:
+            trace("Import: Removing %s %s" % (type,  record_hrn))
+            record_object = existing_records[(record_hrn, type)]
+            sfaImporter.delete_record(record_hrn, type) 
+                                   
+                    
         
 if __name__ == "__main__":
     main()
index da071e2..7925521 100644 (file)
@@ -126,7 +126,6 @@ class sfaImport:
             pkey = Keypair(create=True)
 
         # create the gid
-        print "*", hrn
         person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
         table = GeniTable()
         person_record = GeniRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
@@ -229,57 +228,10 @@ class sfaImport:
             auth_record['record_id'] = existing_record['record_id']
             table.update(auth_record)
 
-        #if 'person_ids' in site:
-        #    for person_id in site['person_ids']:
-        #        persons = shell.GetPersons(plc_auth, [person_id])
-        #        if persons:
-        #            try:
-        #                self.import_person(hrn, persons[0])
-        #            except Exception, e:
-        #                trace("Failed to import: %s (%s)" % (persons[0], e))
-        #if 'slice_ids' in site:
-        #    for slice_id in site['slice_ids']:
-        #        slices = shell.GetSlices(plc_auth, [slice_id])
-        #        if slices:
-        #            try:
-        #                self.import_slice(hrn, slices[0])
-        #            except Exception, e:
-        #                trace("Failed to import: %s (%s)" % (slices[0], e))
-        #if 'node_ids' in site:
-        #    for node_id in site['node_ids']:
-        #        nodes = shell.GetNodes(plc_auth, [node_id])
-        #        if nodes:
-        #            try:
-        #                self.import_node(hrn, nodes[0])
-        #            except Exception, e:
-        #                trace("Failed to import: %s (%s)" % (nodes[0], e))     
-
-    def delete_record(self, parent_hrn, object, type):
-        # get the hrn
-        table = GeniTable()
-        hrn = None
-        if type in ['slice'] and 'name' in object and object['name']:
-            slice_name = object['name'].split("_")[0]
-            hrn = parent_hrn + "." + slice_name
-        elif type in ['user'] and 'email' in object and object['email']:
-            person_name = object['email'].split('@')[0]
-            hrn = parent_hrn + "." + person_name
-        elif type in ['node'] and 'hostname' in object and object['hostname']:
-            node_name =  object['hostname'].replace('.','_')  
-            hrn = parent_hrn + "." + node_name
-        elif type in ['site'] and 'login_base' in object and object['login_base']:
-            site_name = object['login_base']
-            hrn = parent_hrn
-            parent_hrn = get_authority(hrn)
-            type = "authority"
-            # delete all records whos authority is this site
-            records = table.find({'authority': hrn})
-            for record in records:
-                table.remove(record)
-        else:
-            return
-        
+
+    def delete_record(self, hrn, type):
         # delete the record
+        table = GeniTable()
         record_list = table.find({'type': type, 'hrn': hrn})
         for record in record_list:
             table.remove(record)