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. |
mbligh | 410fe2e | 2009-12-02 19:02:50 +0000 | [diff] [blame] | 120 | @params conmux_command: The command to run via the conmux interface |
| 121 | @params timeout: timelimit in seconds before the machine is |
| 122 | considered unreachable |
| 123 | @params wait: Whether or not to wait for the machine to reboot |
| 124 | @params num_attempts: Number of times to attempt hard reset erroring |
| 125 | on the last attempt. |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 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: |
mbligh | 410fe2e | 2009-12-02 19:02:50 +0000 | [diff] [blame] | 136 | warning_msg = ('Serial console failed to respond to hard reset ' |
| 137 | 'attempt (%s/%s)') |
| 138 | for attempt in xrange(num_attempts-1): |
jadmanski | 65eb8f5 | 2009-07-24 18:34:43 +0000 | [diff] [blame] | 139 | try: |
mbligh | 410fe2e | 2009-12-02 19:02:50 +0000 | [diff] [blame] | 140 | self.wait_for_restart(timeout, log_failure=False) |
jadmanski | 65eb8f5 | 2009-07-24 18:34:43 +0000 | [diff] [blame] | 141 | except error.AutoservShutdownError: |
mbligh | 6c881b6 | 2009-12-02 19:03:16 +0000 | [diff] [blame^] | 142 | logging.warning(warning_msg, attempt+1, num_attempts) |
jadmanski | 65eb8f5 | 2009-07-24 18:34:43 +0000 | [diff] [blame] | 143 | else: |
| 144 | break |
| 145 | else: |
mbligh | 410fe2e | 2009-12-02 19:02:50 +0000 | [diff] [blame] | 146 | # Run on num_attempts=1 or last retry |
| 147 | try: |
| 148 | self.wait_for_restart(timeout) |
mbligh | 6c881b6 | 2009-12-02 19:03:16 +0000 | [diff] [blame^] | 149 | except error.AutoservShutdownError: |
mbligh | 410fe2e | 2009-12-02 19:02:50 +0000 | [diff] [blame] | 150 | logging.warning(warning_msg, num_attempts, num_attempts) |
| 151 | msg = "Host did not shutdown" |
| 152 | raise error.AutoservShutdownError(msg) |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 153 | |
mbligh | 1189adf | 2009-05-21 01:42:35 +0000 | [diff] [blame] | 154 | if self.job: |
| 155 | self.job.disable_warnings("POWER_FAILURE") |
| 156 | try: |
| 157 | if wait: |
| 158 | self.log_reboot(reboot) |
| 159 | else: |
| 160 | reboot() |
| 161 | finally: |
| 162 | if self.job: |
| 163 | self.job.enable_warnings("POWER_FAILURE") |