removed debugging output
[sfa.git] / geni / gimport.py
index 0d8e19d..48f1f7d 100755 (executable)
@@ -1,3 +1,6 @@
+#!/usr/bin/python
+#
+#
 ##
 # Import PLC records into the Geni database. It is indended that this tool be
 # run once to create Geni records that reflect the current state of the
@@ -22,15 +25,51 @@ from geni.util.hierarchy import *
 from geni.util.record import *
 from geni.util.genitable import *
 from geni.util.misc import *
+from geni.util.config import *
+
+# get PL account settings from config module
+pl_auth = get_pl_auth()
 
-shell = None
+# connect to planetlab
+if "Url" in pl_auth:
+    from geni.util import remoteshell
+    shell = remoteshell.RemoteShell()
+else:
+    import PLC.Shell
+    shell = PLC.Shell.Shell(globals = globals())
 
 ##
 # Two authorities are specified: the root authority and the level1 authority.
 
-root_auth = "planetlab"
-level1_auth = "planetlab.us"
+#root_auth = "plc"
+#level1_auth = None
+
+#root_auth = "planetlab"
+#level1_auth = "planetlab.us"
+config = Config()
+
+root_auth = config.GENI_REGISTRY_ROOT_AUTH
+level1_auth = config.GENI_REGISTRY_LEVEL1_AUTH
+if not level1_auth or level1_auth in ['']:
+    level1_auth = None
 
+keyconvert = 'keyconvert'
+loaded = False
+default_path = "/usr/share/keyconvert/" + keyconvert
+cwd = os.path.dirname(os.path.abspath(__file__))
+alt_path = os.sep.join(cwd.split(os.sep)[:-1] + ['keyconvert', 'keyconvert'])
+geni_path = config.GENI_BASE_DIR + os.sep + "keyconvert/keyconvert"
+files = [default_path, alt_path, geni_path]
+for path in files:
+    if os.path.isfile(path):
+        keyconvert_fn = path
+        loaded = True
+        break
+
+if not loaded:
+    raise Exception, "Could not find config in " + ", ".join(files)        
+
+keyconvert_fn = config.GENI_BASE_DIR + os.sep + "keyconvert/keyconvert"
 
 def un_unicode(str):
    if isinstance(str, unicode):
@@ -78,6 +117,8 @@ def connect_shell():
         import PLC.Shell
         shell = PLC.Shell.Shell(globals = globals())
 
+    return shell
+
 def get_auth_table(auth_name):
     AuthHierarchy = Hierarchy()
     auth_info = AuthHierarchy.get_auth_info(auth_name)
@@ -110,7 +151,11 @@ def get_pl_pubkey(key_id):
         os.write(ssh_f, key_str)
         os.close(ssh_f)
 
-        cmd = "../keyconvert/keyconvert " + ssh_fn + " " + ssl_fn
+        if not os.path.exists(keyconvert_fn):
+            report.trace("  keyconvert utility " + str(keyconvert_fn) + " does not exist");
+            sys.exit(-1)
+
+        cmd = keyconvert_fn + " " + ssh_fn + " " + ssl_fn
         print cmd
         os.system(cmd)
 
@@ -137,7 +182,11 @@ def get_pl_pubkey(key_id):
         return None
 
 def person_to_hrn(parent_hrn, person):
-    personname = person['last_name'] + "_" + person['first_name']
+    # the old way - Lastname_Firstname
+    #personname = person['last_name'] + "_" + person['first_name']
+
+    # the new way - use email address up to the "@" 
+    personname = person['email'].split("@")[0]
 
     personname = cleanup_string(personname)
 
@@ -158,7 +207,9 @@ def import_person(parent_hrn, person):
 
     person_record = table.resolve("user", hrn)
     if not person_record:
-        key_ids = person["key_ids"]
+        key_ids = []
+        if 'key_ids' in person:    
+            key_ids = person["key_ids"]
 
         if key_ids:
             # get the user's private key from the SSH keys they have uploaded
@@ -179,14 +230,15 @@ def import_person(parent_hrn, person):
         report.trace("  inserting user record for " + hrn)
         table.insert(person_record)
     else:
-       key_ids = person["key_ids"]
-       if key_ids:
-           pkey = get_pl_pubkey(key_ids[0])
-           person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
-            person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
-            report.trace("  updating user record for " + hrn)
-            table.update(person_record)
-           
+        key_ids = person["key_ids"]
+
+    if key_ids:
+        pkey = get_pl_pubkey(key_ids[0])
+        person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
+        person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
+        report.trace("  updating user record for " + hrn)
+        table.update(person_record)
+        
 def import_slice(parent_hrn, slice):
     AuthHierarchy = Hierarchy()
     slicename = slice['name'].split("_",1)[-1]
@@ -211,7 +263,7 @@ def import_slice(parent_hrn, slice):
 
 def import_node(parent_hrn, node):
     AuthHierarchy = Hierarchy()
