6 from optparse import OptionParser
10 # everything in the onelab.eu domain
11 domain = 'pl.sophia.inria.fr'
12 build_boxes = [ "mirror", "liquid", "reed", "velvet", ]
13 plc_boxes = [ "testplc" ]
14 testmaster = 'testmaster'
15 testmaster_boxes = [ testmaster ]
16 # cache the list of qemu boxes in ~/.qemu-boxes
17 # this can be refreshed by running -c
20 def cache_file (self): return os.path.expanduser("~/.qemu-boxes")
22 def load_cache (self):
23 cache=self.cache_file()
24 if os.path.isfile(cache):
25 self.qemu_boxes=file(cache).read().split()
26 self.test_boxes = self.plc_boxes + self.qemu_boxes
28 # run LocalTestResources on testmaster
29 def refresh_cache (self):
31 self.backquote_ssh(self.fqdn(self.testmaster),['LocalTestResources.py'],trash_err=True)
32 remove="."+BuildBoxes.domain
33 retrieved = [ x.replace(remove,"").strip() for x in retrieved.split()]
34 self.qemu_boxes = retrieved
35 cache=self.cache_file()
36 file(cache,'w').write(' '.join(self.qemu_boxes)+'\n')
37 print "New contents of %s:"%cache
38 print file(cache).read(),
43 self.do_tracker_qemus = False
44 self.do_tracker_plcs = False
48 return "%s.%s"%(box,self.domain)
50 ssh_command=['ssh','-o','ConnectTimeout=3']
52 def root (box): return "root@%s"%box
56 return BuildBoxes.ssh_command + [ BuildBoxes.root(box) ]
58 def header (self,message):
59 print "===============",message
62 def run (self,argv,message, trash_err=False):
63 if self.options.dry_run:
68 if message: self.header(message)
70 return subprocess.call(argv)
72 return subprocess.call(argv,stderr=file('/dev/null','w'))
74 def run_ssh (self, box, argv, message, trash_err=False):
75 result=self.run (self.ssh(box) + argv, message, trash_err)
77 print "WARNING: failed to run %s on %s"%(" ".join(argv),box)
80 def backquote (self, argv, trash_err=False):
82 return subprocess.Popen(argv,stdout=subprocess.PIPE).communicate()[0]
84 return subprocess.Popen(argv,stdout=subprocess.PIPE,stderr=file('/dev/null','w')).communicate()[0]
86 def backquote_ssh (self, box, argv, trash_err=False):
87 # first probe the ssh link
88 hostname=self.backquote ( self.ssh(box) + [ "hostname"], trash_err=True )
90 print "%s unreachable"%self.root(box)
93 return self.backquote( ['ssh',self.root(box)] + argv, trash_err)
95 def reboot (self,box):
96 command=['ssh',self.root(box),'shutdown','-r','now']
97 self.run (command,"Rebooting %s"%box)
99 def handle_tracker_plcs (self):
100 box = self.fqdn (self.testmaster)
101 filename="tracker-plcs"
102 if not self.options.probe:
103 command=["rm","-rf",filename]
104 self.run_ssh(box,command,"Cleaning up %s on %s"%(filename,box))
106 self.header ("++++++++++ Inspecting %s on %s"%(filename,box))
107 read_command = ["cat",filename]
108 trackers=self.backquote_ssh(box,read_command)
109 for tracker in trackers.split('\n'):
110 if not tracker: continue
112 tracker=tracker.strip()
113 [hostname,buildname]=tracker.split('@')
114 [left,plcname]=buildname.rsplit('-',1)
115 print self.margin_outline(plcname),tracker
117 print self.margin(""),tracker
119 def handle_tracker_qemus (self):
120 box = self.fqdn (self.testmaster)
121 filename="tracker-qemus"
122 if not self.options.probe:
123 command=["rm","-rf",filename]
124 self.run_ssh(box,command,"Cleaning up %s on %s"%(filename,box))
126 self.header ("++++++++++ Inspecting %s on %s"%(filename,box))
127 read_command = ["cat",filename]
128 trackers=self.backquote_ssh(box,read_command)
129 for tracker in trackers.split('\n'):
130 if not tracker: continue
132 tracker=tracker.strip()
133 [hostname,buildname,nodename]=tracker.split('@')
134 nodename=nodename.split('.')[0]
135 print self.margin_outline(nodename),tracker
137 print self.margin(""),tracker
139 def handle_build_box (self,box):
140 if not self.options.probe:
144 uptime=self.backquote_ssh(box,command,True).strip()
146 command=['pgrep','build']
147 if self.options.dry_run:
148 self.run_ssh(box,command,None)
150 pids=self.backquote_ssh(box,command,True)
152 self.header ('No build process on %s (%s)'%(box,uptime))
154 command=['ps','-o','pid,command'] + [ pid for pid in pids.split("\n") if pid]
155 self.run_ssh(box,command,"Active build processes on %s (%s)"%(box,uptime),True)
157 # this one is more accurate as it locates processes in the vservers as well
158 # but it's so sloooowww
159 def handle_build_box_deep (self,box):
160 if not self.options.probe:
164 uptime=self.backquote_ssh(box,command,True).strip()
167 if self.options.dry_run:
168 self.run_ssh(box,command,None)
170 # simulate grep vbuild
171 vps_lines=[ line for line in self.backquote_ssh(box,command,True).split("\n")
172 if line.find('vbuild') >= 0]
173 pids=[ line.split()[0] for line in vps_lines ]
175 self.header ('No build process on %s (%s)'%(box,uptime))
177 command=['vps','-o','pid,command'] + pids
178 self.run_ssh(box,command,"Active build processes on %s (%s)"%(box,uptime),True)
181 vplc_matcher = re.compile(".*(vplc[0-9]+$)")
182 def vplcname (self, vservername):
183 match = self.vplc_matcher.match(vservername)
184 if match: return match.groups(0)
187 margin_format="%-14s"
188 def margin(self,string): return self.margin_format%string
189 def outline (self, string): return '== %s =='%string
190 def margin_outline (self, string): return self.margin(self.outline(string))
192 def handle_plc_box (self,box):
193 if not self.options.probe:
196 command=['vserver-stat']
197 if self.options.dry_run:
198 self.run_ssh(box,command,"Active vservers on %s"%box)
200 # try to find fullname (vserver_stat truncates to a ridiculously short name)
202 self.header ("vserver map on %s"%box)
203 # fetch the contexts for all vservers on that box
204 map_command=['grep','.','/etc/vservers/*/context','/dev/null',]
205 context_map=self.backquote_ssh (box,map_command)
206 # at this point we have a set of lines like
207 # /etc/vservers/2010.01.20--k27-f12-32-vplc03/context:40144
209 for map_line in context_map.split("\n"):
210 if not map_line: continue
211 [path,xid] = map_line.split(':')
212 ctx_dict[xid]=os.path.basename(os.path.dirname(path))
213 # at this point ctx_id maps context id to vservername
215 vserver_stat = self.backquote_ssh (box,command)
216 for vserver_line in vserver_stat.split("\n"):
217 if not vserver_line: continue
218 context=vserver_line.split()[0]
220 print self.margin(""),vserver_line
222 longname=ctx_dict[context]
223 print self.margin_outline(self.vplcname(longname)),"%(vserver_line)s [=%(longname)s]"%locals()
225 self.run_ssh(box,command,"Fine-grained method failed - fallback to plain vserver-stat")
227 vnode_matcher = re.compile(".*(vnode[0-9]+)")
228 def vnodename (self, ps_line):
229 match = self.vnode_matcher.match(ps_line)
230 if match: return match.groups(0)
233 def handle_qemu_box (self,box):
234 if not self.options.probe:
238 modules=self.backquote_ssh(box,command).split('\n')
239 kqemu_msg='*NO kqemu MODULE LOADED*'
240 for module in modules:
241 if module.find('kqemu')==0:
244 command=['pgrep','qemu']
245 if self.options.dry_run:
246 self.run_ssh(box,command,None)
248 pids=self.backquote_ssh(box,command)
250 self.header ('No qemu process on %s'%box)
252 self.header ("Active qemu processes on %s (%s)"%(box,kqemu_msg))
253 command=['ps','-o','pid,command'] + [ pid for pid in pids.split("\n") if pid]
254 ps_lines = self.backquote_ssh (box,command).split("\n")
255 for ps_line in ps_lines:
256 if not ps_line or ps_line.find('PID') >=0 : continue
257 print self.margin_outline(self.vnodename(ps_line)), ps_line
259 # the ouput of ps -o pid,command gives us <pid> bash <buildname>/run_log
260 def testmaster_buildname (self, ps_line):
261 chunks=ps_line.split()
263 [buildname,command]=path.split('/')
266 def handle_testmaster_box (self, box):
267 if not self.options.probe:
270 command=['pgrep','run_log']
271 if self.options.dry_run:
272 self.run_ssh(box,command,None)
274 pids=self.backquote_ssh(box,command)
276 self.header ('No run_log process on %s'%box)
278 self.header ("Active run_log processes on %s"%(box))
279 command=['ps','-o','pid,command'] + [ pid for pid in pids.split("\n") if pid]
280 ps_lines = self.backquote_ssh (box,command).split("\n")
281 for ps_line in ps_lines:
282 if not ps_line or ps_line.find('PID') >=0 : continue
283 print self.margin_outline(self.testmaster_buildname(ps_line)), ps_line
286 def handle_box(self,box,type):
287 if box in self.qemu_boxes:
288 if type=="qemu": self.handle_qemu_box(self.fqdn(box))
289 elif box in self.plc_boxes:
290 if type=="plc": self.handle_plc_box(self.fqdn(box))
291 elif box in self.testmaster_boxes:
292 if type=='testmaster': self.handle_testmaster_box(self.fqdn(box))
294 if self.options.deep:
295 self.handle_build_box_deep(self.fqdn(box))
297 self.handle_build_box(self.fqdn(box))
299 def handle_disk (self,box):
301 return self.run_ssh(box,["df","-h",],"Disk space on %s"%box)
304 usage="""%prog [options] [hostname..(s)]
305 Default is to act on test boxes only"""
306 parser = OptionParser (usage=usage)
307 parser.add_option ("-n","--dry-run",action="store_true",dest="dry_run",default=False,
309 parser.add_option ("-r","--reboot", action="store_false",dest="probe",default=True,
310 help="Actually reset/reboot stuff instead of just probing it")
311 # no need for -p = probe, as this is the default
312 parser.add_option ("-p","--plc", action="store_true",dest="plc_only",default=False,
313 help="Acts on the plc box only")
315 parser.add_option ("-a","--all",action="store_true",dest="all_boxes",default=False,
316 help="Acts on build and test boxes")
317 parser.add_option ("-b","--build",action="store_true",dest="build_only",default=False,
318 help="Acts on build boxes only")
319 parser.add_option ("-e","--deep",action="store_true", dest="deep", default=False,
320 help="on build boxes, shows vbuild processes in vservers as well; signif. slower")
321 parser.add_option ("-q","--qemu",action="store_true",dest="qemu_only",default=False,
322 help="Only acts on the qemu boxes")
323 parser.add_option ("-t","--trackers",action="store_true",dest="trackers_only",default=False,
324 help="Only wipes trackers")
325 parser.add_option ("-m","--master",action="store_true",dest="testmaster_only",default=False,
326 help="Display the testmaster status")
327 parser.add_option ("-d","--disk",action="store_true",dest="show_disk",default=False,
328 help="Only inspects disk status")
329 parser.add_option ("-c","--refresh-cache",action="store_true",dest="refresh_cache", default=False,
330 help="Refresh cached list of qemu boxes at testmaster - implies -q")
332 (self.options,args) = parser.parse_args()
335 if self.options.refresh_cache:
336 self.options.qemu_only=True
339 # use given hostnames if provided
342 # if hostnames are specified, let's stay on the safe side and don't reset trackers
343 self.do_tracker_plcs = False
344 self.do_tracker_qemus = False
345 elif self.options.all_boxes:
346 self.boxes=self.test_boxes + self.build_boxes + self.testmaster_boxes
347 self.do_tracker_plcs = True
348 self.do_tracker_qemus = True
349 elif self.options.build_only:
350 self.boxes=self.build_boxes
351 self.do_tracker_plcs = False
352 self.do_tracker_qemus = False
353 elif self.options.qemu_only:
354 self.boxes=self.qemu_boxes
355 self.do_tracker_plcs = False
356 self.do_tracker_qemus = True
357 elif self.options.plc_only:
358 self.boxes=self.plc_boxes
359 self.do_tracker_plcs = True
360 self.do_tracker_qemus = False
361 elif self.options.testmaster_only:
362 self.boxes=self.testmaster_boxes
363 self.do_tracker_plcs = False
364 self.do_tracker_qemus = False
365 elif self.options.trackers_only:
367 self.do_tracker_plcs = True
368 self.do_tracker_qemus = True
371 self.boxes = self.test_boxes
372 self.do_tracker_plcs = True
373 self.do_tracker_qemus = True
375 if self.options.show_disk:
376 for box in self.boxes: self.handle_disk(box)
380 if self.do_tracker_plcs:self.handle_tracker_plcs ()
381 for box in self.boxes: self.handle_box (box,"plc")
383 if self.do_tracker_qemus:self.handle_tracker_qemus ()
384 for box in self.boxes: self.handle_box (box,"qemu")
386 for box in self.boxes: self.handle_box (box,"build")
388 for box in self.boxes: self.handle_box (box,"testmaster")
390 if __name__ == "__main__":