Paul Pendlebury | 7c1fdcf | 2011-05-04 12:39:15 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
mbligh | 57e7866 | 2008-06-17 19:53:49 +0000 | [diff] [blame] | 4 | """ |
| 5 | The main job wrapper for the server side. |
| 6 | |
| 7 | This is the core infrastructure. Derived from the client side job.py |
| 8 | |
| 9 | Copyright Martin J. Bligh, Andy Whitcroft 2007 |
| 10 | """ |
| 11 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 12 | import getpass, os, sys, re, stat, tempfile, time, select, subprocess, platform |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 13 | import traceback, shutil, warnings, fcntl, pickle, logging, itertools, errno |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 14 | from autotest_lib.client.bin import sysinfo |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import base_job |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 16 | from autotest_lib.client.common_lib import error, log, utils, packages |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import logging_manager |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 18 | from autotest_lib.server import test, subcommand, profilers |
mbligh | 0a88370 | 2010-04-21 01:58:34 +0000 | [diff] [blame] | 19 | from autotest_lib.server.hosts import abstract_ssh |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 20 | from autotest_lib.tko import db as tko_db, status_lib, utils as tko_utils |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 21 | |
| 22 | |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 23 | def _control_segment_path(name): |
| 24 | """Get the pathname of the named control segment file.""" |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 25 | server_dir = os.path.dirname(os.path.abspath(__file__)) |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 26 | return os.path.join(server_dir, "control_segments", name) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 27 | |
| 28 | |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 29 | CLIENT_CONTROL_FILENAME = 'control' |
| 30 | SERVER_CONTROL_FILENAME = 'control.srv' |
| 31 | MACHINES_FILENAME = '.machines' |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 32 | |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 33 | CLIENT_WRAPPER_CONTROL_FILE = _control_segment_path('client_wrapper') |
| 34 | CRASHDUMPS_CONTROL_FILE = _control_segment_path('crashdumps') |
| 35 | CRASHINFO_CONTROL_FILE = _control_segment_path('crashinfo') |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 36 | INSTALL_CONTROL_FILE = _control_segment_path('install') |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 37 | CLEANUP_CONTROL_FILE = _control_segment_path('cleanup') |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 38 | |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 39 | VERIFY_CONTROL_FILE = _control_segment_path('verify') |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 40 | REPAIR_CONTROL_FILE = _control_segment_path('repair') |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 41 | |
| 42 | |
mbligh | 062ed15 | 2009-01-13 00:57:14 +0000 | [diff] [blame] | 43 | # by default provide a stub that generates no site data |
| 44 | def _get_site_job_data_dummy(job): |
| 45 | return {} |
| 46 | |
| 47 | |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 48 | class status_indenter(base_job.status_indenter): |
| 49 | """Provide a simple integer-backed status indenter.""" |
| 50 | def __init__(self): |
| 51 | self._indent = 0 |
| 52 | |
| 53 | |
| 54 | @property |
| 55 | def indent(self): |
| 56 | return self._indent |
| 57 | |
| 58 | |
| 59 | def increment(self): |
| 60 | self._indent += 1 |
| 61 | |
| 62 | |
| 63 | def decrement(self): |
| 64 | self._indent -= 1 |
| 65 | |
| 66 | |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame] | 67 | def get_context(self): |
| 68 | """Returns a context object for use by job.get_record_context.""" |
| 69 | class context(object): |
| 70 | def __init__(self, indenter, indent): |
| 71 | self._indenter = indenter |
| 72 | self._indent = indent |
| 73 | def restore(self): |
| 74 | self._indenter._indent = self._indent |
| 75 | return context(self, self._indent) |
| 76 | |
| 77 | |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 78 | class server_job_record_hook(object): |
| 79 | """The job.record hook for server job. Used to inject WARN messages from |
| 80 | the console or vlm whenever new logs are written, and to echo any logs |
| 81 | to INFO level logging. Implemented as a class so that it can use state to |
| 82 | block recursive calls, so that the hook can call job.record itself to |
| 83 | log WARN messages. |
| 84 | |
| 85 | Depends on job._read_warnings and job._logger. |
| 86 | """ |
| 87 | def __init__(self, job): |
| 88 | self._job = job |
| 89 | self._being_called = False |
| 90 | |
| 91 | |
| 92 | def __call__(self, entry): |
| 93 | """A wrapper around the 'real' record hook, the _hook method, which |
| 94 | prevents recursion. This isn't making any effort to be threadsafe, |
| 95 | the intent is to outright block infinite recursion via a |
| 96 | job.record->_hook->job.record->_hook->job.record... chain.""" |
| 97 | if self._being_called: |
| 98 | return |
| 99 | self._being_called = True |
| 100 | try: |
| 101 | self._hook(self._job, entry) |
| 102 | finally: |
| 103 | self._being_called = False |
| 104 | |
| 105 | |
| 106 | @staticmethod |
| 107 | def _hook(job, entry): |
| 108 | """The core hook, which can safely call job.record.""" |
| 109 | entries = [] |
| 110 | # poll all our warning loggers for new warnings |
| 111 | for timestamp, msg in job._read_warnings(): |
| 112 | warning_entry = base_job.status_log_entry( |
| 113 | 'WARN', None, None, msg, {}, timestamp=timestamp) |
| 114 | entries.append(warning_entry) |
| 115 | job.record_entry(warning_entry) |
| 116 | # echo rendered versions of all the status logs to info |
| 117 | entries.append(entry) |
| 118 | for entry in entries: |
| 119 | rendered_entry = job._logger.render_entry(entry) |
| 120 | logging.info(rendered_entry) |
jadmanski | e29d0e4 | 2010-06-17 16:06:52 +0000 | [diff] [blame] | 121 | job._parse_status(rendered_entry) |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 122 | |
| 123 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 124 | class base_server_job(base_job.base_job): |
| 125 | """The server-side concrete implementation of base_job. |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 126 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 127 | Optional properties provided by this implementation: |
| 128 | serverdir |
| 129 | conmuxdir |
| 130 | |
| 131 | num_tests_run |
| 132 | num_tests_failed |
| 133 | |
| 134 | warning_manager |
| 135 | warning_loggers |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 136 | """ |
| 137 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 138 | _STATUS_VERSION = 1 |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 139 | |
| 140 | def __init__(self, control, args, resultdir, label, user, machines, |
| 141 | client=False, parse_job='', |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 142 | ssh_user='root', ssh_port=22, ssh_pass='', |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 143 | group_name='', tag='', |
| 144 | control_filename=SERVER_CONTROL_FILENAME): |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 145 | """ |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 146 | Create a server side job object. |
mbligh | b5dac43 | 2008-11-27 00:38:44 +0000 | [diff] [blame] | 147 | |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 148 | @param control: The pathname of the control file. |
| 149 | @param args: Passed to the control file. |
| 150 | @param resultdir: Where to throw the results. |
| 151 | @param label: Description of the job. |
| 152 | @param user: Username for the job (email address). |
| 153 | @param client: True if this is a client-side control file. |
| 154 | @param parse_job: string, if supplied it is the job execution tag that |
| 155 | the results will be passed through to the TKO parser with. |
| 156 | @param ssh_user: The SSH username. [root] |
| 157 | @param ssh_port: The SSH port number. [22] |
| 158 | @param ssh_pass: The SSH passphrase, if needed. |
| 159 | @param group_name: If supplied, this will be written out as |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 160 | host_group_name in the keyvals file for the parser. |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 161 | @param tag: The job execution tag from the scheduler. [optional] |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 162 | @param control_filename: The filename where the server control file |
| 163 | should be written in the results directory. |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 164 | """ |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 165 | super(base_server_job, self).__init__(resultdir=resultdir) |
mbligh | a788dc4 | 2009-03-26 21:10:16 +0000 | [diff] [blame] | 166 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 167 | path = os.path.dirname(__file__) |
| 168 | self.control = control |
| 169 | self._uncollected_log_file = os.path.join(self.resultdir, |
| 170 | 'uncollected_logs') |
| 171 | debugdir = os.path.join(self.resultdir, 'debug') |
| 172 | if not os.path.exists(debugdir): |
| 173 | os.mkdir(debugdir) |
| 174 | |
| 175 | if user: |
| 176 | self.user = user |
| 177 | else: |
| 178 | self.user = getpass.getuser() |
| 179 | |
jadmanski | 808f4b1 | 2010-04-09 22:30:31 +0000 | [diff] [blame] | 180 | self.args = args |
Peter Mayo | 7a87576 | 2012-06-13 14:38:15 -0400 | [diff] [blame] | 181 | self.label = label |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 182 | self.machines = machines |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 183 | self._client = client |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 184 | self.warning_loggers = set() |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 185 | self.warning_manager = warning_manager() |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 186 | self._ssh_user = ssh_user |
| 187 | self._ssh_port = ssh_port |
| 188 | self._ssh_pass = ssh_pass |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 189 | self.tag = tag |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 190 | self.last_boot_tag = None |
jadmanski | 53aaf38 | 2008-11-17 16:22:31 +0000 | [diff] [blame] | 191 | self.hosts = set() |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 192 | self.drop_caches = False |
mbligh | b5dac43 | 2008-11-27 00:38:44 +0000 | [diff] [blame] | 193 | self.drop_caches_between_iterations = False |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 194 | self._control_filename = control_filename |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 195 | |
showard | 75cdfee | 2009-06-10 17:40:41 +0000 | [diff] [blame] | 196 | self.logging = logging_manager.get_logging_manager( |
| 197 | manage_stdout_and_stderr=True, redirect_fds=True) |
| 198 | subcommand.logging_manager_object = self.logging |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 199 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 200 | self.sysinfo = sysinfo.sysinfo(self.resultdir) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 201 | self.profilers = profilers.profilers(self) |
jadmanski | c09fc15 | 2008-10-15 17:56:59 +0000 | [diff] [blame] | 202 | |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 203 | job_data = {'label' : label, 'user' : user, |
| 204 | 'hostname' : ','.join(machines), |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 205 | 'drone' : platform.node(), |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 206 | 'status_version' : str(self._STATUS_VERSION), |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 207 | 'job_started' : str(int(time.time()))} |
mbligh | 374f341 | 2009-05-13 21:29:45 +0000 | [diff] [blame] | 208 | if group_name: |
| 209 | job_data['host_group_name'] = group_name |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 210 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 211 | # only write these keyvals out on the first job in a resultdir |
| 212 | if 'job_started' not in utils.read_keyval(self.resultdir): |
| 213 | job_data.update(get_site_job_data(self)) |
| 214 | utils.write_keyval(self.resultdir, job_data) |
| 215 | |
| 216 | self._parse_job = parse_job |
showard | cc92936 | 2010-01-25 21:20:41 +0000 | [diff] [blame] | 217 | self._using_parser = (self._parse_job and len(machines) <= 1) |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 218 | self.pkgmgr = packages.PackageManager( |
| 219 | self.autodir, run_function_dargs={'timeout':600}) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 220 | self.num_tests_run = 0 |
| 221 | self.num_tests_failed = 0 |
| 222 | |
jadmanski | 550fdc2 | 2008-11-20 16:32:08 +0000 | [diff] [blame] | 223 | self._register_subcommand_hooks() |
| 224 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 225 | # these components aren't usable on the server |
| 226 | self.bootloader = None |
| 227 | self.harness = None |
| 228 | |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 229 | # set up the status logger |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame] | 230 | self._indenter = status_indenter() |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 231 | self._logger = base_job.status_logger( |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame] | 232 | self, self._indenter, 'status.log', 'status.log', |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 233 | record_hook=server_job_record_hook(self)) |
| 234 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 235 | |
| 236 | @classmethod |
| 237 | def _find_base_directories(cls): |
| 238 | """ |
| 239 | Determine locations of autodir, clientdir and serverdir. Assumes |
| 240 | that this file is located within serverdir and uses __file__ along |
| 241 | with relative paths to resolve the location. |
| 242 | """ |
| 243 | serverdir = os.path.abspath(os.path.dirname(__file__)) |
| 244 | autodir = os.path.normpath(os.path.join(serverdir, '..')) |
| 245 | clientdir = os.path.join(autodir, 'client') |
| 246 | return autodir, clientdir, serverdir |
| 247 | |
| 248 | |
| 249 | def _find_resultdir(self, resultdir): |
| 250 | """ |
| 251 | Determine the location of resultdir. For server jobs we expect one to |
| 252 | always be explicitly passed in to __init__, so just return that. |
| 253 | """ |
| 254 | if resultdir: |
| 255 | return os.path.normpath(resultdir) |
| 256 | else: |
| 257 | return None |
| 258 | |
jadmanski | 550fdc2 | 2008-11-20 16:32:08 +0000 | [diff] [blame] | 259 | |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 260 | def _get_status_logger(self): |
| 261 | """Return a reference to the status logger.""" |
| 262 | return self._logger |
| 263 | |
| 264 | |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 265 | @staticmethod |
| 266 | def _load_control_file(path): |
| 267 | f = open(path) |
| 268 | try: |
| 269 | control_file = f.read() |
| 270 | finally: |
| 271 | f.close() |
| 272 | return re.sub('\r', '', control_file) |
| 273 | |
| 274 | |
jadmanski | 550fdc2 | 2008-11-20 16:32:08 +0000 | [diff] [blame] | 275 | def _register_subcommand_hooks(self): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 276 | """ |
| 277 | Register some hooks into the subcommand modules that allow us |
| 278 | to properly clean up self.hosts created in forked subprocesses. |
| 279 | """ |
jadmanski | 550fdc2 | 2008-11-20 16:32:08 +0000 | [diff] [blame] | 280 | def on_fork(cmd): |
| 281 | self._existing_hosts_on_fork = set(self.hosts) |
| 282 | def on_join(cmd): |
| 283 | new_hosts = self.hosts - self._existing_hosts_on_fork |
| 284 | for host in new_hosts: |
| 285 | host.close() |
| 286 | subcommand.subcommand.register_fork_hook(on_fork) |
| 287 | subcommand.subcommand.register_join_hook(on_join) |
| 288 | |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 289 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 290 | def init_parser(self): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 291 | """ |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 292 | Start the continuous parsing of self.resultdir. This sets up |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 293 | the database connection and inserts the basic job object into |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 294 | the database if necessary. |
| 295 | """ |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 296 | if not self._using_parser: |
| 297 | return |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 298 | # redirect parser debugging to .parse.log |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 299 | parse_log = os.path.join(self.resultdir, '.parse.log') |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 300 | parse_log = open(parse_log, 'w', 0) |
| 301 | tko_utils.redirect_parser_debugging(parse_log) |
| 302 | # create a job model object and set up the db |
| 303 | self.results_db = tko_db.db(autocommit=True) |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 304 | self.parser = status_lib.parser(self._STATUS_VERSION) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 305 | self.job_model = self.parser.make_job(self.resultdir) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 306 | self.parser.start(self.job_model) |
| 307 | # check if a job already exists in the db and insert it if |
| 308 | # it does not |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 309 | job_idx = self.results_db.find_job(self._parse_job) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 310 | if job_idx is None: |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 311 | self.results_db.insert_job(self._parse_job, self.job_model) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 312 | else: |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 313 | machine_idx = self.results_db.lookup_machine(self.job_model.machine) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 314 | self.job_model.index = job_idx |
| 315 | self.job_model.machine_idx = machine_idx |
| 316 | |
| 317 | |
| 318 | def cleanup_parser(self): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 319 | """ |
| 320 | This should be called after the server job is finished |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 321 | to carry out any remaining cleanup (e.g. flushing any |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 322 | remaining test results to the results db) |
| 323 | """ |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 324 | if not self._using_parser: |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 325 | return |
| 326 | final_tests = self.parser.end() |
| 327 | for test in final_tests: |
| 328 | self.__insert_test(test) |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 329 | self._using_parser = False |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 330 | |
| 331 | |
| 332 | def verify(self): |
| 333 | if not self.machines: |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 334 | raise error.AutoservError('No machines specified to verify') |
mbligh | 0fce411 | 2008-11-27 00:37:17 +0000 | [diff] [blame] | 335 | if self.resultdir: |
| 336 | os.chdir(self.resultdir) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 337 | try: |
jadmanski | cdd0c40 | 2008-09-19 21:21:31 +0000 | [diff] [blame] | 338 | namespace = {'machines' : self.machines, 'job' : self, |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 339 | 'ssh_user' : self._ssh_user, |
| 340 | 'ssh_port' : self._ssh_port, |
| 341 | 'ssh_pass' : self._ssh_pass} |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 342 | self._execute_code(VERIFY_CONTROL_FILE, namespace, protect=False) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 343 | except Exception, e: |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 344 | msg = ('Verify failed\n' + str(e) + '\n' + traceback.format_exc()) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 345 | self.record('ABORT', None, None, msg) |
| 346 | raise |
| 347 | |
| 348 | |
| 349 | def repair(self, host_protection): |
| 350 | if not self.machines: |
| 351 | raise error.AutoservError('No machines specified to repair') |
mbligh | 0fce411 | 2008-11-27 00:37:17 +0000 | [diff] [blame] | 352 | if self.resultdir: |
| 353 | os.chdir(self.resultdir) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 354 | namespace = {'machines': self.machines, 'job': self, |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 355 | 'ssh_user': self._ssh_user, 'ssh_port': self._ssh_port, |
| 356 | 'ssh_pass': self._ssh_pass, |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 357 | 'protection_level': host_protection} |
mbligh | 25c0b8c | 2009-01-24 01:44:17 +0000 | [diff] [blame] | 358 | |
mbligh | 0931b0a | 2009-04-08 17:44:48 +0000 | [diff] [blame] | 359 | self._execute_code(REPAIR_CONTROL_FILE, namespace, protect=False) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 360 | |
| 361 | |
| 362 | def precheck(self): |
| 363 | """ |
| 364 | perform any additional checks in derived classes. |
| 365 | """ |
| 366 | pass |
| 367 | |
| 368 | |
| 369 | def enable_external_logging(self): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 370 | """ |
| 371 | Start or restart external logging mechanism. |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 372 | """ |
| 373 | pass |
| 374 | |
| 375 | |
| 376 | def disable_external_logging(self): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 377 | """ |
| 378 | Pause or stop external logging mechanism. |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 379 | """ |
| 380 | pass |
| 381 | |
| 382 | |
| 383 | def use_external_logging(self): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 384 | """ |
| 385 | Return True if external logging should be used. |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 386 | """ |
| 387 | return False |
| 388 | |
| 389 | |
mbligh | 415dc21 | 2009-06-15 21:53:34 +0000 | [diff] [blame] | 390 | def _make_parallel_wrapper(self, function, machines, log): |
| 391 | """Wrap function as appropriate for calling by parallel_simple.""" |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 392 | is_forking = not (len(machines) == 1 and self.machines == machines) |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 393 | if self._parse_job and is_forking and log: |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 394 | def wrapper(machine): |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 395 | self._parse_job += "/" + machine |
| 396 | self._using_parser = True |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 397 | self.machines = [machine] |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 398 | self.push_execution_context(machine) |
jadmanski | 609a5f4 | 2008-08-26 20:52:42 +0000 | [diff] [blame] | 399 | os.chdir(self.resultdir) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 400 | utils.write_keyval(self.resultdir, {"hostname": machine}) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 401 | self.init_parser() |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 402 | result = function(machine) |
| 403 | self.cleanup_parser() |
| 404 | return result |
jadmanski | 4dd1a00 | 2008-09-05 20:27:30 +0000 | [diff] [blame] | 405 | elif len(machines) > 1 and log: |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 406 | def wrapper(machine): |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 407 | self.push_execution_context(machine) |
jadmanski | 609a5f4 | 2008-08-26 20:52:42 +0000 | [diff] [blame] | 408 | os.chdir(self.resultdir) |
mbligh | 838d82d | 2009-03-11 17:14:31 +0000 | [diff] [blame] | 409 | machine_data = {'hostname' : machine, |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 410 | 'status_version' : str(self._STATUS_VERSION)} |
mbligh | 838d82d | 2009-03-11 17:14:31 +0000 | [diff] [blame] | 411 | utils.write_keyval(self.resultdir, machine_data) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 412 | result = function(machine) |
| 413 | return result |
| 414 | else: |
| 415 | wrapper = function |
mbligh | 415dc21 | 2009-06-15 21:53:34 +0000 | [diff] [blame] | 416 | return wrapper |
| 417 | |
| 418 | |
| 419 | def parallel_simple(self, function, machines, log=True, timeout=None, |
| 420 | return_results=False): |
| 421 | """ |
| 422 | Run 'function' using parallel_simple, with an extra wrapper to handle |
| 423 | the necessary setup for continuous parsing, if possible. If continuous |
| 424 | parsing is already properly initialized then this should just work. |
| 425 | |
| 426 | @param function: A callable to run in parallel given each machine. |
| 427 | @param machines: A list of machine names to be passed one per subcommand |
| 428 | invocation of function. |
| 429 | @param log: If True, output will be written to output in a subdirectory |
| 430 | named after each machine. |
| 431 | @param timeout: Seconds after which the function call should timeout. |
| 432 | @param return_results: If True instead of an AutoServError being raised |
| 433 | on any error a list of the results|exceptions from the function |
| 434 | called on each arg is returned. [default: False] |
| 435 | |
| 436 | @raises error.AutotestError: If any of the functions failed. |
| 437 | """ |
| 438 | wrapper = self._make_parallel_wrapper(function, machines, log) |
| 439 | return subcommand.parallel_simple(wrapper, machines, |
| 440 | log=log, timeout=timeout, |
| 441 | return_results=return_results) |
| 442 | |
| 443 | |
| 444 | def parallel_on_machines(self, function, machines, timeout=None): |
| 445 | """ |
showard | cd5fac4 | 2009-07-06 20:19:43 +0000 | [diff] [blame] | 446 | @param function: Called in parallel with one machine as its argument. |
mbligh | 415dc21 | 2009-06-15 21:53:34 +0000 | [diff] [blame] | 447 | @param machines: A list of machines to call function(machine) on. |
| 448 | @param timeout: Seconds after which the function call should timeout. |
| 449 | |
| 450 | @returns A list of machines on which function(machine) returned |
| 451 | without raising an exception. |
| 452 | """ |
showard | cd5fac4 | 2009-07-06 20:19:43 +0000 | [diff] [blame] | 453 | results = self.parallel_simple(function, machines, timeout=timeout, |
mbligh | 415dc21 | 2009-06-15 21:53:34 +0000 | [diff] [blame] | 454 | return_results=True) |
| 455 | success_machines = [] |
| 456 | for result, machine in itertools.izip(results, machines): |
| 457 | if not isinstance(result, Exception): |
| 458 | success_machines.append(machine) |
| 459 | return success_machines |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 460 | |
| 461 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 462 | _USE_TEMP_DIR = object() |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 463 | def run(self, cleanup=False, install_before=False, install_after=False, |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 464 | collect_crashdumps=True, namespace={}, control=None, |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 465 | control_file_dir=None, only_collect_crashinfo=False): |
jadmanski | fb9c0fa | 2009-04-29 17:39:16 +0000 | [diff] [blame] | 466 | # for a normal job, make sure the uncollected logs file exists |
| 467 | # for a crashinfo-only run it should already exist, bail out otherwise |
jadmanski | 648c39f | 2010-03-19 17:38:01 +0000 | [diff] [blame] | 468 | created_uncollected_logs = False |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 469 | if self.resultdir and not os.path.exists(self._uncollected_log_file): |
jadmanski | fb9c0fa | 2009-04-29 17:39:16 +0000 | [diff] [blame] | 470 | if only_collect_crashinfo: |
| 471 | # if this is a crashinfo-only run, and there were no existing |
| 472 | # uncollected logs, just bail out early |
| 473 | logging.info("No existing uncollected logs, " |
| 474 | "skipping crashinfo collection") |
| 475 | return |
| 476 | else: |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 477 | log_file = open(self._uncollected_log_file, "w") |
jadmanski | fb9c0fa | 2009-04-29 17:39:16 +0000 | [diff] [blame] | 478 | pickle.dump([], log_file) |
| 479 | log_file.close() |
jadmanski | 648c39f | 2010-03-19 17:38:01 +0000 | [diff] [blame] | 480 | created_uncollected_logs = True |
jadmanski | fb9c0fa | 2009-04-29 17:39:16 +0000 | [diff] [blame] | 481 | |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 482 | # use a copy so changes don't affect the original dictionary |
| 483 | namespace = namespace.copy() |
| 484 | machines = self.machines |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 485 | if control is None: |
jadmanski | 02a3ba2 | 2009-11-13 20:47:27 +0000 | [diff] [blame] | 486 | if self.control is None: |
| 487 | control = '' |
| 488 | else: |
| 489 | control = self._load_control_file(self.control) |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 490 | if control_file_dir is None: |
| 491 | control_file_dir = self.resultdir |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 492 | |
| 493 | self.aborted = False |
| 494 | namespace['machines'] = machines |
jadmanski | 808f4b1 | 2010-04-09 22:30:31 +0000 | [diff] [blame] | 495 | namespace['args'] = self.args |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 496 | namespace['job'] = self |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 497 | namespace['ssh_user'] = self._ssh_user |
| 498 | namespace['ssh_port'] = self._ssh_port |
| 499 | namespace['ssh_pass'] = self._ssh_pass |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 500 | test_start_time = int(time.time()) |
| 501 | |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 502 | if self.resultdir: |
| 503 | os.chdir(self.resultdir) |
jadmanski | 779bd29 | 2009-03-19 17:33:33 +0000 | [diff] [blame] | 504 | # touch status.log so that the parser knows a job is running here |
jadmanski | 382303a | 2009-04-21 19:53:39 +0000 | [diff] [blame] | 505 | open(self.get_status_log_path(), 'a').close() |
mbligh | 80e1eba | 2008-11-19 00:26:18 +0000 | [diff] [blame] | 506 | self.enable_external_logging() |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 507 | |
jadmanski | cdd0c40 | 2008-09-19 21:21:31 +0000 | [diff] [blame] | 508 | collect_crashinfo = True |
mbligh | aebe3b6 | 2008-12-22 14:45:40 +0000 | [diff] [blame] | 509 | temp_control_file_dir = None |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 510 | try: |
showard | cf8d492 | 2009-10-14 16:08:39 +0000 | [diff] [blame] | 511 | try: |
| 512 | if install_before and machines: |
| 513 | self._execute_code(INSTALL_CONTROL_FILE, namespace) |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 514 | |
showard | cf8d492 | 2009-10-14 16:08:39 +0000 | [diff] [blame] | 515 | if only_collect_crashinfo: |
| 516 | return |
| 517 | |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 518 | # determine the dir to write the control files to |
| 519 | cfd_specified = (control_file_dir |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 520 | and control_file_dir is not self._USE_TEMP_DIR) |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 521 | if cfd_specified: |
| 522 | temp_control_file_dir = None |
| 523 | else: |
| 524 | temp_control_file_dir = tempfile.mkdtemp( |
| 525 | suffix='temp_control_file_dir') |
| 526 | control_file_dir = temp_control_file_dir |
| 527 | server_control_file = os.path.join(control_file_dir, |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 528 | self._control_filename) |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 529 | client_control_file = os.path.join(control_file_dir, |
| 530 | CLIENT_CONTROL_FILENAME) |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 531 | if self._client: |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 532 | namespace['control'] = control |
| 533 | utils.open_write_close(client_control_file, control) |
mbligh | feac010 | 2009-04-28 18:31:12 +0000 | [diff] [blame] | 534 | shutil.copyfile(CLIENT_WRAPPER_CONTROL_FILE, |
| 535 | server_control_file) |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 536 | else: |
| 537 | utils.open_write_close(server_control_file, control) |
mbligh | 26f0d88 | 2009-06-22 18:30:01 +0000 | [diff] [blame] | 538 | logging.info("Processing control file") |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 539 | self._execute_code(server_control_file, namespace) |
mbligh | 26f0d88 | 2009-06-22 18:30:01 +0000 | [diff] [blame] | 540 | logging.info("Finished processing control file") |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 541 | |
jadmanski | def0c3c | 2009-03-25 20:07:10 +0000 | [diff] [blame] | 542 | # no error occured, so we don't need to collect crashinfo |
| 543 | collect_crashinfo = False |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 544 | except Exception, e: |
showard | cf8d492 | 2009-10-14 16:08:39 +0000 | [diff] [blame] | 545 | try: |
| 546 | logging.exception( |
| 547 | 'Exception escaped control file, job aborting:') |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 548 | self.record('INFO', None, None, str(e), |
| 549 | {'job_abort_reason': str(e)}) |
showard | cf8d492 | 2009-10-14 16:08:39 +0000 | [diff] [blame] | 550 | except: |
| 551 | pass # don't let logging exceptions here interfere |
| 552 | raise |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 553 | finally: |
mbligh | aebe3b6 | 2008-12-22 14:45:40 +0000 | [diff] [blame] | 554 | if temp_control_file_dir: |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 555 | # Clean up temp directory used for copies of the control files |
mbligh | aebe3b6 | 2008-12-22 14:45:40 +0000 | [diff] [blame] | 556 | try: |
| 557 | shutil.rmtree(temp_control_file_dir) |
| 558 | except Exception, e: |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 559 | logging.warn('Could not remove temp directory %s: %s', |
| 560 | temp_control_file_dir, e) |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 561 | |
jadmanski | cdd0c40 | 2008-09-19 21:21:31 +0000 | [diff] [blame] | 562 | if machines and (collect_crashdumps or collect_crashinfo): |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 563 | namespace['test_start_time'] = test_start_time |
jadmanski | cdd0c40 | 2008-09-19 21:21:31 +0000 | [diff] [blame] | 564 | if collect_crashinfo: |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 565 | # includes crashdumps |
| 566 | self._execute_code(CRASHINFO_CONTROL_FILE, namespace) |
jadmanski | cdd0c40 | 2008-09-19 21:21:31 +0000 | [diff] [blame] | 567 | else: |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 568 | self._execute_code(CRASHDUMPS_CONTROL_FILE, namespace) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 569 | self.disable_external_logging() |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 570 | if cleanup and machines: |
| 571 | self._execute_code(CLEANUP_CONTROL_FILE, namespace) |
Chris Sosa | f4d43ff | 2012-10-30 11:21:05 -0700 | [diff] [blame^] | 572 | if self._uncollected_log_file and created_uncollected_logs: |
| 573 | os.remove(self._uncollected_log_file) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 574 | if install_after and machines: |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 575 | self._execute_code(INSTALL_CONTROL_FILE, namespace) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 576 | |
| 577 | |
| 578 | def run_test(self, url, *args, **dargs): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 579 | """ |
| 580 | Summon a test object and run it. |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 581 | |
| 582 | tag |
| 583 | tag to add to testname |
| 584 | url |
| 585 | url of the test to run |
| 586 | """ |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 587 | group, testname = self.pkgmgr.get_package_name(url, 'test') |
| 588 | testname, subdir, tag = self._build_tagged_test_name(testname, dargs) |
| 589 | outputdir = self._make_test_outputdir(subdir) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 590 | |
| 591 | def group_func(): |
| 592 | try: |
| 593 | test.runtest(self, url, tag, args, dargs) |
| 594 | except error.TestBaseException, e: |
| 595 | self.record(e.exit_status, subdir, testname, str(e)) |
| 596 | raise |
| 597 | except Exception, e: |
| 598 | info = str(e) + "\n" + traceback.format_exc() |
| 599 | self.record('FAIL', subdir, testname, info) |
| 600 | raise |
| 601 | else: |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 602 | self.record('GOOD', subdir, testname, 'completed successfully') |
jadmanski | de292df | 2008-08-26 20:51:14 +0000 | [diff] [blame] | 603 | |
| 604 | result, exc_info = self._run_group(testname, subdir, group_func) |
| 605 | if exc_info and isinstance(exc_info[1], error.TestBaseException): |
| 606 | return False |
| 607 | elif exc_info: |
| 608 | raise exc_info[0], exc_info[1], exc_info[2] |
| 609 | else: |
| 610 | return True |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 611 | |
| 612 | |
| 613 | def _run_group(self, name, subdir, function, *args, **dargs): |
| 614 | """\ |
| 615 | Underlying method for running something inside of a group. |
| 616 | """ |
jadmanski | de292df | 2008-08-26 20:51:14 +0000 | [diff] [blame] | 617 | result, exc_info = None, None |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 618 | try: |
| 619 | self.record('START', subdir, name) |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame] | 620 | result = function(*args, **dargs) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 621 | except error.TestBaseException, e: |
jadmanski | b88d6dc | 2009-01-10 00:33:18 +0000 | [diff] [blame] | 622 | self.record("END %s" % e.exit_status, subdir, name) |
jadmanski | de292df | 2008-08-26 20:51:14 +0000 | [diff] [blame] | 623 | exc_info = sys.exc_info() |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 624 | except Exception, e: |
| 625 | err_msg = str(e) + '\n' |
| 626 | err_msg += traceback.format_exc() |
| 627 | self.record('END ABORT', subdir, name, err_msg) |
| 628 | raise error.JobError(name + ' failed\n' + traceback.format_exc()) |
| 629 | else: |
| 630 | self.record('END GOOD', subdir, name) |
| 631 | |
jadmanski | de292df | 2008-08-26 20:51:14 +0000 | [diff] [blame] | 632 | return result, exc_info |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 633 | |
| 634 | |
| 635 | def run_group(self, function, *args, **dargs): |
| 636 | """\ |
| 637 | function: |
| 638 | subroutine to run |
| 639 | *args: |
| 640 | arguments for the function |
| 641 | """ |
| 642 | |
| 643 | name = function.__name__ |
| 644 | |
| 645 | # Allow the tag for the group to be specified. |
| 646 | tag = dargs.pop('tag', None) |
| 647 | if tag: |
| 648 | name = tag |
| 649 | |
jadmanski | de292df | 2008-08-26 20:51:14 +0000 | [diff] [blame] | 650 | return self._run_group(name, None, function, *args, **dargs)[0] |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 651 | |
| 652 | |
| 653 | def run_reboot(self, reboot_func, get_kernel_func): |
| 654 | """\ |
| 655 | A specialization of run_group meant specifically for handling |
| 656 | a reboot. Includes support for capturing the kernel version |
| 657 | after the reboot. |
| 658 | |
| 659 | reboot_func: a function that carries out the reboot |
| 660 | |
| 661 | get_kernel_func: a function that returns a string |
| 662 | representing the kernel version. |
| 663 | """ |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 664 | try: |
| 665 | self.record('START', None, 'reboot') |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 666 | reboot_func() |
| 667 | except Exception, e: |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 668 | err_msg = str(e) + '\n' + traceback.format_exc() |
| 669 | self.record('END FAIL', None, 'reboot', err_msg) |
jadmanski | 4b51d54 | 2009-04-08 14:17:16 +0000 | [diff] [blame] | 670 | raise |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 671 | else: |
| 672 | kernel = get_kernel_func() |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 673 | self.record('END GOOD', None, 'reboot', |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 674 | optional_fields={"kernel": kernel}) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 675 | |
| 676 | |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 677 | def run_control(self, path): |
| 678 | """Execute a control file found at path (relative to the autotest |
| 679 | path). Intended for executing a control file within a control file, |
| 680 | not for running the top-level job control file.""" |
| 681 | path = os.path.join(self.autodir, path) |
| 682 | control_file = self._load_control_file(path) |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 683 | self.run(control=control_file, control_file_dir=self._USE_TEMP_DIR) |
jadmanski | e432dd2 | 2009-01-30 15:04:51 +0000 | [diff] [blame] | 684 | |
| 685 | |
jadmanski | c09fc15 | 2008-10-15 17:56:59 +0000 | [diff] [blame] | 686 | def add_sysinfo_command(self, command, logfile=None, on_every_test=False): |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 687 | self._add_sysinfo_loggable(sysinfo.command(command, logf=logfile), |
jadmanski | c09fc15 | 2008-10-15 17:56:59 +0000 | [diff] [blame] | 688 | on_every_test) |
| 689 | |
| 690 | |
| 691 | def add_sysinfo_logfile(self, file, on_every_test=False): |
| 692 | self._add_sysinfo_loggable(sysinfo.logfile(file), on_every_test) |
| 693 | |
| 694 | |
| 695 | def _add_sysinfo_loggable(self, loggable, on_every_test): |
| 696 | if on_every_test: |
| 697 | self.sysinfo.test_loggables.add(loggable) |
| 698 | else: |
| 699 | self.sysinfo.boot_loggables.add(loggable) |
| 700 | |
| 701 | |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 702 | def _read_warnings(self): |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 703 | """Poll all the warning loggers and extract any new warnings that have |
| 704 | been logged. If the warnings belong to a category that is currently |
| 705 | disabled, this method will discard them and they will no longer be |
| 706 | retrievable. |
| 707 | |
| 708 | Returns a list of (timestamp, message) tuples, where timestamp is an |
| 709 | integer epoch timestamp.""" |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 710 | warnings = [] |
| 711 | while True: |
| 712 | # pull in a line of output from every logger that has |
| 713 | # output ready to be read |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 714 | loggers, _, _ = select.select(self.warning_loggers, [], [], 0) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 715 | closed_loggers = set() |
| 716 | for logger in loggers: |
| 717 | line = logger.readline() |
| 718 | # record any broken pipes (aka line == empty) |
| 719 | if len(line) == 0: |
| 720 | closed_loggers.add(logger) |
| 721 | continue |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 722 | # parse out the warning |
| 723 | timestamp, msgtype, msg = line.split('\t', 2) |
| 724 | timestamp = int(timestamp) |
| 725 | # if the warning is valid, add it to the results |
| 726 | if self.warning_manager.is_valid(timestamp, msgtype): |
| 727 | warnings.append((timestamp, msg.strip())) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 728 | |
| 729 | # stop listening to loggers that are closed |
| 730 | self.warning_loggers -= closed_loggers |
| 731 | |
| 732 | # stop if none of the loggers have any output left |
| 733 | if not loggers: |
| 734 | break |
| 735 | |
| 736 | # sort into timestamp order |
| 737 | warnings.sort() |
| 738 | return warnings |
| 739 | |
| 740 | |
showard | cc92936 | 2010-01-25 21:20:41 +0000 | [diff] [blame] | 741 | def _unique_subdirectory(self, base_subdirectory_name): |
| 742 | """Compute a unique results subdirectory based on the given name. |
| 743 | |
| 744 | Appends base_subdirectory_name with a number as necessary to find a |
| 745 | directory name that doesn't already exist. |
| 746 | """ |
| 747 | subdirectory = base_subdirectory_name |
| 748 | counter = 1 |
| 749 | while os.path.exists(os.path.join(self.resultdir, subdirectory)): |
| 750 | subdirectory = base_subdirectory_name + '.' + str(counter) |
| 751 | counter += 1 |
| 752 | return subdirectory |
| 753 | |
| 754 | |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame] | 755 | def get_record_context(self): |
| 756 | """Returns an object representing the current job.record context. |
| 757 | |
| 758 | The object returned is an opaque object with a 0-arg restore method |
| 759 | which can be called to restore the job.record context (i.e. indentation) |
| 760 | to the current level. The intention is that it should be used when |
| 761 | something external which generate job.record calls (e.g. an autotest |
| 762 | client) can fail catastrophically and the server job record state |
| 763 | needs to be reset to its original "known good" state. |
| 764 | |
| 765 | @return: A context object with a 0-arg restore() method.""" |
| 766 | return self._indenter.get_context() |
| 767 | |
| 768 | |
showard | cc92936 | 2010-01-25 21:20:41 +0000 | [diff] [blame] | 769 | def record_summary(self, status_code, test_name, reason='', attributes=None, |
| 770 | distinguishing_attributes=(), child_test_ids=None): |
| 771 | """Record a summary test result. |
| 772 | |
| 773 | @param status_code: status code string, see |
| 774 | common_lib.log.is_valid_status() |
| 775 | @param test_name: name of the test |
| 776 | @param reason: (optional) string providing detailed reason for test |
| 777 | outcome |
| 778 | @param attributes: (optional) dict of string keyvals to associate with |
| 779 | this result |
| 780 | @param distinguishing_attributes: (optional) list of attribute names |
| 781 | that should be used to distinguish identically-named test |
| 782 | results. These attributes should be present in the attributes |
| 783 | parameter. This is used to generate user-friendly subdirectory |
| 784 | names. |
| 785 | @param child_test_ids: (optional) list of test indices for test results |
| 786 | used in generating this result. |
| 787 | """ |
| 788 | subdirectory_name_parts = [test_name] |
| 789 | for attribute in distinguishing_attributes: |
| 790 | assert attributes |
| 791 | assert attribute in attributes, '%s not in %s' % (attribute, |
| 792 | attributes) |
| 793 | subdirectory_name_parts.append(attributes[attribute]) |
| 794 | base_subdirectory_name = '.'.join(subdirectory_name_parts) |
| 795 | |
| 796 | subdirectory = self._unique_subdirectory(base_subdirectory_name) |
| 797 | subdirectory_path = os.path.join(self.resultdir, subdirectory) |
| 798 | os.mkdir(subdirectory_path) |
| 799 | |
| 800 | self.record(status_code, subdirectory, test_name, |
| 801 | status=reason, optional_fields={'is_summary': True}) |
| 802 | |
| 803 | if attributes: |
| 804 | utils.write_keyval(subdirectory_path, attributes) |
| 805 | |
| 806 | if child_test_ids: |
| 807 | ids_string = ','.join(str(test_id) for test_id in child_test_ids) |
| 808 | summary_data = {'child_test_ids': ids_string} |
| 809 | utils.write_keyval(os.path.join(subdirectory_path, 'summary_data'), |
| 810 | summary_data) |
| 811 | |
| 812 | |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 813 | def disable_warnings(self, warning_type): |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 814 | self.warning_manager.disable_warnings(warning_type) |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 815 | self.record("INFO", None, None, |
| 816 | "disabling %s warnings" % warning_type, |
| 817 | {"warnings.disable": warning_type}) |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 818 | |
| 819 | |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 820 | def enable_warnings(self, warning_type): |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 821 | self.warning_manager.enable_warnings(warning_type) |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 822 | self.record("INFO", None, None, |
| 823 | "enabling %s warnings" % warning_type, |
| 824 | {"warnings.enable": warning_type}) |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 825 | |
| 826 | |
jadmanski | 779bd29 | 2009-03-19 17:33:33 +0000 | [diff] [blame] | 827 | def get_status_log_path(self, subdir=None): |
| 828 | """Return the path to the job status log. |
| 829 | |
| 830 | @param subdir - Optional paramter indicating that you want the path |
| 831 | to a subdirectory status log. |
| 832 | |
| 833 | @returns The path where the status log should be. |
| 834 | """ |
mbligh | 210bae6 | 2009-04-01 18:33:13 +0000 | [diff] [blame] | 835 | if self.resultdir: |
| 836 | if subdir: |
| 837 | return os.path.join(self.resultdir, subdir, "status.log") |
| 838 | else: |
| 839 | return os.path.join(self.resultdir, "status.log") |
jadmanski | 779bd29 | 2009-03-19 17:33:33 +0000 | [diff] [blame] | 840 | else: |
mbligh | 210bae6 | 2009-04-01 18:33:13 +0000 | [diff] [blame] | 841 | return None |
jadmanski | 779bd29 | 2009-03-19 17:33:33 +0000 | [diff] [blame] | 842 | |
| 843 | |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 844 | def _update_uncollected_logs_list(self, update_func): |
| 845 | """Updates the uncollected logs list in a multi-process safe manner. |
| 846 | |
| 847 | @param update_func - a function that updates the list of uncollected |
| 848 | logs. Should take one parameter, the list to be updated. |
| 849 | """ |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 850 | if self._uncollected_log_file: |
| 851 | log_file = open(self._uncollected_log_file, "r+") |
mbligh | a788dc4 | 2009-03-26 21:10:16 +0000 | [diff] [blame] | 852 | fcntl.flock(log_file, fcntl.LOCK_EX) |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 853 | try: |
| 854 | uncollected_logs = pickle.load(log_file) |
| 855 | update_func(uncollected_logs) |
| 856 | log_file.seek(0) |
| 857 | log_file.truncate() |
| 858 | pickle.dump(uncollected_logs, log_file) |
jadmanski | 3bff909 | 2009-04-22 18:09:47 +0000 | [diff] [blame] | 859 | log_file.flush() |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 860 | finally: |
| 861 | fcntl.flock(log_file, fcntl.LOCK_UN) |
| 862 | log_file.close() |
| 863 | |
| 864 | |
| 865 | def add_client_log(self, hostname, remote_path, local_path): |
| 866 | """Adds a new set of client logs to the list of uncollected logs, |
| 867 | to allow for future log recovery. |
| 868 | |
| 869 | @param host - the hostname of the machine holding the logs |
| 870 | @param remote_path - the directory on the remote machine holding logs |
| 871 | @param local_path - the local directory to copy the logs into |
| 872 | """ |
| 873 | def update_func(logs_list): |
| 874 | logs_list.append((hostname, remote_path, local_path)) |
| 875 | self._update_uncollected_logs_list(update_func) |
| 876 | |
| 877 | |
| 878 | def remove_client_log(self, hostname, remote_path, local_path): |
| 879 | """Removes a set of client logs from the list of uncollected logs, |
| 880 | to allow for future log recovery. |
| 881 | |
| 882 | @param host - the hostname of the machine holding the logs |
| 883 | @param remote_path - the directory on the remote machine holding logs |
| 884 | @param local_path - the local directory to copy the logs into |
| 885 | """ |
| 886 | def update_func(logs_list): |
| 887 | logs_list.remove((hostname, remote_path, local_path)) |
| 888 | self._update_uncollected_logs_list(update_func) |
| 889 | |
| 890 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 891 | def get_client_logs(self): |
| 892 | """Retrieves the list of uncollected logs, if it exists. |
| 893 | |
| 894 | @returns A list of (host, remote_path, local_path) tuples. Returns |
| 895 | an empty list if no uncollected logs file exists. |
| 896 | """ |
| 897 | log_exists = (self._uncollected_log_file and |
| 898 | os.path.exists(self._uncollected_log_file)) |
| 899 | if log_exists: |
| 900 | return pickle.load(open(self._uncollected_log_file)) |
| 901 | else: |
| 902 | return [] |
| 903 | |
| 904 | |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 905 | def _fill_server_control_namespace(self, namespace, protect=True): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 906 | """ |
| 907 | Prepare a namespace to be used when executing server control files. |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 908 | |
| 909 | This sets up the control file API by importing modules and making them |
| 910 | available under the appropriate names within namespace. |
| 911 | |
| 912 | For use by _execute_code(). |
| 913 | |
| 914 | Args: |
| 915 | namespace: The namespace dictionary to fill in. |
| 916 | protect: Boolean. If True (the default) any operation that would |
| 917 | clobber an existing entry in namespace will cause an error. |
| 918 | Raises: |
| 919 | error.AutoservError: When a name would be clobbered by import. |
| 920 | """ |
| 921 | def _import_names(module_name, names=()): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 922 | """ |
| 923 | Import a module and assign named attributes into namespace. |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 924 | |
| 925 | Args: |
| 926 | module_name: The string module name. |
| 927 | names: A limiting list of names to import from module_name. If |
| 928 | empty (the default), all names are imported from the module |
| 929 | similar to a "from foo.bar import *" statement. |
| 930 | Raises: |
| 931 | error.AutoservError: When a name being imported would clobber |
| 932 | a name already in namespace. |
| 933 | """ |
| 934 | module = __import__(module_name, {}, {}, names) |
| 935 | |
| 936 | # No names supplied? Import * from the lowest level module. |
| 937 | # (Ugh, why do I have to implement this part myself?) |
| 938 | if not names: |
| 939 | for submodule_name in module_name.split('.')[1:]: |
| 940 | module = getattr(module, submodule_name) |
| 941 | if hasattr(module, '__all__'): |
| 942 | names = getattr(module, '__all__') |
| 943 | else: |
| 944 | names = dir(module) |
| 945 | |
| 946 | # Install each name into namespace, checking to make sure it |
| 947 | # doesn't override anything that already exists. |
| 948 | for name in names: |
| 949 | # Check for conflicts to help prevent future problems. |
| 950 | if name in namespace and protect: |
| 951 | if namespace[name] is not getattr(module, name): |
| 952 | raise error.AutoservError('importing name ' |
| 953 | '%s from %s %r would override %r' % |
| 954 | (name, module_name, getattr(module, name), |
| 955 | namespace[name])) |
| 956 | else: |
| 957 | # Encourage cleanliness and the use of __all__ for a |
| 958 | # more concrete API with less surprises on '*' imports. |
| 959 | warnings.warn('%s (%r) being imported from %s for use ' |
| 960 | 'in server control files is not the ' |
| 961 | 'first occurrance of that import.' % |
| 962 | (name, namespace[name], module_name)) |
| 963 | |
| 964 | namespace[name] = getattr(module, name) |
| 965 | |
| 966 | |
| 967 | # This is the equivalent of prepending a bunch of import statements to |
| 968 | # the front of the control script. |
mbligh | a2b07dd | 2009-06-22 18:26:13 +0000 | [diff] [blame] | 969 | namespace.update(os=os, sys=sys, logging=logging) |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 970 | _import_names('autotest_lib.server', |
| 971 | ('hosts', 'autotest', 'kvm', 'git', 'standalone_profiler', |
| 972 | 'source_kernel', 'rpm_kernel', 'deb_kernel', 'git_kernel')) |
| 973 | _import_names('autotest_lib.server.subcommand', |
| 974 | ('parallel', 'parallel_simple', 'subcommand')) |
| 975 | _import_names('autotest_lib.server.utils', |
| 976 | ('run', 'get_tmp_dir', 'sh_escape', 'parse_machine')) |
| 977 | _import_names('autotest_lib.client.common_lib.error') |
| 978 | _import_names('autotest_lib.client.common_lib.barrier', ('barrier',)) |
| 979 | |
| 980 | # Inject ourself as the job object into other classes within the API. |
| 981 | # (Yuck, this injection is a gross thing be part of a public API. -gps) |
| 982 | # |
| 983 | # XXX Base & SiteAutotest do not appear to use .job. Who does? |
| 984 | namespace['autotest'].Autotest.job = self |
| 985 | # server.hosts.base_classes.Host uses .job. |
| 986 | namespace['hosts'].Host.job = self |
Eric Li | 10222b8 | 2010-11-24 09:33:15 -0800 | [diff] [blame] | 987 | namespace['hosts'].factory.ssh_user = self._ssh_user |
| 988 | namespace['hosts'].factory.ssh_port = self._ssh_port |
| 989 | namespace['hosts'].factory.ssh_pass = self._ssh_pass |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 990 | |
| 991 | |
| 992 | def _execute_code(self, code_file, namespace, protect=True): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 993 | """ |
| 994 | Execute code using a copy of namespace as a server control script. |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 995 | |
| 996 | Unless protect_namespace is explicitly set to False, the dict will not |
| 997 | be modified. |
| 998 | |
| 999 | Args: |
| 1000 | code_file: The filename of the control file to execute. |
| 1001 | namespace: A dict containing names to make available during execution. |
| 1002 | protect: Boolean. If True (the default) a copy of the namespace dict |
| 1003 | is used during execution to prevent the code from modifying its |
| 1004 | contents outside of this function. If False the raw dict is |
| 1005 | passed in and modifications will be allowed. |
| 1006 | """ |
| 1007 | if protect: |
| 1008 | namespace = namespace.copy() |
| 1009 | self._fill_server_control_namespace(namespace, protect=protect) |
| 1010 | # TODO: Simplify and get rid of the special cases for only 1 machine. |
showard | 3e66e8c | 2008-10-27 19:20:51 +0000 | [diff] [blame] | 1011 | if len(self.machines) > 1: |
mbligh | 084bc17 | 2008-10-18 14:02:45 +0000 | [diff] [blame] | 1012 | machines_text = '\n'.join(self.machines) + '\n' |
| 1013 | # Only rewrite the file if it does not match our machine list. |
| 1014 | try: |
| 1015 | machines_f = open(MACHINES_FILENAME, 'r') |
| 1016 | existing_machines_text = machines_f.read() |
| 1017 | machines_f.close() |
| 1018 | except EnvironmentError: |
| 1019 | existing_machines_text = None |
| 1020 | if machines_text != existing_machines_text: |
| 1021 | utils.open_write_close(MACHINES_FILENAME, machines_text) |
| 1022 | execfile(code_file, namespace, namespace) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 1023 | |
| 1024 | |
jadmanski | e29d0e4 | 2010-06-17 16:06:52 +0000 | [diff] [blame] | 1025 | def _parse_status(self, new_line): |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 1026 | if not self._using_parser: |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 1027 | return |
jadmanski | e29d0e4 | 2010-06-17 16:06:52 +0000 | [diff] [blame] | 1028 | new_tests = self.parser.process_lines([new_line]) |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 1029 | for test in new_tests: |
| 1030 | self.__insert_test(test) |
| 1031 | |
| 1032 | |
| 1033 | def __insert_test(self, test): |
mbligh | 2b92b86 | 2008-11-22 13:25:32 +0000 | [diff] [blame] | 1034 | """ |
| 1035 | An internal method to insert a new test result into the |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 1036 | database. This method will not raise an exception, even if an |
| 1037 | error occurs during the insert, to avoid failing a test |
| 1038 | simply because of unexpected database issues.""" |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1039 | self.num_tests_run += 1 |
| 1040 | if status_lib.is_worse_than_or_equal_to(test.status, 'FAIL'): |
| 1041 | self.num_tests_failed += 1 |
jadmanski | 1064644 | 2008-08-13 14:05:21 +0000 | [diff] [blame] | 1042 | try: |
| 1043 | self.results_db.insert_test(self.job_model, test) |
| 1044 | except Exception: |
| 1045 | msg = ("WARNING: An unexpected error occured while " |
| 1046 | "inserting test results into the database. " |
| 1047 | "Ignoring error.\n" + traceback.format_exc()) |
| 1048 | print >> sys.stderr, msg |
| 1049 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 1050 | |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 1051 | def preprocess_client_state(self): |
| 1052 | """ |
| 1053 | Produce a state file for initializing the state of a client job. |
| 1054 | |
| 1055 | Creates a new client state file with all the current server state, as |
| 1056 | well as some pre-set client state. |
| 1057 | |
| 1058 | @returns The path of the file the state was written into. |
| 1059 | """ |
| 1060 | # initialize the sysinfo state |
| 1061 | self._state.set('client', 'sysinfo', self.sysinfo.serialize()) |
| 1062 | |
| 1063 | # dump the state out to a tempfile |
| 1064 | fd, file_path = tempfile.mkstemp(dir=self.tmpdir) |
| 1065 | os.close(fd) |
mbligh | a2c9949 | 2010-01-27 22:59:50 +0000 | [diff] [blame] | 1066 | |
| 1067 | # write_to_file doesn't need locking, we exclusively own file_path |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 1068 | self._state.write_to_file(file_path) |
| 1069 | return file_path |
| 1070 | |
| 1071 | |
| 1072 | def postprocess_client_state(self, state_path): |
| 1073 | """ |
| 1074 | Update the state of this job with the state from a client job. |
| 1075 | |
| 1076 | Updates the state of the server side of a job with the final state |
| 1077 | of a client job that was run. Updates the non-client-specific state, |
| 1078 | pulls in some specific bits from the client-specific state, and then |
| 1079 | discards the rest. Removes the state file afterwards |
| 1080 | |
| 1081 | @param state_file A path to the state file from the client. |
| 1082 | """ |
| 1083 | # update the on-disk state |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 1084 | try: |
jadmanski | b6e7bdb | 2010-04-13 16:00:39 +0000 | [diff] [blame] | 1085 | self._state.read_from_file(state_path) |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 1086 | os.remove(state_path) |
mbligh | a2c9949 | 2010-01-27 22:59:50 +0000 | [diff] [blame] | 1087 | except OSError, e: |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 1088 | # ignore file-not-found errors |
| 1089 | if e.errno != errno.ENOENT: |
| 1090 | raise |
jadmanski | b6e7bdb | 2010-04-13 16:00:39 +0000 | [diff] [blame] | 1091 | else: |
| 1092 | logging.debug('Client state file %s not found', state_path) |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 1093 | |
| 1094 | # update the sysinfo state |
| 1095 | if self._state.has('client', 'sysinfo'): |
| 1096 | self.sysinfo.deserialize(self._state.get('client', 'sysinfo')) |
| 1097 | |
| 1098 | # drop all the client-specific state |
| 1099 | self._state.discard_namespace('client') |
| 1100 | |
| 1101 | |
mbligh | 0a88370 | 2010-04-21 01:58:34 +0000 | [diff] [blame] | 1102 | def clear_all_known_hosts(self): |
| 1103 | """Clears known hosts files for all AbstractSSHHosts.""" |
| 1104 | for host in self.hosts: |
| 1105 | if isinstance(host, abstract_ssh.AbstractSSHHost): |
| 1106 | host.clear_known_hosts() |
| 1107 | |
| 1108 | |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 1109 | class warning_manager(object): |
| 1110 | """Class for controlling warning logs. Manages the enabling and disabling |
| 1111 | of warnings.""" |
| 1112 | def __init__(self): |
| 1113 | # a map of warning types to a list of disabled time intervals |
| 1114 | self.disabled_warnings = {} |
| 1115 | |
| 1116 | |
| 1117 | def is_valid(self, timestamp, warning_type): |
| 1118 | """Indicates if a warning (based on the time it occured and its type) |
| 1119 | is a valid warning. A warning is considered "invalid" if this type of |
| 1120 | warning was marked as "disabled" at the time the warning occured.""" |
| 1121 | disabled_intervals = self.disabled_warnings.get(warning_type, []) |
| 1122 | for start, end in disabled_intervals: |
| 1123 | if timestamp >= start and (end is None or timestamp < end): |
| 1124 | return False |
| 1125 | return True |
| 1126 | |
| 1127 | |
| 1128 | def disable_warnings(self, warning_type, current_time_func=time.time): |
| 1129 | """As of now, disables all further warnings of this type.""" |
| 1130 | intervals = self.disabled_warnings.setdefault(warning_type, []) |
| 1131 | if not intervals or intervals[-1][1] is not None: |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 1132 | intervals.append((int(current_time_func()), None)) |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 1133 | |
| 1134 | |
| 1135 | def enable_warnings(self, warning_type, current_time_func=time.time): |
| 1136 | """As of now, enables all further warnings of this type.""" |
| 1137 | intervals = self.disabled_warnings.get(warning_type, []) |
| 1138 | if intervals and intervals[-1][1] is None: |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 1139 | intervals[-1] = (intervals[-1][0], int(current_time_func())) |
Paul Pendlebury | 5759356 | 2011-06-15 10:45:49 -0700 | [diff] [blame] | 1140 | |
| 1141 | |
| 1142 | # load up site-specific code for generating site-specific job data |
| 1143 | get_site_job_data = utils.import_site_function(__file__, |
| 1144 | "autotest_lib.server.site_server_job", "get_site_job_data", |
| 1145 | _get_site_job_data_dummy) |
| 1146 | |
| 1147 | |
| 1148 | site_server_job = utils.import_site_class( |
| 1149 | __file__, "autotest_lib.server.site_server_job", "site_server_job", |
| 1150 | base_server_job) |
| 1151 | |
| 1152 | |
| 1153 | class server_job(site_server_job): |
Dale Curtis | 456d3c1 | 2011-07-19 11:42:51 -0700 | [diff] [blame] | 1154 | pass |