5 from optparse import OptionParser
9 from TestPlc import TestPlc
10 from TestSite import TestSite
11 from TestNode import TestNode
15 subversion_id = "$Id$"
17 default_config = [ 'onelab' ]
19 default_steps = ['uninstall','install','install_rpm',
21 'clear_ssh_config','store_keys', 'initscripts',
22 'sites', 'nodes', 'slices',
23 'bootcd', 'nodegroups',
24 'start_nodes','standby', 'check_nodesStatus','check_nodesConnectivity', 'check_slices' ]
25 other_steps = [ 'fresh_install', 'stop',
26 'clean_sites', 'clean_nodes', 'clean_slices', 'clean_keys',
27 'stop_nodes' , 'db_dump' , 'db_restore',
29 default_build_url = "http://svn.planet-lab.org/svn/build/trunk"
32 self.path=os.path.dirname(sys.argv[0])
35 def show_env (options, message):
36 utils.header (message)
37 utils.show_spec("main options",options)
40 def optparse_list (option, opt, value, parser):
42 setattr(parser.values,option.dest,getattr(parser.values,option.dest)+value.split())
44 setattr(parser.values,option.dest,value.split())
47 steps_message="Defaut steps are\n\t%s"%(" ".join(TestMain.default_steps))
48 steps_message += "\nOther useful steps are\n\t %s"%(" ".join(TestMain.other_steps))
49 usage = """usage: %%prog [options] steps
50 myplc-url defaults to the last value used, as stored in arg-myplc-url,
52 build-url defaults to the last value used, as stored in arg-build-url,
54 config defaults to the last value used, as stored in arg-config,
56 ips defaults to the last value used, as stored in arg-ips,
57 default is to use IP scanning
58 steps refer to a method in TestPlc or to a step_* module
60 """%(TestMain.default_build_url,TestMain.default_config)
61 usage += steps_message
62 parser=OptionParser(usage=usage,version=self.subversion_id)
63 parser.add_option("-u","--url",action="store", dest="myplc_url",
64 help="myplc URL - for locating build output")
65 parser.add_option("-b","--build",action="store", dest="build_url",
66 help="Build URL - for using vtest-init-vserver.sh in native mode")
67 parser.add_option("-c","--config",action="callback", callback=TestMain.optparse_list, dest="config",
68 nargs=1,type="string",
69 help="Config module - can be set multiple times, or use quotes")
70 parser.add_option("-a","--all",action="store_true",dest="all_steps", default=False,
71 help="Run all default steps")
72 parser.add_option("-l","--list",action="store_true",dest="list_steps", default=False,
73 help="List known steps")
74 parser.add_option("-s","--state",action="store",dest="dbname",default=None,
75 help="Used by db_dump and db_restore")
76 parser.add_option("-d","--display", action="store", dest="display", default='bellami.inria.fr:0.0',
77 help="Set DISPLAY for vmplayer")
78 parser.add_option("-i","--ip",action="callback", callback=TestMain.optparse_list, dest="ips",
79 nargs=1,type="string",
80 help="Specify the set of IP addresses to use in vserver mode (disable scanning)")
81 parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False,
82 help="Run in verbose mode")
83 parser.add_option("-n","--dry-run", action="store_true", dest="dry_run", default=False,
84 help="Show environment and exits")
85 parser.add_option("-f","--forcenm", action="store_true", dest="forcenm", default=False,
86 help="Force the NM to restart in check_slices step")
87 (self.options, self.args) = parser.parse_args()
89 if len(self.args) == 0:
90 if self.options.all_steps:
91 self.options.steps=TestMain.default_steps
92 elif self.options.dry_run:
93 self.options.steps=TestMain.default_steps
94 elif self.options.list_steps:
98 print 'No step found (do you mean -a ? )'
99 print "Run %s --help for help"%sys.argv[0]
102 self.options.steps = self.args
104 # handle defaults and option persistence
105 for (recname,filename,default) in ( ('myplc_url','arg-myplc-url',"") ,
106 ('build_url','arg-build-url',TestMain.default_build_url) ,
107 ('ips','arg-ips',[]) ,
108 ('config','arg-config',TestMain.default_config) , ) :
109 print 'handling',recname
110 path="%s/%s"%(self.path,filename)
111 is_list = isinstance(default,list)
112 if not getattr(self.options,recname):
114 parsed=file(path).readlines()
115 if not is_list: # strings
117 print "%s - error when parsing %s"%(sys.argv[1],path)
119 parsed=parsed[0].strip()
121 parsed=[x.strip() for x in parsed]
122 setattr(self.options,recname,parsed)
125 setattr(self.options,recname,default)
127 print "Cannot determine",recname
128 print "Run %s --help for help"%sys.argv[0]
130 utils.header('* Using %s = %s'%(recname,getattr(self.options,recname)))
135 fsave.write(getattr(self.options,recname) + "\n")
137 for value in getattr(self.options,recname):
138 fsave.write(value + "\n")
140 utils.header('Saved %s into %s'%(recname,filename))
143 if not self.options.steps:
145 #self.options.steps=['dump','clean','install','populate']
146 self.options.steps=TestMain.default_steps
148 # store self.path in options.path for the various callbacks
149 self.options.path = self.path
151 if self.options.verbose:
152 self.show_env(self.options,"Verbose")
156 for config in self.options.config:
157 modulename='config_'+config
159 m = __import__(modulename)
160 all_plc_specs = m.config(all_plc_specs,self.options)
162 traceback.print_exc()
163 print 'Cannot load config %s -- ignored'%modulename
166 utils.show_spec("Test specifications",all_plc_specs)
167 # build a TestPlc object from the result
168 for spec in all_plc_specs:
169 spec['disabled'] = False
170 all_plcs = [ (x, TestPlc(x)) for x in all_plc_specs]
172 overall_result = True
173 testplc_method_dict = __import__("TestPlc").__dict__['TestPlc'].__dict__
175 for step in self.options.steps:
176 # try and locate a method in TestPlc
177 if testplc_method_dict.has_key(step):
178 all_step_infos += [ (step, testplc_method_dict[step] )]
179 # otherwise search for the 'run' method in the step_<x> module
181 modulename='step_'+step
183 # locate all methods named run* in the module
184 module_dict = __import__(modulename).__dict__
185 names = [ key for key in module_dict.keys() if key.find("run")==0 ]
187 raise Exception,"No run* method in module %s"%modulename
189 all_step_infos += [ ("%s.%s"%(step,name),module_dict[name]) for name in names ]
191 print 'Step %s -- ignored'%(step)
192 traceback.print_exc()
193 overall_result = False
195 if self.options.dry_run:
196 self.show_env(self.options,"Dry run")
199 # do all steps on all plcs
200 for (stepname,method) in all_step_infos:
201 for (spec,obj) in all_plcs:
204 utils.header("Plc %s is disabled - skipping step %s"%(plcname,stepname))
207 utils.header("Running step %s on plc %s"%(stepname,plcname))
208 step_result = method(obj,self.options)
210 utils.header('Successful step %s on %s'%(stepname,plcname))
212 overall_result = False
213 spec['disabled'] = True
214 utils.header('Step %s on %s FAILED - discarding that plc from further steps'%(stepname,plcname))
217 spec['disabled'] = True
218 utils.header ('Step %s on plc %s FAILED (exception) - discarding this plc from further steps'%(stepname,plcname))
219 traceback.print_exc()
220 return overall_result
222 # wrapper to run, returns a shell-compatible result
233 traceback.print_exc()
236 if __name__ == "__main__":
237 sys.exit(TestMain().main())