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
23 from plc_config import PLCConfiguration
28 def init_flavour (flavour):
30 global common_variables
31 if (flavour == "devel"):
33 common_variables=("PLC_DEVEL_FEDORA_URL",
35 config_dir = "/plc/devel/data/etc/planetlab"
38 common_variables=("PLC_NAME",
41 "PLC_MAIL_SUPPORT_ADDRESS",
48 config_dir = "/etc/planetlab"
49 global def_main_config
50 def_main_config= "%s/default_config.xml" % config_dir
51 global def_site_config
52 def_site_config = "%s/configs/site.xml" % config_dir
53 global def_consolidated_config
54 def_consolidated_config = "%s/plc_config.xml" % config_dir
57 mainloop_usage= """Available commands
58 c\tEdits commonly tuned variables
59 e\tEdits all variables
60 p\tPrints all locally-customized vars and values
61 e <var>\tPrompts (edit) fro variable <var>
62 p <var>\tShows current setting for <var>
63 l\tlists all known variables
64 w\tsaves & consolidates
65 r\trestarts %s service
66 q\tQuits without saving
68 Typical usage involves: c, [p,] w, r
72 command_usage="Usage: %s [-d] [-v] [default-xml [site-xml [consolidated-xml]]]"% sys.argv[0]
75 \t default-xml defaults to %s
76 \t site-xml defaults to %s
77 \t consolidated-xml defaults to %s""" % (def_main_config,def_site_config, def_consolidated_config)
79 Unless you specify the -d option, meaning you want to configure
80 myplc-devel instead of regular myplc, in which case"""
81 init_flavour ("devel")
83 \t default-xml defaults to %s
84 \t site-xml defaults to %s
85 \t consolidated-xml defaults to %s""" % (def_main_config,def_site_config, def_consolidated_config)
90 variable_usage= """Special answers :
91 #\tShow variable comments
92 .\tStops prompting, return to mainloop
93 /\tCleans any site-defined value, reverts to default
94 =\tShows default value
95 >\tSkips to next category
100 def get_value (config, category_id, variable_id):
101 (category, variable) = config.get (category_id, variable_id)
102 return variable['value']
104 # refrain from using plc_config's _sanitize
105 def get_varname (config, category_id, variable_id):
106 (category, variable) = config.get (category_id, variable_id)
107 return (category_id+"_"+variable['id']).upper()
109 def prompt_variable (cdef, cread, cwrite, category, variable):
111 assert category.has_key('id')
112 assert variable.has_key('id')
114 category_id = category ['id']
115 variable_id = variable['id']
118 default_value = get_value(cdef,category_id,variable_id)
119 current_value = get_value(cread,category_id, variable_id)
120 varname = get_varname (cread,category_id, variable_id)
122 prompt = "== %s : [%s] " % (varname,current_value)
124 answer = raw_input(prompt).strip()
126 raise Exception ('BailOut')
129 if (answer == "") or (answer == current_value):
131 elif (answer == "."):
132 raise Exception ('BailOut')
133 elif (answer == ">"):
134 raise Exception ('NextCategory')
135 elif (answer == "#"):
136 if friendly_name is not None:
137 print ("### " + friendly_name)
139 print ("!!! No comment associated to %s" % varname)
141 for line in comments:
143 elif (answer == "?"):
144 print variable_usage.strip()
145 elif (answer == "="):
146 print ("%s defaults to %s" %(varname,default_value))
147 # revert to default : remove from cwrite (i.e. site-config)
148 elif (answer == "/"):
149 cwrite.delete(category_id,variable_id)
150 print ("%s reverted to %s" %(varname,default_value))
153 variable['value'] = answer
154 cwrite.set(category,variable)
158 def prompt_all_variables (cdef, cread, cwrite):
160 for (category_id, (category, variables)) in cread.variables().iteritems():
161 print ("========== Category = %s" % category_id)
162 for variable in variables.values():
164 newvar = prompt_variable (cdef, cread, cwrite, category, variable)
165 except Exception, inst:
166 if (str(inst) == 'NextCategory'): break
169 except Exception, inst:
170 if (str(inst) == 'BailOut'): return
175 def consolidate (main_config, site_config, consolidated_config):
177 conso = PLCConfiguration (main_config)
178 conso.load (site_config)
179 conso.save (consolidated_config)
180 except Exception, inst:
181 print "Could not consolidate, %s" % (str(inst))
183 print ("Merged\n\t%s\nand\t%s\ninto\t%s"%(main_config,site_config,consolidated_config))
187 print ("==================== Stopping %s" % service)
188 os.system("service %s stop" % service)
189 print ("==================== Starting %s" % service)
190 os.system("service %s start" % service)
194 re_mainloop_var="^(?P<command>[pe])[ \t]+(?P<varname>\w+)$"
195 matcher_mainloop_var=re.compile(re_mainloop_var)
197 def mainloop (cdef, cread, cwrite,main_config, site_config, consolidated_config):
200 answer = raw_input("Enter command (c for usual changes, w to save, ? for help) ").strip()
203 answer=answer.lower()
204 if (answer == "") or (answer == "?") or (answer == "h"):
206 elif (answer == "q"):
207 # todo check confirmation
209 elif (answer == "e"):
210 prompt_all_variables(cdef, cread, cwrite)
211 elif (answer == "w"):
213 cwrite.save(site_config)
215 print ("Could not save -- fix write access on %s" % site_config)
217 print ("Wrote %s" % site_config)
218 consolidate(main_config, site_config, consolidated_config)
219 print ("You might want to type 'r' (restart plc) or 'q' (quit)")
220 elif (answer == "l"):
221 print ("Config involves the following variables")
222 sys.stdout.write(cread.output_variables())
223 elif (answer == "p"):
224 print ("Current site config")
225 sys.stdout.write(cwrite.output_shell(False))
226 elif (answer == "c"):
228 for varname in common_variables:
229 (category,variable) = cdef.locate_varname(varname)
230 prompt_variable(cdef, cread, cwrite, category, variable)
231 except Exception, inst:
232 if (str(inst) != 'BailOut'):
234 elif (answer == "r"):
237 groups_var = matcher_mainloop_var.match(answer)
239 command = groups_var.group('command')
240 varname = groups_var.group('varname')
241 (category,variable) = cdef.locate_varname(varname)
243 print "Unknown variable %s" % varname
244 elif (command == 'p'):
245 print ("%s = %s" % (varname,get_value(cwrite,
250 prompt_variable(cdef, cread, cwrite, category,variable)
251 except Exception, inst:
252 if (str(inst) != 'BailOut'):
255 print ("Unknown command >%s<" % answer)
264 # default is myplc (non -devel) unless -d is specified
266 optlist,list = getopt.getopt(argv,":dhv")
271 print ("This is %s - %s" %(command,release))
274 init_flavour("devel")
278 (main_config,site_config,consolidated_config) = (def_main_config, def_site_config, def_consolidated_config)
280 (main_config,site_config,consolidated_config) = (argv[0], def_site_config, def_consolidated_config)
282 (main_config, site_config,consolidated_config) = (argv[0], argv[1], def_consolidated_config)
284 (main_config, site_config,consolidated_config) = argv
289 # the default settings only - read only
290 cdef = PLCConfiguration(main_config)
292 # in effect : default settings + local settings - read only
293 cread = PLCConfiguration(main_config)
296 print ("default config files not found, is myplc installed ?")
299 # local settings only, will be modified & saved
300 cwrite=PLCConfiguration()
303 cread.load(site_config)
304 cwrite.load(site_config)
306 cwrite = PLCConfiguration()
308 print ("This is %s - %s -- Type ? at the prompt for help" %(command,release))
309 mainloop (cdef, cread, cwrite,main_config, site_config, consolidated_config)
312 if __name__ == '__main__':