mbligh | d991198 | 2009-04-08 21:18:35 +0000 | [diff] [blame] | 1 | import os, sys, subprocess, logging |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 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 |
mbligh | 3b02e8b | 2009-02-17 15:59:54 +0000 | [diff] [blame] | 5 | from autotest_lib.server.hosts import remote |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 6 | |
| 7 | |
mbligh | 3b02e8b | 2009-02-17 15:59:54 +0000 | [diff] [blame] | 8 | SiteHost = utils.import_site_class( |
| 9 | __file__, "autotest_lib.server.hosts.site_host", "SiteHost", |
| 10 | remote.RemoteHost) |
| 11 | |
| 12 | |
| 13 | class SerialHost(SiteHost): |
| 14 | DEFAULT_REBOOT_TIMEOUT = SiteHost.DEFAULT_REBOOT_TIMEOUT |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 15 | |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 16 | def _initialize(self, conmux_server=None, conmux_attach=None, |
| 17 | console_log="console.log", *args, **dargs): |
| 18 | super(SerialHost, self)._initialize(*args, **dargs) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 19 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 20 | self.__logger = None |
| 21 | self.__console_log = console_log |
| 22 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 23 | self.conmux_server = conmux_server |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 24 | self.conmux_attach = self._get_conmux_attach(conmux_attach) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 25 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 26 | |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 27 | @classmethod |
| 28 | def _get_conmux_attach(cls, conmux_attach=None): |
| 29 | if conmux_attach: |
| 30 | return conmux_attach |
| 31 | |
| 32 | # assume we're using the conmux-attach provided with autotest |
| 33 | server_dir = server_utils.get_server_dir() |
| 34 | path = os.path.join(server_dir, "..", "conmux", "conmux-attach") |
| 35 | path = os.path.abspath(path) |
| 36 | return path |
| 37 | |
| 38 | |
| 39 | @staticmethod |
| 40 | def _get_conmux_hostname(hostname, conmux_server): |
| 41 | if conmux_server: |
| 42 | return "%s/%s" % (conmux_server, hostname) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 43 | else: |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 44 | return hostname |
| 45 | |
| 46 | |
| 47 | def get_conmux_hostname(self): |
| 48 | return self._get_conmux_hostname(self.hostname, self.conmux_server) |
| 49 | |
| 50 | |
| 51 | @classmethod |
| 52 | def host_is_supported(cls, hostname, conmux_server=None, |
| 53 | conmux_attach=None): |
| 54 | """ Returns a boolean indicating if the remote host with "hostname" |
| 55 | supports use as a SerialHost """ |
| 56 | conmux_attach = cls._get_conmux_attach(conmux_attach) |
| 57 | conmux_hostname = cls._get_conmux_hostname(hostname, conmux_server) |
| 58 | cmd = "%s %s echo 2> /dev/null" % (conmux_attach, conmux_hostname) |
mbligh | d991198 | 2009-04-08 21:18:35 +0000 | [diff] [blame] | 59 | try: |
| 60 | result = utils.run(cmd, ignore_status=True, timeout=10) |
| 61 | return result.exit_status == 0 |
| 62 | except error.CmdError: |
| 63 | logging.warning("Timed out while trying to attach to conmux") |
| 64 | |
| 65 | return False |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 66 | |
| 67 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 68 | def start_loggers(self): |
| 69 | super(SerialHost, self).start_loggers() |
| 70 | |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 71 | if self.__console_log is None: |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 72 | return |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 73 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 74 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 75 | return |
| 76 | |
| 77 | r, w = os.pipe() |
jadmanski | 79777f6 | 2008-09-09 22:31:38 +0000 | [diff] [blame] | 78 | script_path = os.path.join(self.monitordir, 'console.py') |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 79 | cmd = [self.conmux_attach, self.get_conmux_hostname(), |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 80 | '%s %s %s %d' % (sys.executable, script_path, |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 81 | self.__console_log, w)] |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 82 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 83 | self.__warning_stream = os.fdopen(r, 'r', 0) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 84 | if self.job: |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 85 | self.job.warning_loggers.add(self.__warning_stream) |
| 86 | |
| 87 | stdout = stderr = open(os.devnull, 'w') |
| 88 | self.__logger = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 89 | os.close(w) |
| 90 | |
| 91 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 92 | def stop_loggers(self): |
| 93 | super(SerialHost, self).stop_loggers() |
| 94 | |
| 95 | if self.__logger: |
| 96 | utils.nuke_subprocess(self.__logger) |
| 97 | self.__logger = None |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 98 | if self.job: |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 99 | self.job.warning_loggers.discard(self.__warning_stream) |
| 100 | self.__warning_stream.close() |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def run_conmux(self, cmd): |
| 104 | """ |
| 105 | Send a command to the conmux session |
| 106 | """ |
| 107 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 108 | return False |
| 109 | cmd = '%s %s echo %s 2> /dev/null' % (self.conmux_attach, |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 110 | self.get_conmux_hostname(), |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 111 | cmd) |
| 112 | result = utils.system(cmd, ignore_status=True) |
| 113 | return result == 0 |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def hardreset(self, timeout=DEFAULT_REBOOT_TIMEOUT, wait=True, |
jadmanski | 65eb8f5 | 2009-07-24 18:34:43 +0000 | [diff] [blame] | 117 | conmux_command='hardreset', num_attempts=1): |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 118 | """ |
| 119 | Reach out and slap the box in the power switch. |
| 120 | Args: |
| 121 | conmux_command: The command to run via the conmux interface |
| 122 | timeout: timelimit in seconds before the machine is |
| 123 | considered unreachable |
| 124 | wait: Whether or not to wait for the machine to reboot |
| 125 | |
| 126 | """ |
| 127 | conmux_command = "'~$%s'" % conmux_command |
| 128 | def reboot(): |
| 129 | if not self.run_conmux(conmux_command): |
| 130 | self.record("ABORT", None, "reboot.start", |
| 131 | "hard reset unavailable") |
| 132 | raise error.AutoservUnsupportedError( |
| 133 | 'Hard reset unavailable') |
| 134 | self.record("GOOD", None, "reboot.start", "hard reset") |
| 135 | if wait: |
jadmanski | 65eb8f5 | 2009-07-24 18:34:43 +0000 | [diff] [blame] | 136 | for _ in xrange(num_attempts): |
| 137 | try: |
| 138 | self.wait_for_restart(timeout) |
| 139 | except error.AutoservShutdownError: |
| 140 | msg = "Serial console failed to respond to hard reset" |
| 141 | logging.warning(msg) |
| 142 | else: |
| 143 | break |
| 144 | else: |
| 145 | msg = "Host did not shutdown" |
| 146 | raise error.AutoservShutdownError(msg) |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 147 | |
mbligh | 1189adf | 2009-05-21 01:42:35 +0000 | [diff] [blame] | 148 | if self.job: |
| 149 | self.job.disable_warnings("POWER_FAILURE") |
| 150 | try: |
| 151 | if wait: |
| 152 | self.log_reboot(reboot) |
| 153 | else: |
| 154 | reboot() |
| 155 | finally: |
| 156 | if self.job: |
| 157 | self.job.enable_warnings("POWER_FAILURE") |