fixed kill_qemus : uses qemu -pidfile for locating pids
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Fri, 28 Mar 2008 11:05:38 +0000 (11:05 +0000)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Fri, 28 Mar 2008 11:05:38 +0000 (11:05 +0000)
system/TestBox.py
system/TestMain.py
system/TestNode.py
system/TestPlc.py
system/qemu_kill.sh [deleted file]
system/template-qemu/kill-qemu-node [new file with mode: 0755]
system/template-qemu/start-qemu-node

index b46ec52..6fc0dba 100644 (file)
@@ -27,7 +27,9 @@ class TestBox:
         return False
     
     def run_in_buildname (self,command):
+        utils.header("Running command %s on testbox %s"%(command,self.hostname()))
         return self.test_ssh.run_in_buildname (command)
+
     # should use rsync instead
     def copy (self,local_file,recursive=False):
         return self.test_ssh.copy (local_file,recursive)
@@ -37,8 +39,8 @@ class TestBox:
 
     def mkdir (self,direname):
         return self.test_ssh.mkdir(direname)
 
     def kill_all_qemus(self):
-        utils.system(self.test_ssh.to_host("killall qemu"))
+        self.run_in_buildname("killall qemu")
+        return True
 
index 35da164..661a8f9 100755 (executable)
@@ -27,7 +27,7 @@ class TestMain:
                      'force_kill_qemus', ]
     other_steps = [ 'stop_all_vservers','fresh_install', 'stop','check_tcp', 
                     'clean_sites', 'clean_nodes', 'clean_slices', 'clean_keys',
-                    'list_all_qemus', 'kill_qemus', 'stop_nodes' ,  
+                    'list_all_qemus', 'list_qemus', 'stop_nodes' ,  
                     'db_dump' , 'db_restore',
                     'standby_1 through 20',
                     ]
@@ -86,6 +86,8 @@ steps refer to a method in TestPlc or to a step_* module
                           help="Specify the set of IP addresses to use in vserver mode (disable scanning)")
         parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
                           help="Run in verbose mode")
