fix tracker probing
[infrastructure.git] / scripts / manage-infrastructure.py
1 #!/usr/bin/python
2
3 import subprocess
4 from optparse import OptionParser
5
6 class BuildBoxes:
7
8     # everything in the onelab.eu domain
9     domain = 'onelab.eu'
10     testmaster = 'testmaster'
11     build_boxes = \
12         [ "mirror" ] + \
13         [ "build%d"%i for i in range (1,4) ]
14     plc_boxes = [ "testplc" ]
15     qemu_boxes = \
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
19
20     def __init__ (self):
21         # dummy defaults
22         self.boxes = []
23         self.do_tracker = False
24
25     def fqdn (self, box):
26         return "%s.%s"%(box,self.domain)
27     @staticmethod
28     def root (box): return "root@%s"%box
29
30     def header (self,message):
31         print "===============",message
32
33     def run (self,argv,message):
34         if self.options.dry_run:
35             print 'DRY_RUN:',
36             print " ".join(argv)
37         else:
38             if message: self.header(message)
39             subprocess.call(argv)
40                 
41     def backquote (self, argv):
42         return subprocess.Popen(argv,stdout=subprocess.PIPE).communicate()[0]
43
44     def reboot (self,box):
45         command=['ssh',self.root(box),'shutdown','-r','now']
46         self.run (command,"Rebooting %s"%box)
47
48     def handle_trackers (self):
49         box = self.fqdn (self.testmaster)
50         if self.options.probe:
51             command=['ssh',self.root(box),"head","-100","tracker*"]
52             self.run(command,"Inspecting trackers on %s"%box)
53         else:
54             command=['ssh',self.root(box),"rm","-rf","tracker*"]
55             self.run(command,"Cleaning up trackers on %s"%box)
56
57     def handle_build_box (self,box):
58         if self.options.probe:
59             command=['ssh',self.root(box),'pgrep','build']
60             if self.options.dry_run:
61                 self.run(command,None)
62             else:
63                 pids=self.backquote(command)
64                 if not pids:
65                     self.header ('No build process on %s'%box)
66                 else:
67                     command=['ssh',self.root(box),'ps'] + [ pid for pid in pids.split("\n") if pid]
68                     self.run(command,"Active build processes on %s"%box)
69         else:
70             self.reboot(box)
71
72     def handle_plc_box (self,box):
73         if self.options.probe:
74             command=['ssh',self.root(box),'vserver-stat']
75             self.run(command,"Active vservers on %s"%box)
76         else:
77             self.reboot(box)
78
79     def handle_qemu_box (self,box):
80         if self.options.probe:
81             command=['ssh',self.root(box),'pgrep','qemu']
82             if self.options.dry_run:
83                 self.run(command,None)
84             else:
85                 pids=self.backquote(command)
86                 if not pids:
87                     self.header ('No qemu process on %s'%box)
88                 else:
89                     command=['ssh',self.root(box),'ps'] + [ pid for pid in pids.split("\n") if pid]
90                     self.run(command,"Active qemu processes on %s"%box)
91         else:
92             self.reboot(box)
93
94     def handle_box(self,box):
95         if box in self.qemu_boxes:
96             self.handle_qemu_box(self.fqdn(box))
97         elif box in self.plc_boxes:
98             self.handle_plc_box(self.fqdn(box))
99         else:
100             self.handle_build_box(self.fqdn(box))
101
102     def main (self):
103         usage="""%prog [options] [hostname..(s)]
104 Default is to act on test boxes only (with trackers clean)"""
105         parser = OptionParser (usage=usage)
106         parser.add_option ("-a","--all",action="store_true",dest="all_boxes",default=False,
107                            help="Acts on build and test boxes")
108         parser.add_option ("-b","--build",action="store_true",dest="build_only",default=False,
109                            help="Acts on build boxes only")
110         parser.add_option ("-t","--trackers",action="store_true",dest="trackers_only",default=False,
111                            help="Only wipes trackers")
112         parser.add_option ("-n","--dry-run",action="store_true",dest="dry_run",default=False,
113                            help="Dry run")
114         parser.add_option ("-r","--reboot", action="store_false",dest="probe",default=True,
115                            help="Actually reset/reboot stuff instead of just probing it")
116         parser.add_option ("-p","--probe", action="store_true",dest="probe",
117                            help="Probe stuff, no side effect")
118
119         (self.options,args) = parser.parse_args()
120
121         # use given hostnames if provided
122         if args:
123             self.boxes=args
124             # if hostnames are specified, let's stay on the safe side and don't reset trackers
125             self.do_tracker = False
126         elif self.options.all_boxes:
127             self.boxes=self.build_boxes + self.test_boxes
128             self.do_tracker = True
129         elif self.options.build_only:
130             self.boxes=self.build_boxes
131             self.do_tracker = False
132         elif self.options.trackers_only:
133             self.boxes = []
134             self.do_tracker = True
135         # default
136         else:
137             self.boxes = self.test_boxes
138             self.do_tracker = True
139
140         if self.do_tracker:
141             self.handle_trackers ()
142         for box in self.boxes:
143             self.handle_box (box)
144
145
146 if __name__ == "__main__":
147     BuildBoxes().main()