attempt to avoid remote qemus from hanging
[tests.git] / system / TestSsh.py
index 4e1698b..d504422 100644 (file)
@@ -1,14 +1,12 @@
 #this class is  used for any ssh command and
 #also for any remote or a local command independently
 #on which box this must be done.
-#new TestSsh object take like an argument an instance
-#of the class where it was created 
 
 import os.path
 import utils
 
 class TestSsh:
-
+    
     # inserts a backslash before each occurence of the following chars
     # \ " ' < > & | ; ( ) $ * ~ 
     @staticmethod
@@ -35,62 +33,77 @@ class TestSsh:
             utils.header("WARNING : something wrong in is_local_hostname with hostname=%s"%hostname)
             return False
 
-    # buildname is the name of a directory that we can use in $hostname's homedir (/root)
-    def __init__(self,caller,buildname=None,key=None):
-        self.caller=caller
+    def __init__(self,hostname,buildname=None,key=None):
+        self.hostname=hostname
         self.buildname=buildname
-        self._key=key
+        self.key=key
 
-    def hostname(self):
-        return self.caller.hostname()
     def is_local(self):
-        return TestSsh.is_local_hostname(self.hostname())
-
+        return TestSsh.is_local_hostname(self.hostname)
+     
+    std_options="-o StrictHostKeyChecking=no -o BatchMode=yes "
+    
+    def key_part (self):
+        if not self.key:
+            return ""
+        return "-i %s.rsa "%self.key
+    
     # command gets run on the right box
-    def to_host(self,command):
+    def actual_command (self, command):
         if self.is_local():
             return command
-        else:
-            return "ssh %s %s"%(self.hostname(),TestSsh.backslash_shell_specials(command))
+        ssh_command = "ssh "
+        ssh_command += TestSsh.std_options
+        ssh_command += self.key_part()
+        ssh_command += "%s %s" %(self.hostname,TestSsh.backslash_shell_specials(command))
+        return ssh_command
 
-    def full_command(self,command):
-        return self.to_host(self.caller.host_to_guest(command))
+    def run(self, command,background=False):
+        local_command = self.actual_command(command)
+        if background:
+            local_command += " &"
+        return utils.system(local_command)
 
-    def run_in_guest (self,command):
-        return utils.system(self.full_command(command))
-    
-    def run_in_host (self,command):
-        return utils.system(self.to_host(command))
+    def clean_dir (self,dirname):
+        if self.is_local():
+            return 0
+        return self.run("rm -rf %s"%dirname)
 
-    # xxx quick n dirty
-    def run_in_guest_piped (self,local,remote):
-        return utils.system(local+" | "+self.full_command(remote))
-    
-    def run_in_buildname (self,command):
-        if not self.buildname:
-            utils.header ("WARNING : TestSsh.run_in_buildname without a buildname")
-            return 1
+    def mkdir (self,dirname=None):
+        if self.is_local():
+            if dirname:
+                return os.path.mkdir(dirname)
+            return 0
+        if dirname:
+            dirname="%s/%s"%(self.buildname,dirname)
+        else:
+            dirname=self.buildname
+        return self.run("mkdir %s"%dirname)
+
+    def create_buildname_once (self):
+        if self.is_local():
+            return
+        # create remote buildname on demand
+        try:
+            self.buildname_created
+        except:
+            self.mkdir()
+            self.buildname_created=True
+
+    def run_in_buildname (self,command, background=False):
         if self.is_local():
             return utils.system(command)
-        ssh_command="ssh "
-        if self.caller.key:
-            ssh_command += "-i %s.rsa "%(self.caller.key)
-        ssh_command += "`%s %s/%s"%(self.hostname(),
-                                    self.buildname,
-                                    TestSsh.backslash_shell_specials(command))
-        return utils.system(ssh_command)
+        self.create_buildname_once()
+        return self.run("cd %s ; %s"%(self.buildname,command),background)
 
     def copy (self,local_file,recursive=False):
-        if not self.buildname:
-            utils.header ("WARNING : TestSsh.copy without a buildname")
-            return 1
         if self.is_local():
             return 0
-        command="scp "
-        if recursive: command += "-r "
-        if self.caller.key:
-            command += "-i %s.rsa "
-        command +="%s %s:%s/%s"%(local_file,self.hostname(),self.buildname,
-                                 os.path.basename(local_file) or ".")
-        return utils.system(command)
+        self.create_buildname_once()
+        scp_command="scp "
+        if recursive: scp_command += "-r "
+        scp_command += self.key_part()
+        scp_command += "%s %s:%s/%s"%(local_file,self.hostname,self.buildname,
+                                      os.path.basename(local_file) or ".")
+        return utils.system(scp_command)