X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=system%2FTestSsh.py;h=cae5fa413d918ef466b2a4665d7a4c3ac4847958;hb=f8436f56fe24e8e545c9b3669897b8332dddffbc;hp=81956b04e49c1c638294d3325af1c45f6258cad6;hpb=aab610412467f640aec01ac74fbbe93d633aef03;p=tests.git diff --git a/system/TestSsh.py b/system/TestSsh.py index 81956b0..cae5fa4 100644 --- a/system/TestSsh.py +++ b/system/TestSsh.py @@ -1,5 +1,5 @@ -# -# Thierry Parmentelat - INRIA +# Thierry Parmentelat +# Copyright (C) 2010 INRIA # # class for issuing commands on a box, either local or remote # @@ -17,6 +17,7 @@ import os.path import utils +import shutil class TestSsh: @@ -46,16 +47,18 @@ 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, username=None): + def __init__(self,hostname,buildname=None,key=None, username=None,unknown_host=True): self.hostname=hostname self.buildname=buildname self.key=key self.username=username + self.unknown_host=unknown_host def is_local(self): return TestSsh.is_local_hostname(self.hostname) - std_options="-o StrictHostKeyChecking=no -o BatchMode=yes " + std_options="-o BatchMode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -o ConnectTimeout=5 " + unknown_option="-o UserKnownHostsFile=/dev/null " def key_part (self): if not self.key: @@ -69,20 +72,41 @@ class TestSsh: 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 + if self.unknown_host: ssh_command += TestSsh.unknown_option ssh_command += self.key_part() ssh_command += "%s %s" %(self.hostname_part(),TestSsh.backslash_shell_specials(command)) return ssh_command - def run(self, command,background=False): + # same in argv form + def actual_argv (self, argv,keep_stdin=False): + if self.is_local(): + return argv + ssh_argv=[] + ssh_argv.append('ssh') + if not keep_stdin: ssh_argv.append('-n') + ssh_argv += TestSsh.std_options.split() + if self.unknown_host: ssh_argv += TestSsh.unknown_option.split() + ssh_argv += self.key_part().split() + ssh_argv.append(self.hostname_part()) + ssh_argv += argv + return ssh_argv + + def header (self,message): + if not message: return + print "===============",message + sys.stdout.flush() + + def run(self, command,message=None,background=False): local_command = self.actual_command(command) - if background: - local_command += " &" - return utils.system(local_command) + self.header(message) + return utils.system(local_command,background) def clean_dir (self,dirname): if self.is_local(): @@ -100,6 +124,17 @@ class TestSsh: dirname=self.buildname return self.run("mkdir -p %s"%dirname) + def rmdir (self,dirname=None): + if self.is_local(): + if dirname: + return shutil.rmtree(dirname) + return 0 + if dirname: + dirname="%s/%s"%(self.buildname,dirname) + else: + dirname=self.buildname + return self.run("rm -rf %s"%dirname) + def create_buildname_once (self): if self.is_local(): return @@ -128,6 +163,21 @@ class TestSsh: self.buildname,os.path.basename(local_file) or ".") return utils.system(scp_command) + def copy_abs (self,local_file,remote_file,recursive=False): + if self.is_local(): + dest="" + else: + dest= "%s:"%self.hostname_part() + scp_command="scp " + scp_command += TestSsh.std_options + if recursive: scp_command += "-r " + scp_command += self.key_part() + scp_command += "%s %s%s"%(local_file,dest,remote_file) + return utils.system(scp_command) + + def copy_home (self, local_file, recursive=False): + return self.copy_abs(local_file,os.path.basename(local_file),recursive) + def fetch (self, remote_file, local_file, recursive=False): if self.is_local(): command="cp " @@ -138,5 +188,20 @@ class TestSsh: 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) + # absolute path - do not preprend buildname + if remote_file.find("/")==0: + remote_path=remote_file + else: + remote_path="%s/%s"%(self.buildname,remote_file) + command += "%s:%s %s"%(self.hostname_part(),remote_path,local_file) + return 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)) + return utils.system("sed -i -e /^%s/d %s"%(self.hostname,known_hosts)) +