should be able to upgrade from any version
[sfa.git] / sfa / importer / sfa-import-plc.py
index 723f473..36617bb 100755 (executable)
@@ -21,13 +21,17 @@ import sys
 from sfa.util.config import Config
 from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn
 from sfa.util.plxrn import hostname_to_hrn, slicename_to_hrn, email_to_hrn, hrn_to_pl_slicename
-from sfa.storage.table import SfaTable
-from sfa.storage.record import SfaRecord
+
 from sfa.trust.gid import create_uuid    
 from sfa.trust.certificate import convert_public_key, Keypair
-from sfa.importer.sfaImport import sfaImport, _cleanup_string
+
 from sfa.plc.plshell import PlShell    
 
+from sfa.storage.alchemy import dbsession
+from sfa.storage.model import RegRecord, RegAuthority, RegUser, RegSlice, RegNode
+
+from sfa.importer.sfaImport import sfaImport, _cleanup_string
+
 def process_options():
 
    (options, args) = getopt.getopt(sys.argv[1:], '', [])
@@ -62,31 +66,29 @@ def _get_site_hrn(interface_hrn, site):
             hrn = ".".join([interface_hrn, "internet2", site['login_base']])
     return hrn
 
+# one would think this code could use a call to DBSchema 
+# however now import s expected to be done after service creation
 def main():
 
     process_options()
     config = Config()
+    sfaImporter = sfaImport()
+    logger=sfaImporter.logger
+    logger.setLevelFromOptVerbose(config.SFA_API_LOGLEVEL)
     if not config.SFA_REGISTRY_ENABLED:
         sys.exit(0)
     root_auth = config.SFA_REGISTRY_ROOT_AUTH
     interface_hrn = config.SFA_INTERFACE_HRN
-    keys_filename = config.config_path + os.sep + 'person_keys.py' 
-    sfaImporter = sfaImport()
-    sfaImporter.create_top_level_records()
-    logger=sfaImporter.logger
-    logger.setLevelFromOptVerbose(config.SFA_API_LOGLEVEL)
     shell = PlShell (config)
+    sfaImporter.create_top_level_records()
     
     # create dict of all existing sfa records
     existing_records = {}
     existing_hrns = []
     key_ids = []
-    person_keys = {} 
-    table = SfaTable()
-    results = table.find()
-    for result in results:
-        existing_records[(result['hrn'], result['type'])] = result
-        existing_hrns.append(result['hrn']) 
+    for record in dbsession.query(RegRecord):
+        existing_records[ (record.hrn, record.type,) ] = record
+        existing_hrns.append(record.hrn) 
             
     # Get all plc sites
     sites = shell.GetSites({'peer_id': None})
@@ -109,7 +111,9 @@ def main():
         keys_dict[key['key_id']] = key['key']
 
     # create a dict of person keys keyed on key_id 
+    keys_filename = config.config_path + os.sep + 'person_keys.py' 
     old_person_keys = load_keys(keys_filename)
+    person_keys = {} 
     for person in persons:
         pubkeys = []
         for key_id in person['key_ids']:
@@ -134,41 +138,46 @@ def main():
         i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
                     'login_base': 'internet2', 'site_id': -1}
         site_hrn = _get_site_hrn(interface_hrn, i2site)
-        logger.info("Importing site: %s" % site_hrn)
         # 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:
-            logger.info("Import: site %s " % site_hrn)
             urn = hrn_to_urn(site_hrn, 'authority')
             if not sfaImporter.AuthHierarchy.auth_exists(urn):
                 sfaImporter.AuthHierarchy.create_auth(urn)
             auth_info = sfaImporter.AuthHierarchy.get_auth_info(urn)
-            auth_record = SfaRecord(hrn=site_hrn, gid=auth_info.get_gid_object(), \
-                                    type="authority", pointer=site['site_id'], 
-                                    authority=get_authority(site_hrn))
-            auth_record.sync(verbose=True)
+            auth_record = RegAuthority()
+            auth_record.type='authority'
+            auth_record.hrn=site_hrn
+            auth_record.gid=auth_info.get_gid_object()
+            auth_record.pointer=site['site_id']
+            auth_record.authority=get_authority(site_hrn)
+            dbsession.add(auth_record)
+            dbsession.commit()
+            logger.info("Import: Imported authority (vini site) %s"%auth_record)
 
     # start importing 
     for site in sites:
         site_hrn = _get_site_hrn(interface_hrn, site)
-        logger.info("Importing site: %s" % site_hrn)
 
         # 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:
             try:
-                logger.info("Import: site %s " % site_hrn)
                 urn = hrn_to_urn(site_hrn, 'authority')
                 if not sfaImporter.AuthHierarchy.auth_exists(urn):
                     sfaImporter.AuthHierarchy.create_auth(urn)
                 auth_info = sfaImporter.AuthHierarchy.get_auth_info(urn)
-                auth_record = SfaRecord(hrn=site_hrn, gid=auth_info.get_gid_object(), \
-                                        type="authority", pointer=site['site_id'], 
-                                        authority=get_authority(site_hrn))
-                logger.info("Import: importing site: %s" % auth_record.summary_string())  
-                auth_record.sync()
+                auth_record = RegAuthority()
+                auth_record.type='authority'
+                auth_record.hrn=site_hrn
+                auth_record.gid=auth_info.get_gid_object()
+                auth_record.pointer=site['site_id']
+                auth_record.authority=get_authority(site_hrn)
+                dbsession.add(auth_record)
+                dbsession.commit()
+                logger.info("Import: imported authority (site) : %s" % auth_record)  
             except:
                 # if the site import fails then there is no point in trying to import the
                 # site's child records (node, slices, persons), so skip them.
