add more usual settings
[sfa.git] / geni-config-tty
index da3ef72..dae6c6a 100755 (executable)
@@ -16,11 +16,15 @@ import os
 import re
 import readline
 import traceback
+import distutils.file_util
 from optparse import OptionParser
 
 from geni.util.config import Config
+from geni.util.hierarchy import *
+from geni.util.misc import *
 
-usual_variables = ["GENI_REGISTRY_ROOT_AUTH",
+
+all_variables   = ["GENI_REGISTRY_ROOT_AUTH",
                    "GENI_REGISTRY_LEVEL1_AUTH",
                    "GENI_REGISTRY_ENABLED",
                    "GENI_REGISTRY_HOST", 
@@ -37,6 +41,11 @@ usual_variables = ["GENI_REGISTRY_ROOT_AUTH",
                    "GENI_PLC_PORT",
                    "GENI_PLC_API_PATH"
                    ]
+usual_variables = ["GENI_REGISTRY_ROOT_AUTH",
+                   "GENI_REGISTRY_LEVEL1_AUTH",
+                   "GENI_PLC_USER",
+                   "GENI_PLC_PASSWORD",    
+                   ]
 
 
 mainloop_usage= """Available commands:
@@ -68,12 +77,12 @@ def save_config(changes, config_file):
     
     cfile = open(config_file, 'r')
     lines = cfile.readlines()
-    newlines = []
     cfile.close()
+    newlines = []
     for line in lines:
         added = False
         for variable in changes:
-            if line.startswith(variable):
+            if line.startswith(variable+'='):
                 try:
                     value = int(changes[variable])
                     newline = '%s=%s\n' % (variable, value)
@@ -90,14 +99,16 @@ def save_config(changes, config_file):
     cfile = open(config_file, 'w')
     cfile.writelines(newlines)
     cfile.close()
+    print 'updated config file',config_file
 
 def validate(changes):
     defaults = get_defaults()
     
     if not changes:
-        return
+        return {}
 
-    # GENI_INTERFACE_HRN is generated by combining GENI_REGISTRY_ROOT_AUTH and 
+    # GENI_INTERFACE_HRN is GENI_REGISTRY_LEVEL1_AUTH, if thats blank it
+    # then defaults to GENI_REGISTRY_ROOT_AUTH 
     # GENI_REGISTRY_LEVEL1_AUTH, so if either of these are present we must 
     # update GENI_INTERFACE_HRN
     if 'GENI_REGISTRY_ROOT_AUTH' in changes:
@@ -108,16 +119,19 @@ def validate(changes):
     if 'GENI_REGISTRY_LEVEL1_AUTH' in changes:
         level1_auth = changes['GENI_REGISTRY_LEVEL1_AUTH']
     else:
-        level1_auth = default['GENI_REGISTRY_LEVEL1_AUTH']
+        level1_auth = defaults['GENI_REGISTRY_LEVEL1_AUTH']
                 
-    interface_hrn = ".".join([root_auth, level1_auth])
+    if level1_auth:
+        interface_hrn = level1_auth
+    else:
+        interface_hrn = root_auth
     changes['GENI_INTERFACE_HRN'] = interface_hrn
     return changes                            
 
 def get_defaults():
     geni_config = Config()
     plc_vars = {'PLC_API_MAINTENANCE_PASSWORD': 'GENI_PLC_PASSWORD',
-                'PLC_API_MAINTENCE_USER': 'GENI_PLC_USER'
+                'PLC_API_MAINTENANCE_USER': 'GENI_PLC_USER'
                }
     try:
         from geni.util.config import plcConfig
@@ -189,7 +203,7 @@ def mainloop (default_config, config_file):
             print "help"  
 
         if (answer in ["q","Q"]):
-            return
+            break
         elif (answer == "w"):
             save_config(changes, config_file)
         elif (answer == "u"):
@@ -218,6 +232,41 @@ def mainloop (default_config, config_file):
         else:
             print ("Unknown command >%s< -- use h for help" % answer)
 
+    result = {}
+    result.update(default_config)
+    result.update(changes)
+    return result
+    
+def setup_server_key(config_dict):
+    hrn = config_dict.get('GENI_INTERFACE_HRN')
+    if not hrn: return
+   
+    # Get the path to the authorities directory hierarchy
+    hierarchy = Hierarchy()
+    path = hierarchy.basedir
+    auth_path = hrn.replace(".", os.sep)
+    # define some useful variables   
+    key = 'server.key'
+    cert = 'server.cert'
+    hrn_leaf = get_leaf(hrn)
+    if not hrn_leaf:
+        hrn_leaf = hrn
+    new_server_key = os.sep.join([path, auth_path, hrn_leaf]) + ".pkey"
+    old_server_key = os.sep.join([path, key])
+    old_server_cert = os.sep.join([path, cert])
+
+    # remove old key/cert
+    for fd in [old_server_key, old_server_cert]:
+        if os.path.isfile(fd):
+            os.remove(fd)
+
+    # create new server.key
+    distutils.file_util.copy_file(src=new_server_key, dst=old_server_key, verbose=1)
+    print "\t\t%(old_server_key)s\ncopied from\t%(new_server_key)s" % locals()
+    
+    
+
 ####################
 def main ():
 
@@ -240,8 +289,10 @@ def main ():
     # if -d is specified dont prompt, just configure with defaults
     if '-d' in argv:
         save_config(defaults, config_file)
+        results = defaults
     else:        
-        mainloop (defaults, config_file)
+        results = mainloop (defaults, config_file)
+    setup_server_key(results)
     return 0
 
 if __name__ == '__main__':