retrieve vserver fullname and outline attached vplc
[infrastructure.git] / scripts / manage-infrastructure.py
1 #!/usr/bin/python
2
3 import os.path
4 import re
5 import subprocess
6 from optparse import OptionParser
7
8 class BuildBoxes:
9
10     # everything in the onelab.eu domain
11     domain = 'onelab.eu'
12     testmaster = 'testmaster'
13     build_boxes = \
14         [ "mirror" ] + \
15         [ "build%d"%i for i in range (1,4) ]
16     plc_boxes = [ "testplc" ]
17     qemu_boxes = \
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
21
22     def __init__ (self):
23         # dummy defaults
24         self.boxes = []
25         self.do_tracker = False
26
27     def fqdn (self, box):
28         return "%s.%s"%(box,self.domain)
29     @staticmethod
30     def root (box): return "root@%s"%box
31
32     def header (self,message):
33         print "===============",message
34
35     def run (self,argv,message):
36         if self.options.dry_run:
37             print 'DRY_RUN:',
38             print " ".join(argv)
39         else:
40             if message: self.header(message)
41             subprocess.call(argv)
42                 
43     def backquote (self, argv):
44         return subprocess.Popen(argv,stdout=subprocess.PIPE).communicate()[0]
45
46     def reboot (self,box):
47         command=['ssh',self.root(box),'shutdown','-r','now']
48         self.run (command,"Rebooting %s"%box)
49
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)
55         else:
56             command=['ssh',self.root(box),"rm","-rf","tracker*"]
57             self.run(command,"Cleaning up trackers on %s"%box)
58
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)
64             else:
65                 pids=self.backquote(command)
66                 if not pids:
67                     self.header ('No build process on %s'%box)
68                 else:
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)
71         else:
72             self.reboot(box)
73
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)
78         else: return ""
79
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)
85             else:
86                 # try to find fullname (vserver_stat truncates to a ridiculously short name)
87                 try:
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
94                     ctx_dict={}
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
100
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]
105                         if context=="CTX": 
106                             print vserver_line
107                             continue
108                         longname=ctx_dict[context]
109                         plcname=self.vplcname(longname)
110                         if plcname: print "== %s =="%plcname
111                         print "%(vserver_line)s [=%(longname)s]"%locals()
112                 except:
113                     self.run(command,"Fine-grained method failed - fallback to plain vserver-stat")
114
115         else:
116             self.reboot(box)
117
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)
123             else:
124                 pids=self.backquote(command)
125                 if not pids:
126                     self.header ('No qemu process on %s'%box)
127                 else:
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)
130         else:
131             self.reboot(box)
132
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))
138         else:
139             self.handle_build_box(self.fqdn(box))
140
141     def main (self):
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,
146                            help="Dry run")
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")
152
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")
161
162         (self.options,args) = parser.parse_args()
163
164         # use given hostnames if provided
165         if args:
166             self.boxes=args
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:
182             self.boxes = []
183             self.do_tracker = True
184         # default
185         else:
186             self.boxes = self.test_boxes
187             self.do_tracker = True
188
189         if self.do_tracker:
190             self.handle_trackers ()
191         for box in self.boxes:
192             self.handle_box (box)
193
194
195 if __name__ == "__main__":
196     BuildBoxes().main()