From: Thierry Parmentelat Date: Tue, 26 May 2009 13:45:30 +0000 (+0000) Subject: no more plc-devel to configure, so the simpler is better X-Git-Tag: MyPLC-4.3-12~2 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=d6abc618d545cb4ca4e4edd22d81de51f4c7b174;p=myplc.git no more plc-devel to configure, so the simpler is better --- diff --git a/plc-config-tty b/plc-config-tty index 7cfcdf7..2c2505b 100755 --- a/plc-config-tty +++ b/plc-config-tty @@ -22,103 +22,46 @@ from plc_config import ConfigurationException #################### release_id = "$Id$" release_rev = "$Revision$" +release_url = "$URL$" - -flavours={} - -def noop_validator(v): - pass - -def plc_validator(validated_variables): +def validator (validated_variables): maint_user = validated_variables["PLC_API_MAINTENANCE_USER"] root_user = validated_variables["PLC_ROOT_USER"] if maint_user == root_user: - raise ConfigurationException("The Maintenance Account email address cannot be the same as the Root User email address") - -flavours["plc"]={'service':"plc", - 'usual_variables':["PLC_NAME", - "PLC_SHORTNAME", - "PLC_SLICE_PREFIX", - "PLC_ROOT_USER", - "PLC_ROOT_PASSWORD", - "PLC_MAIL_ENABLED", - "PLC_MAIL_SUPPORT_ADDRESS", - "PLC_DB_HOST", - "PLC_API_HOST", - "PLC_WWW_HOST", - "PLC_BOOT_HOST", - "PLC_NET_DNS1", - "PLC_NET_DNS2", - ], - 'config_dir':"/etc/planetlab", - 'validate_variables':{"PLC_API":"MAINTENANCE_USER","PLC":"ROOT_USER"}, - 'validator':plc_validator, - } - -defined_flavour = "plc" + raise ConfigurationException("PLC_API_MAINTENANCE_USER=%s cannot be the same as PLC_ROOT_USER=%s"%(maint_user,root_user)) # historically we could also configure the devel pkg.... -def init_flavour (flavour): - global service, usual_variables - - global defined_flavour - if flavours.has_key(flavour): - defined_flavour = flavour - else: - defined_flavour = "plc" - - flav=flavours.get(flavour,flavours["plc"]) - service=flav["service"] - usual_variables=flav["usual_variables"] - config_dir=flav["config_dir"] - - global def_default_config, def_site_config, def_consolidated_config - def_default_config= "%s/default_config.xml" % config_dir - def_site_config = "%s/configs/site.xml" % config_dir - def_consolidated_config = "%s/plc_config.xml" % config_dir - - global mainloop_usage - mainloop_usage= """Available commands: - Uppercase versions give variables comments, when available - u/U\t\t\tEdit usual variables - w/W\t\t\tWrite / Write & reload - r\t\t\tRestart %s service - q\t\t\tQuit (without saving) - h/?\t\t\tThis help ---- - l/L [|]\tShow Locally modified variables/values - s/S [|]\tShow variables/values (all, in category, single) - e/E [|]\tEdit variables (all, in category, single) ---- - c\t\t\tList categories - v/V [|]List Variables (all, in category, single) ---- -Typical usage involves: u, [l,] w, r, q -""" % service +globals = { + 'def_default_config' : "/etc/planetlab/default_config.xml", + 'def_site_config' : "/etc/planetlab/configs/site.xml", + 'def_consolidated_config' : "/etc/planetlab/plc_config.xml", + 'usual_variables': ["PLC_NAME", + "PLC_SHORTNAME", + "PLC_SLICE_PREFIX", + "PLC_ROOT_USER", + "PLC_ROOT_PASSWORD", + "PLC_MAIL_ENABLED", + "PLC_MAIL_SUPPORT_ADDRESS", + "PLC_DB_HOST", + "PLC_API_HOST", + "PLC_WWW_HOST", + "PLC_BOOT_HOST", + "PLC_NET_DNS1", + "PLC_NET_DNS2", + ], + } def usage (): command_usage="%prog [options] [default-xml [site-xml [consolidated-xml]]]" - init_flavour ("plc") - command_usage +=""" -\t default-xml defaults to %s -\t site-xml defaults to %s -\t consolidated-xml defaults to %s""" % (def_default_config,def_site_config, def_consolidated_config) + global globals command_usage +=""" \t default-xml defaults to %s \t site-xml defaults to %s -\t consolidated-xml defaults to %s""" % (def_default_config,def_site_config, def_consolidated_config) +\t consolidated-xml defaults to %s""" % (globals['def_default_config'], + globals['def_site_config'], + globals['def_consolidated_config']) return command_usage -#################### -variable_usage= """Edit Commands : -#\tShow variable comments -.\tStops prompting, return to mainloop -/\tCleans any site-defined value, reverts to default -=\tShows default value ->\tSkips to next category -?\tThis help -""" - #################### def get_value (config, category_id, variable_id): (category, variable) = config.get (category_id, variable_id) @@ -190,7 +133,6 @@ def print_category (config, cid, show_comments=True): #################### def consolidate (default_config, site_config, consolidated_config): - global service try: conso = PLCConfiguration (default_config) conso.load (site_config) @@ -202,16 +144,14 @@ def consolidate (default_config, site_config, consolidated_config): consolidated_config)) def reload_service (): - global service - os.system("set -x ; service %s reload" % service) + os.system("set -x ; service plc reload") #################### def restart_service (): - global service - print ("==================== Stopping %s" % service) - os.system("service %s stop" % service) - print ("==================== Starting %s" % service) - os.system("service %s start" % service) + print ("==================== Stopping plc" ) + os.system("service plc stop" ) + print ("==================== Starting plc" ) + os.system("service plc start") #################### def prompt_variable (cdef, cread, cwrite, category, variable, @@ -247,6 +187,14 @@ def prompt_variable (cdef, cread, cwrite, category, variable, elif (answer == "#"): print_name_comments(cread,category_id,variable_id) elif (answer == "?"): + variable_usage = """Edit Commands : +#\tShow variable comments +.\tStops prompting, return to mainloop +/\tCleans any site-defined value, reverts to default +=\tShows default value +>\tSkips to next category +?\tThis help +""" print variable_usage.strip() elif (answer == "="): print ("%s defaults to %s" %(varname,default_value)) @@ -336,7 +284,6 @@ matcher_mainloop_0arg=re.compile(re_mainloop_0arg) matcher_mainloop_1arg=re.compile(re_mainloop_1arg) def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_config): - global service while True: try: answer = raw_input("Enter command (u for usual changes, w to save, ? for help) ").strip() @@ -347,6 +294,23 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con sys.exit() if (answer == "") or (answer in "?hH"): + mainloop_usage = """Available commands: + Uppercase versions give variables comments, when available + u/U\t\t\tEdit usual variables + w/W\t\t\tWrite / Write & reload + r\t\t\tRestart plc service + q\t\t\tQuit (without saving) + h/?\t\t\tThis help +--- + l/L [|]\tShow Locally modified variables/values + s/S [|]\tShow variables/values (all, in category, single) + e/E [|]\tEdit variables (all, in category, single) +--- + c\t\t\tList categories + v/V [|]List Variables (all, in category, single) +--- +Typical usage involves: u, [l,] w, r, q +""" print mainloop_usage continue groups_parse = matcher_mainloop_0arg.match(answer) @@ -392,9 +356,8 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con global defined_flavour try: # Confirm that various constraints are met before saving file. - validate_variables = flavours[defined_flavour].get('validate_variables',{}) + validate_variables = {"PLC_API":"MAINTENANCE_USER","PLC":"ROOT_USER"} validated_variables = cwrite.verify(cdef, cread, validate_variables) - validator = flavours[defined_flavour].get('validator',noop_validator) validator(validated_variables) cwrite.save(site_config) except ConfigurationException, e: @@ -404,13 +367,13 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con print traceback.print_exc() print ("Could not save -- fix write access on %s" % site_config) break - print ("Wrote %s" % site_config) + print "Wrote",site_config consolidate(default_config, site_config, consolidated_config) - print ("You might want to type 'r' (restart %s), 'R' (reload %s) or 'q' (quit)" % \ - (service,service)) + print "You might want to type 'r' (restart plc), 'R' (reload plc) or 'q' (quit)" elif (command == "u"): try: - for varname in usual_variables: + global globals + for varname in globals['usual_variables']: (category,variable) = cdef.locate_varname(varname) prompt_variable(cdef, cread, cwrite, category, variable, False) except Exception, inst: @@ -456,11 +419,11 @@ def check_dir (config_file): try: os.makedirs(dirname,0755) except OSError, e: - print "Cannot create dir %s due to %s - exiting" % (dirname,e) + print "Cannot create dir %s for file %s - error=[%s] - exiting" % (dirname,config_file,e) sys.exit(1) if (not os.path.exists (dirname)): - print "Cannot create dir %s - exiting" % dirname + print "Cannot create dir %s for file %s - exiting" % (dirname,config_file) sys.exit(1) else: print "Created directory %s" % dirname @@ -468,52 +431,16 @@ def check_dir (config_file): #################### def main (): - command=sys.argv[0] - argv = sys.argv[1:] - save = True - parser = OptionParser(usage=usage(), version="%prog 1.0") - parser.set_defaults(flavour="plc", - config="flavour.config", - config_dir=None, - service=None, - usual_variables=[]) - parser.add_option("","--configdir",dest="config_dir",help="specify configuration directory") - parser.add_option("","--service",dest="service",help="specify /etc/init.d style service name") - parser.add_option("","--usual_variable",dest="usual_variables",action="append", help="add a usual variable") - parser.add_option("","--flavour",dest="flavour", help="Sets the configuration flavour") - + parser = OptionParser(usage=usage(), version="%prog " + release_rev + release_url) (config,args) = parser.parse_args() if len(args)>3: parser.error("too many arguments") - if config.flavour not in flavours: - if config.service==None: - parser.error("unknown flavour '%s'" % config.flavour) - else: - flavours[config.flavour]={} - flavour=flavours[config.flavour] - flavour['service']=config.service - flavour['usual_variables']=config.usual_variables - if config.config_dir==None: - flavour['config_dir']="/etc/%s"%config.service - else: - flavour['config_dir']=config.config_dir - else: - flavour=flavours[config.flavour] - - # in case the config dir should be something other than /etc/planetlab - if config.config_dir <> None: - flavour['config_dir']=config.config_dir - - # add in new usual_variables defined on the command line - for usual_variable in config.usual_variables: - if usual_variable not in flavour['usual_variables']: - flavour['usual_variables'].append(usual_variable) - - # intialize flavour - init_flavour(config.flavour) - - (default_config,site_config,consolidated_config) = (def_default_config, def_site_config, def_consolidated_config) + # use any provided arg as advertised + global globals + (default_config,site_config,consolidated_config) = (globals['def_default_config'], + globals['def_site_config'], + globals['def_consolidated_config']) if len(args) >= 1: default_config=args[0] if len(args) >= 2: