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 | |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 21 | import types, os, sys, signal, subprocess, time, re, socket, pdb |
| 22 | |
| 23 | from autotest_lib.client.common_lib import error |
| 24 | from autotest_lib.server import utils |
| 25 | import remote, bootloader |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 26 | |
| 27 | |
mbligh | bda9c9c | 2008-04-08 17:45:00 +0000 | [diff] [blame] | 28 | |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 29 | class SSHHost(remote.RemoteHost): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 30 | """ |
| 31 | This class represents a remote machine controlled through an ssh |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 32 | session on which you can run programs. |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 33 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 34 | It is not the machine autoserv is running on. The machine must be |
| 35 | configured for password-less login, for example through public key |
| 36 | authentication. |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 37 | |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 38 | It includes support for controlling the machine through a serial |
| 39 | console on which you can run programs. If such a serial console is |
| 40 | set up on the machine then capabilities such as hard reset and |
| 41 | boot strap monitoring are available. If the machine does not have a |
| 42 | serial console available then ordinary SSH-based commands will |
| 43 | still be available, but attempts to use extensions such as |
| 44 | console logging or hard reset will fail silently. |
| 45 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 46 | Implementation details: |
| 47 | This is a leaf class in an abstract class hierarchy, it must |
| 48 | implement the unimplemented methods in parent classes. |
| 49 | """ |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 50 | |
mbligh | 31a49de | 2007-11-05 18:41:19 +0000 | [diff] [blame] | 51 | DEFAULT_REBOOT_TIMEOUT = 1800 |
| 52 | job = None |
mbligh | 0faf91f | 2007-10-18 03:10:48 +0000 | [diff] [blame] | 53 | |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 54 | def __init__(self, hostname, user="root", port=22, initialize=True, |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 55 | conmux_log="console.log", |
mbligh | e6c995f | 2007-10-26 19:43:01 +0000 | [diff] [blame] | 56 | conmux_server=None, conmux_attach=None, |
mbligh | da13d54 | 2008-01-03 16:28:34 +0000 | [diff] [blame] | 57 | netconsole_log=None, netconsole_port=6666, autodir=None): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 58 | """ |
| 59 | Construct a SSHHost object |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 60 | |
| 61 | Args: |
| 62 | hostname: network hostname or address of remote machine |
| 63 | user: user to log in as on the remote machine |
| 64 | port: port the ssh daemon is listening on on the remote |
| 65 | machine |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 66 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 67 | self.hostname= hostname |
| 68 | self.user= user |
| 69 | self.port= port |
| 70 | self.tmp_dirs= [] |
mbligh | 137a05c | 2007-10-04 15:56:51 +0000 | [diff] [blame] | 71 | self.initialize = initialize |
mbligh | da13d54 | 2008-01-03 16:28:34 +0000 | [diff] [blame] | 72 | self.autodir = autodir |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 73 | |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 74 | super(SSHHost, self).__init__() |
| 75 | |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 76 | self.conmux_server = conmux_server |
mbligh | 70cf0ec | 2008-01-18 17:57:14 +0000 | [diff] [blame] | 77 | if conmux_attach: |
| 78 | self.conmux_attach = conmux_attach |
| 79 | else: |
| 80 | self.conmux_attach = os.path.abspath(os.path.join( |
| 81 | self.serverdir, '..', |
| 82 | 'conmux', 'conmux-attach')) |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 83 | self.logger_popen = None |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 84 | self.warning_stream = None |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 85 | self.__start_console_log(conmux_log) |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 86 | |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 87 | self.bootloader = bootloader.Bootloader(self) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 88 | |
mbligh | c0e9239 | 2007-11-05 19:10:10 +0000 | [diff] [blame] | 89 | self.__netconsole_param = "" |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 90 | self.netlogger_popen = None |
mbligh | c0e9239 | 2007-11-05 19:10:10 +0000 | [diff] [blame] | 91 | if netconsole_log: |
| 92 | self.__init_netconsole_params(netconsole_port) |
| 93 | self.__start_netconsole_log(netconsole_log, netconsole_port) |
| 94 | self.__load_netconsole_module() |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 95 | |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 96 | |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 97 | @staticmethod |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 98 | def __kill(popen): |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 99 | return_code = popen.poll() |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 100 | if return_code is None: |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 101 | try: |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 102 | os.kill(popen.pid, signal.SIGTERM) |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 103 | except OSError: |
| 104 | pass |
| 105 | |
| 106 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 107 | def __del__(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 108 | """ |
| 109 | Destroy a SSHHost object |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 110 | """ |
| 111 | for dir in self.tmp_dirs: |
| 112 | try: |
| 113 | self.run('rm -rf "%s"' % (utils.sh_escape(dir))) |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 114 | except error.AutoservRunError: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 115 | pass |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 116 | # kill the console logger |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 117 | if getattr(self, 'logger_popen', None): |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 118 | self.__kill(self.logger_popen) |
mbligh | 632f838 | 2008-02-27 16:39:41 +0000 | [diff] [blame] | 119 | self.job.warning_loggers.discard(self.warning_stream) |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 120 | self.warning_stream.close() |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 121 | # kill the netconsole logger |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 122 | if getattr(self, 'netlogger_popen', None): |
mbligh | e6c995f | 2007-10-26 19:43:01 +0000 | [diff] [blame] | 123 | self.__unload_netconsole_module() |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 124 | self.__kill(self.netlogger_popen) |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def __init_netconsole_params(self, port): |
| 128 | """ |
| 129 | Connect to the remote machine and determine the values to use for the |
| 130 | required netconsole parameters. |
| 131 | """ |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 132 | # PROBLEM: on machines with multiple IPs this may not make any sense |
| 133 | # It also doesn't work with IPv6 |
| 134 | remote_ip = socket.gethostbyname(self.hostname) |
| 135 | local_ip = socket.gethostbyname(socket.gethostname()) |
| 136 | # Get the gateway of the remote machine |
| 137 | try: |
| 138 | traceroute = self.run('traceroute -n %s' % local_ip) |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 139 | except error.AutoservRunError: |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 140 | return |
| 141 | first_node = traceroute.stdout.split("\n")[0] |
| 142 | match = re.search(r'\s+((\d+\.){3}\d+)\s+', first_node) |
| 143 | if match: |
| 144 | router_ip = match.group(1) |
| 145 | else: |
| 146 | return |
| 147 | # Look up the MAC address of the gateway |
| 148 | try: |
| 149 | self.run('ping -c 1 %s' % router_ip) |
| 150 | arp = self.run('arp -n -a %s' % router_ip) |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 151 | except error.AutoservRunError: |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 152 | return |
| 153 | match = re.search(r'\s+(([0-9A-F]{2}:){5}[0-9A-F]{2})\s+', arp.stdout) |
| 154 | if match: |
| 155 | gateway_mac = match.group(1) |
| 156 | else: |
| 157 | return |
| 158 | self.__netconsole_param = 'netconsole=@%s/,%s@%s/%s' % (remote_ip, |
| 159 | port, |
| 160 | local_ip, |
| 161 | gateway_mac) |
| 162 | |
| 163 | |
| 164 | def __start_netconsole_log(self, logfilename, port): |
| 165 | """ |
| 166 | Log the output of netconsole to a specified file |
| 167 | """ |
| 168 | if logfilename == None: |
| 169 | return |
| 170 | cmd = ['nc', '-u', '-l', '-p', str(port)] |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 171 | logfile = open(logfilename, 'a', 0) |
| 172 | self.netlogger_popen = subprocess.Popen(cmd, stdout=logfile) |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 173 | |
| 174 | |
| 175 | def __load_netconsole_module(self): |
| 176 | """ |
| 177 | Make a best effort to load the netconsole module. |
| 178 | |
| 179 | Note that loading the module can fail even when the remote machine is |
| 180 | working correctly if netconsole is already compiled into the kernel |
| 181 | and started. |
| 182 | """ |
mbligh | c0e9239 | 2007-11-05 19:10:10 +0000 | [diff] [blame] | 183 | if not self.__netconsole_param: |
| 184 | return |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 185 | try: |
| 186 | self.run('modprobe netconsole %s' % self.__netconsole_param) |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 187 | except error.AutoservRunError: |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 188 | # if it fails there isn't much we can do, just keep going |
| 189 | pass |
| 190 | |
| 191 | |
| 192 | def __unload_netconsole_module(self): |
| 193 | try: |
| 194 | self.run('modprobe -r netconsole') |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 195 | except error.AutoservRunError: |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 196 | pass |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 197 | |
| 198 | |
mbligh | 5deff3d | 2008-01-04 21:21:28 +0000 | [diff] [blame] | 199 | def wait_for_restart(self, timeout=DEFAULT_REBOOT_TIMEOUT): |
mbligh | d567f72 | 2007-10-30 15:37:33 +0000 | [diff] [blame] | 200 | if not self.wait_down(300): # Make sure he's dead, Jim |
mbligh | f3b7893 | 2007-11-07 16:52:47 +0000 | [diff] [blame] | 201 | self.__record("ABORT", None, "reboot.verify", "shutdown failed") |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 202 | raise error.AutoservRebootError( |
| 203 | "Host did not shut down") |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 204 | self.wait_up(timeout) |
| 205 | time.sleep(2) # this is needed for complete reliability |
mbligh | cf3d83a | 2007-11-05 19:21:39 +0000 | [diff] [blame] | 206 | if self.wait_up(timeout): |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 207 | self.__record("GOOD", None, "reboot.verify") |
mbligh | cf3d83a | 2007-11-05 19:21:39 +0000 | [diff] [blame] | 208 | else: |
mbligh | 71d2422 | 2008-03-11 21:31:56 +0000 | [diff] [blame] | 209 | self.__record("ABORT", None, "reboot.verify", "Host did not return from reboot") |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 210 | raise error.AutoservRebootError( |
| 211 | "Host did not return from reboot") |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 212 | print "Reboot complete" |
| 213 | |
| 214 | |
mbligh | 80d2077 | 2007-10-29 17:10:10 +0000 | [diff] [blame] | 215 | def hardreset(self, timeout=DEFAULT_REBOOT_TIMEOUT, wait=True): |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 216 | """ |
| 217 | Reach out and slap the box in the power switch |
| 218 | """ |
mbligh | f3b7893 | 2007-11-07 16:52:47 +0000 | [diff] [blame] | 219 | if not self.__console_run(r"'~$hardreset'"): |
| 220 | self.__record("ABORT", None, "reboot.start", "hard reset unavailable") |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 221 | raise error.AutoservUnsupportedError( |
| 222 | 'Hard reset unavailable') |
mbligh | 37d53c3 | 2008-01-14 16:16:00 +0000 | [diff] [blame] | 223 | |
| 224 | if wait: |
| 225 | self.wait_for_restart(timeout) |
mbligh | a4d4f37 | 2008-01-22 15:49:50 +0000 | [diff] [blame] | 226 | self.__record("GOOD", None, "reboot.start", "hard reset") |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 227 | |
| 228 | |
mbligh | e6c995f | 2007-10-26 19:43:01 +0000 | [diff] [blame] | 229 | def __conmux_hostname(self): |
| 230 | if self.conmux_server: |
| 231 | return '%s/%s' % (self.conmux_server, self.hostname) |
| 232 | else: |
| 233 | return self.hostname |
| 234 | |
| 235 | |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 236 | def __start_console_log(self, logfilename): |
| 237 | """ |
| 238 | Log the output of the console session to a specified file |
| 239 | """ |
| 240 | if logfilename == None: |
| 241 | return |
| 242 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 243 | return |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 244 | |
| 245 | r, w = os.pipe() |
| 246 | script_path = os.path.join(self.serverdir, |
| 247 | 'warning_monitor.py') |
mbligh | fbb0354 | 2008-02-11 16:27:29 +0000 | [diff] [blame] | 248 | cmd = [self.conmux_attach, self.__conmux_hostname(), |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 249 | '%s %s %s %d' % (sys.executable, script_path, |
| 250 | logfilename, w)] |
mbligh | 0c5ce31 | 2008-02-21 16:24:11 +0000 | [diff] [blame] | 251 | dev_null = open(os.devnull, 'w') |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 252 | |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 253 | self.warning_stream = os.fdopen(r, 'r', 0) |
| 254 | self.job.warning_loggers.add(self.warning_stream) |
| 255 | self.logger_popen = subprocess.Popen(cmd, stderr=dev_null) |
| 256 | os.close(w) |
mbligh | e6c995f | 2007-10-26 19:43:01 +0000 | [diff] [blame] | 257 | |
| 258 | |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 259 | def __console_run(self, cmd): |
| 260 | """ |
| 261 | Send a command to the conmux session |
| 262 | """ |
| 263 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 264 | return False |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 265 | cmd = '%s %s echo %s 2> /dev/null' % (self.conmux_attach, |
mbligh | e6c995f | 2007-10-26 19:43:01 +0000 | [diff] [blame] | 266 | self.__conmux_hostname(), |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 267 | cmd) |
mbligh | 0f5ad64 | 2008-01-22 16:37:40 +0000 | [diff] [blame] | 268 | result = utils.system(cmd, ignore_status=True) |
mbligh | 3409ee7 | 2007-10-16 23:58:33 +0000 | [diff] [blame] | 269 | return result == 0 |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 270 | |
| 271 | |
mbligh | 31a49de | 2007-11-05 18:41:19 +0000 | [diff] [blame] | 272 | def __record(self, status_code, subdir, operation, status = ''): |
| 273 | if self.job: |
| 274 | self.job.record(status_code, subdir, operation, status) |
| 275 | else: |
| 276 | if not subdir: |
| 277 | subdir = "----" |
| 278 | msg = "%s\t%s\t%s\t%s" % (status_code, subdir, operation, status) |
| 279 | sys.stderr.write(msg + "\n") |
| 280 | |
| 281 | |
mbligh | fa97160 | 2008-01-03 01:57:20 +0000 | [diff] [blame] | 282 | def ssh_base_command(self, connect_timeout=30): |
| 283 | SSH_BASE_COMMAND = '/usr/bin/ssh -a -x -o ' + \ |
mbligh | 0ad21ba | 2008-03-14 15:06:21 +0000 | [diff] [blame] | 284 | 'BatchMode=yes -o ConnectTimeout=%d ' + \ |
| 285 | '-o ServerAliveInterval=300' |
mbligh | fa97160 | 2008-01-03 01:57:20 +0000 | [diff] [blame] | 286 | assert isinstance(connect_timeout, (int, long)) |
| 287 | assert connect_timeout > 0 # can't disable the timeout |
| 288 | return SSH_BASE_COMMAND % connect_timeout |
| 289 | |
| 290 | |
| 291 | def ssh_command(self, connect_timeout=30): |
mbligh | e6647d1 | 2007-10-17 00:00:01 +0000 | [diff] [blame] | 292 | """Construct an ssh command with proper args for this host.""" |
mbligh | fa97160 | 2008-01-03 01:57:20 +0000 | [diff] [blame] | 293 | ssh = self.ssh_base_command(connect_timeout) |
| 294 | return r'%s -l %s -p %d %s' % (ssh, |
mbligh | 0faf91f | 2007-10-18 03:10:48 +0000 | [diff] [blame] | 295 | self.user, |
| 296 | self.port, |
| 297 | self.hostname) |
mbligh | e6647d1 | 2007-10-17 00:00:01 +0000 | [diff] [blame] | 298 | |
| 299 | |
mbligh | 07a923f | 2008-01-16 17:49:04 +0000 | [diff] [blame] | 300 | def run(self, command, timeout=3600, ignore_status=False, |
mbligh | fa97160 | 2008-01-03 01:57:20 +0000 | [diff] [blame] | 301 | stdout_tee=None, stderr_tee=None, connect_timeout=30): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 302 | """ |
| 303 | Run a command on the remote host. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 304 | |
| 305 | Args: |
| 306 | command: the command line string |
| 307 | timeout: time limit in seconds before attempting to |
| 308 | kill the running process. The run() function |
| 309 | will take a few seconds longer than 'timeout' |
| 310 | to complete if it has to kill the process. |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 311 | ignore_status: do not raise an exception, no matter |
| 312 | what the exit code of the command is. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 313 | |
| 314 | Returns: |
| 315 | a hosts.base_classes.CmdResult object |
| 316 | |
| 317 | Raises: |
| 318 | AutoservRunError: the exit code of the command |
| 319 | execution was not 0 |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 320 | AutoservSSHTimeout: ssh connection has timed out |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 321 | """ |
mbligh | adf2aab | 2007-11-29 18:16:43 +0000 | [diff] [blame] | 322 | stdout = stdout_tee or sys.stdout |
mbligh | 8d4baaa | 2008-03-12 14:48:24 +0000 | [diff] [blame] | 323 | stderr = stderr_tee or sys.stdout |
mbligh | 7995cc6 | 2007-11-30 15:53:23 +0000 | [diff] [blame] | 324 | print "ssh: %s" % (command,) |
mbligh | adf2aab | 2007-11-29 18:16:43 +0000 | [diff] [blame] | 325 | env = " ".join("=".join(pair) for pair in self.env.iteritems()) |
mbligh | 34faa28 | 2008-01-16 17:44:49 +0000 | [diff] [blame] | 326 | full_cmd = '%s "%s %s"' % (self.ssh_command(connect_timeout), |
| 327 | env, utils.sh_escape(command)) |
| 328 | result = utils.run(full_cmd, timeout, True, stdout, stderr) |
| 329 | if result.exit_status == 255: # ssh's exit status for timeout |
| 330 | if re.match(r'^ssh: connect to host .* port .*: ' + |
| 331 | r'Connection timed out\r$', result.stderr): |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 332 | raise error.AutoservSSHTimeout("ssh timed out", |
| 333 | result) |
mbligh | 34faa28 | 2008-01-16 17:44:49 +0000 | [diff] [blame] | 334 | if not ignore_status and result.exit_status > 0: |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 335 | raise error.AutoservRunError("command execution error", |
| 336 | result) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 337 | return result |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 338 | |
| 339 | |
mbligh | bda9c9c | 2008-04-08 17:45:00 +0000 | [diff] [blame] | 340 | def run_short(self, command, **kwargs): |
| 341 | """ |
| 342 | Calls the run() command with a short default timeout. |
| 343 | |
| 344 | Args: |
| 345 | Takes the same arguments as does run(), |
| 346 | with the exception of the timeout argument which |
| 347 | here is fixed at 60 seconds. |
| 348 | It returns the result of run. |
| 349 | """ |
| 350 | return self.run(command, timeout=60, **kwargs) |
| 351 | |
| 352 | |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 353 | def run_grep(self, command, timeout=30, ignore_status=False, |
| 354 | stdout_ok_regexp=None, stdout_err_regexp=None, |
| 355 | stderr_ok_regexp=None, stderr_err_regexp=None, |
| 356 | connect_timeout=30): |
| 357 | """ |
| 358 | Run a command on the remote host and look for regexp |
| 359 | in stdout or stderr to determine if the command was |
| 360 | successul or not. |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 361 | |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 362 | Args: |
| 363 | command: the command line string |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 364 | timeout: time limit in seconds before attempting to |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 365 | kill the running process. The run() function |
| 366 | will take a few seconds longer than 'timeout' |
| 367 | to complete if it has to kill the process. |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 368 | ignore_status: do not raise an exception, no matter |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 369 | what the exit code of the command is. |
| 370 | stdout_ok_regexp: regexp that should be in stdout |
| 371 | if the command was successul. |
| 372 | stdout_err_regexp: regexp that should be in stdout |
| 373 | if the command failed. |
| 374 | stderr_ok_regexp: regexp that should be in stderr |
| 375 | if the command was successul. |
| 376 | stderr_err_regexp: regexp that should be in stderr |
| 377 | if the command failed. |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 378 | |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 379 | Returns: |
| 380 | if the command was successul, raises an exception |
| 381 | otherwise. |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 382 | |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 383 | Raises: |
| 384 | AutoservRunError: |
| 385 | - the exit code of the command execution was not 0. |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 386 | - If stderr_err_regexp is found in stderr, |
| 387 | - If stdout_err_regexp is found in stdout, |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 388 | - If stderr_ok_regexp is not found in stderr. |
| 389 | - If stdout_ok_regexp is not found in stdout, |
| 390 | """ |
| 391 | |
| 392 | # We ignore the status, because we will handle it at the end. |
| 393 | result = self.run(command, timeout, ignore_status=True, |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 394 | connect_timeout=connect_timeout) |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 395 | |
| 396 | # Look for the patterns, in order |
| 397 | for (regexp, stream) in ((stderr_err_regexp, result.stderr), |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 398 | (stdout_err_regexp, result.stdout)): |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 399 | if regexp and stream: |
| 400 | err_re = re.compile (regexp) |
| 401 | if err_re.search(stream): |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 402 | raise error.AutoservRunError( |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 403 | '%s failed, found error pattern: ' |
| 404 | '"%s"' % (command, regexp), result) |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 405 | |
| 406 | for (regexp, stream) in ((stderr_ok_regexp, result.stderr), |
mbligh | 6a2a2df | 2008-01-16 17:41:55 +0000 | [diff] [blame] | 407 | (stdout_ok_regexp, result.stdout)): |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 408 | if regexp and stream: |
| 409 | ok_re = re.compile (regexp) |
| 410 | if ok_re.search(stream): |
| 411 | if ok_re.search(stream): |
| 412 | return |
| 413 | |
| 414 | if not ignore_status and result.exit_status > 0: |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 415 | raise error.AutoservRunError("command execution error", |
| 416 | result) |
mbligh | 78669ff | 2008-01-10 16:33:07 +0000 | [diff] [blame] | 417 | |
| 418 | |
mbligh | 80d2077 | 2007-10-29 17:10:10 +0000 | [diff] [blame] | 419 | def reboot(self, timeout=DEFAULT_REBOOT_TIMEOUT, label=None, |
| 420 | kernel_args=None, wait=True): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 421 | """ |
| 422 | Reboot the remote host. |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 423 | |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 424 | Args: |
| 425 | timeout |
mbligh | 8b85dfb | 2007-08-28 09:50:31 +0000 | [diff] [blame] | 426 | """ |
mbligh | 33ae090 | 2007-11-24 19:27:08 +0000 | [diff] [blame] | 427 | self.reboot_setup() |
| 428 | |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 429 | # forcibly include the "netconsole" kernel arg |
| 430 | if self.__netconsole_param: |
| 431 | if kernel_args is None: |
| 432 | kernel_args = self.__netconsole_param |
| 433 | else: |
| 434 | kernel_args += " " + self.__netconsole_param |
| 435 | # unload the (possibly loaded) module to avoid shutdown issues |
| 436 | self.__unload_netconsole_module() |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 437 | if label or kernel_args: |
| 438 | self.bootloader.install_boottool() |
| 439 | if label: |
| 440 | self.bootloader.set_default(label) |
| 441 | if kernel_args: |
| 442 | if not label: |
| 443 | default = int(self.bootloader.get_default()) |
| 444 | label = self.bootloader.get_titles()[default] |
| 445 | self.bootloader.add_args(label, kernel_args) |
mbligh | d742a22 | 2007-09-30 01:27:06 +0000 | [diff] [blame] | 446 | print "Reboot: initiating reboot" |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 447 | self.__record("GOOD", None, "reboot.start") |
mbligh | cf3d83a | 2007-11-05 19:21:39 +0000 | [diff] [blame] | 448 | try: |
mbligh | f3b7893 | 2007-11-07 16:52:47 +0000 | [diff] [blame] | 449 | self.run('(sleep 5; reboot) </dev/null >/dev/null 2>&1 &') |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 450 | except error.AutoservRunError: |
mbligh | f3b7893 | 2007-11-07 16:52:47 +0000 | [diff] [blame] | 451 | self.__record("ABORT", None, "reboot.start", |
| 452 | "reboot command failed") |
mbligh | cf3d83a | 2007-11-05 19:21:39 +0000 | [diff] [blame] | 453 | raise |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 454 | if wait: |
mbligh | 5deff3d | 2008-01-04 21:21:28 +0000 | [diff] [blame] | 455 | self.wait_for_restart(timeout) |
mbligh | de38437 | 2007-10-17 04:25:37 +0000 | [diff] [blame] | 456 | self.__load_netconsole_module() # if the builtin fails |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 457 | |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 458 | |
mbligh | cfc7ab3 | 2008-01-25 16:35:28 +0000 | [diff] [blame] | 459 | def __copy_files(self, sources, dest): |
| 460 | """ |
| 461 | Copy files from one machine to another. |
| 462 | |
| 463 | This is for internal use by other methods that intend to move |
| 464 | files between machines. It expects a list of source files and |
| 465 | a destination (a filename if the source is a single file, a |
| 466 | destination otherwise). The names must already be |
| 467 | pre-processed into the appropriate rsync/scp friendly |
| 468 | format (%s@%s:%s). |
| 469 | """ |
| 470 | # wait until there are only a small number of copies running |
| 471 | # before starting this one |
| 472 | MAXIMUM_SIMULTANEOUS_COPIES = 4 |
| 473 | while True: |
| 474 | copy_count = 0 |
| 475 | procs = utils.system_output('ps -ef') |
| 476 | for line in procs: |
| 477 | if 'rsync ' in line or 'scp ' in line: |
| 478 | copy_count += 1 |
| 479 | if copy_count < MAXIMUM_SIMULTANEOUS_COPIES: |
| 480 | break |
| 481 | time.sleep(60) |
| 482 | |
mbligh | 22fdf17 | 2008-04-07 18:34:56 +0000 | [diff] [blame] | 483 | print '__copy_files: copying %s to %s' % (sources, dest) |
mbligh | cfc7ab3 | 2008-01-25 16:35:28 +0000 | [diff] [blame] | 484 | try: |
| 485 | utils.run('rsync --rsh="%s" -az %s %s' % ( |
| 486 | self.ssh_base_command(), ' '.join(sources), dest)) |
| 487 | except Exception: |
| 488 | utils.run('scp -rpq %s "%s"' % ( |
| 489 | ' '.join(sources), dest)) |
| 490 | |
| 491 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 492 | def get_file(self, source, dest): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 493 | """ |
| 494 | Copy files from the remote host to a local path. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 495 | |
| 496 | Directories will be copied recursively. |
| 497 | If a source component is a directory with a trailing slash, |
| 498 | the content of the directory will be copied, otherwise, the |
| 499 | directory itself and its content will be copied. This |
| 500 | behavior is similar to that of the program 'rsync'. |
| 501 | |
| 502 | Args: |
| 503 | source: either |
| 504 | 1) a single file or directory, as a string |
| 505 | 2) a list of one or more (possibly mixed) |
| 506 | files or directories |
| 507 | dest: a file or a directory (if source contains a |
| 508 | directory or more than one element, you must |
| 509 | supply a directory dest) |
| 510 | |
| 511 | Raises: |
| 512 | AutoservRunError: the scp command failed |
| 513 | """ |
| 514 | if isinstance(source, types.StringTypes): |
| 515 | source= [source] |
| 516 | |
| 517 | processed_source= [] |
| 518 | for entry in source: |
| 519 | if entry.endswith('/'): |
| 520 | format_string= '%s@%s:"%s*"' |
| 521 | else: |
| 522 | format_string= '%s@%s:"%s"' |
| 523 | entry= format_string % (self.user, self.hostname, |
| 524 | utils.scp_remote_escape(entry)) |
| 525 | processed_source.append(entry) |
| 526 | |
| 527 | processed_dest= os.path.abspath(dest) |
| 528 | if os.path.isdir(dest): |
| 529 | processed_dest= "%s/" % (utils.sh_escape(processed_dest),) |
| 530 | else: |
| 531 | processed_dest= utils.sh_escape(processed_dest) |
mbligh | cfc7ab3 | 2008-01-25 16:35:28 +0000 | [diff] [blame] | 532 | |
| 533 | self.__copy_files(processed_source, processed_dest) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 534 | |
| 535 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 536 | def send_file(self, source, dest): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 537 | """ |
| 538 | Copy files from a local path to the remote host. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 539 | |
| 540 | Directories will be copied recursively. |
| 541 | If a source component is a directory with a trailing slash, |
| 542 | the content of the directory will be copied, otherwise, the |
| 543 | directory itself and its content will be copied. This |
| 544 | behavior is similar to that of the program 'rsync'. |
| 545 | |
| 546 | Args: |
| 547 | source: either |
| 548 | 1) a single file or directory, as a string |
| 549 | 2) a list of one or more (possibly mixed) |
| 550 | files or directories |
| 551 | dest: a file or a directory (if source contains a |
| 552 | directory or more than one element, you must |
| 553 | supply a directory dest) |
| 554 | |
| 555 | Raises: |
| 556 | AutoservRunError: the scp command failed |
| 557 | """ |
| 558 | if isinstance(source, types.StringTypes): |
| 559 | source= [source] |
| 560 | |
| 561 | processed_source= [] |
| 562 | for entry in source: |
| 563 | if entry.endswith('/'): |
| 564 | format_string= '"%s/"*' |
| 565 | else: |
| 566 | format_string= '"%s"' |
| 567 | entry= format_string % (utils.sh_escape(os.path.abspath(entry)),) |
| 568 | processed_source.append(entry) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 569 | |
mbligh | 0faf91f | 2007-10-18 03:10:48 +0000 | [diff] [blame] | 570 | remote_dest = '%s@%s:"%s"' % ( |
| 571 | self.user, self.hostname, |
| 572 | utils.scp_remote_escape(dest)) |
mbligh | cfc7ab3 | 2008-01-25 16:35:28 +0000 | [diff] [blame] | 573 | |
| 574 | self.__copy_files(processed_source, remote_dest) |
mbligh | a4eb0fa | 2008-04-11 15:16:50 +0000 | [diff] [blame^] | 575 | self.run('find "%s" -type d | xargs -i -r chmod o+rx "{}"' % dest) |
| 576 | self.run('find "%s" -type f | xargs -i -r chmod o+r "{}"' % dest) |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 577 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 578 | def get_tmp_dir(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 579 | """ |
| 580 | Return the pathname of a directory on the host suitable |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 581 | for temporary file storage. |
| 582 | |
| 583 | The directory and its content will be deleted automatically |
| 584 | on the destruction of the Host object that was used to obtain |
| 585 | it. |
| 586 | """ |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 587 | dir_name= self.run("mktemp -d /tmp/autoserv-XXXXXX").stdout.rstrip(" \n") |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 588 | self.tmp_dirs.append(dir_name) |
| 589 | return dir_name |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 590 | |
| 591 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 592 | def is_up(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 593 | """ |
| 594 | Check if the remote host is up. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 595 | |
| 596 | Returns: |
| 597 | True if the remote host is up, False otherwise |
| 598 | """ |
| 599 | try: |
mbligh | 4cfa76a | 2007-11-26 20:45:16 +0000 | [diff] [blame] | 600 | self.ssh_ping() |
mbligh | eadfbb1 | 2007-11-26 23:03:12 +0000 | [diff] [blame] | 601 | except: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 602 | return False |
mbligh | eadfbb1 | 2007-11-26 23:03:12 +0000 | [diff] [blame] | 603 | return True |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 604 | |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 605 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 606 | def wait_up(self, timeout=None): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 607 | """ |
| 608 | Wait until the remote host is up or the timeout expires. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 609 | |
| 610 | In fact, it will wait until an ssh connection to the remote |
| 611 | host can be established. |
| 612 | |
| 613 | Args: |
| 614 | timeout: time limit in seconds before returning even |
| 615 | if the host is not up. |
| 616 | |
| 617 | Returns: |
| 618 | True if the host was found to be up, False otherwise |
| 619 | """ |
| 620 | if timeout: |
| 621 | end_time= time.time() + timeout |
| 622 | |
| 623 | while not timeout or time.time() < end_time: |
| 624 | try: |
mbligh | 4cfa76a | 2007-11-26 20:45:16 +0000 | [diff] [blame] | 625 | self.ssh_ping() |
mbligh | eadfbb1 | 2007-11-26 23:03:12 +0000 | [diff] [blame] | 626 | except: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 627 | pass |
| 628 | else: |
mbligh | eadfbb1 | 2007-11-26 23:03:12 +0000 | [diff] [blame] | 629 | return True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 630 | time.sleep(1) |
| 631 | |
| 632 | return False |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 633 | |
| 634 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 635 | def wait_down(self, timeout=None): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 636 | """ |
| 637 | Wait until the remote host is down or the timeout expires. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 638 | |
| 639 | In fact, it will wait until an ssh connection to the remote |
| 640 | host fails. |
| 641 | |
| 642 | Args: |
| 643 | timeout: time limit in seconds before returning even |
| 644 | if the host is not up. |
| 645 | |
| 646 | Returns: |
| 647 | True if the host was found to be down, False otherwise |
| 648 | """ |
| 649 | if timeout: |
| 650 | end_time= time.time() + timeout |
| 651 | |
| 652 | while not timeout or time.time() < end_time: |
| 653 | try: |
mbligh | 4cfa76a | 2007-11-26 20:45:16 +0000 | [diff] [blame] | 654 | self.ssh_ping() |
mbligh | eadfbb1 | 2007-11-26 23:03:12 +0000 | [diff] [blame] | 655 | except: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 656 | return True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 657 | time.sleep(1) |
| 658 | |
| 659 | return False |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 660 | |
| 661 | |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 662 | def ensure_up(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 663 | """ |
| 664 | Ensure the host is up if it is not then do not proceed; |
| 665 | this prevents cacading failures of tests |
| 666 | """ |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 667 | print 'Ensuring that %s is up before continuing' % self.hostname |
| 668 | if hasattr(self, 'hardreset') and not self.wait_up(300): |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 669 | print "Performing a hardreset on %s" % self.hostname |
mbligh | 4ba0b46 | 2007-11-05 23:05:40 +0000 | [diff] [blame] | 670 | try: |
| 671 | self.hardreset() |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 672 | except error.AutoservUnsupportedError: |
mbligh | 4ba0b46 | 2007-11-05 23:05:40 +0000 | [diff] [blame] | 673 | print "Hardreset is unsupported on %s" % self.hostname |
mbligh | a9563b9 | 2007-10-25 14:45:56 +0000 | [diff] [blame] | 674 | if not self.wait_up(60 * 30): |
| 675 | # 30 minutes should be more than enough |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 676 | raise error.AutoservHostError |
mbligh | a0452c8 | 2007-08-08 20:24:57 +0000 | [diff] [blame] | 677 | print 'Host up, continuing' |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 678 | |
| 679 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 680 | def get_num_cpu(self): |
mbligh | 7d2bde8 | 2007-08-02 16:26:10 +0000 | [diff] [blame] | 681 | """ |
| 682 | Get the number of CPUs in the host according to |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 683 | /proc/cpuinfo. |
| 684 | |
| 685 | Returns: |
| 686 | The number of CPUs |
| 687 | """ |
| 688 | |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 689 | proc_cpuinfo = self.run("cat /proc/cpuinfo").stdout |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 690 | cpus = 0 |
| 691 | for line in proc_cpuinfo.splitlines(): |
| 692 | if line.startswith('processor'): |
| 693 | cpus += 1 |
| 694 | return cpus |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 695 | |
| 696 | |
| 697 | def check_uptime(self): |
| 698 | """ |
| 699 | Check that uptime is available and monotonically increasing. |
| 700 | """ |
| 701 | if not self.ping(): |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 702 | raise error.AutoservHostError('Client is not pingable') |
mbligh | 5f876ad | 2007-10-12 23:59:53 +0000 | [diff] [blame] | 703 | result = self.run("/bin/cat /proc/uptime", 30) |
| 704 | return result.stdout.strip().split()[0] |
| 705 | |
| 706 | |
| 707 | def get_arch(self): |
| 708 | """ |
| 709 | Get the hardware architecture of the remote machine |
| 710 | """ |
| 711 | arch = self.run('/bin/uname -m').stdout.rstrip() |
| 712 | if re.match(r'i\d86$', arch): |
| 713 | arch = 'i386' |
| 714 | return arch |
| 715 | |
| 716 | |
| 717 | def get_kernel_ver(self): |
| 718 | """ |
| 719 | Get the kernel version of the remote machine |
| 720 | """ |
| 721 | return self.run('/bin/uname -r').stdout.rstrip() |
| 722 | |
| 723 | |
| 724 | def get_cmdline(self): |
| 725 | """ |
| 726 | Get the kernel command line of the remote machine |
| 727 | """ |
| 728 | return self.run('cat /proc/cmdline').stdout.rstrip() |
| 729 | |
| 730 | |
| 731 | def ping(self): |
| 732 | """ |
| 733 | Ping the remote system, and return whether it's available |
| 734 | """ |
| 735 | fpingcmd = "%s -q %s" % ('/usr/bin/fping', self.hostname) |
| 736 | rc = utils.system(fpingcmd, ignore_status = 1) |
| 737 | return (rc == 0) |
mbligh | d2e4605 | 2007-11-05 18:31:00 +0000 | [diff] [blame] | 738 | |
mbligh | f014ff4 | 2007-11-26 21:33:11 +0000 | [diff] [blame] | 739 | |
mbligh | 4cfa76a | 2007-11-26 20:45:16 +0000 | [diff] [blame] | 740 | def ssh_ping(self, timeout = 60): |
mbligh | 4ff46b0 | 2008-02-01 17:33:37 +0000 | [diff] [blame] | 741 | self.run('true', timeout = timeout, connect_timeout = timeout) |
mbligh | da13d54 | 2008-01-03 16:28:34 +0000 | [diff] [blame] | 742 | |
| 743 | |
| 744 | def get_autodir(self): |
| 745 | return self.autodir |