3 # Interactively prompts for variable values
4 # expected arguments are
5 # command -d [default-xml [custom-xml [ consolidated-xml ]]]
7 # -d is for the myplc-devel package
9 # we use 3 instances of PLCConfiguration throughout:
10 # cdef : models the defaults, from plc_default.xml
11 # cread : merged from plc_default & configs/site.xml
12 # cwrite : site.xml + pending changes
19 import distutils.file_util
20 from optparse import OptionParser
22 from geni.util.config import Config
23 from geni.util.hierarchy import *
24 from geni.util.misc import *
27 all_variables = ["GENI_REGISTRY_ROOT_AUTH",
28 "GENI_REGISTRY_LEVEL1_AUTH",
29 "GENI_REGISTRY_ENABLED",
32 "GENI_AGGREGATE_ENABLED",
33 "GENI_AGGREGATE_HOST",
34 "GENI_AGGREGATE_PORT",
44 usual_variables = ["GENI_REGISTRY_ROOT_AUTH",
45 "GENI_REGISTRY_LEVEL1_AUTH",
51 mainloop_usage= """Available commands:
52 u/U\t\t\tEdit usual variables
53 w/W\t\t\tWrite / Write & reload
54 q\t\t\tQuit (without saving)
57 l/L [<var>]\tShow Locally modified variables/values
58 s/S [<var>]\tShow all current variables/values
59 e/E [<var>]\tEdit variables (all, in category, single)
63 command_usage="%prog [options]"
65 Unless you specify the -d option, meaning you want to configure
66 using defaults without interactive prompts"""
68 variable_usage= """Edit Commands :
69 .\tStops prompting, return to mainloop
70 =\tShows default value
74 def save_config(changes, config_file):
75 # always validate before saving
76 changes = validate(changes)
78 cfile = open(config_file, 'r')
79 lines = cfile.readlines()
84 for variable in changes:
85 if line.startswith(variable+'='):
87 value = int(changes[variable])
88 newline = '%s=%s\n' % (variable, value)
89 newlines.append(newline)
91 value = changes[variable]
92 newline = '%s="%s"\n' % (variable, value)
93 newlines.append(newline)
99 cfile = open(config_file, 'w')
100 cfile.writelines(newlines)
102 print 'updated config file',config_file
104 def validate(changes):
109 defaults = get_defaults()
111 # GENI_INTERFACE_HRN is GENI_REGISTRY_LEVEL1_AUTH, if thats blank it
112 # then defaults to GENI_REGISTRY_ROOT_AUTH
113 # GENI_REGISTRY_LEVEL1_AUTH, so if either of these are present we must
114 # update GENI_INTERFACE_HRN
115 if 'GENI_REGISTRY_ROOT_AUTH' in changes:
116 root_auth = changes['GENI_REGISTRY_ROOT_AUTH']
118 root_auth = defaults['GENI_REGISTRY_ROOT_AUTH']
120 if 'GENI_REGISTRY_LEVEL1_AUTH' in changes:
121 level1_auth = changes['GENI_REGISTRY_LEVEL1_AUTH']
123 level1_auth = defaults['GENI_REGISTRY_LEVEL1_AUTH']
126 interface_hrn = level1_auth
128 interface_hrn = root_auth
129 changes['GENI_INTERFACE_HRN'] = interface_hrn
133 geni_config = Config()
134 plc_vars = {'PLC_API_MAINTENANCE_PASSWORD': 'GENI_PLC_PASSWORD',
135 'PLC_API_MAINTENANCE_USER': 'GENI_PLC_USER'
138 from geni.util.config import plcConfig
139 plc_config = plcConfig
144 for var in dir(geni_config):
145 if var.startswith('GENI'):
146 value = eval("geni_config.%s" % var)
147 defaults[var] = value
149 # some defaults come from plc_config
150 for var in dir(plc_config):
152 value = eval("plc_config.%s" % var)
153 defaults[plc_vars[var]] = value
157 def prompt_variable(variable, default_config):
158 if variable in default_config:
159 default_value = default_config[variable]
164 prompt = "%(variable)s : [%(default_value)s] " % locals()
166 answer = raw_input(prompt).strip()
168 raise Exception ('BailOut')
169 except KeyboardInterrupt:
171 raise Exception ('BailOut')
173 if (answer == "") or (answer == default_value):
175 elif answer in ['""', "''"]:
177 elif (answer == "."):
178 raise Exception ('BailOut')
179 elif (answer == "?"):
180 print variable_usage.strip()
181 elif (answer == "="):
182 print ("%s defaults to %s" %(variable,default_value))
186 def show_variable(variable, value_dict):
187 print "%s=%s" % (variable, value_dict[variable])
189 def mainloop (default_config, config_file):
193 answer = raw_input("Enter command (u for usual changes, w to save, ? for help) ").strip()
196 except KeyboardInterrupt:
200 if (answer == "") or (answer in "?hH"):
206 if (answer in ["q","Q"]):
208 elif (answer == "w"):
209 save_config(changes, config_file)
210 elif (answer == "u"):
212 for varname in usual_variables:
213 changes[varname] = prompt_variable(varname, default_config)
214 except Exception, inst:
215 if (str(inst) != 'BailOut'):
217 elif (answer in ["e","E"]):
219 prompt_variable (cdef,cread,cwrite,category,variable,
221 except Exception, inst:
222 if (str(inst) != 'BailOut'):
224 elif (answer in "sS"):
225 for varname in usual_variables:
226 show_variable (varname, default_config)
227 elif (answer in "lL"):
229 print "No changes to display"
231 for varname in changes:
232 show_variable(varname, changes)
234 print ("Unknown command >%s< -- use h for help" % answer)
237 result.update(default_config)
238 result.update(changes)
241 def setup_server_key(config_dict):
242 hrn = config_dict.get('GENI_INTERFACE_HRN')
245 # Get the path to the authorities directory hierarchy
246 hierarchy = Hierarchy()
247 path = hierarchy.basedir
248 auth_path = hrn.replace(".", os.sep)
250 # define some useful variables
253 hrn_leaf = get_leaf(hrn)
256 new_server_key = os.sep.join([path, auth_path, hrn_leaf]) + ".pkey"
257 old_server_key = os.sep.join([path, key])
258 old_server_cert = os.sep.join([path, cert])
260 # remove old key/cert
261 for fd in [old_server_key, old_server_cert]:
262 if os.path.isfile(fd):
265 # create new server.key
267 distutils.file_util.copy_file(src=new_server_key, dst=old_server_key, verbose=1)
268 print "\t\t%(old_server_key)s\ncopied from\t%(new_server_key)s" % locals()
269 # this is expected when running geni-config-tty for the first time (before gimport.py)
271 print "Could not create %(old_server_key)s - ignore if you haven't run gimport yet"%locals()
281 parser = OptionParser(usage=command_usage, version="%prog 1.0")
282 parser.set_defaults(config_dir="/etc/geni",
284 parser.add_option("","--configdir",dest="config_dir",action="append", help="specify configuration directory")
285 parser.add_option("","--usual_variable",dest="usual_variables",action="append", help="add a usual variable")
286 parser.add_option("-d", "--default", action="count", help="dont prompt for values, just use defaults")
287 (config,args) = parser.parse_args()
289 parser.error("too many arguments")
291 config_dir = parser.values.config_dir
292 config_file = os.sep.join([config_dir, 'geni_config'])
293 defaults = get_defaults()
294 # if -d is specified dont prompt, just configure with defaults
296 save_config(defaults, config_file)
299 results = mainloop (defaults, config_file)
300 setup_server_key(results)
303 if __name__ == '__main__':