3 # Interactively prompts for variable values
4 # expected arguments are
5 # command [default-xml [custom-xml]]
8 # (1) scans all variables (todo: pass categories as arguments)
9 # and prompts for value
10 # current value proposed as default
11 # also allows to remove site-dependent setting
12 # (2) epilogue : allows to
13 # list the site-dependent vars with values
14 # and to locally (re-)edit a variable from its shell name
15 # quit with or without saving
22 from plc_config import PLCConfiguration
27 def_main_config = "/etc/planetlab/default_config.xml"
28 def_site_config = "/etc/planetlab/configs/site.xml"
29 def_consolidated_config = "/etc/planetlab/plc_config.xml"
31 command_usage="""Usage: %s [default-xml [site-xml [consolidated-xml]]]
32 \t default-xml defaults to %s
33 \t site-xml defaults to %s
34 \t consolidated-xml defaults to %s
35 """ % (sys.argv[0],def_main_config,def_site_config, def_consolidated_config)
38 variable_usage= """Special answers :
39 #\tShow variable comments
40 .\tStops prompting, return to mainloop
41 /\tCleans any site-defined value, reverts to default
42 =\tShows default value
43 >\tSkips to next category
52 def get_value (config, category_id, variable_id):
53 (category, variable) = config.get (category_id, variable_id)
54 return variable['value']
56 # refrain from using plc_config's _sanitize
57 def get_varname (config, category_id, variable_id):
58 (category, variable) = config.get (category_id, variable_id)
59 return (category_id+"_"+variable['id']).upper()
61 def prompt_variable (cdef, cread, cwrite, category, variable):
63 assert category.has_key('id')
64 assert variable.has_key('id')
66 category_id = category ['id']
67 variable_id = variable['id']
70 default_value = get_value(cdef,category_id,variable_id)
71 current_value = get_value(cread,category_id, variable_id)
72 varname = get_varname (cread,category_id, variable_id)
74 prompt = "== %s : [%s] " % (varname,current_value)
76 answer = raw_input(prompt).strip()
78 raise Exception ('BailOut')
81 if (answer == "") or (answer == current_value):
84 raise Exception ('BailOut')
86 raise Exception ('NextCategory')
88 if friendly_name is not None:
89 print ("### " + friendly_name)
91 print ("!!! No comment associated to %s" % varname)
96 print variable_usage.strip()
98 print ("%s defaults to %s" %(varname,default_value))
99 # revert to default : remove from cwrite (i.e. site-config)
100 elif (answer == "/"):
101 cwrite.delete(category_id,variable_id)
102 print ("%s reverted to %s" %(varname,default_value))
105 variable['value'] = answer
106 cwrite.set(category,variable)
110 def prompt_all_variables (cdef, cread, cwrite):
112 for (category_id, (category, variables)) in cread.variables().iteritems():
113 print ("========== Category = %s" % category_id)
114 for variable in variables.values():
116 newvar = prompt_variable (cdef, cread, cwrite, category, variable)
117 except Exception, inst:
118 if (str(inst) == 'NextCategory'): break
121 except Exception, inst:
122 if (str(inst) == 'BailOut'): return
127 def consolidate (main_config, site_config, consolidated_config):
129 conso = PLCConfiguration (main_config)
130 conso.load (site_config)
131 conso.save (consolidated_config)
132 except Exception, inst:
133 print "Could not consolidate, %s" % (str(inst))
135 print ("Overwote %s\n\tfrom %s\n\tand %s"%(consolidated_config,main_config,site_config))
139 print ("==================== Stopping plc")
140 os.system("service plc stop")
141 print ("==================== Starting plc")
142 os.system("service plc start")
145 mainloop_usage= """Available commands
146 c\tEdits commonly tuned variables
147 e\tEdits all variables
148 p\tPrints all locally-customized vars and values
149 e <var>\tPrompts (edit) fro variable <var>
150 p <var>\tShows current setting for <var>
151 l\tlists all known variables
153 W\tsaves, consolidates and quit
154 r\trestarts plc service
155 q\tQuits without saving
158 re_mainloop_var="^(?P<command>[pe])[ \t]+(?P<varname>\w+)$"
159 matcher_mainloop_var=re.compile(re_mainloop_var)
161 common_variables=("PLC_NAME",
164 "PLC_MAIL_SUPPORT_ADDRESS",
172 def mainloop (cdef, cread, cwrite,main_config, site_config, consolidated_config):
175 answer = raw_input("Enter command (c for usual changes, w to save, ? for help) ").strip()
178 if (answer == "") or (answer == "?") or (answer == "h"):
180 elif (answer == "q"):
181 # todo check confirmation
183 elif (answer == "e"):
184 prompt_all_variables(cdef, cread, cwrite)
185 elif (answer.lower() == "w"):
187 cwrite.save(site_config)
189 print ("Could not save -- fix write access on %s" % site_config)
191 print ("Wrote %s" % site_config)
193 consolidate(main_config, site_config, consolidated_config)
194 print ("You might want to type 'r' (restart plc) or 'q' (quit)")
195 elif (answer == "l"):
196 print ("Config involves the following variables")
197 sys.stdout.write(cread.output_variables())
198 elif (answer == "p"):
199 print ("Current site config")
200 sys.stdout.write(cwrite.output_shell(False))
201 elif (answer == "c"):
203 for varname in common_variables:
204 (category,variable) = cdef.locate_varname(varname)
205 prompt_variable(cdef, cread, cwrite, category, variable)
206 except Exception, inst:
207 if (str(inst) != 'BailOut'):
209 elif (answer == "r"):
212 groups_var = matcher_mainloop_var.match(answer)
214 command = groups_var.group('command')
215 varname = groups_var.group('varname')
216 (category,variable) = cdef.locate_varname(varname)
218 print "Unknown variable %s" % varname
219 elif (command == 'p'):
220 print ("%s = %s" % (varname,get_value(cwrite,
225 prompt_variable(cdef, cread, cwrite, category,variable)
226 except Exception, inst:
227 if (str(inst) != 'BailOut'):
230 print ("Unknown command >%s<" % answer)
239 (main_config,site_config,consolidated_config) = (def_main_config, def_site_config, def_consolidated_config)
241 (main_config,site_config,consolidated_config) = (argv[1], def_site_config, def_consolidated_config)
243 (main_config, site_config,consolidated_config) = (argv[1], argv[2], def_consolidated_config)
245 (main_config, site_config,consolidated_config) = argv
250 # the default settings only - read only
251 cdef = PLCConfiguration(main_config)
253 # in effect : default settings + local settings - read only
254 cread = PLCConfiguration(main_config)
257 print ("default config files not found, is myplc installed ?")
260 # local settings only, will be modified & saved
261 cwrite=PLCConfiguration()
264 cread.load(site_config)
265 cwrite.load(site_config)
267 cwrite = PLCConfiguration()
269 print ("This is %s - %s -- Type ? at the prompt for help" %(command,release))
270 mainloop (cdef, cread, cwrite,main_config, site_config, consolidated_config)
273 if __name__ == '__main__':