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