mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 1 | """This class defines the Remote host class, mixing in the SiteHost class |
| 2 | if it is available.""" |
| 3 | |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 4 | import os, time, pickle, logging |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 5 | from autotest_lib.client.common_lib import error |
jadmanski | da0aeff | 2009-02-18 18:53:05 +0000 | [diff] [blame] | 6 | from autotest_lib.server import utils, profiler |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 7 | from autotest_lib.server.hosts import base_classes, bootloader |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 8 | |
| 9 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 10 | class RemoteHost(base_classes.Host): |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 11 | """ |
| 12 | This class represents a remote machine on which you can run |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 13 | programs. |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 14 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 15 | It may be accessed through a network, a serial line, ... |
| 16 | It is not the machine autoserv is running on. |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 17 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 18 | Implementation details: |
| 19 | This is an abstract class, leaf subclasses must implement the methods |
| 20 | listed here and in parent classes which have no implementation. They |
| 21 | may reimplement methods which already have an implementation. You |
| 22 | must not instantiate this class but should instantiate one of those |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 23 | leaf subclasses. |
| 24 | """ |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 25 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 26 | DEFAULT_REBOOT_TIMEOUT = base_classes.Host.DEFAULT_REBOOT_TIMEOUT |
| 27 | LAST_BOOT_TAG = object() |
| 28 | |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 29 | def _initialize(self, hostname, autodir=None, *args, **dargs): |
| 30 | super(RemoteHost, self)._initialize(*args, **dargs) |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 31 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 32 | self.hostname = hostname |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 33 | self.autodir = autodir |
| 34 | self.tmp_dirs = [] |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 35 | |
| 36 | |
jadmanski | 53aaf38 | 2008-11-17 16:22:31 +0000 | [diff] [blame] | 37 | def close(self): |
| 38 | super(RemoteHost, self).close() |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 39 | self.stop_loggers() |
| 40 | |
| 41 | if hasattr(self, 'tmp_dirs'): |
| 42 | for dir in self.tmp_dirs: |
| 43 | try: |
| 44 | self.run('rm -rf "%s"' % (utils.sh_escape(dir))) |
| 45 | except error.AutoservRunError: |
| 46 | pass |
| 47 | |
| 48 | |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 49 | def job_start(self): |
| 50 | """ |
| 51 | Abstract method, called the first time a remote host object |
| 52 | is created for a specific host after a job starts. |
| 53 | |
| 54 | This method depends on the create_host factory being used to |
| 55 | construct your host object. If you directly construct host objects |
| 56 | you will need to call this method yourself (and enforce the |
| 57 | single-call rule). |
| 58 | """ |
| 59 | pass |
| 60 | |
| 61 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 62 | def get_autodir(self): |
| 63 | return self.autodir |
| 64 | |
| 65 | |
| 66 | def set_autodir(self, autodir): |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 67 | """ |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 68 | This method is called to make the host object aware of the |
| 69 | where autotest is installed. Called in server/autotest.py |
| 70 | after a successful install |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 71 | """ |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 72 | self.autodir = autodir |
| 73 | |
| 74 | |
| 75 | def sysrq_reboot(self): |
| 76 | self.run('echo b > /proc/sysrq-trigger &') |
| 77 | |
| 78 | |
| 79 | def reboot(self, timeout=DEFAULT_REBOOT_TIMEOUT, label=LAST_BOOT_TAG, |
mbligh | 959ed87 | 2009-04-17 22:18:25 +0000 | [diff] [blame] | 80 | kernel_args=None, wait=True, fastsync=False, |
| 81 | reboot_cmd=None, **dargs): |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 82 | """ |
| 83 | Reboot the remote host. |
| 84 | |
| 85 | Args: |
| 86 | timeout - How long to wait for the reboot. |
| 87 | label - The label we should boot into. If None, we will |
| 88 | boot into the default kernel. If it's LAST_BOOT_TAG, |
| 89 | we'll boot into whichever kernel was .boot'ed last |
| 90 | (or the default kernel if we haven't .boot'ed in this |
| 91 | job). If it's None, we'll boot into the default kernel. |
| 92 | If it's something else, we'll boot into that. |
| 93 | wait - Should we wait to see if the machine comes back up. |
mbligh | 2b94977 | 2009-02-26 00:59:36 +0000 | [diff] [blame] | 94 | fastsync - Don't wait for the sync to complete, just start one |
| 95 | and move on. This is for cases where rebooting prompty |
| 96 | is more important than data integrity and/or the |
| 97 | machine may have disks that cause sync to never return. |
mbligh | 959ed87 | 2009-04-17 22:18:25 +0000 | [diff] [blame] | 98 | reboot_cmd - Reboot command to execute. |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 99 | """ |
| 100 | if self.job: |
| 101 | if label == self.LAST_BOOT_TAG: |
| 102 | label = self.job.last_boot_tag |
| 103 | else: |
| 104 | self.job.last_boot_tag = label |
| 105 | |
| 106 | self.reboot_setup(label=label, kernel_args=kernel_args, **dargs) |
| 107 | |
| 108 | if label or kernel_args: |
| 109 | self.bootloader.install_boottool() |
| 110 | if not label: |
| 111 | default = int(self.bootloader.get_default()) |
| 112 | label = self.bootloader.get_titles()[default] |
| 113 | self.bootloader.boot_once(label) |
| 114 | if kernel_args: |
| 115 | self.bootloader.add_args(label, kernel_args) |
| 116 | |
| 117 | # define a function for the reboot and run it in a group |
| 118 | print "Reboot: initiating reboot" |
| 119 | def reboot(): |
| 120 | self.record("GOOD", None, "reboot.start") |
| 121 | try: |
jadmanski | d544a35 | 2009-01-14 23:36:28 +0000 | [diff] [blame] | 122 | # sync before starting the reboot, so that a long sync during |
| 123 | # shutdown isn't timed out by wait_down's short timeout |
mbligh | 2b94977 | 2009-02-26 00:59:36 +0000 | [diff] [blame] | 124 | if not fastsync: |
mbligh | 959ed87 | 2009-04-17 22:18:25 +0000 | [diff] [blame] | 125 | self.run('sync; sync', timeout=timeout, ignore_status=True) |
jadmanski | d544a35 | 2009-01-14 23:36:28 +0000 | [diff] [blame] | 126 | |
mbligh | 959ed87 | 2009-04-17 22:18:25 +0000 | [diff] [blame] | 127 | if reboot_cmd: |
| 128 | self.run(reboot_cmd) |
| 129 | else: |
| 130 | # Try several methods of rebooting in increasing harshness. |
| 131 | self.run('((' |
| 132 | ' sync &' |
| 133 | ' sleep 5; reboot &' |
| 134 | ' sleep 60; reboot -f &' |
| 135 | ' sleep 10; reboot -nf &' |
| 136 | ' sleep 10; telinit 6 &' |
| 137 | ') </dev/null >/dev/null 2>&1 &)') |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 138 | except error.AutoservRunError: |
| 139 | self.record("ABORT", None, "reboot.start", |
| 140 | "reboot command failed") |
| 141 | raise |
| 142 | if wait: |
jadmanski | d778ae4 | 2009-01-07 15:07:36 +0000 | [diff] [blame] | 143 | self.wait_for_restart(timeout, **dargs) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 144 | |
| 145 | # if this is a full reboot-and-wait, run the reboot inside a group |
| 146 | if wait: |
| 147 | self.log_reboot(reboot) |
| 148 | else: |
| 149 | reboot() |
| 150 | |
| 151 | |
jadmanski | 4f90925 | 2008-12-01 20:47:10 +0000 | [diff] [blame] | 152 | def reboot_followup(self, *args, **dargs): |
| 153 | super(RemoteHost, self).reboot_followup(*args, **dargs) |
| 154 | if self.job: |
| 155 | self.job.profilers.handle_reboot(self) |
| 156 | |
| 157 | |
jadmanski | d778ae4 | 2009-01-07 15:07:36 +0000 | [diff] [blame] | 158 | def wait_for_restart(self, timeout=DEFAULT_REBOOT_TIMEOUT, **dargs): |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 159 | """ |
| 160 | Wait for the host to come back from a reboot. This wraps the |
| 161 | generic wait_for_restart implementation in a reboot group. |
| 162 | """ |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 163 | def reboot_func(): |
jadmanski | d778ae4 | 2009-01-07 15:07:36 +0000 | [diff] [blame] | 164 | super(RemoteHost, self).wait_for_restart(timeout=timeout, **dargs) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 165 | self.log_reboot(reboot_func) |
| 166 | |
| 167 | |
mbligh | 1264b51 | 2008-11-05 22:21:49 +0000 | [diff] [blame] | 168 | def cleanup(self): |
| 169 | super(RemoteHost, self).cleanup() |
| 170 | self.reboot() |
| 171 | |
| 172 | |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 173 | def get_tmp_dir(self, parent='/tmp'): |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 174 | """ |
| 175 | Return the pathname of a directory on the host suitable |
| 176 | for temporary file storage. |
| 177 | |
| 178 | The directory and its content will be deleted automatically |
| 179 | on the destruction of the Host object that was used to obtain |
| 180 | it. |
| 181 | """ |
jadmanski | 9f7dd11 | 2008-11-17 16:40:05 +0000 | [diff] [blame] | 182 | self.run("mkdir -p %s" % parent) |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 183 | template = os.path.join(parent, 'autoserv-XXXXXX') |
jadmanski | 9f7dd11 | 2008-11-17 16:40:05 +0000 | [diff] [blame] | 184 | dir_name = self.run("mktemp -d %s" % template).stdout.rstrip() |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 185 | self.tmp_dirs.append(dir_name) |
| 186 | return dir_name |
| 187 | |
| 188 | |
jadmanski | ea45566 | 2009-03-25 22:25:39 +0000 | [diff] [blame] | 189 | def delete_tmp_dir(self, tmpdir): |
| 190 | """ |
| 191 | Delete the given temporary directory on the remote machine. |
| 192 | """ |
| 193 | self.run('rm -rf "%s"' % utils.sh_escape(tmpdir), ignore_status=True) |
| 194 | self.tmp_dirs.remove(tmpdir) |
| 195 | |
| 196 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 197 | def ping(self): |
| 198 | """ |
| 199 | Ping the remote system, and return whether it's available |
| 200 | """ |
| 201 | fpingcmd = "%s -q %s" % ('/usr/bin/fping', self.hostname) |
| 202 | rc = utils.system(fpingcmd, ignore_status = 1) |
| 203 | return (rc == 0) |
| 204 | |
| 205 | |
| 206 | def check_uptime(self): |
| 207 | """ |
| 208 | Check that uptime is available and monotonically increasing. |
| 209 | """ |
| 210 | if not self.ping(): |
| 211 | raise error.AutoservHostError('Client is not pingable') |
| 212 | result = self.run("/bin/cat /proc/uptime", 30) |
| 213 | return result.stdout.strip().split()[0] |
| 214 | |
| 215 | |
| 216 | def get_crashinfo(self, test_start_time): |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 217 | logging.info("Collecting crash information...") |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 218 | super(RemoteHost, self).get_crashinfo(test_start_time) |
| 219 | |
| 220 | # wait for four hours, to see if the machine comes back up |
| 221 | current_time = time.strftime("%b %d %H:%M:%S", time.localtime()) |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 222 | logging.info("Waiting four hours for %s to come up (%s)", |
| 223 | self.hostname, current_time) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 224 | if not self.wait_up(timeout=4*60*60): |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 225 | logging.warning("%s down, unable to collect crash info", |
| 226 | self.hostname) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 227 | return |
| 228 | else: |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 229 | logging.info("%s is back up, collecting crash info", self.hostname) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 230 | |
| 231 | # find a directory to put the crashinfo into |
mbligh | 210bae6 | 2009-04-01 18:33:13 +0000 | [diff] [blame] | 232 | try: |
| 233 | self.job.resultsdir |
| 234 | except AttributeError: |
| 235 | self.job.resultsdir = None |
| 236 | |
| 237 | if self.job.resultsdir: |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 238 | infodir = self.job.resultdir |
| 239 | else: |
| 240 | infodir = os.path.abspath(os.getcwd()) |
| 241 | infodir = os.path.join(infodir, "crashinfo.%s" % self.hostname) |
| 242 | if not os.path.exists(infodir): |
| 243 | os.mkdir(infodir) |
| 244 | |
| 245 | # collect various log files |
| 246 | log_files = ["/var/log/messages", "/var/log/monitor-ssh-reboots"] |
| 247 | for log in log_files: |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 248 | logging.info("Collecting %s...", log) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 249 | try: |
| 250 | self.get_file(log, infodir) |
mbligh | a2c940d | 2009-01-30 22:35:19 +0000 | [diff] [blame] | 251 | except Exception: |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 252 | logging.warning("Collection of %s failed", log) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 253 | |
| 254 | # collect dmesg |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 255 | logging.info("Collecting dmesg (saved to crashinfo/dmesg)...") |
mbligh | 78a013a | 2009-01-13 19:34:28 +0000 | [diff] [blame] | 256 | devnull = open("/dev/null", "w") |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 257 | try: |
mbligh | 78a013a | 2009-01-13 19:34:28 +0000 | [diff] [blame] | 258 | try: |
| 259 | result = self.run("dmesg", stdout_tee=devnull).stdout |
| 260 | file(os.path.join(infodir, "dmesg"), "w").write(result) |
| 261 | except Exception, e: |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 262 | logging.warning("Collection of dmesg failed:\n%s", e) |
mbligh | 78a013a | 2009-01-13 19:34:28 +0000 | [diff] [blame] | 263 | finally: |
| 264 | devnull.close() |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 265 | |
jadmanski | da0aeff | 2009-02-18 18:53:05 +0000 | [diff] [blame] | 266 | # collect any profiler data we can find |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 267 | logging.info("Collecting any server-side profiler data lying around...") |
jadmanski | da0aeff | 2009-02-18 18:53:05 +0000 | [diff] [blame] | 268 | try: |
| 269 | cmd = "ls %s" % profiler.PROFILER_TMPDIR |
| 270 | profiler_dirs = [path for path in self.run(cmd).stdout.split() |
| 271 | if path.startswith("autoserv-")] |
| 272 | for profiler_dir in profiler_dirs: |
| 273 | remote_path = profiler.get_profiler_results_dir(profiler_dir) |
| 274 | remote_exists = self.run("ls %s" % remote_path, |
| 275 | ignore_status=True).exit_status == 0 |
| 276 | if not remote_exists: |
| 277 | continue |
| 278 | local_path = os.path.join(infodir, "profiler." + profiler_dir) |
| 279 | os.mkdir(local_path) |
| 280 | self.get_file(remote_path + "/", local_path) |
| 281 | except Exception, e: |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 282 | logging.warning("Collection of profiler data failed with:\n%s", e) |
| 283 | |
| 284 | |
| 285 | # collect any uncollected logs we see (for this host) |
mbligh | 210bae6 | 2009-04-01 18:33:13 +0000 | [diff] [blame] | 286 | if not self.job.uncollected_log_file: |
| 287 | self.job.uncollected_log_file = '' |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 288 | if self.job and os.path.exists(self.job.uncollected_log_file): |
| 289 | try: |
| 290 | logs = pickle.load(open(self.job.uncollected_log_file)) |
| 291 | for hostname, remote_path, local_path in logs: |
| 292 | if hostname == self.hostname: |
| 293 | logging.info("Retrieving logs from %s:%s into %s", |
| 294 | hostname, remote_path, local_path) |
| 295 | self.get_file(remote_path + "/", local_path + "/") |
| 296 | except Exception, e: |
| 297 | logging.warning("Error while trying to collect stranded " |
| 298 | "Autotest client logs: %s", e) |
jadmanski | da0aeff | 2009-02-18 18:53:05 +0000 | [diff] [blame] | 299 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 300 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 301 | def are_wait_up_processes_up(self): |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 302 | """ |
| 303 | Checks if any HOSTS waitup processes are running yet on the |
| 304 | remote host. |
| 305 | |
| 306 | Returns True if any the waitup processes are running, False |
| 307 | otherwise. |
| 308 | """ |
| 309 | processes = self.get_wait_up_processes() |
| 310 | if len(processes) == 0: |
| 311 | return True # wait up processes aren't being used |
| 312 | for procname in processes: |
| 313 | exit_status = self.run("{ ps -e || ps; } | grep '%s'" % procname, |
| 314 | ignore_status=True).exit_status |
| 315 | if exit_status == 0: |
| 316 | return True |
| 317 | return False |