-    nodename = node['hostname']
+    nodename = node['hostname'].split(".")[0]
     nodename = cleanup_string(nodename)
 
     if not nodename:
@@ -253,57 +305,50 @@ def import_site(parent_hrn, site):
 
     table = get_auth_table(parent_hrn)
 
-    sa_record = table.resolve("sa", hrn)
-    if not sa_record:
-        sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
-        report.trace("  inserting sa record for " + hrn)
-        table.insert(sa_record)
-
-    ma_record = table.resolve("ma", hrn)
-    if not ma_record:
-        ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
-        report.trace("  inserting ma record for " + hrn)
-        table.insert(ma_record)
-
-    for person_id in site['person_ids']:
-        persons = shell.GetPersons(pl_auth, [person_id])
-        if persons:
-            try: 
-                import_person(hrn, persons[0])
-            except:
-                report.trace("Failed to import: %s" % persons[0])
-    for slice_id in site['slice_ids']:
-        slices = shell.GetSlices(pl_auth, [slice_id])
-        if slices:
-            try:
-                import_slice(hrn, slices[0])
-            except:
-                report.trace("Failed to import: %s" % slices[0])
-    for node_id in site['node_ids']:
-        nodes = shell.GetNodes(pl_auth, [node_id])
-        if nodes:
-            try:
-                import_node(hrn, nodes[0])
-            except:
-                report.trace("Failed to import: %s" % nodes[0])
+    auth_record = table.resolve("authority", hrn)
+    if not auth_record:
+        auth_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
+        report.trace("  inserting authority record for " + hrn)
+        table.insert(auth_record)
+
+    if 'person_ids' in site: 
+        for person_id in site['person_ids']:
+            persons = shell.GetPersons(pl_auth, [person_id])
+            if persons:
+                try: 
+                    import_person(hrn, persons[0])
+                except:
+                    report.trace("Failed to import: %s" % persons[0])
+    if 'slice_ids' in site:
+        for slice_id in site['slice_ids']:
+            slices = shell.GetSlices(pl_auth, [slice_id])
+            if slices:
+                try:
+                    import_slice(hrn, slices[0])
+                except:
+                    report.trace("Failed to import: %s" % slices[0])
+    if 'node_ids' in site:
+        for node_id in site['node_ids']:
+            nodes = shell.GetNodes(pl_auth, [node_id])
+            if nodes:
+                try:
+                    import_node(hrn, nodes[0])
+                except:
+                    report.trace("Failed to import: %s" % nodes[0])
 
 def create_top_level_auth_records(hrn):
     parent_hrn = get_authority(hrn)
-    print hrn, ":", parent_hrn 
+    print hrn, ":", parent_hrn
+    if not parent_hrn:
+        parent_hrn = hrn    
     auth_info = AuthHierarchy.get_auth_info(parent_hrn)
     table = get_auth_table(parent_hrn)
 
-    sa_record = table.resolve("sa", hrn)
-    if not sa_record:
-        sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=-1)
-        report.trace("  inserting sa record for " + hrn)
-        table.insert(sa_record)
-
-    ma_record = table.resolve("ma", hrn)
-    if not ma_record:
-        ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=-1)
-        report.trace("  inserting ma record for " + hrn)
-        table.insert(ma_record)
+    auth_record = table.resolve("authority", hrn)
+    if not auth_record:
+        auth_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
+        report.trace("  inserting authority record for " + hrn)
+        table.insert(auth_record)
 
 def main():
     global AuthHierarchy
@@ -311,6 +356,8 @@ def main():
 
     process_options()
 
+    print "Base Directory: ", config.GENI_BASE_DIR
+
     AuthHierarchy = Hierarchy()
     TrustedRoots = TrustedRootList()
 
@@ -318,10 +365,15 @@ def main():
 
     if not AuthHierarchy.auth_exists(root_auth):
         AuthHierarchy.create_auth(root_auth)
-    #create_top_level_auth_records(root_auth)
-    if not AuthHierarchy.auth_exists(level1_auth):
-        AuthHierarchy.create_auth(level1_auth)
-    create_top_level_auth_records(level1_auth)
+
+    create_top_level_auth_records(root_auth)
+    if level1_auth:
+        if not AuthHierarchy.auth_exists(level1_auth):
+            AuthHierarchy.create_auth(level1_auth)
+        create_top_level_auth_records(level1_auth)
+        import_auth = level1_auth
+    else:
+        import_auth = root_auth
 
     print "Import: adding", root_auth, "to trusted list"
     root = AuthHierarchy.get_auth_info(root_auth)
@@ -331,7 +383,7 @@ def main():
 
     sites = shell.GetSites(pl_auth)
     for site in sites:
-        import_site(level1_auth, site)
+        import_site(import_auth, site)
 
 if __name__ == "__main__":
     main()