initial checkin of interactive configuration script
[sfa.git] / geni-config-tty
1 #!/usr/bin/python
2
3 # Interactively prompts for variable values
4 # expected arguments are
5 # command -d [default-xml [custom-xml [ consolidated-xml ]]]
6 #
7 # -d is for the myplc-devel package
8
9 # we use 3 instances of PLCConfiguration throughout:
10 # cdef : models the defaults, from plc_default.xml
11 # cread : merged from plc_default & configs/site.xml
12 # cwrite : site.xml + pending changes
13
14 import sys
15 import os
16 import re
17 import readline
18 import traceback
19 from optparse import OptionParser
20
21 from geni.util.config import Config
22
23 usual_variables = ["GENI_REGISTRY_ROOT_AUTH",
24                    "GENI_REGISTRY_LEVEL1_AUTH",
25                    "GENI_REGISTRY_ENABLED",
26                    "GENI_REGISTRY_HOST" 
27                    "GENI_REGISTRY_PORT",
28                    "GENI_AGGREGATE_ENABLED",
29                    "GENI_AGGREGATE_HOST",
30                    "GENI_AGGREGATE_PORT",
31                    "GENI_SM_ENABLED",
32                    "GENI_SM_HOST",
33                    "GENI_SM_PORT",
34                    "GENI_PLC_USER",
35                    "GENI_PLC_PASSWORD",    
36                    "GENI_PLC_HOST",
37                    "GENI_PLC_PORT",
38                    "GENI_PLC_API_PATH"
39                    ]
40
41
42 mainloop_usage= """Available commands:
43  Uppercase versions give variables comments, when available
44  u/U\t\t\tEdit usual variables
45  w/W\t\t\tWrite / Write & reload
46  q\t\t\tQuit (without saving)
47  h/?\t\t\tThis help
48 ---
49  l/L [<var>]\tShow Locally modified variables/values
50  s/S [<var>]\tShow variables/values (all, in category, single)
51  e/E [<var>]\tEdit variables (all, in category, single)
52 ---
53 """ 
54
55 command_usage="%prog [options]"
56 command_usage += """
57   Unless you specify the -d option, meaning you want to configure
58   myplc-devel instead of regular myplc, in which case""" 
59
60 variable_usage= """Edit Commands :
61 #\tShow variable comments
62 .\tStops prompting, return to mainloop
63 /\tCleans any site-defined value, reverts to default
64 =\tShows default value
65 >\tSkips to next category
66 ?\tThis help
67 """
68
69 def save():
70     print "save"
71
72 def get_defaults():
73     geni_config = Config()
74     plc_vars = {'PLC_API_MAINTENANCE_PASSWORD': 'GENI_PLC_PASSWORD',
75                 'PLC_API_MAINTENCE_USER': 'GENI_PLC_PASSWORD'
76                }
77     try:
78         from geni.util.config import plcConfig
79         plc_config = plcConfig
80     except:
81         plc_config = None
82     
83     defaults = {}
84     for var in dir(geni_config):
85         if var.startswith('GENI'):
86             value = eval("geni_config.%s" % var)
87             defaults[var] = value
88
89     # some defaults come from plc_config
90     for var in dir(plc_config):
91         if var in plc_vars:
92             value = eval("plc_config.%s" % var)
93             defaults[plc_vars[var]] = value
94
95     return defaults       
96
97 def prompt_variable(variable, default_config):
98     if variable in default_config:
99         default_value = default_config[variable]
100     else:
101         default_value = ""  
102    #while True:
103     
104 def mainloop (default_config):
105     changes = {}
106     while True:
107         try:
108             answer = raw_input("Enter command (u for usual changes, w to save, ? for help) ").strip()
109         except EOFError:
110             answer =""
111         except KeyboardInterrupt:
112             print "\nBye"
113             sys.exit()
114
115         if (answer == "") or (answer in "?hH"):
116             print mainloop_usage
117             continue
118         if answer in ['?']:
119             print "help"  
120
121         if (answer in ["q","Q"]):
122             # todo check confirmation
123             return
124         elif (answer == "w"):
125             save()
126         elif (answer == "u"):
127             try: 
128                 for varname in usual_variables:
129                     print varname
130                     changes[varname] = prompt_variable(varname, default_config)
131             except Exception, inst:
132                 if (str(inst) != 'BailOut'):
133                     raise
134         elif (answer in ["e","E"]):
135             try:
136                 prompt_variable (cdef,cread,cwrite,category,variable,
137                                  show_comments,False)
138             except Exception, inst:
139                 if (str(inst) != 'BailOut'):
140                     raise
141         elif (command in "vVsSlL"):
142             pass
143             #show_variable (c1,c2,c3,category,variable,show_value,show_comments)
144         else:
145             print ("Unknown command >%s< -- use h for help" % answer)
146
147 ####################
148 def main ():
149
150     command=sys.argv[0]
151     argv = sys.argv[1:]
152     save = True
153     parser = OptionParser(usage=command_usage, version="%prog 1.0")
154     parser.set_defaults(config_dir="/etc/geni",
155                         usual_variables=[])
156     parser.add_option("","--configdir",dest="config_dir",help="specify configuration directory")
157     parser.add_option("","--usual_variable",dest="usual_variables",action="append", help="add a usual variable")
158
159     (config,args) = parser.parse_args()
160     if len(args)>3:
161         parser.error("too many arguments")
162
163     defaults = get_defaults()
164     mainloop (defaults)
165     return 0
166
167 if __name__ == '__main__':
168     main()