6 from optparse import OptionParser
10 # everything in the onelab.eu domain
12 testmaster = 'testmaster'
13 build_boxes = [ "mirror", "liquid", "reed", "velvet", ]
14 plc_boxes = [ "testplc" ]
16 [ "testqemu%d"%i for i in range (1,4) ] + \
17 [ "testqemu32-%d"%i for i in range (1,6) ]
18 test_boxes = plc_boxes + qemu_boxes
23 self.do_tracker = False
26 return "%s.%s"%(box,self.domain)
28 def root (box): return "root@%s"%box
30 def header (self,message):
31 print "===============",message
33 def run (self,argv,message, trash_err=False):
34 if self.options.dry_run:
38 if message: self.header(message)
42 subprocess.call(argv,stderr=file('/dev/null','w'))
44 def backquote (self, argv, trash_err=False):
46 return subprocess.Popen(argv,stdout=subprocess.PIPE).communicate()[0]
48 null = open('/dev/null','w')
49 result = subprocess.Popen(argv,stdout=subprocess.PIPE,stderr=null).communicate()[0]
53 def reboot (self,box):
54 command=['ssh',self.root(box),'shutdown','-r','now']
55 self.run (command,"Rebooting %s"%box)
57 def handle_trackers (self):
58 box = self.fqdn (self.testmaster)
59 if self.options.probe:
60 command=['ssh',self.root(box),"head","-100","tracker*"]
61 self.run(command,"Inspecting trackers on %s"%box)
63 command=['ssh',self.root(box),"rm","-rf","tracker*"]
64 self.run(command,"Cleaning up trackers on %s"%box)
66 def handle_build_box (self,box):
67 if not self.options.probe:
70 command=['ssh',self.root(box),'uptime']
71 uptime=self.backquote(command,True).strip()
73 command=['ssh',self.root(box),'pgrep','build']
74 if self.options.dry_run:
75 self.run(command,None)
77 pids=self.backquote(command,True)
79 self.header ('No build process on %s (%s)'%(box,uptime))
81 command=['ssh',self.root(box),'ps','-o','pid,command'] + [ pid for pid in pids.split("\n") if pid]
82 self.run(command,"Active build processes on %s (%s)"%(box,uptime),True)
84 vplc_matcher = re.compile(".*(vplc[0-9]+$)")
85 def vplcname (self, vservername):
86 match = self.vplc_matcher.match(vservername)
87 if match: return match.groups(0)
90 def handle_plc_box (self,box):
91 if not self.options.probe:
94 command=['ssh',self.root(box),'vserver-stat']
95 if self.options.dry_run:
96 self.run(command,"Active vservers on %s"%box)
98 # try to find fullname (vserver_stat truncates to a ridiculously short name)
100 self.header ("vserver map on %s"%box)
101 # fetch the contexts for all vservers on that box
102 map_command=['ssh',self.root(box),'grep','.','/etc/vservers/*/context','/dev/null',]
103 context_map=self.backquote (map_command)
104 # at this point we have a set of lines like
105 # /etc/vservers/2010.01.20--k27-f12-32-vplc03/context:40144
107 for map_line in context_map.split("\n"):
108 if not map_line: continue
109 [path,xid] = map_line.split(':')
110 ctx_dict[xid]=os.path.basename(os.path.dirname(path))
111 # at this point ctx_id maps context id to vservername
113 vserver_stat = self.backquote (command)
114 for vserver_line in vserver_stat.split("\n"):
115 if not vserver_line: continue
116 context=vserver_line.split()[0]
120 longname=ctx_dict[context]
121 plcname=self.vplcname(longname)
122 if plcname: print "== %s =="%plcname
123 print "%(vserver_line)s [=%(longname)s]"%locals()
125 self.run(command,"Fine-grained method failed - fallback to plain vserver-stat")
127 vnode_matcher = re.compile(".*(vnode[0-9]+)")
128 def vnodename (self, ps_line):
129 match = self.vnode_matcher.match(ps_line)
130 if match: return match.groups(0)
134 def handle_qemu_box (self,box):
135 if not self.options.probe:
138 command=['ssh',self.root(box),'pgrep','qemu']
139 if self.options.dry_run:
140 self.run(command,None)
142 pids=self.backquote(command)
144 self.header ('No qemu process on %s'%box)
146 self.header ("Active qemu processes on %s"%box)
147 command=['ssh',self.root(box),'ps','-o','pid,command'] + [ pid for pid in pids.split("\n") if pid]
148 ps_lines = self.backquote (command).split("\n")
149 for ps_line in ps_lines:
150 if not ps_line or ps_line.find('PID') >=0 : continue
151 node=self.vnodename(ps_line)
152 if node: print "== %s =="%node
155 def handle_box(self,box):
156 if box in self.qemu_boxes:
157 self.handle_qemu_box(self.fqdn(box))
158 elif box in self.plc_boxes:
159 self.handle_plc_box(self.fqdn(box))
161 self.handle_build_box(self.fqdn(box))
164 usage="""%prog [options] [hostname..(s)]
165 Default is to act on test boxes only (with trackers clean)"""
166 parser = OptionParser (usage=usage)
167 parser.add_option ("-n","--dry-run",action="store_true",dest="dry_run",default=False,
169 parser.add_option ("-r","--reboot", action="store_false",dest="probe",default=True,
170 help="Actually reset/reboot stuff instead of just probing it")
171 # no need for -p = probe, as this is the default
172 parser.add_option ("-p","--plc", action="store_true",dest="plc_only",default=False,
173 help="Acts on the plc box only")
175 parser.add_option ("-a","--all",action="store_true",dest="all_boxes",default=False,
176 help="Acts on build and test boxes")
177 parser.add_option ("-b","--build",action="store_true",dest="build_only",default=False,
178 help="Acts on build boxes only")
179 parser.add_option ("-q","--qemu",action="store_true",dest="qemu_only",default=False,
180 help="Only acts on the qemu boxes")
181 parser.add_option ("-t","--trackers",action="store_true",dest="trackers_only",default=False,
182 help="Only wipes trackers")
184 (self.options,args) = parser.parse_args()
186 # use given hostnames if provided
189 # if hostnames are specified, let's stay on the safe side and don't reset trackers
190 self.do_tracker = False
191 elif self.options.all_boxes:
192 self.boxes=self.test_boxes + self.build_boxes
193 self.do_tracker = True
194 elif self.options.build_only:
195 self.boxes=self.build_boxes
196 self.do_tracker = False
197 elif self.options.qemu_only:
198 self.boxes=self.qemu_boxes
199 self.do_tracker = False
200 elif self.options.plc_only:
201 self.boxes=self.plc_boxes
202 self.do_tracker = False
203 elif self.options.trackers_only:
205 self.do_tracker = True
208 self.boxes = self.test_boxes
209 self.do_tracker = True
212 self.handle_trackers ()
213 for box in self.boxes:
214 self.handle_box (box)
217 if __name__ == "__main__":