6 from optparse import OptionParser
10 # everything in the onelab.eu domain
12 testmaster = 'testmaster'
15 [ "build%d"%i for i in range (1,4) ]
16 plc_boxes = [ "testplc" ]
18 [ "testqemu%d"%i for i in range (1,4) ] + \
19 [ "testqemu32-%d"%i for i in range (1,6) ]
20 test_boxes = plc_boxes + qemu_boxes
25 self.do_tracker = False
28 return "%s.%s"%(box,self.domain)
30 def root (box): return "root@%s"%box
32 def header (self,message):
33 print "===============",message
35 def run (self,argv,message):
36 if self.options.dry_run:
40 if message: self.header(message)
43 def backquote (self, argv):
44 return subprocess.Popen(argv,stdout=subprocess.PIPE).communicate()[0]
46 def reboot (self,box):
47 command=['ssh',self.root(box),'shutdown','-r','now']
48 self.run (command,"Rebooting %s"%box)
50 def handle_trackers (self):
51 box = self.fqdn (self.testmaster)
52 if self.options.probe:
53 command=['ssh',self.root(box),"head","-100","tracker*"]
54 self.run(command,"Inspecting trackers on %s"%box)
56 command=['ssh',self.root(box),"rm","-rf","tracker*"]
57 self.run(command,"Cleaning up trackers on %s"%box)
59 def handle_build_box (self,box):
60 if self.options.probe:
61 command=['ssh',self.root(box),'pgrep','build']
62 if self.options.dry_run:
63 self.run(command,None)
65 pids=self.backquote(command)
67 self.header ('No build process on %s'%box)
69 command=['ssh',self.root(box),'ps'] + [ pid for pid in pids.split("\n") if pid]
70 self.run(command,"Active build processes on %s"%box)
74 vplc_matcher = re.compile(".*(vplc[0-9]+$)")
75 def vplcname (self, vservername):
76 match = self.vplc_matcher.match(vservername)
77 if match: return match.groups(0)
80 def handle_plc_box (self,box):
81 if self.options.probe:
82 command=['ssh',self.root(box),'vserver-stat']
83 if self.options.dry_run:
84 self.run(command,"Active vservers on %s"%box)
86 # try to find fullname (vserver_stat truncates to a ridiculously short name)
88 self.header ("vserver map on %s"%box)
89 # fetch the contexts for all vservers on that box
90 map_command=['ssh',self.root(box),'grep','.','/etc/vservers/*/context','/dev/null',]
91 context_map=self.backquote (map_command)
92 # at this point we have a set of lines like
93 # /etc/vservers/2010.01.20--k27-f12-32-vplc03/context:40144
95 for map_line in context_map.split("\n"):
96 if not map_line: continue
97 [path,xid] = map_line.split(':')
98 ctx_dict[xid]=os.path.basename(os.path.dirname(path))
99 # at this point ctx_id maps context id to vservername
101 vserver_stat = self.backquote (command)
102 for vserver_line in vserver_stat.split("\n"):
103 if not vserver_line: continue
104 context=vserver_line.split()[0]
108 longname=ctx_dict[context]
109 plcname=self.vplcname(longname)
110 if plcname: print "== %s =="%plcname
111 print "%(vserver_line)s [=%(longname)s]"%locals()
113 self.run(command,"Fine-grained method failed - fallback to plain vserver-stat")
118 def handle_qemu_box (self,box):
119 if self.options.probe:
120 command=['ssh',self.root(box),'pgrep','qemu']
121 if self.options.dry_run:
122 self.run(command,None)
124 pids=self.backquote(command)
126 self.header ('No qemu process on %s'%box)
128 command=['ssh',self.root(box),'ps'] + [ pid for pid in pids.split("\n") if pid]
129 self.run(command,"Active qemu processes on %s"%box)
133 def handle_box(self,box):
134 if box in self.qemu_boxes:
135 self.handle_qemu_box(self.fqdn(box))
136 elif box in self.plc_boxes:
137 self.handle_plc_box(self.fqdn(box))
139 self.handle_build_box(self.fqdn(box))
142 usage="""%prog [options] [hostname..(s)]
143 Default is to act on test boxes only (with trackers clean)"""
144 parser = OptionParser (usage=usage)
145 parser.add_option ("-n","--dry-run",action="store_true",dest="dry_run",default=False,
147 parser.add_option ("-r","--reboot", action="store_false",dest="probe",default=True,
148 help="Actually reset/reboot stuff instead of just probing it")
149 # no need for -p = probe, as this is the default
150 parser.add_option ("-p","--plc", action="store_true",dest="plc_only",default=False,
151 help="Acts on the plc box only")
153 parser.add_option ("-a","--all",action="store_true",dest="all_boxes",default=False,
154 help="Acts on build and test boxes")
155 parser.add_option ("-b","--build",action="store_true",dest="build_only",default=False,
156 help="Acts on build boxes only")
157 parser.add_option ("-q","--qemu",action="store_true",dest="qemu_only",default=False,
158 help="Only acts on the qemu boxes")
159 parser.add_option ("-t","--trackers",action="store_true",dest="trackers_only",default=False,
160 help="Only wipes trackers")
162 (self.options,args) = parser.parse_args()
164 # use given hostnames if provided
167 # if hostnames are specified, let's stay on the safe side and don't reset trackers
168 self.do_tracker = False
169 elif self.options.all_boxes:
170 self.boxes=self.build_boxes + self.test_boxes
171 self.do_tracker = True
172 elif self.options.build_only:
173 self.boxes=self.build_boxes
174 self.do_tracker = False
175 elif self.options.qemu_only:
176 self.boxes=self.qemu_boxes
177 self.do_tracker = False
178 elif self.options.plc_only:
179 self.boxes=self.plc_boxes
180 self.do_tracker = False
181 elif self.options.trackers_only:
183 self.do_tracker = True
186 self.boxes = self.test_boxes
187 self.do_tracker = True
190 self.handle_trackers ()
191 for box in self.boxes:
192 self.handle_box (box)
195 if __name__ == "__main__":