jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 1 | import os, sys, subprocess |
| 2 | |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 3 | from autotest_lib.client.common_lib import utils, error |
| 4 | from autotest_lib.server import utils as server_utils |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 5 | from autotest_lib.server.hosts import site_host |
| 6 | |
| 7 | |
| 8 | class SerialHost(site_host.SiteHost): |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 9 | DEFAULT_REBOOT_TIMEOUT = site_host.SiteHost.DEFAULT_REBOOT_TIMEOUT |
| 10 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 11 | def __init__(self, conmux_server=None, conmux_attach=None, |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 12 | console_log="console.log", *args, **dargs): |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 13 | super(SerialHost, self).__init__(*args, **dargs) |
| 14 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 15 | self.__logger = None |
| 16 | self.__console_log = console_log |
| 17 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 18 | self.conmux_server = conmux_server |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 19 | self.conmux_attach = self._get_conmux_attach(conmux_attach) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 20 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 21 | |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 22 | @classmethod |
| 23 | def _get_conmux_attach(cls, conmux_attach=None): |
| 24 | if conmux_attach: |
| 25 | return conmux_attach |
| 26 | |
| 27 | # assume we're using the conmux-attach provided with autotest |
| 28 | server_dir = server_utils.get_server_dir() |
| 29 | path = os.path.join(server_dir, "..", "conmux", "conmux-attach") |
| 30 | path = os.path.abspath(path) |
| 31 | return path |
| 32 | |
| 33 | |
| 34 | @staticmethod |
| 35 | def _get_conmux_hostname(hostname, conmux_server): |
| 36 | if conmux_server: |
| 37 | return "%s/%s" % (conmux_server, hostname) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 38 | else: |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 39 | return hostname |
| 40 | |
| 41 | |
| 42 | def get_conmux_hostname(self): |
| 43 | return self._get_conmux_hostname(self.hostname, self.conmux_server) |
| 44 | |
| 45 | |
| 46 | @classmethod |
| 47 | def host_is_supported(cls, hostname, conmux_server=None, |
| 48 | conmux_attach=None): |
| 49 | """ Returns a boolean indicating if the remote host with "hostname" |
| 50 | supports use as a SerialHost """ |
| 51 | conmux_attach = cls._get_conmux_attach(conmux_attach) |
| 52 | conmux_hostname = cls._get_conmux_hostname(hostname, conmux_server) |
| 53 | cmd = "%s %s echo 2> /dev/null" % (conmux_attach, conmux_hostname) |
jadmanski | 74394b6 | 2008-08-25 19:24:45 +0000 | [diff] [blame] | 54 | result = utils.run(cmd, ignore_status=True) |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 55 | return result.exit_status == 0 |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 56 | |
| 57 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 58 | def start_loggers(self): |
| 59 | super(SerialHost, self).start_loggers() |
| 60 | |
| 61 | if self.__console_log == None: |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 62 | return |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 63 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 64 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 65 | return |
| 66 | |
| 67 | r, w = os.pipe() |
| 68 | script_path = os.path.join(self.serverdir, |
| 69 | 'warning_monitor.py') |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 70 | cmd = [self.conmux_attach, self.get_conmux_hostname(), |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 71 | '%s %s %s %d' % (sys.executable, script_path, |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 72 | self.__console_log, w)] |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 73 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 74 | self.__warning_stream = os.fdopen(r, 'r', 0) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 75 | if self.job: |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 76 | self.job.warning_loggers.add(self.__warning_stream) |
| 77 | |
| 78 | stdout = stderr = open(os.devnull, 'w') |
| 79 | self.__logger = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 80 | os.close(w) |
| 81 | |
| 82 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 83 | def stop_loggers(self): |
| 84 | super(SerialHost, self).stop_loggers() |
| 85 | |
| 86 | if self.__logger: |
| 87 | utils.nuke_subprocess(self.__logger) |
| 88 | self.__logger = None |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 89 | if self.job: |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 90 | self.job.warning_loggers.discard(self.__warning_stream) |
| 91 | self.__warning_stream.close() |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | def run_conmux(self, cmd): |
| 95 | """ |
| 96 | Send a command to the conmux session |
| 97 | """ |
| 98 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 99 | return False |
| 100 | cmd = '%s %s echo %s 2> /dev/null' % (self.conmux_attach, |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 101 | self.get_conmux_hostname(), |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 102 | cmd) |
| 103 | result = utils.system(cmd, ignore_status=True) |
| 104 | return result == 0 |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def hardreset(self, timeout=DEFAULT_REBOOT_TIMEOUT, wait=True, |
| 108 | conmux_command='hardreset'): |
| 109 | """ |
| 110 | Reach out and slap the box in the power switch. |
| 111 | Args: |
| 112 | conmux_command: The command to run via the conmux interface |
| 113 | timeout: timelimit in seconds before the machine is |
| 114 | considered unreachable |
| 115 | wait: Whether or not to wait for the machine to reboot |
| 116 | |
| 117 | """ |
| 118 | conmux_command = "'~$%s'" % conmux_command |
| 119 | def reboot(): |
| 120 | if not self.run_conmux(conmux_command): |
| 121 | self.record("ABORT", None, "reboot.start", |
| 122 | "hard reset unavailable") |
| 123 | raise error.AutoservUnsupportedError( |
| 124 | 'Hard reset unavailable') |
| 125 | self.record("GOOD", None, "reboot.start", "hard reset") |
| 126 | if wait: |
| 127 | self.wait_for_restart(timeout) |
| 128 | |
| 129 | if wait: |
| 130 | self.log_reboot(reboot) |
| 131 | else: |
| 132 | reboot() |