remove (some) dependencies on open fd's - give up using tee - make sure that runtest...
[tests.git] / system / TestSsh.py
index d504422..d392659 100644 (file)
@@ -1,6 +1,19 @@
-#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.
+#
+# Thierry Parmentelat - INRIA
+#
+# class for issuing commands on a box, either local or remote
+#
+# the notion of 'buildname' is for providing each test run with a dir of its own
+# buildname is generally the name of the build being tested, and can be considered unique
+#
+# thus 'run_in_buildname' mostly :
+# (*) either runs locally in . - as on a local node we are already in a dedicated directory
+# (*) or makes sure that there's a remote dir called 'buildname' and runs in it
+#
+# also, the copy operations
+# (*) either do nothing if ran locally
+# (*) or copy a local file into the remote 'buildname' 
+# 
 
 import os.path
 import utils
@@ -33,10 +46,11 @@ class TestSsh:
             utils.header("WARNING : something wrong in is_local_hostname with hostname=%s"%hostname)
             return False
 
-    def __init__(self,hostname,buildname=None,key=None):
+    def __init__(self,hostname,buildname=None,key=None, username=None):
         self.hostname=hostname
         self.buildname=buildname
         self.key=key
+        self.username=username
 
     def is_local(self):
         return TestSsh.is_local_hostname(self.hostname)
@@ -46,23 +60,29 @@ class TestSsh:
     def key_part (self):
         if not self.key:
             return ""
-        return "-i %s.rsa "%self.key
+        return "-i %s "%self.key
+
+    def hostname_part (self):
+        if not self.username:
+            return self.hostname
+        else:
+            return "%s@%s"%(self.username,self.hostname)
     
     # command gets run on the right box
-    def actual_command (self, command):
+    def actual_command (self, command,keep_stdin=False):
         if self.is_local():
             return command
         ssh_command = "ssh "
+        if not keep_stdin:
+            ssh_command += "-n "
         ssh_command += TestSsh.std_options
         ssh_command += self.key_part()
-        ssh_command += "%s %s" %(self.hostname,TestSsh.backslash_shell_specials(command))
+        ssh_command += "%s %s" %(self.hostname_part(),TestSsh.backslash_shell_specials(command))
         return ssh_command
 
     def run(self, command,background=False):
         local_command = self.actual_command(command)
-        if background:
-            local_command += " &"
-        return utils.system(local_command)
+        return utils.system(local_command,background)
 
     def clean_dir (self,dirname):
         if self.is_local():
@@ -78,7 +98,7 @@ class TestSsh:
             dirname="%s/%s"%(self.buildname,dirname)
         else:
             dirname=self.buildname
-        return self.run("mkdir %s"%dirname)
+        return self.run("mkdir -p %s"%dirname)
 
     def create_buildname_once (self):
         if self.is_local():
@@ -92,7 +112,7 @@ class TestSsh:
 
     def run_in_buildname (self,command, background=False):
         if self.is_local():
-            return utils.system(command)
+            return utils.system(command,background)
         self.create_buildname_once()
         return self.run("cd %s ; %s"%(self.buildname,command),background)
 
@@ -101,9 +121,32 @@ class TestSsh:
             return 0
         self.create_buildname_once()
         scp_command="scp "
+        scp_command += TestSsh.std_options
         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 ".")
+        scp_command += "%s %s:%s/%s"%(local_file,self.hostname_part(),
+                                      self.buildname,os.path.basename(local_file) or ".")
         return utils.system(scp_command)
+
+    def fetch (self, remote_file, local_file, recursive=False):
+        if self.is_local():
+            command="cp "
+            if recursive: command += "-r "
+            command += "%s %s"%(remote_file,local_file)
+        else:
+            command="scp "
+            command += TestSsh.std_options
+            if recursive: command += "-r "
+            command += self.key_part()
+            command += "%s:%s/%s %s"%(self.hostname_part(),self.buildname,remote_file,local_file)
+        utils.system(command)
+
+    # this is only to avoid harmless message when host cannot be identified
+    # convenience only
+    # the only place where this is needed is when tring to reach a slice in a node,
+    # which is done from the test master box
+    def clear_known_hosts (self):
+        known_hosts = "%s/.ssh/known_hosts"%os.getenv("HOME")
+        utils.header("Clearing entry for %s in %s"%(self.hostname,known_hosts))
+        utils.system("sed -i -e /^%s/d %s"%(self.hostname,known_hosts))