+        parser.add_option("-q","--quiet", action="store_true", dest="quiet", default=False, 
+                          help="Run in quiet mode")
         parser.add_option("-n","--dry-run", action="store_true", dest="dry_run", default=False,
                           help="Show environment and exits")
         parser.add_option("-f","--forcenm", action="store_true", dest="forcenm", default=False, 
@@ -135,7 +137,8 @@ steps refer to a method in TestPlc or to a step_* module
                         print "Cannot determine",recname
                         print "Run %s --help for help"%sys.argv[0]                        
                         sys.exit(1)
-            utils.header('* Using %s = %s'%(recname,getattr(self.options,recname)))
+            if not self.options.quiet:
+                utils.header('* Using %s = %s'%(recname,getattr(self.options,recname)))
 
             # save for next run
             fsave=open(path,"w")
@@ -171,11 +174,11 @@ steps refer to a method in TestPlc or to a step_* module
                 print 'Cannot load config %s -- ignored'%modulename
                 raise
         # show config
-        utils.show_test_spec("Test specifications",all_plc_specs)
+        if not self.options.quiet:
+            utils.show_test_spec("Test specifications",all_plc_specs)
         # build a TestPlc object from the result
         for spec in all_plc_specs:
             spec['disabled'] = False
-            spec['forced']= True
         all_plcs = [ (x, TestPlc(x)) for x in all_plc_specs]
         # expose to the various objects
         for (spec,obj) in all_plcs:
@@ -185,9 +188,14 @@ steps refer to a method in TestPlc or to a step_* module
         testplc_method_dict = __import__("TestPlc").__dict__['TestPlc'].__dict__
         all_step_infos=[]
         for step in self.options.steps:
+            force=False
+            # is it a forcedstep
+            if step.find("force_") == 0:
+                step=step.replace("force_","")
+                force=True
             # try and locate a method in TestPlc
             if testplc_method_dict.has_key(step):
-                all_step_infos += [ (step, testplc_method_dict[step] )]
+                all_step_infos += [ (step, testplc_method_dict[step] , force)]
             # otherwise search for the 'run' method in the step_<x> module
             else:
                 modulename='step_'+step
@@ -198,7 +206,7 @@ steps refer to a method in TestPlc or to a step_* module
                     if not names:
                         raise Exception,"No run* method in module %s"%modulename
                     names.sort()
-                    all_step_infos += [ ("%s.%s"%(step,name),module_dict[name]) for name in names ]
+                    all_step_infos += [ ("%s.%s"%(step,name),module_dict[name],force) for name in names ]
                 except :
                     print 'Step %s -- ignored'%(step)
                     traceback.print_exc()
@@ -209,24 +217,16 @@ steps refer to a method in TestPlc or to a step_* module
             return 0
             
         # do all steps on all plcs
-        for (stepname,method) in all_step_infos:
+        for (stepname,method,force) in all_step_infos:
             for (spec,obj) in all_plcs:
                 plcname=spec['name']
-                if spec['disabled']:
-                    utils.header("Plc %s is disabled - skipping step %s"%(plcname,stepname))
-                    if (stepname.find("force")==0 and spec['forced']) :
-                        utils.header("Plc %s is disabled but running step %s anyway"
-                                     %(plcname,stepname))
-                        step_result = method(obj,self.options)
-                        if step_result:
-                            utils.header('********** SUCCESSFUL step %s on %s'%(stepname,plcname))
-                        else:
-                            overall_result = False
-                            spec['forced'] = False
-                            utils.header('********** Step %s on %s FAILED - discarding that plc from further steps'%(stepname,plcname))
-                else:
+
+                # run the step
+                if not spec['disabled'] or force:
                     try:
-                        utils.header("Running step %s on plc %s"%(stepname,plcname))
+                        force_msg=""
+                        if force: force_msg=" (forced)"
+                        utils.header("Running step %s%s on plc %s"%(stepname,force_msg,plcname))
                         step_result = method(obj,self.options)
                         if step_result:
                             utils.header('********** SUCCESSFUL step %s on %s'%(stepname,plcname))
@@ -237,8 +237,13 @@ steps refer to a method in TestPlc or to a step_* module
                     except:
                         overall_result=False
                         spec['disabled'] = True
-                        utils.header ('********** Step %s on plc %s FAILED (exception) - discarding this plc from further steps'%(stepname,plcname))
                         traceback.print_exc()
+                        utils.header ('********** Step %s on plc %s FAILED (exception) - discarding this plc from further steps'%(stepname,plcname))
+
+                # do not run, just display it's skipped
+                else:
+                    utils.header("Plc %s is disabled - skipping step %s"%(plcname,stepname))
+
         return overall_result
 
     # wrapper to run, returns a shell-compatible result
index 7b5bccd..80c4a35 100644 (file)
@@ -158,6 +158,12 @@ class TestNode:
         test_box.run_in_buildname("qemu-%s/env-qemu start >> nodeslogs/%s.log"%(self.name(),self.name()))
         test_box.run_in_buildname("qemu-%s/start-qemu-node 2>&1 >> nodeslogs/%s.log &"%(self.name(),self.name()))
 
+    def list_qemu (self):
+        utils.header("Listing qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
+        command="qemu-%s/kill-qemu-node -l %s"%(self.name(),self.name())
+        self.test_box().run_in_buildname(command)
+        return True
+
     def kill_qemu (self):
         #Prepare the log file before killing the nodes
         test_box = self.test_box()
@@ -165,6 +171,6 @@ class TestNode:
             utils.header("Failed to get the nodes log files")
         # kill the right processes 
         utils.header("Stopping qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
-        command="qemu_kill.sh %s"%self.name()
+        command="qemu-%s/kill-qemu-node %s"%(self.name(),self.name())
         self.test_box().run_in_buildname(command)
         return True
index 9192f89..75a90cd 100644 (file)
@@ -45,7 +45,7 @@ class TestPlc:
         except:
             self.vserver=False
             self.url="https://%s:443/PLCAPI/"%plc_spec['hostname']
-        utils.header('Using API url %s'%self.url)
+#        utils.header('Using API url %s'%self.url)
        self.server=xmlrpclib.Server(self.url,allow_none=True)
         
     def name(self):
@@ -168,17 +168,21 @@ class TestPlc:
     # make this a valid step
     def list_all_qemus(self,options):
         for (box,nodes) in self.gather_hostBoxes().iteritems():
-           # push the script
-           TestBox(box,options.buildname).copy("qemu_kill.sh") 
             # this is the brute force version, kill all qemus on that host box
-            TestBox(box,options.buildname).run_in_buildname("qemu_kill.sh -l")
+            TestBox(box,options.buildname).run_in_buildname("qemu-%s/kill-qemu-node -l %s"%(node.name(),node.name()))
         return True
 
     # kill only the right qemus
-    def force_kill_qemus(self,options):
+    def list_qemus(self,options):
+        for (box,nodes) in self.gather_hostBoxes().iteritems():
+            # the fine-grain version
+            for node in nodes:
+                node.list_qemu()
+        return True
+
+    # kill only the right qemus
+    def kill_qemus(self,options):
         for (box,nodes) in self.gather_hostBoxes().iteritems():
-           # push the script
-           TestBox(box,options.buildname).copy("qemu_kill.sh") 
             # the fine-grain version
             for node in nodes:
                 node.kill_qemu()
diff --git a/system/qemu_kill.sh b/system/qemu_kill.sh
deleted file mode 100755 (executable)
index f108228..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-#!/bin/sh
-# $Id$
-COMMAND=$(basename $0)
-
-hostname=$1; shift
-
-# -l option 
-if [ "$hostname" = "-l" ] ; then
-    echo $COMMAND - listing qemu processes on $(hostname)
-    pids="$(pgrep -x qemu) $(pgrep -x start-qemu-node)"
-    [ -n "$(echo $pids)" ] && ps $pids
-    exit 0
-fi
-
-# locate only the actual qemu 
-qemu_pids="$(pgrep -x start-qemu-node) $(pgrep -x qemu)"
-
-if [ -z "$(echo $qemu_pids)" ] ; then
-    echo $COMMAND - no qemu found on $(hostname)
-    exit 0
-fi
-
-pids="$(ps $qemu_pids | grep $hostname | awk '{print $1;}')"
-
-if [ -z "$pids" ] ; then
-    echo $COMMAND: no qemu instance for $hostname found on $(hostname)
-    exit 0
-fi
-
-echo Killing $pids
-kill $pids
-(sleep 1; 
- if ps $pids &> /dev/null ; then
-     echo still alive - killing -9 $pids
-     kill -9 $pids
- fi ) &
-echo Done
-exit 0
diff --git a/system/template-qemu/kill-qemu-node b/system/template-qemu/kill-qemu-node
new file mode 100755 (executable)
index 0000000..ecbec21
--- /dev/null
@@ -0,0 +1,79 @@
+#!/bin/sh
+# $Id$
+COMMAND=$(basename $0)
+cd $(dirname $0)
+cd ..
+
+function usage () {
+    echo "Usage: $COMMAND -l"
+    echo "  lists current qemu processes"
+    echo "usage: $COMMAND hostname"
+    echo "  kill qemu instance for that node"
+    exit 1
+}
+
+function list_pids () {
+    hostnames="$@"
+    if [[ -n "$hostnames" ]] ; then
+       for hostname in $hostnames; do
+           nodedir=qemu-$hostname
+           cat $nodedir/qemu.pid $nodedir/shell.pid 2> /dev/null
+       done
+    else
+       pgrep qemu
+    fi
+}
+
+function kill_from_file () {
+    file=$1; shift
+    if [ -f $file ] ; then
+       pid=$(cat $file)
+       echo Killing $pid
+       kill $pid
+       mv $file $file.killed
+    fi
+}    
+
+function kill_pids () {
+    hostnames="$@"
+    if [[ -n "$hostnames" ]] ; then
+       for hostname in $hostnames; do
+           nodedir=qemu-$hostname
+           kill_from_file $nodedir/qemu.pid
+           kill_from_file $nodedir/shell.pid
+       done
+    else
+       echo Killing all processes mathing qemu
+       pkill qemu
+    fi
+}
+
+function show_pids () {
+    pids=$(list_pids "$@")
+    if [ -n "$pids" ] ; then
+       ps $pids
+    else
+       echo Nothing to show
+    fi
+}
+
+function main () { 
+    while getopts "lk" opt; do
+       case $opt in
+           l) OPT_LIST=true ;;
+           k) OPT_GREP=true ;;
+           *) usage ;;
+       esac
+    done
+    shift $(($OPTIND -1))
+
+    # listing
+    if [ -n "$OPT_LIST" ] ; then
+       show_pids "$@"
+       exit 0
+    fi
+
+    kill_pids "$@"
+}
+
+main "$@"    
index ab23af1..7cb9501 100755 (executable)
@@ -13,7 +13,9 @@ fi
 
 #default Value
 SCRIPT=./qemu-ifup
-HDA=hda_5.raw
+HDA=hda_10.raw
+# make sure to check qemu_kill.sh if you change this
+QEMU=qemu-system-x86_64
 
 # qemu parameters
 RAM=520;
@@ -36,9 +38,10 @@ fi
 echo "New $HDA is created..."
 
 #Command for running the Qemu Emulator
-ARGS="-boot d -cdrom ${NODE_ISO} -hda ${HDA} -m ${RAM}  -net nic,macaddr=${MACADDR} -net $TAP -nographic "
-echo "Running qemu $ARGS";
-qemu-system-x86_64 $ARGS
+ARGS="-boot d -cdrom ${NODE_ISO} -hda ${HDA} -m ${RAM}  -net nic,macaddr=${MACADDR} -net $TAP -nographic -pidfile qemu.pid"
+echo $$ > shell.pid
+echo "Running qemu $ARGS"
+$QEMU $ARGS
 
 
 exit