1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
4 # class for issuing commands on a box, either local or remote
6 # the notion of 'buildname' is for providing each test run with a dir of its own
7 # buildname is generally the name of the build being tested, and can be considered unique
9 # thus 'run_in_buildname' mostly :
10 # (*) either runs locally in . - as on a local node we are already in a dedicated directory
11 # (*) or makes sure that there's a remote dir called 'buildname' and runs in it
13 # also, the copy operations
14 # (*) either do nothing if ran locally
15 # (*) or copy a local file into the remote 'buildname'
25 # inserts a backslash before each occurence of the following chars
26 # \ " ' < > & | ; ( ) $ * ~
28 def backslash_shell_specials(command):
31 if char in "\\\"'<>&|;()$*~":
37 # check main IP address against the provided hostname
39 def is_local_hostname(hostname):
40 if hostname == "localhost":
44 local_ip = socket.gethostbyname(socket.gethostname())
45 remote_ip = socket.gethostbyname(hostname)
46 return local_ip == remote_ip
48 utils.header("WARNING : something wrong in is_local_hostname with hostname={}".format(hostname))
51 # some boxes have their working space in user's homedir (/root),
52 # some others in a dedicated area with max. space (/vservers)
53 # when root is not specified we use the homedir
54 def __init__(self, hostname, buildname=None, key=None, username=None, unknown_host=True, root=None):
55 self.hostname = hostname
56 self.buildname = buildname
58 self.username = username
59 self.unknown_host = unknown_host
63 result = "{}@{}".format(self.username or 'root', self.hostname)
65 result += " <key {}>".format(self.key)
69 return TestSsh.is_local_hostname(self.hostname)
71 std_options="-o BatchMode=yes -o StrictHostKeyChecking=no -o CheckHostIP=no -o ConnectTimeout=5 "
72 unknown_option="-o UserKnownHostsFile=/dev/null "
77 return "-i {} ".format(self.key)
79 def hostname_part(self):
83 return "{}@{}".format(self.username,self.hostname)
85 # command gets run on the right box
86 def actual_command(self, command, keep_stdin=False, dry_run=False, backslash=True):
93 ssh_command += TestSsh.std_options
94 if self.unknown_host: ssh_command += TestSsh.unknown_option
95 ssh_command += self.key_part()
96 ssh_command += self.hostname_part() + " "
98 ssh_command += TestSsh.backslash_shell_specials(command)
100 ssh_command += command
104 def actual_argv (self, argv, keep_stdin=False, dry_run=False):
108 ssh_argv.append('ssh')
111 ssh_argv.append('-n')
112 ssh_argv += TestSsh.std_options.split()
113 if self.unknown_host:
114 ssh_argv += TestSsh.unknown_option.split()
115 ssh_argv += self.key_part().split()
116 ssh_argv.append(self.hostname_part())
120 def header(self, message):
123 print("===============",message)
126 def run(self, command, message=None, background=False, dry_run=False):
127 local_command = self.actual_command(command, dry_run=dry_run)
129 utils.header("DRY RUN " + local_command)
133 return utils.system(local_command, background)
135 def run_in_buildname(self, command, background=False, dry_run=False):
137 return utils.system(command, background)
138 self.create_buildname_once(dry_run)
139 return self.run("cd {} ; {}".format(self.fullname(self.buildname), command),
140 background=background, dry_run=dry_run)
142 def fullname(self, dirname):
146 return os.path.join(self.root,dirname)
148 def mkdir(self, dirname=None, abs=False, dry_run=False):
151 return os.path.mkdir(dirname)
153 # ab. paths remain as-is
156 dirname = "{}/{}".format(self.buildname, dirname)
158 dirname = self.buildname
159 dirname = self.fullname(dirname)
162 return self.run("mkdir -p {}".format(dirname), dry_run=dry_run)
164 def rmdir(self, dirname=None, dry_run=False):
167 return shutil.rmtree(dirname)
170 dirname = "{}/{}".format(self.buildname, dirname)
172 dirname = self.buildname
173 dirname = self.fullname(dirname)
174 return self.run("rm -rf {}".format(dirname), dry_run=dry_run)
176 def create_buildname_once(self, dry_run):
179 # create remote buildname on demand
181 self.buildname_created
183 self.mkdir(dry_run=dry_run)
184 self.buildname_created = True
186 def copy(self, local_file, recursive=False, dry_run=False):
189 self.create_buildname_once(dry_run)
192 scp_command += TestSsh.std_options
195 scp_command += self.key_part()
196 scp_command += "{} {}:{}/{}".format(local_file, self.hostname_part(),
197 self.fullname(self.buildname),
198 os.path.basename(local_file) or ".")
200 utils.header("DRY RUN TestSsh.copy {}".format(scp_command))
201 # need to be consistent with the non-dry-run mode
203 return utils.system(scp_command)
205 def copy_abs(self, local_file, remote_file,
206 recursive=False, dry_run=False):
210 dest = "{}:".format(self.hostname_part())
212 scp_command += TestSsh.std_options
215 scp_command += self.key_part()
216 scp_command += "{} {}{}".format(local_file, dest, remote_file)
218 utils.header("DRY RUN TestSsh.copy {}".format(scp_command))
219 # need to be consistent with the non-dry-run mode
221 return utils.system(scp_command)
223 def copy_home(self, local_file, recursive=False):
224 return self.copy_abs(local_file, os.path.basename(local_file), recursive)
226 def fetch (self, remote_file, local_file, recursive=False, dry_run=False):
231 command += "{} {}".format(remote_file, local_file)
235 command += TestSsh.std_options
238 command += self.key_part()
239 # absolute path - do not preprend buildname
240 if remote_file.find("/") == 0:
241 remote_path = remote_file
243 remote_path = "{}/{}".format(self.buildname, remote_file)
244 remote_path = self.fullname(remote_path)
245 command += "{}:{} {}".format(self.hostname_part(), remote_path, local_file)
246 return utils.system(command)
248 # this is only to avoid harmless message when host cannot be identified
250 # the only place where this is needed is when tring to reach a slice in a node,
251 # which is done from the test master box
252 def clear_known_hosts(self):
253 known_hosts = "{}/.ssh/known_hosts".format(os.getenv("HOME"))
254 utils.header("Clearing entry for {} in {}".format(self.hostname, known_hosts))
255 return utils.system("sed -i -e /^{}/d {}".format(self.hostname, known_hosts))