blob: 6c426f4da5a46805f454726dd71debce0c96f422 [file] [log] [blame]
mbligh57e78662008-06-17 19:53:49 +00001"""
2The main job wrapper for the server side.
3
4This is the core infrastructure. Derived from the client side job.py
5
6Copyright Martin J. Bligh, Andy Whitcroft 2007
7"""
8
jadmanski6bb32d72009-03-19 20:25:24 +00009import getpass, os, sys, re, stat, tempfile, time, select, subprocess
mblighfc3da5b2010-01-06 18:37:22 +000010import traceback, shutil, warnings, fcntl, pickle, logging, itertools, errno
showard75cdfee2009-06-10 17:40:41 +000011from autotest_lib.client.bin import sysinfo
mbligh0d0f67d2009-11-06 03:15:03 +000012from autotest_lib.client.common_lib import base_job
mbligh09108442008-10-15 16:27:38 +000013from autotest_lib.client.common_lib import error, log, utils, packages
showard75cdfee2009-06-10 17:40:41 +000014from autotest_lib.client.common_lib import logging_manager
jadmanski043e1132008-11-19 17:10:32 +000015from autotest_lib.server import test, subcommand, profilers
jadmanski10646442008-08-13 14:05:21 +000016from autotest_lib.tko import db as tko_db, status_lib, utils as tko_utils
jadmanski10646442008-08-13 14:05:21 +000017
18
mbligh084bc172008-10-18 14:02:45 +000019def _control_segment_path(name):
20 """Get the pathname of the named control segment file."""
jadmanski10646442008-08-13 14:05:21 +000021 server_dir = os.path.dirname(os.path.abspath(__file__))
mbligh084bc172008-10-18 14:02:45 +000022 return os.path.join(server_dir, "control_segments", name)
jadmanski10646442008-08-13 14:05:21 +000023
24
mbligh084bc172008-10-18 14:02:45 +000025CLIENT_CONTROL_FILENAME = 'control'
26SERVER_CONTROL_FILENAME = 'control.srv'
27MACHINES_FILENAME = '.machines'
jadmanski10646442008-08-13 14:05:21 +000028
mbligh084bc172008-10-18 14:02:45 +000029CLIENT_WRAPPER_CONTROL_FILE = _control_segment_path('client_wrapper')
30CRASHDUMPS_CONTROL_FILE = _control_segment_path('crashdumps')
31CRASHINFO_CONTROL_FILE = _control_segment_path('crashinfo')
mbligh084bc172008-10-18 14:02:45 +000032INSTALL_CONTROL_FILE = _control_segment_path('install')
showard45ae8192008-11-05 19:32:53 +000033CLEANUP_CONTROL_FILE = _control_segment_path('cleanup')
jadmanski10646442008-08-13 14:05:21 +000034
mbligh084bc172008-10-18 14:02:45 +000035VERIFY_CONTROL_FILE = _control_segment_path('verify')
mbligh084bc172008-10-18 14:02:45 +000036REPAIR_CONTROL_FILE = _control_segment_path('repair')
jadmanski10646442008-08-13 14:05:21 +000037
38
mbligh062ed152009-01-13 00:57:14 +000039# by default provide a stub that generates no site data
40def _get_site_job_data_dummy(job):
41 return {}
42
43
jadmanski10646442008-08-13 14:05:21 +000044# load up site-specific code for generating site-specific job data
mbligh062ed152009-01-13 00:57:14 +000045get_site_job_data = utils.import_site_function(__file__,
jadmanskic0a623d2009-03-03 21:11:48 +000046 "autotest_lib.server.site_server_job", "get_site_job_data",
mbligh062ed152009-01-13 00:57:14 +000047 _get_site_job_data_dummy)
jadmanski10646442008-08-13 14:05:21 +000048
49
mbligh0d0f67d2009-11-06 03:15:03 +000050class base_server_job(base_job.base_job):
51 """The server-side concrete implementation of base_job.
jadmanski10646442008-08-13 14:05:21 +000052
mbligh0d0f67d2009-11-06 03:15:03 +000053 Optional properties provided by this implementation:
54 serverdir
55 conmuxdir
56
57 num_tests_run
58 num_tests_failed
59
60 warning_manager
61 warning_loggers
jadmanski10646442008-08-13 14:05:21 +000062 """
63
mbligh0d0f67d2009-11-06 03:15:03 +000064 _STATUS_VERSION = 1
jadmanski10646442008-08-13 14:05:21 +000065
66 def __init__(self, control, args, resultdir, label, user, machines,
67 client=False, parse_job='',
mbligh374f3412009-05-13 21:29:45 +000068 ssh_user='root', ssh_port=22, ssh_pass='',
mblighe7d9c602009-07-02 19:02:33 +000069 group_name='', tag=''):
jadmanski10646442008-08-13 14:05:21 +000070 """
mbligh374f3412009-05-13 21:29:45 +000071 Create a server side job object.
mblighb5dac432008-11-27 00:38:44 +000072
mblighe7d9c602009-07-02 19:02:33 +000073 @param control: The pathname of the control file.
74 @param args: Passed to the control file.
75 @param resultdir: Where to throw the results.
76 @param label: Description of the job.
77 @param user: Username for the job (email address).
78 @param client: True if this is a client-side control file.
79 @param parse_job: string, if supplied it is the job execution tag that
80 the results will be passed through to the TKO parser with.
81 @param ssh_user: The SSH username. [root]
82 @param ssh_port: The SSH port number. [22]
83 @param ssh_pass: The SSH passphrase, if needed.
84 @param group_name: If supplied, this will be written out as
mbligh374f3412009-05-13 21:29:45 +000085 host_group_name in the keyvals file for the parser.
mblighe7d9c602009-07-02 19:02:33 +000086 @param tag: The job execution tag from the scheduler. [optional]
jadmanski10646442008-08-13 14:05:21 +000087 """
mbligh0d0f67d2009-11-06 03:15:03 +000088 super(base_server_job, self).__init__(resultdir=resultdir)
mbligha788dc42009-03-26 21:10:16 +000089
mbligh0d0f67d2009-11-06 03:15:03 +000090 path = os.path.dirname(__file__)
91 self.control = control
92 self._uncollected_log_file = os.path.join(self.resultdir,
93 'uncollected_logs')
94 debugdir = os.path.join(self.resultdir, 'debug')
95 if not os.path.exists(debugdir):
96 os.mkdir(debugdir)
97
98 if user:
99 self.user = user
100 else:
101 self.user = getpass.getuser()
102
103 self._args = args
jadmanski10646442008-08-13 14:05:21 +0000104 self.machines = machines
mbligh0d0f67d2009-11-06 03:15:03 +0000105 self._client = client
106 self._record_prefix = ''
jadmanski10646442008-08-13 14:05:21 +0000107 self.warning_loggers = set()
jadmanskif37df842009-02-11 00:03:26 +0000108 self.warning_manager = warning_manager()
mbligh0d0f67d2009-11-06 03:15:03 +0000109 self._ssh_user = ssh_user
110 self._ssh_port = ssh_port
111 self._ssh_pass = ssh_pass
mblighe7d9c602009-07-02 19:02:33 +0000112 self.tag = tag
mbligh09108442008-10-15 16:27:38 +0000113 self.last_boot_tag = None
jadmanski53aaf382008-11-17 16:22:31 +0000114 self.hosts = set()
mbligh0d0f67d2009-11-06 03:15:03 +0000115 self.drop_caches = False
mblighb5dac432008-11-27 00:38:44 +0000116 self.drop_caches_between_iterations = False
jadmanski10646442008-08-13 14:05:21 +0000117
showard75cdfee2009-06-10 17:40:41 +0000118 self.logging = logging_manager.get_logging_manager(
119 manage_stdout_and_stderr=True, redirect_fds=True)
120 subcommand.logging_manager_object = self.logging
jadmanski10646442008-08-13 14:05:21 +0000121
mbligh0d0f67d2009-11-06 03:15:03 +0000122 self.sysinfo = sysinfo.sysinfo(self.resultdir)
jadmanski043e1132008-11-19 17:10:32 +0000123 self.profilers = profilers.profilers(self)
jadmanskic09fc152008-10-15 17:56:59 +0000124
jadmanski10646442008-08-13 14:05:21 +0000125 job_data = {'label' : label, 'user' : user,
126 'hostname' : ','.join(machines),
mbligh0d0f67d2009-11-06 03:15:03 +0000127 'status_version' : str(self._STATUS_VERSION),
showard170873e2009-01-07 00:22:26 +0000128 'job_started' : str(int(time.time()))}
mbligh374f3412009-05-13 21:29:45 +0000129 if group_name:
130 job_data['host_group_name'] = group_name
jadmanski10646442008-08-13 14:05:21 +0000131
mbligh0d0f67d2009-11-06 03:15:03 +0000132 # only write these keyvals out on the first job in a resultdir
133 if 'job_started' not in utils.read_keyval(self.resultdir):
134 job_data.update(get_site_job_data(self))
135 utils.write_keyval(self.resultdir, job_data)
136
137 self._parse_job = parse_job
mbligh4608b002010-01-05 18:22:35 +0000138 self._using_parser = (self._parse_job and len(machines) == 1)
mbligh0d0f67d2009-11-06 03:15:03 +0000139 self.pkgmgr = packages.PackageManager(
140 self.autodir, run_function_dargs={'timeout':600})
showard21baa452008-10-21 00:08:39 +0000141 self.num_tests_run = 0
142 self.num_tests_failed = 0
143
jadmanski550fdc22008-11-20 16:32:08 +0000144 self._register_subcommand_hooks()
145
mbligh0d0f67d2009-11-06 03:15:03 +0000146 # these components aren't usable on the server
147 self.bootloader = None
148 self.harness = None
149
150
151 @classmethod
152 def _find_base_directories(cls):
153 """
154 Determine locations of autodir, clientdir and serverdir. Assumes
155 that this file is located within serverdir and uses __file__ along
156 with relative paths to resolve the location.
157 """
158 serverdir = os.path.abspath(os.path.dirname(__file__))
159 autodir = os.path.normpath(os.path.join(serverdir, '..'))
160 clientdir = os.path.join(autodir, 'client')
161 return autodir, clientdir, serverdir
162
163
164 def _find_resultdir(self, resultdir):
165 """
166 Determine the location of resultdir. For server jobs we expect one to
167 always be explicitly passed in to __init__, so just return that.
168 """
169 if resultdir:
170 return os.path.normpath(resultdir)
171 else:
172 return None
173
jadmanski550fdc22008-11-20 16:32:08 +0000174
jadmanskie432dd22009-01-30 15:04:51 +0000175 @staticmethod
176 def _load_control_file(path):
177 f = open(path)
178 try:
179 control_file = f.read()
180 finally:
181 f.close()
182 return re.sub('\r', '', control_file)
183
184
jadmanski550fdc22008-11-20 16:32:08 +0000185 def _register_subcommand_hooks(self):
mbligh2b92b862008-11-22 13:25:32 +0000186 """
187 Register some hooks into the subcommand modules that allow us
188 to properly clean up self.hosts created in forked subprocesses.
189 """
jadmanski550fdc22008-11-20 16:32:08 +0000190 def on_fork(cmd):
191 self._existing_hosts_on_fork = set(self.hosts)
192 def on_join(cmd):
193 new_hosts = self.hosts - self._existing_hosts_on_fork
194 for host in new_hosts:
195 host.close()
196 subcommand.subcommand.register_fork_hook(on_fork)
197 subcommand.subcommand.register_join_hook(on_join)
198
jadmanski10646442008-08-13 14:05:21 +0000199
mbligh4608b002010-01-05 18:22:35 +0000200 def init_parser(self):
mbligh2b92b862008-11-22 13:25:32 +0000201 """
mbligh4608b002010-01-05 18:22:35 +0000202 Start the continuous parsing of self.resultdir. This sets up
jadmanski10646442008-08-13 14:05:21 +0000203 the database connection and inserts the basic job object into
mbligh2b92b862008-11-22 13:25:32 +0000204 the database if necessary.
205 """
mbligh4608b002010-01-05 18:22:35 +0000206 if not self._using_parser:
207 return
jadmanski10646442008-08-13 14:05:21 +0000208 # redirect parser debugging to .parse.log
mbligh4608b002010-01-05 18:22:35 +0000209 parse_log = os.path.join(self.resultdir, '.parse.log')
jadmanski10646442008-08-13 14:05:21 +0000210 parse_log = open(parse_log, 'w', 0)
211 tko_utils.redirect_parser_debugging(parse_log)
212 # create a job model object and set up the db
213 self.results_db = tko_db.db(autocommit=True)
mbligh0d0f67d2009-11-06 03:15:03 +0000214 self.parser = status_lib.parser(self._STATUS_VERSION)
mbligh4608b002010-01-05 18:22:35 +0000215 self.job_model = self.parser.make_job(self.resultdir)
jadmanski10646442008-08-13 14:05:21 +0000216 self.parser.start(self.job_model)
217 # check if a job already exists in the db and insert it if
218 # it does not
mbligh0d0f67d2009-11-06 03:15:03 +0000219 job_idx = self.results_db.find_job(self._parse_job)
jadmanski10646442008-08-13 14:05:21 +0000220 if job_idx is None:
mbligh0d0f67d2009-11-06 03:15:03 +0000221 self.results_db.insert_job(self._parse_job, self.job_model)
jadmanski10646442008-08-13 14:05:21 +0000222 else:
mbligh2b92b862008-11-22 13:25:32 +0000223 machine_idx = self.results_db.lookup_machine(self.job_model.machine)
jadmanski10646442008-08-13 14:05:21 +0000224 self.job_model.index = job_idx
225 self.job_model.machine_idx = machine_idx
226
227
228 def cleanup_parser(self):
mbligh2b92b862008-11-22 13:25:32 +0000229 """
230 This should be called after the server job is finished
jadmanski10646442008-08-13 14:05:21 +0000231 to carry out any remaining cleanup (e.g. flushing any
mbligh2b92b862008-11-22 13:25:32 +0000232 remaining test results to the results db)
233 """
mbligh0d0f67d2009-11-06 03:15:03 +0000234 if not self._using_parser:
jadmanski10646442008-08-13 14:05:21 +0000235 return
236 final_tests = self.parser.end()
237 for test in final_tests:
238 self.__insert_test(test)
mbligh0d0f67d2009-11-06 03:15:03 +0000239 self._using_parser = False
jadmanski10646442008-08-13 14:05:21 +0000240
241
242 def verify(self):
243 if not self.machines:
mbligh084bc172008-10-18 14:02:45 +0000244 raise error.AutoservError('No machines specified to verify')
mbligh0fce4112008-11-27 00:37:17 +0000245 if self.resultdir:
246 os.chdir(self.resultdir)
jadmanski10646442008-08-13 14:05:21 +0000247 try:
jadmanskicdd0c402008-09-19 21:21:31 +0000248 namespace = {'machines' : self.machines, 'job' : self,
mbligh0d0f67d2009-11-06 03:15:03 +0000249 'ssh_user' : self._ssh_user,
250 'ssh_port' : self._ssh_port,
251 'ssh_pass' : self._ssh_pass}
mbligh084bc172008-10-18 14:02:45 +0000252 self._execute_code(VERIFY_CONTROL_FILE, namespace, protect=False)
jadmanski10646442008-08-13 14:05:21 +0000253 except Exception, e:
mbligh2b92b862008-11-22 13:25:32 +0000254 msg = ('Verify failed\n' + str(e) + '\n' + traceback.format_exc())
jadmanski10646442008-08-13 14:05:21 +0000255 self.record('ABORT', None, None, msg)
256 raise
257
258
259 def repair(self, host_protection):
260 if not self.machines:
261 raise error.AutoservError('No machines specified to repair')
mbligh0fce4112008-11-27 00:37:17 +0000262 if self.resultdir:
263 os.chdir(self.resultdir)
jadmanski10646442008-08-13 14:05:21 +0000264 namespace = {'machines': self.machines, 'job': self,
mbligh0d0f67d2009-11-06 03:15:03 +0000265 'ssh_user': self._ssh_user, 'ssh_port': self._ssh_port,
266 'ssh_pass': self._ssh_pass,
jadmanski10646442008-08-13 14:05:21 +0000267 'protection_level': host_protection}
mbligh25c0b8c2009-01-24 01:44:17 +0000268
mbligh0931b0a2009-04-08 17:44:48 +0000269 self._execute_code(REPAIR_CONTROL_FILE, namespace, protect=False)
jadmanski10646442008-08-13 14:05:21 +0000270
271
272 def precheck(self):
273 """
274 perform any additional checks in derived classes.
275 """
276 pass
277
278
279 def enable_external_logging(self):
mbligh2b92b862008-11-22 13:25:32 +0000280 """
281 Start or restart external logging mechanism.
jadmanski10646442008-08-13 14:05:21 +0000282 """
283 pass
284
285
286 def disable_external_logging(self):
mbligh2b92b862008-11-22 13:25:32 +0000287 """
288 Pause or stop external logging mechanism.
jadmanski10646442008-08-13 14:05:21 +0000289 """
290 pass
291
292
293 def use_external_logging(self):
mbligh2b92b862008-11-22 13:25:32 +0000294 """
295 Return True if external logging should be used.
jadmanski10646442008-08-13 14:05:21 +0000296 """
297 return False
298
299
mbligh415dc212009-06-15 21:53:34 +0000300 def _make_parallel_wrapper(self, function, machines, log):
301 """Wrap function as appropriate for calling by parallel_simple."""
mbligh2b92b862008-11-22 13:25:32 +0000302 is_forking = not (len(machines) == 1 and self.machines == machines)
mbligh0d0f67d2009-11-06 03:15:03 +0000303 if self._parse_job and is_forking and log:
jadmanski10646442008-08-13 14:05:21 +0000304 def wrapper(machine):
mbligh0d0f67d2009-11-06 03:15:03 +0000305 self._parse_job += "/" + machine
306 self._using_parser = True
jadmanski10646442008-08-13 14:05:21 +0000307 self.machines = [machine]
mbligh0d0f67d2009-11-06 03:15:03 +0000308 self.push_execution_context(machine)
jadmanski609a5f42008-08-26 20:52:42 +0000309 os.chdir(self.resultdir)
showard2bab8f42008-11-12 18:15:22 +0000310 utils.write_keyval(self.resultdir, {"hostname": machine})
mbligh4608b002010-01-05 18:22:35 +0000311 self.init_parser()
jadmanski10646442008-08-13 14:05:21 +0000312 result = function(machine)
313 self.cleanup_parser()
314 return result
jadmanski4dd1a002008-09-05 20:27:30 +0000315 elif len(machines) > 1 and log:
jadmanski10646442008-08-13 14:05:21 +0000316 def wrapper(machine):
mbligh0d0f67d2009-11-06 03:15:03 +0000317 self.push_execution_context(machine)
jadmanski609a5f42008-08-26 20:52:42 +0000318 os.chdir(self.resultdir)
mbligh838d82d2009-03-11 17:14:31 +0000319 machine_data = {'hostname' : machine,
mbligh0d0f67d2009-11-06 03:15:03 +0000320 'status_version' : str(self._STATUS_VERSION)}
mbligh838d82d2009-03-11 17:14:31 +0000321 utils.write_keyval(self.resultdir, machine_data)
jadmanski10646442008-08-13 14:05:21 +0000322 result = function(machine)
323 return result
324 else:
325 wrapper = function
mbligh415dc212009-06-15 21:53:34 +0000326 return wrapper
327
328
329 def parallel_simple(self, function, machines, log=True, timeout=None,
330 return_results=False):
331 """
332 Run 'function' using parallel_simple, with an extra wrapper to handle
333 the necessary setup for continuous parsing, if possible. If continuous
334 parsing is already properly initialized then this should just work.
335
336 @param function: A callable to run in parallel given each machine.
337 @param machines: A list of machine names to be passed one per subcommand
338 invocation of function.
339 @param log: If True, output will be written to output in a subdirectory
340 named after each machine.
341 @param timeout: Seconds after which the function call should timeout.
342 @param return_results: If True instead of an AutoServError being raised
343 on any error a list of the results|exceptions from the function
344 called on each arg is returned. [default: False]
345
346 @raises error.AutotestError: If any of the functions failed.
347 """
348 wrapper = self._make_parallel_wrapper(function, machines, log)
349 return subcommand.parallel_simple(wrapper, machines,
350 log=log, timeout=timeout,
351 return_results=return_results)
352
353
354 def parallel_on_machines(self, function, machines, timeout=None):
355 """
showardcd5fac42009-07-06 20:19:43 +0000356 @param function: Called in parallel with one machine as its argument.
mbligh415dc212009-06-15 21:53:34 +0000357 @param machines: A list of machines to call function(machine) on.
358 @param timeout: Seconds after which the function call should timeout.
359
360 @returns A list of machines on which function(machine) returned
361 without raising an exception.
362 """
showardcd5fac42009-07-06 20:19:43 +0000363 results = self.parallel_simple(function, machines, timeout=timeout,
mbligh415dc212009-06-15 21:53:34 +0000364 return_results=True)
365 success_machines = []
366 for result, machine in itertools.izip(results, machines):
367 if not isinstance(result, Exception):
368 success_machines.append(machine)
369 return success_machines
jadmanski10646442008-08-13 14:05:21 +0000370
371
mbligh0d0f67d2009-11-06 03:15:03 +0000372 _USE_TEMP_DIR = object()
mbligh2b92b862008-11-22 13:25:32 +0000373 def run(self, cleanup=False, install_before=False, install_after=False,
jadmanskie432dd22009-01-30 15:04:51 +0000374 collect_crashdumps=True, namespace={}, control=None,
jadmanskidef0c3c2009-03-25 20:07:10 +0000375 control_file_dir=None, only_collect_crashinfo=False):
jadmanskifb9c0fa2009-04-29 17:39:16 +0000376 # for a normal job, make sure the uncollected logs file exists
377 # for a crashinfo-only run it should already exist, bail out otherwise
mbligh0d0f67d2009-11-06 03:15:03 +0000378 if self.resultdir and not os.path.exists(self._uncollected_log_file):
jadmanskifb9c0fa2009-04-29 17:39:16 +0000379 if only_collect_crashinfo:
380 # if this is a crashinfo-only run, and there were no existing
381 # uncollected logs, just bail out early
382 logging.info("No existing uncollected logs, "
383 "skipping crashinfo collection")
384 return
385 else:
mbligh0d0f67d2009-11-06 03:15:03 +0000386 log_file = open(self._uncollected_log_file, "w")
jadmanskifb9c0fa2009-04-29 17:39:16 +0000387 pickle.dump([], log_file)
388 log_file.close()
389
jadmanski10646442008-08-13 14:05:21 +0000390 # use a copy so changes don't affect the original dictionary
391 namespace = namespace.copy()
392 machines = self.machines
jadmanskie432dd22009-01-30 15:04:51 +0000393 if control is None:
jadmanski02a3ba22009-11-13 20:47:27 +0000394 if self.control is None:
395 control = ''
396 else:
397 control = self._load_control_file(self.control)
jadmanskie432dd22009-01-30 15:04:51 +0000398 if control_file_dir is None:
399 control_file_dir = self.resultdir
jadmanski10646442008-08-13 14:05:21 +0000400
401 self.aborted = False
402 namespace['machines'] = machines
mbligh0d0f67d2009-11-06 03:15:03 +0000403 namespace['args'] = self._args
jadmanski10646442008-08-13 14:05:21 +0000404 namespace['job'] = self
mbligh0d0f67d2009-11-06 03:15:03 +0000405 namespace['ssh_user'] = self._ssh_user
406 namespace['ssh_port'] = self._ssh_port
407 namespace['ssh_pass'] = self._ssh_pass
jadmanski10646442008-08-13 14:05:21 +0000408 test_start_time = int(time.time())
409
mbligh80e1eba2008-11-19 00:26:18 +0000410 if self.resultdir:
411 os.chdir(self.resultdir)
jadmanski779bd292009-03-19 17:33:33 +0000412 # touch status.log so that the parser knows a job is running here
jadmanski382303a2009-04-21 19:53:39 +0000413 open(self.get_status_log_path(), 'a').close()
mbligh80e1eba2008-11-19 00:26:18 +0000414 self.enable_external_logging()
jadmanskie432dd22009-01-30 15:04:51 +0000415
jadmanskicdd0c402008-09-19 21:21:31 +0000416 collect_crashinfo = True
mblighaebe3b62008-12-22 14:45:40 +0000417 temp_control_file_dir = None
jadmanski10646442008-08-13 14:05:21 +0000418 try:
showardcf8d4922009-10-14 16:08:39 +0000419 try:
420 if install_before and machines:
421 self._execute_code(INSTALL_CONTROL_FILE, namespace)
jadmanskie432dd22009-01-30 15:04:51 +0000422
showardcf8d4922009-10-14 16:08:39 +0000423 if only_collect_crashinfo:
424 return
425
jadmanskidef0c3c2009-03-25 20:07:10 +0000426 # determine the dir to write the control files to
427 cfd_specified = (control_file_dir
mbligh0d0f67d2009-11-06 03:15:03 +0000428 and control_file_dir is not self._USE_TEMP_DIR)
jadmanskidef0c3c2009-03-25 20:07:10 +0000429 if cfd_specified:
430 temp_control_file_dir = None
431 else:
432 temp_control_file_dir = tempfile.mkdtemp(
433 suffix='temp_control_file_dir')
434 control_file_dir = temp_control_file_dir
435 server_control_file = os.path.join(control_file_dir,
436 SERVER_CONTROL_FILENAME)
437 client_control_file = os.path.join(control_file_dir,
438 CLIENT_CONTROL_FILENAME)
mbligh0d0f67d2009-11-06 03:15:03 +0000439 if self._client:
jadmanskidef0c3c2009-03-25 20:07:10 +0000440 namespace['control'] = control
441 utils.open_write_close(client_control_file, control)
mblighfeac0102009-04-28 18:31:12 +0000442 shutil.copyfile(CLIENT_WRAPPER_CONTROL_FILE,
443 server_control_file)
jadmanskidef0c3c2009-03-25 20:07:10 +0000444 else:
445 utils.open_write_close(server_control_file, control)
mbligh26f0d882009-06-22 18:30:01 +0000446 logging.info("Processing control file")
jadmanskidef0c3c2009-03-25 20:07:10 +0000447 self._execute_code(server_control_file, namespace)
mbligh26f0d882009-06-22 18:30:01 +0000448 logging.info("Finished processing control file")
jadmanski10646442008-08-13 14:05:21 +0000449
jadmanskidef0c3c2009-03-25 20:07:10 +0000450 # no error occured, so we don't need to collect crashinfo
451 collect_crashinfo = False
showardcf8d4922009-10-14 16:08:39 +0000452 except:
453 try:
454 logging.exception(
455 'Exception escaped control file, job aborting:')
456 except:
457 pass # don't let logging exceptions here interfere
458 raise
jadmanski10646442008-08-13 14:05:21 +0000459 finally:
mblighaebe3b62008-12-22 14:45:40 +0000460 if temp_control_file_dir:
jadmanskie432dd22009-01-30 15:04:51 +0000461 # Clean up temp directory used for copies of the control files
mblighaebe3b62008-12-22 14:45:40 +0000462 try:
463 shutil.rmtree(temp_control_file_dir)
464 except Exception, e:
mblighe7d9c602009-07-02 19:02:33 +0000465 logging.warn('Could not remove temp directory %s: %s',
466 temp_control_file_dir, e)
jadmanskie432dd22009-01-30 15:04:51 +0000467
jadmanskicdd0c402008-09-19 21:21:31 +0000468 if machines and (collect_crashdumps or collect_crashinfo):
jadmanski10646442008-08-13 14:05:21 +0000469 namespace['test_start_time'] = test_start_time
jadmanskicdd0c402008-09-19 21:21:31 +0000470 if collect_crashinfo:
mbligh084bc172008-10-18 14:02:45 +0000471 # includes crashdumps
472 self._execute_code(CRASHINFO_CONTROL_FILE, namespace)
jadmanskicdd0c402008-09-19 21:21:31 +0000473 else:
mbligh084bc172008-10-18 14:02:45 +0000474 self._execute_code(CRASHDUMPS_CONTROL_FILE, namespace)
mbligh0d0f67d2009-11-06 03:15:03 +0000475 if self._uncollected_log_file:
476 os.remove(self._uncollected_log_file)
jadmanski10646442008-08-13 14:05:21 +0000477 self.disable_external_logging()
showard45ae8192008-11-05 19:32:53 +0000478 if cleanup and machines:
479 self._execute_code(CLEANUP_CONTROL_FILE, namespace)
jadmanski10646442008-08-13 14:05:21 +0000480 if install_after and machines:
mbligh084bc172008-10-18 14:02:45 +0000481 self._execute_code(INSTALL_CONTROL_FILE, namespace)
jadmanski10646442008-08-13 14:05:21 +0000482
483
484 def run_test(self, url, *args, **dargs):
mbligh2b92b862008-11-22 13:25:32 +0000485 """
486 Summon a test object and run it.
jadmanski10646442008-08-13 14:05:21 +0000487
488 tag
489 tag to add to testname
490 url
491 url of the test to run
492 """
mblighfc3da5b2010-01-06 18:37:22 +0000493 group, testname = self.pkgmgr.get_package_name(url, 'test')
494 testname, subdir, tag = self._build_tagged_test_name(testname, dargs)
495 outputdir = self._make_test_outputdir(subdir)
jadmanski10646442008-08-13 14:05:21 +0000496
497 def group_func():
498 try:
499 test.runtest(self, url, tag, args, dargs)
500 except error.TestBaseException, e:
501 self.record(e.exit_status, subdir, testname, str(e))
502 raise
503 except Exception, e:
504 info = str(e) + "\n" + traceback.format_exc()
505 self.record('FAIL', subdir, testname, info)
506 raise
507 else:
mbligh2b92b862008-11-22 13:25:32 +0000508 self.record('GOOD', subdir, testname, 'completed successfully')
jadmanskide292df2008-08-26 20:51:14 +0000509
510 result, exc_info = self._run_group(testname, subdir, group_func)
511 if exc_info and isinstance(exc_info[1], error.TestBaseException):
512 return False
513 elif exc_info:
514 raise exc_info[0], exc_info[1], exc_info[2]
515 else:
516 return True
jadmanski10646442008-08-13 14:05:21 +0000517
518
519 def _run_group(self, name, subdir, function, *args, **dargs):
520 """\
521 Underlying method for running something inside of a group.
522 """
jadmanskide292df2008-08-26 20:51:14 +0000523 result, exc_info = None, None
mbligh0d0f67d2009-11-06 03:15:03 +0000524 old_record_prefix = self._record_prefix
jadmanski10646442008-08-13 14:05:21 +0000525 try:
526 self.record('START', subdir, name)
mbligh0d0f67d2009-11-06 03:15:03 +0000527 self._record_prefix += '\t'
jadmanski10646442008-08-13 14:05:21 +0000528 try:
529 result = function(*args, **dargs)
530 finally:
mbligh0d0f67d2009-11-06 03:15:03 +0000531 self._record_prefix = old_record_prefix
jadmanski10646442008-08-13 14:05:21 +0000532 except error.TestBaseException, e:
jadmanskib88d6dc2009-01-10 00:33:18 +0000533 self.record("END %s" % e.exit_status, subdir, name)
jadmanskide292df2008-08-26 20:51:14 +0000534 exc_info = sys.exc_info()
jadmanski10646442008-08-13 14:05:21 +0000535 except Exception, e:
536 err_msg = str(e) + '\n'
537 err_msg += traceback.format_exc()
538 self.record('END ABORT', subdir, name, err_msg)
539 raise error.JobError(name + ' failed\n' + traceback.format_exc())
540 else:
541 self.record('END GOOD', subdir, name)
542
jadmanskide292df2008-08-26 20:51:14 +0000543 return result, exc_info
jadmanski10646442008-08-13 14:05:21 +0000544
545
546 def run_group(self, function, *args, **dargs):
547 """\
548 function:
549 subroutine to run
550 *args:
551 arguments for the function
552 """
553
554 name = function.__name__
555
556 # Allow the tag for the group to be specified.
557 tag = dargs.pop('tag', None)
558 if tag:
559 name = tag
560
jadmanskide292df2008-08-26 20:51:14 +0000561 return self._run_group(name, None, function, *args, **dargs)[0]
jadmanski10646442008-08-13 14:05:21 +0000562
563
564 def run_reboot(self, reboot_func, get_kernel_func):
565 """\
566 A specialization of run_group meant specifically for handling
567 a reboot. Includes support for capturing the kernel version
568 after the reboot.
569
570 reboot_func: a function that carries out the reboot
571
572 get_kernel_func: a function that returns a string
573 representing the kernel version.
574 """
575
mbligh0d0f67d2009-11-06 03:15:03 +0000576 old_record_prefix = self._record_prefix
jadmanski10646442008-08-13 14:05:21 +0000577 try:
578 self.record('START', None, 'reboot')
mbligh0d0f67d2009-11-06 03:15:03 +0000579 self._record_prefix += '\t'
jadmanski10646442008-08-13 14:05:21 +0000580 reboot_func()
581 except Exception, e:
mbligh0d0f67d2009-11-06 03:15:03 +0000582 self._record_prefix = old_record_prefix
jadmanski10646442008-08-13 14:05:21 +0000583 err_msg = str(e) + '\n' + traceback.format_exc()
584 self.record('END FAIL', None, 'reboot', err_msg)
jadmanski4b51d542009-04-08 14:17:16 +0000585 raise
jadmanski10646442008-08-13 14:05:21 +0000586 else:
587 kernel = get_kernel_func()
mbligh0d0f67d2009-11-06 03:15:03 +0000588 self._record_prefix = old_record_prefix
jadmanski10646442008-08-13 14:05:21 +0000589 self.record('END GOOD', None, 'reboot',
590 optional_fields={"kernel": kernel})
591
592
jadmanskie432dd22009-01-30 15:04:51 +0000593 def run_control(self, path):
594 """Execute a control file found at path (relative to the autotest
595 path). Intended for executing a control file within a control file,
596 not for running the top-level job control file."""
597 path = os.path.join(self.autodir, path)
598 control_file = self._load_control_file(path)
mbligh0d0f67d2009-11-06 03:15:03 +0000599 self.run(control=control_file, control_file_dir=self._USE_TEMP_DIR)
jadmanskie432dd22009-01-30 15:04:51 +0000600
601
jadmanskic09fc152008-10-15 17:56:59 +0000602 def add_sysinfo_command(self, command, logfile=None, on_every_test=False):
mbligh4395bbd2009-03-25 19:34:17 +0000603 self._add_sysinfo_loggable(sysinfo.command(command, logf=logfile),
jadmanskic09fc152008-10-15 17:56:59 +0000604 on_every_test)
605
606
607 def add_sysinfo_logfile(self, file, on_every_test=False):
608 self._add_sysinfo_loggable(sysinfo.logfile(file), on_every_test)
609
610
611 def _add_sysinfo_loggable(self, loggable, on_every_test):
612 if on_every_test:
613 self.sysinfo.test_loggables.add(loggable)
614 else:
615 self.sysinfo.boot_loggables.add(loggable)
616
617
jadmanski10646442008-08-13 14:05:21 +0000618 def record(self, status_code, subdir, operation, status='',
619 optional_fields=None):
620 """
621 Record job-level status
622
623 The intent is to make this file both machine parseable and
624 human readable. That involves a little more complexity, but
625 really isn't all that bad ;-)
626
627 Format is <status code>\t<subdir>\t<operation>\t<status>
628
mbligh1b3b3762008-09-25 02:46:34 +0000629 status code: see common_lib.log.is_valid_status()
jadmanski10646442008-08-13 14:05:21 +0000630 for valid status definition
631
632 subdir: MUST be a relevant subdirectory in the results,
633 or None, which will be represented as '----'
634
635 operation: description of what you ran (e.g. "dbench", or
636 "mkfs -t foobar /dev/sda9")
637
638 status: error message or "completed sucessfully"
639
640 ------------------------------------------------------------
641
642 Initial tabs indicate indent levels for grouping, and is
mbligh0d0f67d2009-11-06 03:15:03 +0000643 governed by self._record_prefix
jadmanski10646442008-08-13 14:05:21 +0000644
645 multiline messages have secondary lines prefaced by a double
646 space (' ')
647
648 Executing this method will trigger the logging of all new
649 warnings to date from the various console loggers.
650 """
651 # poll all our warning loggers for new warnings
652 warnings = self._read_warnings()
mbligh0d0f67d2009-11-06 03:15:03 +0000653 old_record_prefix = self._record_prefix
jadmanski2de83112009-04-01 18:21:04 +0000654 try:
655 if status_code.startswith("END "):
mbligh0d0f67d2009-11-06 03:15:03 +0000656 self._record_prefix += "\t"
jadmanski2de83112009-04-01 18:21:04 +0000657 for timestamp, msg in warnings:
658 self._record("WARN", None, None, msg, timestamp)
659 finally:
mbligh0d0f67d2009-11-06 03:15:03 +0000660 self._record_prefix = old_record_prefix
jadmanski10646442008-08-13 14:05:21 +0000661
662 # write out the actual status log line
663 self._record(status_code, subdir, operation, status,
664 optional_fields=optional_fields)
665
666
667 def _read_warnings(self):
jadmanskif37df842009-02-11 00:03:26 +0000668 """Poll all the warning loggers and extract any new warnings that have
669 been logged. If the warnings belong to a category that is currently
670 disabled, this method will discard them and they will no longer be
671 retrievable.
672
673 Returns a list of (timestamp, message) tuples, where timestamp is an
674 integer epoch timestamp."""
jadmanski10646442008-08-13 14:05:21 +0000675 warnings = []
676 while True:
677 # pull in a line of output from every logger that has
678 # output ready to be read
mbligh2b92b862008-11-22 13:25:32 +0000679 loggers, _, _ = select.select(self.warning_loggers, [], [], 0)
jadmanski10646442008-08-13 14:05:21 +0000680 closed_loggers = set()
681 for logger in loggers:
682 line = logger.readline()
683 # record any broken pipes (aka line == empty)
684 if len(line) == 0:
685 closed_loggers.add(logger)
686 continue
jadmanskif37df842009-02-11 00:03:26 +0000687 # parse out the warning
688 timestamp, msgtype, msg = line.split('\t', 2)
689 timestamp = int(timestamp)
690 # if the warning is valid, add it to the results
691 if self.warning_manager.is_valid(timestamp, msgtype):
692 warnings.append((timestamp, msg.strip()))
jadmanski10646442008-08-13 14:05:21 +0000693
694 # stop listening to loggers that are closed
695 self.warning_loggers -= closed_loggers
696
697 # stop if none of the loggers have any output left
698 if not loggers:
699 break
700
701 # sort into timestamp order
702 warnings.sort()
703 return warnings
704
705
jadmanski16a7ff72009-04-01 18:19:53 +0000706 def disable_warnings(self, warning_type):
jadmanskif37df842009-02-11 00:03:26 +0000707 self.warning_manager.disable_warnings(warning_type)
jadmanski16a7ff72009-04-01 18:19:53 +0000708 self.record("INFO", None, None,
709 "disabling %s warnings" % warning_type,
710 {"warnings.disable": warning_type})
jadmanskif37df842009-02-11 00:03:26 +0000711
712
jadmanski16a7ff72009-04-01 18:19:53 +0000713 def enable_warnings(self, warning_type):
jadmanskif37df842009-02-11 00:03:26 +0000714 self.warning_manager.enable_warnings(warning_type)
jadmanski16a7ff72009-04-01 18:19:53 +0000715 self.record("INFO", None, None,
716 "enabling %s warnings" % warning_type,
717 {"warnings.enable": warning_type})
jadmanskif37df842009-02-11 00:03:26 +0000718
719
jadmanski779bd292009-03-19 17:33:33 +0000720 def get_status_log_path(self, subdir=None):
721 """Return the path to the job status log.
722
723 @param subdir - Optional paramter indicating that you want the path
724 to a subdirectory status log.
725
726 @returns The path where the status log should be.
727 """
mbligh210bae62009-04-01 18:33:13 +0000728 if self.resultdir:
729 if subdir:
730 return os.path.join(self.resultdir, subdir, "status.log")
731 else:
732 return os.path.join(self.resultdir, "status.log")
jadmanski779bd292009-03-19 17:33:33 +0000733 else:
mbligh210bae62009-04-01 18:33:13 +0000734 return None
jadmanski779bd292009-03-19 17:33:33 +0000735
736
jadmanski6bb32d72009-03-19 20:25:24 +0000737 def _update_uncollected_logs_list(self, update_func):
738 """Updates the uncollected logs list in a multi-process safe manner.
739
740 @param update_func - a function that updates the list of uncollected
741 logs. Should take one parameter, the list to be updated.
742 """
mbligh0d0f67d2009-11-06 03:15:03 +0000743 if self._uncollected_log_file:
744 log_file = open(self._uncollected_log_file, "r+")
mbligha788dc42009-03-26 21:10:16 +0000745 fcntl.flock(log_file, fcntl.LOCK_EX)
jadmanski6bb32d72009-03-19 20:25:24 +0000746 try:
747 uncollected_logs = pickle.load(log_file)
748 update_func(uncollected_logs)
749 log_file.seek(0)
750 log_file.truncate()
751 pickle.dump(uncollected_logs, log_file)
jadmanski3bff9092009-04-22 18:09:47 +0000752 log_file.flush()
jadmanski6bb32d72009-03-19 20:25:24 +0000753 finally:
754 fcntl.flock(log_file, fcntl.LOCK_UN)
755 log_file.close()
756
757
758 def add_client_log(self, hostname, remote_path, local_path):
759 """Adds a new set of client logs to the list of uncollected logs,
760 to allow for future log recovery.
761
762 @param host - the hostname of the machine holding the logs
763 @param remote_path - the directory on the remote machine holding logs
764 @param local_path - the local directory to copy the logs into
765 """
766 def update_func(logs_list):
767 logs_list.append((hostname, remote_path, local_path))
768 self._update_uncollected_logs_list(update_func)
769
770
771 def remove_client_log(self, hostname, remote_path, local_path):
772 """Removes a set of client logs from the list of uncollected logs,
773 to allow for future log recovery.
774
775 @param host - the hostname of the machine holding the logs
776 @param remote_path - the directory on the remote machine holding logs
777 @param local_path - the local directory to copy the logs into
778 """
779 def update_func(logs_list):
780 logs_list.remove((hostname, remote_path, local_path))
781 self._update_uncollected_logs_list(update_func)
782
783
mbligh0d0f67d2009-11-06 03:15:03 +0000784 def get_client_logs(self):
785 """Retrieves the list of uncollected logs, if it exists.
786
787 @returns A list of (host, remote_path, local_path) tuples. Returns
788 an empty list if no uncollected logs file exists.
789 """
790 log_exists = (self._uncollected_log_file and
791 os.path.exists(self._uncollected_log_file))
792 if log_exists:
793 return pickle.load(open(self._uncollected_log_file))
794 else:
795 return []
796
797
jadmanski10646442008-08-13 14:05:21 +0000798 def _render_record(self, status_code, subdir, operation, status='',
799 epoch_time=None, record_prefix=None,
800 optional_fields=None):
801 """
802 Internal Function to generate a record to be written into a
803 status log. For use by server_job.* classes only.
804 """
805 if subdir:
806 if re.match(r'[\n\t]', subdir):
mbligh2b92b862008-11-22 13:25:32 +0000807 raise ValueError('Invalid character in subdir string')
jadmanski10646442008-08-13 14:05:21 +0000808 substr = subdir
809 else:
810 substr = '----'
811
mbligh1b3b3762008-09-25 02:46:34 +0000812 if not log.is_valid_status(status_code):
mbligh2b92b862008-11-22 13:25:32 +0000813 raise ValueError('Invalid status code supplied: %s' % status_code)
jadmanski10646442008-08-13 14:05:21 +0000814 if not operation:
815 operation = '----'
816 if re.match(r'[\n\t]', operation):
mbligh2b92b862008-11-22 13:25:32 +0000817 raise ValueError('Invalid character in operation string')
jadmanski10646442008-08-13 14:05:21 +0000818 operation = operation.rstrip()
819 status = status.rstrip()
820 status = re.sub(r"\t", " ", status)
821 # Ensure any continuation lines are marked so we can
822 # detect them in the status file to ensure it is parsable.
mbligh0d0f67d2009-11-06 03:15:03 +0000823 status = re.sub(r"\n", "\n" + self._record_prefix + " ", status)
jadmanski10646442008-08-13 14:05:21 +0000824
825 if not optional_fields:
826 optional_fields = {}
827
828 # Generate timestamps for inclusion in the logs
829 if epoch_time is None:
830 epoch_time = int(time.time())
831 local_time = time.localtime(epoch_time)
832 optional_fields["timestamp"] = str(epoch_time)
833 optional_fields["localtime"] = time.strftime("%b %d %H:%M:%S",
834 local_time)
835
836 fields = [status_code, substr, operation]
837 fields += ["%s=%s" % x for x in optional_fields.iteritems()]
838 fields.append(status)
839
840 if record_prefix is None:
mbligh0d0f67d2009-11-06 03:15:03 +0000841 record_prefix = self._record_prefix
jadmanski10646442008-08-13 14:05:21 +0000842
843 msg = '\t'.join(str(x) for x in fields)
jadmanski10646442008-08-13 14:05:21 +0000844 return record_prefix + msg + '\n'
845
846
847 def _record_prerendered(self, msg):
848 """
849 Record a pre-rendered msg into the status logs. The only
850 change this makes to the message is to add on the local
851 indentation. Should not be called outside of server_job.*
852 classes. Unlike _record, this does not write the message
853 to standard output.
854 """
855 lines = []
jadmanski779bd292009-03-19 17:33:33 +0000856 status_file = self.get_status_log_path()
jadmanski10646442008-08-13 14:05:21 +0000857 status_log = open(status_file, 'a')
858 for line in msg.splitlines():
mbligh0d0f67d2009-11-06 03:15:03 +0000859 line = self._record_prefix + line + '\n'
jadmanski10646442008-08-13 14:05:21 +0000860 lines.append(line)
861 status_log.write(line)
862 status_log.close()
863 self.__parse_status(lines)
864
865
mbligh084bc172008-10-18 14:02:45 +0000866 def _fill_server_control_namespace(self, namespace, protect=True):
mbligh2b92b862008-11-22 13:25:32 +0000867 """
868 Prepare a namespace to be used when executing server control files.
mbligh084bc172008-10-18 14:02:45 +0000869
870 This sets up the control file API by importing modules and making them
871 available under the appropriate names within namespace.
872
873 For use by _execute_code().
874
875 Args:
876 namespace: The namespace dictionary to fill in.
877 protect: Boolean. If True (the default) any operation that would
878 clobber an existing entry in namespace will cause an error.
879 Raises:
880 error.AutoservError: When a name would be clobbered by import.
881 """
882 def _import_names(module_name, names=()):
mbligh2b92b862008-11-22 13:25:32 +0000883 """
884 Import a module and assign named attributes into namespace.
mbligh084bc172008-10-18 14:02:45 +0000885
886 Args:
887 module_name: The string module name.
888 names: A limiting list of names to import from module_name. If
889 empty (the default), all names are imported from the module
890 similar to a "from foo.bar import *" statement.
891 Raises:
892 error.AutoservError: When a name being imported would clobber
893 a name already in namespace.
894 """
895 module = __import__(module_name, {}, {}, names)
896
897 # No names supplied? Import * from the lowest level module.
898 # (Ugh, why do I have to implement this part myself?)
899 if not names:
900 for submodule_name in module_name.split('.')[1:]:
901 module = getattr(module, submodule_name)
902 if hasattr(module, '__all__'):
903 names = getattr(module, '__all__')
904 else:
905 names = dir(module)
906
907 # Install each name into namespace, checking to make sure it
908 # doesn't override anything that already exists.
909 for name in names:
910 # Check for conflicts to help prevent future problems.
911 if name in namespace and protect:
912 if namespace[name] is not getattr(module, name):
913 raise error.AutoservError('importing name '
914 '%s from %s %r would override %r' %
915 (name, module_name, getattr(module, name),
916 namespace[name]))
917 else:
918 # Encourage cleanliness and the use of __all__ for a
919 # more concrete API with less surprises on '*' imports.
920 warnings.warn('%s (%r) being imported from %s for use '
921 'in server control files is not the '
922 'first occurrance of that import.' %
923 (name, namespace[name], module_name))
924
925 namespace[name] = getattr(module, name)
926
927
928 # This is the equivalent of prepending a bunch of import statements to
929 # the front of the control script.
mbligha2b07dd2009-06-22 18:26:13 +0000930 namespace.update(os=os, sys=sys, logging=logging)
mbligh084bc172008-10-18 14:02:45 +0000931 _import_names('autotest_lib.server',
932 ('hosts', 'autotest', 'kvm', 'git', 'standalone_profiler',
933 'source_kernel', 'rpm_kernel', 'deb_kernel', 'git_kernel'))
934 _import_names('autotest_lib.server.subcommand',
935 ('parallel', 'parallel_simple', 'subcommand'))
936 _import_names('autotest_lib.server.utils',
937 ('run', 'get_tmp_dir', 'sh_escape', 'parse_machine'))
938 _import_names('autotest_lib.client.common_lib.error')
939 _import_names('autotest_lib.client.common_lib.barrier', ('barrier',))
940
941 # Inject ourself as the job object into other classes within the API.
942 # (Yuck, this injection is a gross thing be part of a public API. -gps)
943 #
944 # XXX Base & SiteAutotest do not appear to use .job. Who does?
945 namespace['autotest'].Autotest.job = self
946 # server.hosts.base_classes.Host uses .job.
947 namespace['hosts'].Host.job = self
948
949
950 def _execute_code(self, code_file, namespace, protect=True):
mbligh2b92b862008-11-22 13:25:32 +0000951 """
952 Execute code using a copy of namespace as a server control script.
mbligh084bc172008-10-18 14:02:45 +0000953
954 Unless protect_namespace is explicitly set to False, the dict will not
955 be modified.
956
957 Args:
958 code_file: The filename of the control file to execute.
959 namespace: A dict containing names to make available during execution.
960 protect: Boolean. If True (the default) a copy of the namespace dict
961 is used during execution to prevent the code from modifying its
962 contents outside of this function. If False the raw dict is
963 passed in and modifications will be allowed.
964 """
965 if protect:
966 namespace = namespace.copy()
967 self._fill_server_control_namespace(namespace, protect=protect)
968 # TODO: Simplify and get rid of the special cases for only 1 machine.
showard3e66e8c2008-10-27 19:20:51 +0000969 if len(self.machines) > 1:
mbligh084bc172008-10-18 14:02:45 +0000970 machines_text = '\n'.join(self.machines) + '\n'
971 # Only rewrite the file if it does not match our machine list.
972 try:
973 machines_f = open(MACHINES_FILENAME, 'r')
974 existing_machines_text = machines_f.read()
975 machines_f.close()
976 except EnvironmentError:
977 existing_machines_text = None
978 if machines_text != existing_machines_text:
979 utils.open_write_close(MACHINES_FILENAME, machines_text)
980 execfile(code_file, namespace, namespace)
jadmanski10646442008-08-13 14:05:21 +0000981
982
983 def _record(self, status_code, subdir, operation, status='',
984 epoch_time=None, optional_fields=None):
985 """
986 Actual function for recording a single line into the status
987 logs. Should never be called directly, only by job.record as
988 this would bypass the console monitor logging.
989 """
990
mbligh2b92b862008-11-22 13:25:32 +0000991 msg = self._render_record(status_code, subdir, operation, status,
992 epoch_time, optional_fields=optional_fields)
jadmanski10646442008-08-13 14:05:21 +0000993
jadmanski779bd292009-03-19 17:33:33 +0000994 status_file = self.get_status_log_path()
jadmanski10646442008-08-13 14:05:21 +0000995 sys.stdout.write(msg)
mbligh210bae62009-04-01 18:33:13 +0000996 if status_file:
997 open(status_file, "a").write(msg)
jadmanski10646442008-08-13 14:05:21 +0000998 if subdir:
jadmanski779bd292009-03-19 17:33:33 +0000999 sub_status_file = self.get_status_log_path(subdir)
1000 open(sub_status_file, "a").write(msg)
jadmanski10646442008-08-13 14:05:21 +00001001 self.__parse_status(msg.splitlines())
1002
1003
1004 def __parse_status(self, new_lines):
mbligh0d0f67d2009-11-06 03:15:03 +00001005 if not self._using_parser:
jadmanski10646442008-08-13 14:05:21 +00001006 return
1007 new_tests = self.parser.process_lines(new_lines)
1008 for test in new_tests:
1009 self.__insert_test(test)
1010
1011
1012 def __insert_test(self, test):
mbligh2b92b862008-11-22 13:25:32 +00001013 """
1014 An internal method to insert a new test result into the
jadmanski10646442008-08-13 14:05:21 +00001015 database. This method will not raise an exception, even if an
1016 error occurs during the insert, to avoid failing a test
1017 simply because of unexpected database issues."""
showard21baa452008-10-21 00:08:39 +00001018 self.num_tests_run += 1
1019 if status_lib.is_worse_than_or_equal_to(test.status, 'FAIL'):
1020 self.num_tests_failed += 1
jadmanski10646442008-08-13 14:05:21 +00001021 try:
1022 self.results_db.insert_test(self.job_model, test)
1023 except Exception:
1024 msg = ("WARNING: An unexpected error occured while "
1025 "inserting test results into the database. "
1026 "Ignoring error.\n" + traceback.format_exc())
1027 print >> sys.stderr, msg
1028
mblighcaa62c22008-04-07 21:51:17 +00001029
mblighfc3da5b2010-01-06 18:37:22 +00001030 def preprocess_client_state(self):
1031 """
1032 Produce a state file for initializing the state of a client job.
1033
1034 Creates a new client state file with all the current server state, as
1035 well as some pre-set client state.
1036
1037 @returns The path of the file the state was written into.
1038 """
1039 # initialize the sysinfo state
1040 self._state.set('client', 'sysinfo', self.sysinfo.serialize())
1041
1042 # dump the state out to a tempfile
1043 fd, file_path = tempfile.mkstemp(dir=self.tmpdir)
1044 os.close(fd)
1045 self._state.write_to_file(file_path)
1046 return file_path
1047
1048
1049 def postprocess_client_state(self, state_path):
1050 """
1051 Update the state of this job with the state from a client job.
1052
1053 Updates the state of the server side of a job with the final state
1054 of a client job that was run. Updates the non-client-specific state,
1055 pulls in some specific bits from the client-specific state, and then
1056 discards the rest. Removes the state file afterwards
1057
1058 @param state_file A path to the state file from the client.
1059 """
1060 # update the on-disk state
1061 self._state.read_from_file(state_path)
1062 try:
1063 os.remove(state_path)
1064 except IOError, e:
1065 # ignore file-not-found errors
1066 if e.errno != errno.ENOENT:
1067 raise
1068
1069 # update the sysinfo state
1070 if self._state.has('client', 'sysinfo'):
1071 self.sysinfo.deserialize(self._state.get('client', 'sysinfo'))
1072
1073 # drop all the client-specific state
1074 self._state.discard_namespace('client')
1075
1076
mbligha7007722009-01-13 00:37:11 +00001077site_server_job = utils.import_site_class(
1078 __file__, "autotest_lib.server.site_server_job", "site_server_job",
1079 base_server_job)
jadmanski0afbb632008-06-06 21:10:57 +00001080
mbligh0a8c3322009-04-28 18:32:19 +00001081class server_job(site_server_job):
jadmanski0afbb632008-06-06 21:10:57 +00001082 pass
jadmanskif37df842009-02-11 00:03:26 +00001083
1084
1085class warning_manager(object):
1086 """Class for controlling warning logs. Manages the enabling and disabling
1087 of warnings."""
1088 def __init__(self):
1089 # a map of warning types to a list of disabled time intervals
1090 self.disabled_warnings = {}
1091
1092
1093 def is_valid(self, timestamp, warning_type):
1094 """Indicates if a warning (based on the time it occured and its type)
1095 is a valid warning. A warning is considered "invalid" if this type of
1096 warning was marked as "disabled" at the time the warning occured."""
1097 disabled_intervals = self.disabled_warnings.get(warning_type, [])
1098 for start, end in disabled_intervals:
1099 if timestamp >= start and (end is None or timestamp < end):
1100 return False
1101 return True
1102
1103
1104 def disable_warnings(self, warning_type, current_time_func=time.time):
1105 """As of now, disables all further warnings of this type."""
1106 intervals = self.disabled_warnings.setdefault(warning_type, [])
1107 if not intervals or intervals[-1][1] is not None:
jadmanski16a7ff72009-04-01 18:19:53 +00001108 intervals.append((int(current_time_func()), None))
jadmanskif37df842009-02-11 00:03:26 +00001109
1110
1111 def enable_warnings(self, warning_type, current_time_func=time.time):
1112 """As of now, enables all further warnings of this type."""
1113 intervals = self.disabled_warnings.get(warning_type, [])
1114 if intervals and intervals[-1][1] is None:
jadmanski16a7ff72009-04-01 18:19:53 +00001115 intervals[-1] = (intervals[-1][0], int(current_time_func()))