jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame^] | 1 | import os, sys, subprocess |
| 2 | |
| 3 | from autotest_lib.client.common_lib import utils |
| 4 | from autotest_lib.server.hosts import site_host |
| 5 | |
| 6 | |
| 7 | class SerialHost(site_host.SiteHost): |
| 8 | def __init__(self, conmux_server=None, conmux_attach=None, |
| 9 | conmux_log="console.log", *args, **dargs): |
| 10 | super(SerialHost, self).__init__(*args, **dargs) |
| 11 | |
| 12 | self.conmux_server = conmux_server |
| 13 | if conmux_attach: |
| 14 | self.conmux_attach = conmux_attach |
| 15 | else: |
| 16 | self.conmux_attach = os.path.abspath(os.path.join( |
| 17 | self.serverdir, '..', 'conmux', 'conmux-attach')) |
| 18 | |
| 19 | self.logger_popen = None |
| 20 | self.warning_stream = None |
| 21 | self.__start_console_log(conmux_log) |
| 22 | |
| 23 | |
| 24 | def __del__(self): |
| 25 | self.__stop_console_log() |
| 26 | |
| 27 | |
| 28 | def __conmux_hostname(self): |
| 29 | if self.conmux_server: |
| 30 | return '%s/%s' % (self.conmux_server, self.hostname) |
| 31 | else: |
| 32 | return self.hostname |
| 33 | |
| 34 | |
| 35 | def __start_console_log(self, logfilename): |
| 36 | """ |
| 37 | Log the output of the console session to a specified file |
| 38 | """ |
| 39 | if logfilename == None: |
| 40 | return |
| 41 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 42 | return |
| 43 | |
| 44 | r, w = os.pipe() |
| 45 | script_path = os.path.join(self.serverdir, |
| 46 | 'warning_monitor.py') |
| 47 | cmd = [self.conmux_attach, self.__conmux_hostname(), |
| 48 | '%s %s %s %d' % (sys.executable, script_path, |
| 49 | logfilename, w)] |
| 50 | dev_null = open(os.devnull, 'w') |
| 51 | |
| 52 | self.warning_stream = os.fdopen(r, 'r', 0) |
| 53 | if self.job: |
| 54 | self.job.warning_loggers.add(self.warning_stream) |
| 55 | self.logger_popen = subprocess.Popen(cmd, stderr=dev_null) |
| 56 | os.close(w) |
| 57 | |
| 58 | |
| 59 | def __stop_console_log(self): |
| 60 | if getattr(self, 'logger_popen', None): |
| 61 | utils.nuke_subprocess(self.logger_popen) |
| 62 | if self.job: |
| 63 | self.job.warning_loggers.discard(self.warning_stream) |
| 64 | self.warning_stream.close() |
| 65 | |
| 66 | |
| 67 | def run_conmux(self, cmd): |
| 68 | """ |
| 69 | Send a command to the conmux session |
| 70 | """ |
| 71 | if not self.conmux_attach or not os.path.exists(self.conmux_attach): |
| 72 | return False |
| 73 | cmd = '%s %s echo %s 2> /dev/null' % (self.conmux_attach, |
| 74 | self.__conmux_hostname(), |
| 75 | cmd) |
| 76 | result = utils.system(cmd, ignore_status=True) |
| 77 | return result == 0 |