first draft
authorthierry <thierry@41d37cc5-eb28-0410-a9bf-d37491348ade>
Mon, 25 Jan 2010 10:35:06 +0000 (10:35 +0000)
committerthierry <thierry@41d37cc5-eb28-0410-a9bf-d37491348ade>
Mon, 25 Jan 2010 10:35:06 +0000 (10:35 +0000)
scripts/manage-infrastructure.py [new file with mode: 0755]

diff --git a/scripts/manage-infrastructure.py b/scripts/manage-infrastructure.py
new file mode 100755 (executable)
index 0000000..cb34056
--- /dev/null
@@ -0,0 +1,136 @@
+#!/usr/bin/python
+
+import subprocess
+from optparse import OptionParser
+
+class BuildBoxes:
+
+    # everything in the onelab.eu domain
+    domain = 'onelab.eu'
+    testmaster = 'testmaster'
+    build_boxes = \
+        [ "mirror" ] + \
+        [ "build%d"%i for i in range (1,4) ]
+    plc_boxes = [ "testplc" ]
+    qemu_boxes = \
+        [ "testqemu%d"%i for i in range (1,4) ] + \
+        [ "testqemu32-%d"%i for i in range (1,6) ]
+    test_boxes = plc_boxes + qemu_boxes
+
+    def __init__ (self):
+        # dummy defaults
+        self.boxes = []
+        self.do_tracker = False
+
+    def fqdn (self, box):
+        return "%s.%s"%(box,self.domain)
+    @staticmethod
+    def root (box): return "root@%s"%box
+
+    def run (self,argv,message):
+        if self.options.dry_run:
+            print 'DRY_RUN:',
+            print " ".join(argv)
+        else:
+            if message: print "===============",message
+            subprocess.call(argv)
+                
+    def backquote (self, argv):
+        return subprocess.Popen(argv,stdout=subprocess.PIPE).communicate()[0]
+
+    def reboot (self,box):
+        command=['ssh',self.root(box),'shutdown','-r','now']
+        self.run (command,"Rebooting %s"%box)
+
+    def handle_trackers (self):
+        box = self.fqdn (self.testmaster)
+        if self.options.probe:
+            command=['ssh',self.root(box),"head","-100","'trackers*'"]
+            self.run(command,"========== Inspecting trackers on %s"%box)
+        else:
+            command=['ssh',self.root(box),"rm","-rf","'trackers*'"]
+            self.run(command,"========== Cleaning up trackers on %s"%box)
+
+    def handle_build_box (self,box):
+        if self.options.probe:
+            command=['ssh',self.root(box),'pgrep','build']
+            self.run(command,"Listing build processes on %s"%box)
+        else:
+            self.reboot(box)
+
+    def handle_plc_box (self,box):
+        if self.options.probe:
+            command=['ssh',self.root(box),'vserver-stat']
+            self.run(command,"Active vservers on %s"%box)
+        else:
+            self.reboot(box)
+
+    def handle_qemu_box (self,box):
+        if self.options.probe:
+            command=['ssh',self.root(box),'pgrep','qemu']
+            if self.options.dry_run:
+                self.run(command,None)
+            else:
+                pids=self.backquote(command)
+                if not pids:
+                    print 'No qemu process on',box
+                else:
+                    command=['ssh',self.root(box),'ps'] + [ pid for pid in pids.split("\n") if pid]
+                    self.run(command,"Active qemu processes on %s"%box)
+        else:
+            self.reboot(box)
+
+    def handle_box(self,box):
+        if box in self.qemu_boxes:
+            self.handle_qemu_box(self.fqdn(box))
+        elif box in self.plc_boxes:
+            self.handle_plc_box(self.fqdn(box))
+        else:
+            self.handle_build_box(self.fqdn(box))
+
+    def main (self):
+        usage="""%prog [options] [hostname..(s)]
+Default is to act on test boxes only (with trackers clean)"""
+        parser = OptionParser (usage=usage)
+        parser.add_option ("-a","--all",action="store_true",dest="all_boxes",default=False,
+                           help="Acts on build and test boxes")
+        parser.add_option ("-b","--build",action="store_true",dest="build_only",default=False,
+                           help="Acts on build boxes only")
+        parser.add_option ("-t","--trackers",action="store_true",dest="trackers_only",default=False,
+                           help="Only wipes trackers")
+        parser.add_option ("-n","--dry-run",action="store_true",dest="dry_run",default=False,
+                           help="Dry run")
+        parser.add_option ("-r","--reboot", action="store_false",dest="probe",default=True,
+                           help="Actually reset/reboot stuff instead of just probing it")
+        parser.add_option ("-p","--probe", action="store_true",dest="probe",
+                           help="Probe stuff, no side effect")
+
+        (self.options,args) = parser.parse_args()
+
+        # use given hostnames if provided
+        if args:
+            self.boxes=args
+            # if hostnames are specified, let's stay on the safe side and don't reset trackers
+            self.do_tracker = False
+        elif self.options.all_boxes:
+            self.boxes=self.build_boxes + self.test_boxes
+            self.do_tracker = True
+        elif self.options.build_only:
+            self.boxes=self.build_boxes
+            self.do_tracker = False
+        elif self.options.trackers_only:
+            self.boxes = []
+            self.do_tracker = True
+        # default
+        else:
+            self.boxes = self.test_boxes
+            self.do_tracker = True
+
+        if self.do_tracker:
+            self.handle_trackers ()
+        for box in self.boxes:
+            self.handle_box (box)
+
+
+if __name__ == "__main__":
+    BuildBoxes().main()