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 from optparse import OptionParser
21 from geni.util.config import Config
23 usual_variables = ["GENI_REGISTRY_ROOT_AUTH",
24 "GENI_REGISTRY_LEVEL1_AUTH",
25 "GENI_REGISTRY_ENABLED",
28 "GENI_AGGREGATE_ENABLED",
29 "GENI_AGGREGATE_HOST",
30 "GENI_AGGREGATE_PORT",
42 mainloop_usage= """Available commands:
43 u/U\t\t\tEdit usual variables
44 w/W\t\t\tWrite / Write & reload
45 q\t\t\tQuit (without saving)
48 l/L [<var>]\tShow Locally modified variables/values
49 s/S [<var>]\tShow all current variables/values
50 e/E [<var>]\tEdit variables (all, in category, single)
54 command_usage="%prog [options]"
56 Unless you specify the -d option, meaning you want to configure
57 using defaults without interactive prompts"""
59 variable_usage= """Edit Commands :
60 .\tStops prompting, return to mainloop
61 =\tShows default value
65 def save_config(changes, config_file):
66 # always validate before saving
67 changes = validate(changes)
69 cfile = open(config_file, 'r')
70 lines = cfile.readlines()
75 for variable in changes:
76 if line.startswith(variable):
78 value = int(changes[variable])
79 newline = '%s=%s\n' % (variable, value)
80 newlines.append(newline)
82 value = changes[variable]
83 newline = '%s="%s"\n' % (variable, value)
84 newlines.append(newline)
90 cfile = open(config_file, 'w')
91 cfile.writelines(newlines)
94 def validate(changes):
95 defaults = get_defaults()
100 # GENI_INTERFACE_HRN is GENI_REGISTRY_LEVEL1_AUTH, if thats blank it
101 # then defaults to GENI_REGISTRY_ROOT_AUTH
102 # GENI_REGISTRY_LEVEL1_AUTH, so if either of these are present we must
103 # update GENI_INTERFACE_HRN
104 if 'GENI_REGISTRY_ROOT_AUTH' in changes:
105 root_auth = changes['GENI_REGISTRY_ROOT_AUTH']
107 root_auth = defaults['GENI_REGISTRY_ROOT_AUTH']
109 if 'GENI_REGISTRY_LEVEL1_AUTH' in changes:
110 level1_auth = changes['GENI_REGISTRY_LEVEL1_AUTH']
112 level1_auth = default['GENI_REGISTRY_LEVEL1_AUTH']
115 interface_hrn = level1_auth
117 interface_hrn = root_auth
118 changes['GENI_INTERFACE_HRN'] = interface_hrn
122 geni_config = Config()
123 plc_vars = {'PLC_API_MAINTENANCE_PASSWORD': 'GENI_PLC_PASSWORD',
124 'PLC_API_MAINTENANCE_USER': 'GENI_PLC_USER'
127 from geni.util.config import plcConfig
128 plc_config = plcConfig
133 for var in dir(geni_config):
134 if var.startswith('GENI'):
135 value = eval("geni_config.%s" % var)
136 defaults[var] = value
138 # some defaults come from plc_config
139 for var in dir(plc_config):
141 value = eval("plc_config.%s" % var)
142 defaults[plc_vars[var]] = value
146 def prompt_variable(variable, default_config):
147 if variable in default_config:
148 default_value = default_config[variable]
153 prompt = "%(variable)s : [%(default_value)s] " % locals()
155 answer = raw_input(prompt).strip()
157 raise Exception ('BailOut')
158 except KeyboardInterrupt:
160 raise Excception ('BailOut')
162 if (answer == "") or (answer == default_value):
164 elif answer in ['""', "''"]:
166 elif (answer == "."):
167 raise Exception ('BailOut')
168 elif (answer == "?"):
169 print variable_usage.strip()
170 elif (answer == "="):
171 print ("%s defaults to %s" %(variable,default_value))
175 def show_variable(variable, value_dict):
176 print "%s=%s" % (variable, value_dict[variable])
178 def mainloop (default_config, config_file):
182 answer = raw_input("Enter command (u for usual changes, w to save, ? for help) ").strip()
185 except KeyboardInterrupt:
189 if (answer == "") or (answer in "?hH"):
195 if (answer in ["q","Q"]):
197 elif (answer == "w"):
198 save_config(changes, config_file)
199 elif (answer == "u"):
201 for varname in usual_variables:
202 changes[varname] = prompt_variable(varname, default_config)
203 except Exception, inst:
204 if (str(inst) != 'BailOut'):
206 elif (answer in ["e","E"]):
208 prompt_variable (cdef,cread,cwrite,category,variable,
210 except Exception, inst:
211 if (str(inst) != 'BailOut'):
213 elif (answer in "sS"):
214 for varname in usual_variables:
215 show_variable (varname, default_config)
216 elif (answer in "lL"):
218 print "No changes to display"
220 for varname in changes:
221 show_variable(varname, changes)
223 print ("Unknown command >%s< -- use h for help" % answer)
231 parser = OptionParser(usage=command_usage, version="%prog 1.0")
232 parser.set_defaults(config_dir="/etc/geni",
234 parser.add_option("","--configdir",dest="config_dir",action="append", help="specify configuration directory")
235 parser.add_option("","--usual_variable",dest="usual_variables",action="append", help="add a usual variable")
236 parser.add_option("-d", "--default", action="count", help="dont prompt for values, just use defaults")
237 (config,args) = parser.parse_args()
239 parser.error("too many arguments")
241 config_dir = parser.values.config_dir
242 config_file = os.sep.join([config_dir, 'geni_config'])
243 defaults = get_defaults()
244 # if -d is specified dont prompt, just configure with defaults
246 save_config(defaults, config_file)
248 mainloop (defaults, config_file)
251 if __name__ == '__main__':