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 |
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) |
jadmanski | 74394b6 | 2008-08-25 19:24:45 +0000 | [diff] [blame] | 59 | result = utils.run(cmd, ignore_status=True) |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 60 | return result.exit_status == 0 |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 61 | |
| 62 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 63 | def start_loggers(self): |
| 64 | super(SerialHost, self).start_loggers() |
| 65 | |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 66 | if self.__console_log is None: |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 67 | return |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 68 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 69 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 70 | return |
| 71 | |
| 72 | r, w = os.pipe() |
jadmanski | 79777f6 | 2008-09-09 22:31:38 +0000 | [diff] [blame] | 73 | script_path = os.path.join(self.monitordir, 'console.py') |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 74 | cmd = [self.conmux_attach, self.get_conmux_hostname(), |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 75 | '%s %s %s %d' % (sys.executable, script_path, |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 76 | self.__console_log, w)] |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 77 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 78 | self.__warning_stream = os.fdopen(r, 'r', 0) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 79 | if self.job: |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 80 | self.job.warning_loggers.add(self.__warning_stream) |
| 81 | |
| 82 | stdout = stderr = open(os.devnull, 'w') |
| 83 | self.__logger = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 84 | os.close(w) |
| 85 | |
| 86 | |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 87 | def stop_loggers(self): |
| 88 | super(SerialHost, self).stop_loggers() |
| 89 | |
| 90 | if self.__logger: |
| 91 | utils.nuke_subprocess(self.__logger) |
| 92 | self.__logger = None |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 93 | if self.job: |
jadmanski | c533bd1 | 2008-09-03 20:48:51 +0000 | [diff] [blame] | 94 | self.job.warning_loggers.discard(self.__warning_stream) |
| 95 | self.__warning_stream.close() |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def run_conmux(self, cmd): |
| 99 | """ |
| 100 | Send a command to the conmux session |
| 101 | """ |
| 102 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 103 | return False |
| 104 | cmd = '%s %s echo %s 2> /dev/null' % (self.conmux_attach, |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 105 | self.get_conmux_hostname(), |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 106 | cmd) |
| 107 | result = utils.system(cmd, ignore_status=True) |
| 108 | return result == 0 |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def hardreset(self, timeout=DEFAULT_REBOOT_TIMEOUT, wait=True, |
| 112 | conmux_command='hardreset'): |
| 113 | """ |
| 114 | Reach out and slap the box in the power switch. |
| 115 | Args: |
| 116 | conmux_command: The command to run via the conmux interface |
| 117 | timeout: timelimit in seconds before the machine is |
| 118 | considered unreachable |
| 119 | wait: Whether or not to wait for the machine to reboot |
| 120 | |
| 121 | """ |
| 122 | conmux_command = "'~$%s'" % conmux_command |
| 123 | def reboot(): |
| 124 | if not self.run_conmux(conmux_command): |
| 125 | self.record("ABORT", None, "reboot.start", |
| 126 | "hard reset unavailable") |
| 127 | raise error.AutoservUnsupportedError( |
| 128 | 'Hard reset unavailable') |
| 129 | self.record("GOOD", None, "reboot.start", "hard reset") |
| 130 | if wait: |
| 131 | self.wait_for_restart(timeout) |
| 132 | |
| 133 | if wait: |
| 134 | self.log_reboot(reboot) |
| 135 | else: |
| 136 | reboot() |