use existing private keys when creating authorities if they are present
[sfa.git] / geni / util / hierarchy.py
index 5aa3bc9..d262ddc 100644 (file)
@@ -103,7 +103,10 @@ class Hierarchy():
     #
     # @param basedir the base directory to store the hierarchy in
 
-    def __init__(self, basedir="."):
+    def __init__(self, basedir = None):
+        config = Config()
+        if not basedir:
+            basedir = config.GENI_BASE_DIR + os.sep + 'geni' + os.sep
         self.basedir = os.path.join(basedir, "authorities")
 
     ##
@@ -132,7 +135,7 @@ class Hierarchy():
     def auth_exists(self, hrn):
         (directory, gid_filename, privkey_filename, dbinfo_filename) = \
             self.get_auth_filenames(hrn)
-
+    
         return os.path.exists(gid_filename) and \
                os.path.exists(privkey_filename) and \
                os.path.exists(dbinfo_filename)
@@ -163,8 +166,12 @@ class Hierarchy():
             if errno == 17:\r
                 pass
 
-        pkey = Keypair(create = True)
-        pkey.save_to_file(privkey_filename)
+        if os.path.exists(privkey_filename):
+            print "using existing key", privkey_filename, "for authority", hrn
+            pkey = Keypair(filename = privkey_filename)
+        else:
+            pkey = Keypair(create = True)
+            pkey.save_to_file(privkey_filename)
 
         gid = self.create_gid(hrn, create_uuid(), pkey)
         gid.save_to_file(gid_filename, save_parents=True)
@@ -185,7 +192,7 @@ class Hierarchy():
 
     def get_auth_info(self, hrn):
         #report.trace("Hierarchy: getting authority: " + hrn)
-
+    
         if not self.auth_exists(hrn):
             raise MissingAuthority(hrn)