verify phase fixed and re-enabled
[myplc.git] / plc-config-tty
index 9bb8abe..1e45cdc 100755 (executable)
@@ -6,6 +6,11 @@
 #
 # -d is for the myplc-devel package
 
+# we use 3 instances of PLCConfiguration throughout:
+# cdef : models the defaults, from plc_default.xml
+# cread : merged from plc_default & configs/site.xml
+# cwrite : site.xml + pending changes
+
 import sys
 import os
 import re
@@ -13,31 +18,35 @@ import readline
 import getopt
 
 from plc_config import PLCConfiguration
+from plc_config import ConfigurationException
 
 ####################
-release_id = "$Id: plc-config-tty,v 1.5 2006/08/08 16:59:23 thierry Exp $"
+release_id = "$Id$"
 release_rev = "$Revision$"
 
 def init_flavour (flavour):
     global service
-    global common_variables
+    global usual_variables
     if (flavour == "devel"):
         service="plc-devel"
-        common_variables=("PLC_DEVEL_FEDORA_URL",
+        usual_variables=("PLC_DEVEL_FEDORA_URL",
                           "PLC_DEVEL_CVSROOT")
         config_dir = "/plc/devel/data/etc/planetlab"
     else:
         service="plc"
-        common_variables=("PLC_NAME",
-                          "PLC_ROOT_USER",
-                          "PLC_ROOT_PASSWORD",
-                          "PLC_MAIL_SUPPORT_ADDRESS",
-                          "PLC_DB_HOST",
-                          "PLC_API_HOST",
-                          "PLC_WWW_HOST",
-                          "PLC_BOOT_HOST",
-                          "PLC_NET_DNS1",
-                          "PLC_NET_DNS2")
+        usual_variables=("PLC_NAME",
+                         "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"
     global def_default_config
     def_default_config= "%s/default_config.xml" % config_dir
@@ -49,18 +58,18 @@ def init_flavour (flavour):
     global mainloop_usage
     mainloop_usage= """Available commands:
  Uppercase versions give variables comments, when available
--u/U\t\t\tEdit usual variables
--w\t\t\tWrite & consolidate
--r\t\t\tRestart %s service
--q\t\t\tQuit (without saving)
--h/?\t\t\tThis help
+ u/U\t\t\tEdit usual variables
+ w\t\t\tWrite & consolidate
+ r\t\t\tRestart %s service
+ q\t\t\tQuit (without saving)
+ h/?\t\t\tThis help
 ---
-l/L [<cat>|<var>]\tShow Locally modified variables/values
--s/S [<cat>|<var>]\tShow variables/values (all, in category, single)
--e/E [<cat>|<var>]\tEdit variables (all, in category, single)
+ l/L [<cat>|<var>]\tShow Locally modified variables/values
+ s/S [<cat>|<var>]\tShow variables/values (all, in category, single)
+ e/E [<cat>|<var>]\tEdit variables (all, in category, single)
 ---
--c\t\t\tList categories
--v/V [<cat>|<var>]List Variables (all, in category, single)
+ c\t\t\tList categories
+ v/V [<cat>|<var>]List Variables (all, in category, single)
 ---
 Typical usage involves: u, [l,] w, r, q
 """ % service
@@ -174,6 +183,7 @@ def consolidate (default_config, site_config, consolidated_config):
         return
     print ("Merged\n\t%s\nand\t%s\ninto\t%s"%(default_config,site_config,
                                               consolidated_config))
+    os.system("set -x ; service plc reload")
         
 ####################
 def restart_plc ():
@@ -351,8 +361,14 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con
             return
         elif (command in "wW"):
             try:
+                # Confirm that various constraints are met before saving file.
+                cwrite.verify(cdef, cread)
                 cwrite.save(site_config)
+            except ConfigurationException, e:
+                print "Save failed due to a configuration exception: %s" % e
+                break;
             except:
+                import traceback; print traceback.print_exc()
                 print ("Could not save -- fix write access on %s" % site_config)
                 break
             print ("Wrote %s" % site_config)
@@ -360,7 +376,7 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con
             print ("You might want to type 'r' (restart plc) or 'q' (quit)")
         elif (command == "u"):
             try:
-                for varname in common_variables:
+                for varname in usual_variables:
                     (category,variable) = cdef.locate_varname(varname)
                     prompt_variable(cdef, cread, cwrite, category, variable, False)
             except Exception, inst:
@@ -376,8 +392,12 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con
             elif mode == 'CATEGORY':
                 prompt_variables_category(cdef,cread,cwrite,category_id,show_comments)
             elif mode == 'VARIABLE':
-                prompt_variable (cdef,cread,cwrite,category,variable,
-                                 show_comments,False)
+                try:
+                    prompt_variable (cdef,cread,cwrite,category,variable,
+                                     show_comments,False)
+                except Exception, inst:
+                    if (str(inst) != 'BailOut'):
+                        raise
         elif (command in "vVsSlL"):
             show_value=(command in "sSlL")
             (c1,c2,c3) = (cdef, cread, cwrite)
@@ -393,23 +413,16 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con
             print ("Unknown command >%s< -- use h for help" % answer)
 
 ####################
+# creates directory for file if not yet existing
 def check_dir (config_file):
     dirname = os.path.dirname (config_file)
     if (not os.path.exists (dirname)):
-        print "Config file %s located under a non-existing directory" % config_file
-        answer=raw_input("Want to create %s [y]/n ? " % dirname)
-        answer = answer.lower()
-        if (answer == 'n'):
-            print "Cannot proceed - good bye"
+        os.makedirs(dirname,0755)
+        if (not os.path.exists (dirname)):
+            print "Cannot create dir %s - exiting" % dirname
             sys.exit(1)
         else:
-            os.makedirs(dirname,0755)
-            if (not os.path.exists (dirname)):
-                print "Cannot create dir %s - exiting" % dirname
-                sys.exit(1)
-            else:
-                print "Created directory %s" % dirname
-
+            print "Created directory %s" % dirname
                 
 ####################
 def main ():
@@ -425,7 +438,7 @@ def main ():
         if opt[0] == "-h":
             usage()
         if opt[0] == "-v":
-            print ("This is %s - %s" %(command,release_id))
+            print ("This is %s - %s" %(command,release_rev))
             sys.exit(1)
         if opt[0] == "-d":
             init_flavour("devel")