mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 5 | """ |
| 6 | This module defines the SSHHost class. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | |
| 8 | Implementation details: |
| 9 | You should import the "hosts" package instead of importing each type of host. |
| 10 | |
| 11 | SSHHost: a remote machine with a ssh access |
| 12 | """ |
| 13 | |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 14 | __author__ = """ |
| 15 | mbligh@google.com (Martin J. Bligh), |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 16 | poirier@google.com (Benjamin Poirier), |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 17 | stutsman@google.com (Ryan Stutsman) |
| 18 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | import types |
| 22 | import os |
| 23 | import time |
| 24 | |
| 25 | import base_classes |
| 26 | import utils |
| 27 | import errors |
| 28 | |
| 29 | |
| 30 | class SSHHost(base_classes.RemoteHost): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 31 | """ |
| 32 | This class represents a remote machine controlled through an ssh |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 33 | session on which you can run programs. |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 34 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 35 | It is not the machine autoserv is running on. The machine must be |
| 36 | configured for password-less login, for example through public key |
| 37 | authentication. |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 38 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 39 | Implementation details: |
| 40 | This is a leaf class in an abstract class hierarchy, it must |
| 41 | implement the unimplemented methods in parent classes. |
| 42 | """ |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 43 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 44 | def __init__(self, hostname, user="root", port=22): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 45 | """ |
| 46 | Construct a SSHHost object |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 47 | |
| 48 | Args: |
| 49 | hostname: network hostname or address of remote machine |
| 50 | user: user to log in as on the remote machine |
| 51 | port: port the ssh daemon is listening on on the remote |
| 52 | machine |
| 53 | """ |
| 54 | super(SSHHost, self).__init__() |
| 55 | |
| 56 | self.hostname= hostname |
| 57 | self.user= user |
| 58 | self.port= port |
| 59 | self.tmp_dirs= [] |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 60 | |
| 61 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 62 | def __del__(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 63 | """ |
| 64 | Destroy a SSHHost object |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 65 | """ |
| 66 | for dir in self.tmp_dirs: |
| 67 | try: |
| 68 | self.run('rm -rf "%s"' % (utils.sh_escape(dir))) |
| 69 | except errors.AutoservRunError: |
| 70 | pass |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 71 | |
| 72 | |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 73 | def run(self, command, timeout=None, ignore_status=False): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 74 | """ |
| 75 | Run a command on the remote host. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 76 | |
| 77 | Args: |
| 78 | command: the command line string |
| 79 | timeout: time limit in seconds before attempting to |
| 80 | kill the running process. The run() function |
| 81 | will take a few seconds longer than 'timeout' |
| 82 | to complete if it has to kill the process. |
| 83 | |
| 84 | Returns: |
| 85 | a hosts.base_classes.CmdResult object |
| 86 | |
| 87 | Raises: |
| 88 | AutoservRunError: the exit code of the command |
| 89 | execution was not 0 |
| 90 | """ |
| 91 | #~ print "running %s" % (command,) |
| 92 | result= utils.run(r'ssh -l %s -p %d %s "%s"' % (self.user, |
| 93 | self.port, self.hostname, utils.sh_escape(command)), |
mbligh | cf965b0 | 2007-07-25 16:49:45 +0000 | [diff] [blame] | 94 | timeout, ignore_status) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 95 | return result |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 96 | |
| 97 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 98 | def reboot(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 99 | """ |
| 100 | Reboot the remote host. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 101 | |
| 102 | TODO(poirier): Should the function return only after having |
| 103 | done a self.wait_down()? or should this be left to |
| 104 | the control file? |
| 105 | pro: A common usage pattern would be reboot(), |
| 106 | wait_down(), wait_up(), [more commands]. If wait_down() |
| 107 | is not there, wait_up() is likely to return right away |
| 108 | because the ssh daemon has not yet shutdown, so a |
| 109 | control file expecting the host to have rebooted will |
| 110 | run eronously. Doing the wait_down() in reboot |
| 111 | eliminates the risk of confusion. Also, making the |
| 112 | wait_down() external might lead to race conditions if |
| 113 | the control file does a reboot() does some other things, |
| 114 | then there's no way to know if it should wait_down() |
| 115 | first or wait_up() right away. |
| 116 | con: wait_down() just after reboot will be mandatory, |
| 117 | this might be undesirable if there are other operations |
| 118 | that can be executed right after the reboot, for |
| 119 | example many hosts have to be rebooted at the same |
| 120 | time. The solution to this is to use multiple |
| 121 | threads of execution in the control file. |
| 122 | """ |
| 123 | self.run("reboot") |
| 124 | self.wait_down() |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 125 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 126 | def get_file(self, source, dest): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 127 | """ |
| 128 | Copy files from the remote host to a local path. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 129 | |
| 130 | Directories will be copied recursively. |
| 131 | If a source component is a directory with a trailing slash, |
| 132 | the content of the directory will be copied, otherwise, the |
| 133 | directory itself and its content will be copied. This |
| 134 | behavior is similar to that of the program 'rsync'. |
| 135 | |
| 136 | Args: |
| 137 | source: either |
| 138 | 1) a single file or directory, as a string |
| 139 | 2) a list of one or more (possibly mixed) |
| 140 | files or directories |
| 141 | dest: a file or a directory (if source contains a |
| 142 | directory or more than one element, you must |
| 143 | supply a directory dest) |
| 144 | |
| 145 | Raises: |
| 146 | AutoservRunError: the scp command failed |
| 147 | """ |
| 148 | if isinstance(source, types.StringTypes): |
| 149 | source= [source] |
| 150 | |
| 151 | processed_source= [] |
| 152 | for entry in source: |
| 153 | if entry.endswith('/'): |
| 154 | format_string= '%s@%s:"%s*"' |
| 155 | else: |
| 156 | format_string= '%s@%s:"%s"' |
| 157 | entry= format_string % (self.user, self.hostname, |
| 158 | utils.scp_remote_escape(entry)) |
| 159 | processed_source.append(entry) |
| 160 | |
| 161 | processed_dest= os.path.abspath(dest) |
| 162 | if os.path.isdir(dest): |
| 163 | processed_dest= "%s/" % (utils.sh_escape(processed_dest),) |
| 164 | else: |
| 165 | processed_dest= utils.sh_escape(processed_dest) |
| 166 | |
| 167 | utils.run('scp -rpq %s "%s"' % ( |
| 168 | " ".join(processed_source), |
| 169 | processed_dest)) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 170 | |
| 171 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 172 | def send_file(self, source, dest): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 173 | """ |
| 174 | Copy files from a local path to the remote host. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 175 | |
| 176 | Directories will be copied recursively. |
| 177 | If a source component is a directory with a trailing slash, |
| 178 | the content of the directory will be copied, otherwise, the |
| 179 | directory itself and its content will be copied. This |
| 180 | behavior is similar to that of the program 'rsync'. |
| 181 | |
| 182 | Args: |
| 183 | source: either |
| 184 | 1) a single file or directory, as a string |
| 185 | 2) a list of one or more (possibly mixed) |
| 186 | files or directories |
| 187 | dest: a file or a directory (if source contains a |
| 188 | directory or more than one element, you must |
| 189 | supply a directory dest) |
| 190 | |
| 191 | Raises: |
| 192 | AutoservRunError: the scp command failed |
| 193 | """ |
| 194 | if isinstance(source, types.StringTypes): |
| 195 | source= [source] |
| 196 | |
| 197 | processed_source= [] |
| 198 | for entry in source: |
| 199 | if entry.endswith('/'): |
| 200 | format_string= '"%s/"*' |
| 201 | else: |
| 202 | format_string= '"%s"' |
| 203 | entry= format_string % (utils.sh_escape(os.path.abspath(entry)),) |
| 204 | processed_source.append(entry) |
| 205 | |
| 206 | utils.run('scp -rpq %s %s@%s:"%s"' % ( |
| 207 | " ".join(processed_source), self.user, self.hostname, |
| 208 | utils.scp_remote_escape(dest))) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 209 | |
| 210 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 211 | def get_tmp_dir(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 212 | """ |
| 213 | Return the pathname of a directory on the host suitable |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 214 | for temporary file storage. |
| 215 | |
| 216 | The directory and its content will be deleted automatically |
| 217 | on the destruction of the Host object that was used to obtain |
| 218 | it. |
| 219 | """ |
| 220 | dir_name= self.run("mktemp -dt autoserv-XXXXXX").stdout.rstrip(" \n") |
| 221 | self.tmp_dirs.append(dir_name) |
| 222 | return dir_name |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 223 | |
| 224 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 225 | def is_up(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 226 | """ |
| 227 | Check if the remote host is up. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 228 | |
| 229 | Returns: |
| 230 | True if the remote host is up, False otherwise |
| 231 | """ |
| 232 | try: |
| 233 | result= self.run("true", timeout=10) |
| 234 | except errors.AutoservRunError: |
| 235 | return False |
| 236 | else: |
| 237 | if result.exit_status == 0: |
| 238 | return True |
| 239 | else: |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 240 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 241 | return False |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 242 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 243 | def wait_up(self, timeout=None): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 244 | """ |
| 245 | Wait until the remote host is up or the timeout expires. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 246 | |
| 247 | In fact, it will wait until an ssh connection to the remote |
| 248 | host can be established. |
| 249 | |
| 250 | Args: |
| 251 | timeout: time limit in seconds before returning even |
| 252 | if the host is not up. |
| 253 | |
| 254 | Returns: |
| 255 | True if the host was found to be up, False otherwise |
| 256 | """ |
| 257 | if timeout: |
| 258 | end_time= time.time() + timeout |
| 259 | |
| 260 | while not timeout or time.time() < end_time: |
| 261 | try: |
| 262 | if timeout: |
| 263 | run_timeout= end_time - time.time() |
| 264 | else: |
| 265 | run_timeout= 10 |
| 266 | result= self.run("true", timeout=run_timeout) |
| 267 | except errors.AutoservRunError: |
| 268 | pass |
| 269 | else: |
| 270 | if result.exit_status == 0: |
| 271 | return True |
| 272 | time.sleep(1) |
| 273 | |
| 274 | return False |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 275 | |
| 276 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 277 | def wait_down(self, timeout=None): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 278 | """ |
| 279 | Wait until the remote host is down or the timeout expires. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 280 | |
| 281 | In fact, it will wait until an ssh connection to the remote |
| 282 | host fails. |
| 283 | |
| 284 | Args: |
| 285 | timeout: time limit in seconds before returning even |
| 286 | if the host is not up. |
| 287 | |
| 288 | Returns: |
| 289 | True if the host was found to be down, False otherwise |
| 290 | """ |
| 291 | if timeout: |
| 292 | end_time= time.time() + timeout |
| 293 | |
| 294 | while not timeout or time.time() < end_time: |
| 295 | try: |
mbligh | 7e1e964 | 2007-07-31 18:00:45 +0000 | [diff] [blame] | 296 | run_timeout= 10 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 297 | result= self.run("true", timeout=run_timeout) |
| 298 | except errors.AutoservRunError: |
| 299 | return True |
| 300 | else: |
| 301 | if result.aborted: |
| 302 | return True |
| 303 | time.sleep(1) |
| 304 | |
| 305 | return False |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 306 | |
| 307 | |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 308 | def ensure_up(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 309 | """ |
| 310 | Ensure the host is up if it is not then do not proceed; |
| 311 | this prevents cacading failures of tests |
| 312 | """ |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 313 | if not self.wait_up(300) and hasattr(self, 'hardreset'): |
| 314 | print "Performing a hardreset on %s" % self.hostname |
| 315 | self.hardreset() |
| 316 | self.wait_up() |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 317 | |
| 318 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 319 | def get_num_cpu(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame^] | 320 | """ |
| 321 | Get the number of CPUs in the host according to |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 322 | /proc/cpuinfo. |
| 323 | |
| 324 | Returns: |
| 325 | The number of CPUs |
| 326 | """ |
| 327 | |
| 328 | proc_cpuinfo= self.run("cat /proc/cpuinfo").stdout |
| 329 | cpus = 0 |
| 330 | for line in proc_cpuinfo.splitlines(): |
| 331 | if line.startswith('processor'): |
| 332 | cpus += 1 |
| 333 | return cpus |