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"
30 command_usage="""Usage: %s [default-xml [site-xml]]
31 \t default-xml defaults to %s
32 \t site-xml defaults to %s
33 """ % (sys.argv[0],def_main_config,def_site_config)
36 variable_usage= """Special answers :
37 #\tShow variable comments
38 .\tStops prompting, return to mainloop
39 /\tCleans any site-defined value, reverts to default
40 =\tShows default value
41 >\tSkips to next category
50 def get_value (config, category_id, variable_id):
51 (category, variable) = config.get (category_id, variable_id)
52 return variable['value']
54 # refrain from using plc_config's _sanitize
55 def get_varname (config, category_id, variable_id):
56 (category, variable) = config.get (category_id, variable_id)
57 return (category_id+"_"+variable['id']).upper()
59 def prompt_variable (cdef, cread, cwrite, category, variable):
61 assert category.has_key('id')
62 assert variable.has_key('id')
64 category_id = category ['id']
65 variable_id = variable['id']
68 default_value = get_value(cdef,category_id,variable_id)
69 current_value = get_value(cread,category_id, variable_id)
70 varname = get_varname (cread,category_id, variable_id)
72 prompt = "== %s : [%s] " % (varname,current_value)
74 answer = raw_input(prompt).strip()
76 raise Exception ('BailOut')
79 if (answer == "") or (answer == current_value):
82 raise Exception ('BailOut')
84 raise Exception ('NextCategory')
86 if friendly_name is not None:
87 print ("### " + friendly_name)
89 print ("!!! No comment associated to %s" % varname)
94 print variable_usage.strip()
96 print ("%s defaults to %s" %(varname,default_value))
97 # revert to default : remove from cwrite (i.e. site-config)
99 cwrite.delete(category_id,variable_id)
100 print ("%s reverted to %s" %(varname,default_value))
103 variable['value'] = answer
104 cwrite.set(category,variable)
108 def prompt_all_variables (cdef, cread, cwrite):
110 for (category_id, (category, variables)) in cread.variables().iteritems():
111 print ("========== Category = %s" % category_id)
112 for variable in variables.values():
114 newvar = prompt_variable (cdef, cread, cwrite, category, variable)
115 except Exception, inst:
116 if (str(inst) == 'NextCategory'): break
119 except Exception, inst:
120 if (str(inst) == 'BailOut'): return
126 print ("==================== Stopping plc")
127 os.system("service plc stop")
128 print ("==================== Starting plc")
129 os.system("service plc start")
132 mainloop_usage= """Available commands
133 c\tEdits commonly tuned variables
134 e\tEdits all variables
135 p\tPrints all locally-customized vars and values
136 e <var>\tPrompts (edit) fro variable <var>
137 p <var>\tShows current setting for <var>
138 l\tlists all known variables
140 r\trestarts plc service
141 q\tQuits without saving
144 re_mainloop_var="^(?P<command>[pe])[ \t]+(?P<varname>\w+)$"
145 matcher_mainloop_var=re.compile(re_mainloop_var)
147 common_variables=("PLC_NAME",
150 "PLC_MAIL_SUPPORT_ADDRESS",
158 def mainloop (cdef, cread, cwrite,site_config):
161 answer = raw_input("Enter command (c for usual changes, w to save, ? for help) ").strip()
164 if (answer == "") or (answer == "?") or (answer == "h"):
166 elif (answer == "q"):
167 # todo check confirmation
169 elif (answer == "e"):
170 prompt_all_variables(cdef, cread, cwrite)
171 elif (answer == "w"):
173 cwrite.save(site_config)
175 print ("Could not save -- fix write access on %s" % site_config)
177 print ("Wrote %s" % site_config)
179 elif (answer == "l"):
180 print ("Config involves the following variables")
181 sys.stdout.write(cread.output_variables())
182 elif (answer == "p"):
183 print ("Current site config")
184 sys.stdout.write(cwrite.output_shell(False))
185 elif (answer == "c"):
187 for varname in common_variables:
188 (category,variable) = cdef.locate_varname(varname)
189 prompt_variable(cdef, cread, cwrite, category,variable)
190 except Exception, inst:
191 if (str(inst) != 'BailOut'):
193 elif (answer == "r"):
196 groups_var = matcher_mainloop_var.match(answer)
198 command = groups_var.group('command')
199 varname = groups_var.group('varname')
200 (category,variable) = cdef.locate_varname(varname)
202 print "Unknown variable %s" % varname
203 elif (command == 'p'):
204 print ("%s = %s" % (varname,get_value(cwrite,
209 prompt_variable(cdef, cread, cwrite, category,variable)
210 except Exception, inst:
211 if (str(inst) != 'BailOut'):
214 print ("Unknown command >%s<" % answer)
223 (main_config,site_config) = (def_main_config, def_site_config)
225 (main_config,site_config) = (argv[1], def_site_config)
227 (main_config, site_config) = argv
232 # the default settings only - read only
233 cdef = PLCConfiguration(main_config)
235 # in effect : default settings + local settings - read only
236 cread = PLCConfiguration(main_config)
239 print ("default config files not found, is myplc installed ?")
242 # local settings only, will be modified & saved
243 cwrite=PLCConfiguration()
246 cread.load(site_config)
247 cwrite.load(site_config)
249 cwrite = PLCConfiguration()
251 print ("This is %s - %s -- Type ? at the prompt for help" %(command,release))
252 mainloop (cdef, cread, cwrite,site_config)
255 if __name__ == '__main__':