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