@@ -191,9 +200,15 @@ def main():
                     pkey = Keypair(create=True)
                     urn = hrn_to_urn(hrn, 'node')
                     node_gid = sfaImporter.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
-                    node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'], authority=get_authority(hrn))    
-                    logger.info("Import: importing node: %s" % node_record.summary_string())  
-                    node_record.sync()
+                    node_record = RegNode ()
+                    node_record.type='node'
+                    node_record.hrn=hrn
+                    node_record.gid=node_gid
+                    node_record.pointer =node['node_id']
+                    node_record.authority=get_authority(hrn)
+                    dbsession.add(node_record)
+                    dbsession.commit()
+                    logger.info("Import: imported node: %s" % node_record)  
                 except:
                     logger.log_exc("Import: failed to import node") 
                     
@@ -212,10 +227,15 @@ def main():
                     pkey = Keypair(create=True)
                     urn = hrn_to_urn(hrn, 'slice')
                     slice_gid = sfaImporter.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
-                    slice_record = SfaRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'],
-                                             authority=get_authority(hrn))
-                    logger.info("Import: importing slice: %s" % slice_record.summary_string())  
-                    slice_record.sync()
+                    slice_record = RegSlice ()
+                    slice_record.type='slice'
+                    slice_record.hrn=hrn
+                    slice_record.gid=slice_gid
+                    slice_record.pointer=slice['slice_id']
+                    slice_record.authority=get_authority(hrn)
+                    dbsession.add(slice_record)
+                    dbsession.commit()
+                    logger.info("Import: imported slice: %s" % slice_record)  
                 except:
                     logger.log_exc("Import: failed to  import slice")
 
@@ -228,8 +248,8 @@ def main():
             if len(hrn) > 64:
                 hrn = hrn[:64]
 
-            # if user's primary key has chnaged then we need to update the 
-            # users gid by forcing a update here
+            # if user's primary key has changed then we need to update the 
+            # users gid by forcing an update here
             old_keys = []
             new_keys = []
             if person_id in old_person_keys:
@@ -257,10 +277,15 @@ def main():
                         pkey = Keypair(create=True) 
                     urn = hrn_to_urn(hrn, 'user')
                     person_gid = sfaImporter.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
-                    person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", \
-                                              pointer=person['person_id'], authority=get_authority(hrn))
-                    logger.info("Import: importing person: %s" % person_record.summary_string())  
-                    person_record.sync()
+                    person_record = RegUser ()
+                    person_record.type='user'
+                    person_record.hrn=hrn
+                    person_record.gid=person_gid
+                    person_record.pointer=person['person_id']
+                    person_record.authority=get_authority(hrn)
+                    dbsession.add (person_record)
+                    dbsession.commit()
+                    logger.info("Import: imported person: %s" % person_record)
                 except:
                     logger.log_exc("Import: failed to import person.") 
     
@@ -271,7 +296,7 @@ def main():
             continue
         
         record = existing_records[(record_hrn, type)]
-        if record['peer_authority']:
+        if record.peer_authority:
             continue
 
         # dont delete vini's internet2 placeholdder record
@@ -285,7 +310,7 @@ def main():
         if type == 'authority':    
             for site in sites:
                 site_hrn = interface_hrn + "." + site['login_base']
-                if site_hrn == record_hrn and site['site_id'] == record['pointer']:
+                if site_hrn == record_hrn and site['site_id'] == record.pointer:
                     found = True
                     break
 
@@ -299,7 +324,7 @@ def main():
                     alt_username = person['email'].split("@")[0].replace(".", "_").replace("+", "_")
                     if username in [tmp_username, alt_username] and \
                        site['site_id'] in person['site_ids'] and \
-                       person['person_id'] == record['pointer']:
+                       person['person_id'] == record.pointer:
                         found = True
                         break
         
@@ -307,7 +332,7 @@ def main():
             slicename = hrn_to_pl_slicename(record_hrn)
             for slice in slices:
                 if slicename == slice['name'] and \
-                   slice['slice_id'] == record['pointer']:
+                   slice['slice_id'] == record.pointer:
                     found = True
                     break    
  
@@ -320,7 +345,7 @@ def main():
                     tmp_nodename = node['hostname']
                     if tmp_nodename == nodename and \
                        node['site_id'] == site['site_id'] and \
-                       node['node_id'] == record['pointer']:
+                       node['node_id'] == record.pointer:
                         found = True
                         break  
         else:
@@ -329,9 +354,9 @@ def main():
         if not found:
             try:
                 record_object = existing_records[(record_hrn, type)]
-                record = SfaRecord(dict=record_object)
-                logger.info("Import: deleting record: %s" % record.summary_string())
-                record.delete()
+                logger.info("Import: deleting record: %s" % record)
+                dbsession.delete(record_object)
+                dbsession.commit()
             except:
                 logger.log_exc("Import: failded to delete record")                    
     # save pub keys