blob: c4624cada56e0fa036bbd874233d20e3df1883af [file] [log] [blame]
mblighf1c52842007-10-16 15:21:38 +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
9__author__ = """
10Martin J. Bligh <mbligh@google.com>
11Andy Whitcroft <apw@shadowen.org>
12"""
13
mbligh6e294382007-11-05 18:11:29 +000014import os, sys, re, time
mbligh03f4fc72007-11-29 20:56:14 +000015import test
mblighf1c52842007-10-16 15:21:38 +000016from utils import *
mblighf31b0c02007-11-29 18:19:22 +000017from common.error import *
mblighf1c52842007-10-16 15:21:38 +000018
mbligh3f4bced2007-11-05 17:55:53 +000019# this magic incantation should give us access to a client library
20server_dir = os.path.dirname(__file__)
21client_dir = os.path.join(server_dir, "..", "client", "bin")
22sys.path.append(client_dir)
23import fd_stack
24sys.path.pop()
25
mblighed5a4102007-11-20 00:46:41 +000026# load up a control segment
27# these are all stored in <server_dir>/control_segments
28def load_control_segment(name):
29 server_dir = os.path.dirname(os.path.abspath(__file__))
mbligh7f86e0b2007-11-24 19:45:07 +000030 script_file = os.path.join(server_dir, "control_segments", name)
mblighed5a4102007-11-20 00:46:41 +000031 if os.path.exists(script_file):
32 return file(script_file).read()
33 else:
34 return ""
35
36
mblighf1c52842007-10-16 15:21:38 +000037preamble = """\
38import os, sys
39
mblighb3c9f372008-01-14 16:39:44 +000040import hosts, autotest, kvm, git, standalone_profiler
mblighd0868ab2007-12-04 22:47:46 +000041import source_kernel, rpm_kernel, deb_kernel, git_kernel
mbligh03f4fc72007-11-29 20:56:14 +000042from common.error import *
mblighe1417fa2007-12-10 16:55:13 +000043from common import barrier
mblighf1c52842007-10-16 15:21:38 +000044from subcommand import *
45from utils import run, get_tmp_dir, sh_escape
46
mbligh119c12a2007-11-12 22:13:44 +000047autotest.Autotest.job = job
mbligh31a49de2007-11-05 18:41:19 +000048hosts.SSHHost.job = job
mblighe1417fa2007-12-10 16:55:13 +000049barrier = barrier.barrier
mblighf1c52842007-10-16 15:21:38 +000050"""
51
52client_wrapper = """
53at = autotest.Autotest()
54
55def run_client(machine):
56 host = hosts.SSHHost(machine)
57 at.run(control, host=host)
58
59if len(machines) > 1:
mbligh7b32ba32007-11-05 18:14:20 +000060 open('.machines', 'w').write('\\n'.join(machines) + '\\n')
mblighf1c52842007-10-16 15:21:38 +000061 parallel_simple(run_client, machines)
62else:
63 run_client(machines[0])
64"""
65
mbligh303ccac2007-11-05 18:07:28 +000066crashdumps = """
67def crashdumps(machine):
68 host = hosts.SSHHost(machine, initialize=False)
69 host.get_crashdumps(test_start_time)
70
71parallel_simple(crashdumps, machines, log=False)
72"""
73
mbligh98ff1462007-12-19 16:27:55 +000074reboot_segment="""\
75def reboot(machine):
mbligh17f0c662007-11-05 18:28:19 +000076 host = hosts.SSHHost(machine, initialize=False)
77 host.reboot()
mblighf1c52842007-10-16 15:21:38 +000078
mbligh98ff1462007-12-19 16:27:55 +000079parallel_simple(reboot, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000080"""
81
mblighf36243d2007-10-30 15:36:16 +000082install="""\
83def install(machine):
mbligh17f0c662007-11-05 18:28:19 +000084 host = hosts.SSHHost(machine, initialize=False)
85 host.machine_install()
mblighf36243d2007-10-30 15:36:16 +000086
mbligh009b25a2007-11-05 18:38:51 +000087parallel_simple(install, machines, log=False)
mblighf36243d2007-10-30 15:36:16 +000088"""
89
mbligh7f86e0b2007-11-24 19:45:07 +000090# load up the verifier control segment, with an optional site-specific hook
mblighed5a4102007-11-20 00:46:41 +000091verify = load_control_segment("site_verify")
92verify += load_control_segment("verify")
mbligh1d42d4e2007-11-05 22:42:00 +000093
mbligh7f86e0b2007-11-24 19:45:07 +000094# load up the repair control segment, with an optional site-specific hook
95repair = load_control_segment("site_repair")
96repair += load_control_segment("repair")
97
mbligh1d42d4e2007-11-05 22:42:00 +000098
mblighf1c52842007-10-16 15:21:38 +000099class server_job:
100 """The actual job against which we do everything.
101
102 Properties:
103 autodir
104 The top level autotest directory (/usr/local/autotest).
105 serverdir
106 <autodir>/server/
107 clientdir
108 <autodir>/client/
109 conmuxdir
110 <autodir>/conmux/
111 testdir
112 <autodir>/server/tests/
113 control
114 the control file for this job
115 """
116
mblighe8b37a92007-12-19 15:54:11 +0000117 def __init__(self, control, args, resultdir, label, user, machines,
118 client = False):
mblighf1c52842007-10-16 15:21:38 +0000119 """
120 control
121 The control file (pathname of)
122 args
123 args to pass to the control file
124 resultdir
125 where to throw the results
mbligh18420c22007-10-16 22:27:14 +0000126 label
127 label for the job
mblighf1c52842007-10-16 15:21:38 +0000128 user
129 Username for the job (email address)
130 client
131 True if a client-side control file
132 """
mbligh05269362007-10-16 16:58:11 +0000133 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +0000134 self.autodir = os.path.abspath(os.path.join(path, '..'))
135 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +0000136 self.testdir = os.path.join(self.serverdir, 'tests')
137 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +0000138 self.conmuxdir = os.path.join(self.autodir, 'conmux')
139 self.clientdir = os.path.join(self.autodir, 'client')
mblighe25fd5b2008-01-22 17:23:37 +0000140 if control:
141 self.control = open(control, 'r').read()
142 self.control = re.sub('\r', '', self.control)
143 else:
144 self.control = None
mblighf1c52842007-10-16 15:21:38 +0000145 self.resultdir = resultdir
146 if not os.path.exists(resultdir):
147 os.mkdir(resultdir)
mbligh3ccb8592007-11-05 18:13:40 +0000148 self.debugdir = os.path.join(resultdir, 'debug')
149 if not os.path.exists(self.debugdir):
150 os.mkdir(self.debugdir)
mbligh3dcf2c92007-10-16 22:24:00 +0000151 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +0000152 self.label = label
mblighf1c52842007-10-16 15:21:38 +0000153 self.user = user
154 self.args = args
mblighe8b37a92007-12-19 15:54:11 +0000155 self.machines = machines
mblighf1c52842007-10-16 15:21:38 +0000156 self.client = client
157 self.record_prefix = ''
158
mbligh3f4bced2007-11-05 17:55:53 +0000159 self.stdout = fd_stack.fd_stack(1, sys.stdout)
160 self.stderr = fd_stack.fd_stack(2, sys.stderr)
161
mbligh3dcf2c92007-10-16 22:24:00 +0000162 if os.path.exists(self.status):
163 os.unlink(self.status)
mblighe8b37a92007-12-19 15:54:11 +0000164 job_data = { 'label' : label, 'user' : user,
165 'hostname' : ','.join(machines) }
mblighf1c52842007-10-16 15:21:38 +0000166 write_keyval(self.resultdir, job_data)
167
168
mblighe25fd5b2008-01-22 17:23:37 +0000169 def verify(self):
170 if not self.machines:
171 raise AutoservError('No machines specified to verify')
172 try:
173 namespace = {'machines' : self.machines, 'job' : self}
174 exec(preamble + verify, namespace, namespace)
175 except Exception, e:
176 msg = 'Verify failed\n' + str(e) + '\n' + format_error()
177 self.record('ABORT', None, None, msg)
178 raise
179
180
181 def repair(self):
182 if not self.machines:
183 raise AutoservError('No machines specified to repair')
184 namespace = {'machines' : self.machines, 'job' : self}
185 exec(preamble + repair, namespace, namespace)
186 self.repair()
187
188
mblighe8b37a92007-12-19 15:54:11 +0000189 def run(self, reboot = False, install_before = False,
mblighf36243d2007-10-30 15:36:16 +0000190 install_after = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000191 # use a copy so changes don't affect the original dictionary
192 namespace = namespace.copy()
mblighe8b37a92007-12-19 15:54:11 +0000193 machines = self.machines
mbligh60dbd502007-10-26 14:59:31 +0000194
mblighfaf0cd42007-11-19 16:00:24 +0000195 self.aborted = False
mblighf1c52842007-10-16 15:21:38 +0000196 namespace['machines'] = machines
197 namespace['args'] = self.args
198 namespace['job'] = self
mbligh6e294382007-11-05 18:11:29 +0000199 test_start_time = int(time.time())
mblighf1c52842007-10-16 15:21:38 +0000200
mbligh87c5d882007-10-29 17:07:24 +0000201 os.chdir(self.resultdir)
202
203 status_log = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000204 try:
mblighf36243d2007-10-30 15:36:16 +0000205 if install_before and machines:
206 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000207 if self.client:
208 namespace['control'] = self.control
209 open('control', 'w').write(self.control)
210 open('control.srv', 'w').write(client_wrapper)
211 server_control = client_wrapper
212 else:
213 open('control.srv', 'w').write(self.control)
214 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000215 exec(preamble + server_control, namespace, namespace)
216
217 finally:
mbligh6e294382007-11-05 18:11:29 +0000218 if machines:
219 namespace['test_start_time'] = test_start_time
mbligh98ff1462007-12-19 16:27:55 +0000220 exec(preamble + crashdumps,
221 namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000222 if reboot and machines:
mbligh98ff1462007-12-19 16:27:55 +0000223 exec(preamble + reboot_segment,
224 namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000225 if install_after and machines:
226 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000227
228
229 def run_test(self, url, *args, **dargs):
230 """Summon a test object and run it.
231
232 tag
233 tag to add to testname
234 url
235 url of the test to run
236 """
237
mblighf1c52842007-10-16 15:21:38 +0000238 (group, testname) = test.testname(url)
239 tag = None
240 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000241
mblighf1c52842007-10-16 15:21:38 +0000242 if dargs.has_key('tag'):
243 tag = dargs['tag']
244 del dargs['tag']
245 if tag:
246 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000247
mbligh43ac5222007-10-16 15:55:01 +0000248 try:
249 test.runtest(self, url, tag, args, dargs)
250 self.record('GOOD', subdir, testname, 'completed successfully')
251 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000252 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000253
254
255 def run_group(self, function, *args, **dargs):
256 """\
257 function:
258 subroutine to run
259 *args:
260 arguments for the function
261 """
262
263 result = None
264 name = function.__name__
265
266 # Allow the tag for the group to be specified.
267 if dargs.has_key('tag'):
268 tag = dargs['tag']
269 del dargs['tag']
270 if tag:
271 name = tag
272
273 # if tag:
274 # name += '.' + tag
275 old_record_prefix = self.record_prefix
276 try:
277 try:
278 self.record('START', None, name)
279 self.record_prefix += '\t'
280 result = function(*args, **dargs)
281 self.record_prefix = old_record_prefix
282 self.record('END GOOD', None, name)
283 except:
284 self.record_prefix = old_record_prefix
285 self.record('END FAIL', None, name, format_error())
286 # We don't want to raise up an error higher if it's just
287 # a TestError - we want to carry on to other tests. Hence
288 # this outer try/except block.
289 except TestError:
290 pass
291 except:
292 raise TestError(name + ' failed\n' + format_error())
293
294 return result
295
296
297 def record(self, status_code, subdir, operation, status = ''):
298 """
299 Record job-level status
300
301 The intent is to make this file both machine parseable and
302 human readable. That involves a little more complexity, but
303 really isn't all that bad ;-)
304
305 Format is <status code>\t<subdir>\t<operation>\t<status>
306
307 status code: (GOOD|WARN|FAIL|ABORT)
308 or START
309 or END (GOOD|WARN|FAIL|ABORT)
310
311 subdir: MUST be a relevant subdirectory in the results,
312 or None, which will be represented as '----'
313
314 operation: description of what you ran (e.g. "dbench", or
315 "mkfs -t foobar /dev/sda9")
316
317 status: error message or "completed sucessfully"
318
319 ------------------------------------------------------------
320
321 Initial tabs indicate indent levels for grouping, and is
322 governed by self.record_prefix
323
324 multiline messages have secondary lines prefaced by a double
325 space (' ')
326 """
327
328 if subdir:
329 if re.match(r'[\n\t]', subdir):
mbligh4d6feff2008-01-14 16:48:56 +0000330 raise ValueError('Invalid character in subdir string')
mblighf1c52842007-10-16 15:21:38 +0000331 substr = subdir
332 else:
333 substr = '----'
334
335 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
336 status_code):
mbligh4d6feff2008-01-14 16:48:56 +0000337 raise ValueError('Invalid status code supplied: %s' % status_code)
mblighe25fd5b2008-01-22 17:23:37 +0000338 if not operation:
339 operation = '----'
mblighf1c52842007-10-16 15:21:38 +0000340 if re.match(r'[\n\t]', operation):
mbligh4d6feff2008-01-14 16:48:56 +0000341 raise ValueError('Invalid character in operation string')
mblighf1c52842007-10-16 15:21:38 +0000342 operation = operation.rstrip()
343 status = status.rstrip()
344 status = re.sub(r"\t", " ", status)
345 # Ensure any continuation lines are marked so we can
346 # detect them in the status file to ensure it is parsable.
347 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
348
mbligh30270302007-11-05 20:33:52 +0000349 # Generate timestamps for inclusion in the logs
350 epoch_time = int(time.time()) # seconds since epoch, in UTC
351 local_time = time.localtime(epoch_time)
352 epoch_time_str = "timestamp=%d" % (epoch_time,)
353 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
354 local_time)
355
356 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
357 epoch_time_str, local_time_str,
358 status))
mblighf1c52842007-10-16 15:21:38 +0000359
mbligh31a49de2007-11-05 18:41:19 +0000360 status_file = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000361 print msg
362 open(status_file, "a").write(self.record_prefix + msg + "\n")
363 if subdir:
mblighd56eb592008-01-22 16:36:34 +0000364 test_dir = os.path.join(self.resultdir, subdir)
365 if not os.path.exists(test_dir):
366 os.mkdir(test_dir)
367 status_file = os.path.join(test_dir, 'status')
mblighf1c52842007-10-16 15:21:38 +0000368 open(status_file, "a").write(msg + "\n")