first step towards a cleaner TestSsh, buildname provided to constructor
[tests.git] / system / TestSsh.py
1 #this class is  used for any ssh command and
2 #also for any remote or a local command independently
3 #on which box this must be done.
4 #new TestSsh object take like an argument an instance
5 #of the class where it was created 
6
7 import os.path
8 import utils
9
10 class TestSsh:
11
12     # inserts a backslash before each occurence of the following chars
13     # \ " ' < > & | ; ( ) $ * ~ 
14     @staticmethod
15     def backslash_shell_specials (command):
16         result=''
17         for char in command:
18             if char in "\\\"'<>&|;()$*~":
19                 result +='\\'+char
20             else:
21                 result +=char
22         return result
23
24     # check main IP address against the provided hostname
25     @staticmethod
26     def is_local_hostname (hostname):
27         if hostname == "localhost":
28             return True
29         import socket
30         try:
31             local_ip = socket.gethostbyname(socket.gethostname())
32             remote_ip = socket.gethostbyname(hostname)
33             return local_ip==remote_ip
34         except:
35             utils.header("WARNING : something wrong in is_local_hostname with hostname=%s"%hostname)
36             return False
37
38     # buildname is the name of a directory that we can use in $hostname's homedir (/root)
39     def __init__(self,caller,buildname=None,key=None):
40         self.caller=caller
41         self.buildname=buildname
42         self._key=key
43
44     def hostname(self):
45         return self.caller.hostname()
46     def is_local(self):
47         return TestSsh.is_local_hostname(self.hostname())
48
49     # command gets run on the right box
50     def to_host(self,command):
51         if self.is_local():
52             return command
53         else:
54             return "ssh %s %s"%(self.hostname(),TestSsh.backslash_shell_specials(command))
55
56     def full_command(self,command):
57         return self.to_host(self.caller.host_to_guest(command))
58
59     def run_in_guest (self,command):
60         return utils.system(self.full_command(command))
61     
62     def run_in_host (self,command):
63         return utils.system(self.to_host(command))
64
65     # xxx quick n dirty
66     def run_in_guest_piped (self,local,remote):
67         return utils.system(local+" | "+self.full_command(remote))
68     
69     def run_in_buildname (self,command):
70         if not self.buildname:
71             utils.header ("WARNING : TestSsh.run_in_buildname without a buildname")
72             return 1
73         if self.is_local():
74             return utils.system(command)
75         ssh_command="ssh "
76         if self.caller.key:
77             ssh_command += "-i %s.rsa "%(self.caller.key)
78         ssh_command += "%s/%s"%(self.buildname,TestSsh.backslash_shell_specials(command))
79         return utils.system(ssh_command)
80
81     def copy (self,local_file,recursive=False):
82         if not self.buildname:
83             utils.header ("WARNING : TestSsh.copy without a buildname")
84             return 1
85         if self.is_local():
86             return 0
87         command="scp "
88         if recursive: command += "-r "
89         if self.caller.key:
90             command += "-i %s.rsa "
91         command +="%s %s:%s/%s"%(local_file,self.hostname(),self.buildname,
92                                  os.path.basename(local_file) or ".")
93         return utils.system(command)
94