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