introduces the TestBox class - review how qemu gets stopped
Thierry Parmentelat [Thu, 14 Feb 2008 11:32:30 +0000 (11:32 +0000)]
system/TestBox.py [new file with mode: 0644]
system/TestNode.py
system/TestPlc.py

diff --git a/system/TestBox.py b/system/TestBox.py
new file mode 100644 (file)
index 0000000..5e39b51
--- /dev/null
@@ -0,0 +1,23 @@
+# $Id$
+# this models a box that hosts qemu nodes
+# could probably also be used for boxes that host plc instances
+import utils
+
+class TestBox:
+
+    def __init__(self,hostname,key=None):
+        self.hostname=hostname
+        self.key=key
+
+    def run (command):
+        if self.hostname == "localhost":
+            return utils.system(command)
+        else:
+            if self.key:
+                to_run="ssh -i %s.rsa %s %s"%(self.key,self.hostname,command)
+            else:
+                to_run="ssh %s %s"%(self.key,self.hostname,command)
+            return utils.system(to_run)
+        
+    def step_all_qemus(hostname):
+        self.run("killall qemu")
index c916f84..df6eb56 100644 (file)
@@ -3,6 +3,7 @@ import xmlrpclib
 
 import utils
 from TestUser import TestUser
+from TestBox import TestBox
 
 class TestNode:
 
@@ -27,10 +28,15 @@ class TestNode:
         return TestNode.is_real_model (self.node_spec['node_fields']['model'])
 
     def host_box (self):
-        try:
-            return self.node_spec['host_box']
-        except:
-            return 'localhost'
+        if self.is_real ():
+            utils.header("WARNING : real nodes dont have a host box")
+            return None
+        else:
+            try:
+                return self.node_spec['host_box']
+            except:
+                utils.header("WARNING : qemu nodes need a host box")
+                return 'localhost'
 
     def create_node (self):
         ownername = self.node_spec['owner']
@@ -163,16 +169,19 @@ class TestNode:
         self.test_plc.run_in_host("ssh root@%s ~/%s/%s/env-qemu start"%(host_box, path, dest_dir ))
         self.test_plc.run_in_host("ssh  root@%s DISPLAY=%s  ~/%s/start-qemu-node %s & "%( host_box, display, dest_dir, dest_dir))
         
-    def stop_qemu(self,node_spec):
-        try:
-            if self.is_qemu_model(node_spec['node_fields']['model']):
-                hostname=node_spec['node_fields']['hostname']
-                host_box=node_spec['host_box']
-                self.test_plc.run_in_host('ssh root@%s  killall qemu'%host_box)
-                utils.header('Stoping qemu emulation of %s on the host machine %s and Restoring the initial network'
-                             %(hostname,host_box))
-                self.test_plc.run_in_host("ssh root@%s ~/qemu-%s/env-qemu stop "%(host_box, hostname ))
-            return True
-        except Exception,e :
-            print str(e)
-            return False
+# needs rework - node_spec is a local atribute, no need to pass it
+# the code that stops ALL qemu instance on a given box has moved to TestBox
+# this code below should only kill THE qemu instance that goes with that particular hostname
+#    def stop_qemu(self,node_spec):
+#        try:
+#            if self.is_qemu_model(node_spec['node_fields']['model']):
+#                hostname=node_spec['node_fields']['hostname']
+#                host_box=node_spec['host_box']
+#                self.test_plc.run_in_host('ssh root@%s  killall qemu'%host_box)
+#                utils.header('Stoping qemu emulation of %s on the host machine %s and Restoring the initial network'
+#                             %(hostname,host_box))
+#                self.test_plc.run_in_host("ssh root@%s ~/qemu-%s/env-qemu stop "%(host_box, hostname ))
+#            return True
+#        except Exception,e :
+#            print str(e)
+#            return False
index 8be37e6..4694d1c 100644 (file)
@@ -14,6 +14,7 @@ from TestNode import TestNode
 from TestUser import TestUser
 from TestKey import TestKey
 from TestSlice import TestSlice
+from TestBox import TestBox
 
 # inserts a backslash before each occurence of the following chars
 # \ " ' < > & | ; ( ) $ * ~ 
@@ -140,27 +141,37 @@ class TestPlc:
                 return key
         raise Exception,"Cannot locate key %s"%keyname
 
-    #this to catch up all different hostboxes used in this plc
-    def locate_hostBoxes(self,site_spec):
-        #Get The first host box to avoid returning a long list with the same host box
-        #in case  only one is used for all the nodes
-        HostBoxes=[site_spec['nodes'][0]['host_box']]
-        for node_spec in site_spec['nodes']:
-            if node_spec['host_box']!= HostBoxes[0]:
-                HostBoxes.append( node_spec['host_box'])
-
-        return HostBoxes
-            
-    def kill_all_qemus(self):
+    # all different hostboxes used in this plc
+    def gather_hostBoxes(self):
+        # maps on sites and nodes, return [ (host_box,hostname) ]
+        tuples=[]
         for site_spec in self.plc_spec['sites']:
             test_site = TestSite (self,site_spec)
-            hostboxes_list=self.locate_hostBoxes(site_spec)
-            if (hostboxes_list):
-                for node_spec in site_spec['nodes']:
-                    TestNode(self,test_site,node_spec).stop_qemu(node_spec)
+            for node_spec in site_spec['nodes']:
+                test_node = TestNode (self, test_site, node_spec)
+                if not test_node.is_real():
+                    tuples.append( (test_node.host_box(),node_spec['node_fields']['hostname']) )
+        # transform into a dict { 'host_box' -> [ hostnames .. ] }
+        result = {}
+        for (box,hostname) in tuples:
+            if not result.has_key(box):
+                result[box]=[hostname]
             else:
-                utils.header("No emulated node running on this PLC config ignore the kill() step")
-            
+                result[box].append(hostname)
+        return result
+                    
+    # a step for checking this stuff
+    def showboxes (self,options):
+        print 'showboxes'
+        for (box,hosts) in self.gather_hostBoxes().iteritems():
+            print box,":"," + ".join(hosts)
+        return True
+
+    def kill_all_qemus(self):
+        for (box,hosts) in self.gather_hostBoxes().iteritems():
+            # this is the brute force version, kill all qemus on that host box
+            TestBox(box).kill_all_qemus()
+
     def clear_ssh_config (self,options):
         # install local ssh_config file as root's .ssh/config - ssh should be quiet
         # dir might need creation first