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