blob: fb734128b0660aaa62b51e0df2b368d4e11c37a2 [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
mbligh7f86e0b2007-11-24 19:45:07 +000015import test, errors
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
40import errors, hosts, autotest, kvm
41import source_kernel, rpm_kernel, deb_kernel
42from subcommand import *
43from utils import run, get_tmp_dir, sh_escape
44
mbligh119c12a2007-11-12 22:13:44 +000045autotest.Autotest.job = job
mbligh31a49de2007-11-05 18:41:19 +000046hosts.SSHHost.job = job
mblighf1c52842007-10-16 15:21:38 +000047"""
48
49client_wrapper = """
50at = autotest.Autotest()
51
52def run_client(machine):
53 host = hosts.SSHHost(machine)
54 at.run(control, host=host)
55
56if len(machines) > 1:
mbligh7b32ba32007-11-05 18:14:20 +000057 open('.machines', 'w').write('\\n'.join(machines) + '\\n')
mblighf1c52842007-10-16 15:21:38 +000058 parallel_simple(run_client, machines)
59else:
60 run_client(machines[0])
61"""
62
mbligh303ccac2007-11-05 18:07:28 +000063crashdumps = """
64def crashdumps(machine):
65 host = hosts.SSHHost(machine, initialize=False)
66 host.get_crashdumps(test_start_time)
67
68parallel_simple(crashdumps, machines, log=False)
69"""
70
mblighf1c52842007-10-16 15:21:38 +000071cleanup="""\
72def cleanup(machine):
mbligh17f0c662007-11-05 18:28:19 +000073 host = hosts.SSHHost(machine, initialize=False)
74 host.reboot()
mblighf1c52842007-10-16 15:21:38 +000075
mbligh84c0ab12007-10-24 21:28:58 +000076parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000077"""
78
mblighf36243d2007-10-30 15:36:16 +000079install="""\
80def install(machine):
mbligh17f0c662007-11-05 18:28:19 +000081 host = hosts.SSHHost(machine, initialize=False)
82 host.machine_install()
mblighf36243d2007-10-30 15:36:16 +000083
mbligh009b25a2007-11-05 18:38:51 +000084parallel_simple(install, machines, log=False)
mblighf36243d2007-10-30 15:36:16 +000085"""
86
mbligh7f86e0b2007-11-24 19:45:07 +000087# load up the verifier control segment, with an optional site-specific hook
mblighed5a4102007-11-20 00:46:41 +000088verify = load_control_segment("site_verify")
89verify += load_control_segment("verify")
mbligh1d42d4e2007-11-05 22:42:00 +000090
mbligh7f86e0b2007-11-24 19:45:07 +000091# load up the repair control segment, with an optional site-specific hook
92repair = load_control_segment("site_repair")
93repair += load_control_segment("repair")
94
95cleanup="""\
mbligh1d42d4e2007-11-05 22:42:00 +000096def cleanup(machine):
97 host = hosts.SSHHost(machine, initialize=False)
98 host.ssh_ping(150*60) # wait for 2.5 hours
99
100parallel_simple(cleanup, machines, log=False)
101"""
102
103def verify_machines(machines):
mbligh7f86e0b2007-11-24 19:45:07 +0000104 if not machines:
105 raise errors.AutoservError('No machines specified to verify')
mbligh1d42d4e2007-11-05 22:42:00 +0000106 namespace = {'machines' : machines, 'job' : None}
107 exec(preamble + verify, namespace, namespace)
108
109
110def repair_machines(machines):
mbligh7f86e0b2007-11-24 19:45:07 +0000111 if not machines:
112 raise errors.AutoservError('No machines specified to repair')
mbligh1d42d4e2007-11-05 22:42:00 +0000113 namespace = {'machines' : machines, 'job' : None}
114 exec(preamble + repair, namespace, namespace)
115
116
mblighf1c52842007-10-16 15:21:38 +0000117class server_job:
118 """The actual job against which we do everything.
119
120 Properties:
121 autodir
122 The top level autotest directory (/usr/local/autotest).
123 serverdir
124 <autodir>/server/
125 clientdir
126 <autodir>/client/
127 conmuxdir
128 <autodir>/conmux/
129 testdir
130 <autodir>/server/tests/
131 control
132 the control file for this job
133 """
134
mbligh18420c22007-10-16 22:27:14 +0000135 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +0000136 """
137 control
138 The control file (pathname of)
139 args
140 args to pass to the control file
141 resultdir
142 where to throw the results
mbligh18420c22007-10-16 22:27:14 +0000143 label
144 label for the job
mblighf1c52842007-10-16 15:21:38 +0000145 user
146 Username for the job (email address)
147 client
148 True if a client-side control file
149 """
mbligh05269362007-10-16 16:58:11 +0000150 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +0000151 self.autodir = os.path.abspath(os.path.join(path, '..'))
152 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +0000153 self.testdir = os.path.join(self.serverdir, 'tests')
154 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +0000155 self.conmuxdir = os.path.join(self.autodir, 'conmux')
156 self.clientdir = os.path.join(self.autodir, 'client')
157 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
158 self.resultdir = resultdir
159 if not os.path.exists(resultdir):
160 os.mkdir(resultdir)
mbligh3ccb8592007-11-05 18:13:40 +0000161 self.debugdir = os.path.join(resultdir, 'debug')
162 if not os.path.exists(self.debugdir):
163 os.mkdir(self.debugdir)
mbligh3dcf2c92007-10-16 22:24:00 +0000164 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +0000165 self.label = label
mblighf1c52842007-10-16 15:21:38 +0000166 self.user = user
167 self.args = args
168 self.client = client
169 self.record_prefix = ''
170
mbligh3f4bced2007-11-05 17:55:53 +0000171 self.stdout = fd_stack.fd_stack(1, sys.stdout)
172 self.stderr = fd_stack.fd_stack(2, sys.stderr)
173
mbligh3dcf2c92007-10-16 22:24:00 +0000174 if os.path.exists(self.status):
175 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000176 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000177 write_keyval(self.resultdir, job_data)
178
179
mblighf36243d2007-10-30 15:36:16 +0000180 def run(self, machines, reboot = False, install_before = False,
181 install_after = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000182 # use a copy so changes don't affect the original dictionary
183 namespace = namespace.copy()
184
mblighfaf0cd42007-11-19 16:00:24 +0000185 self.aborted = False
mblighf1c52842007-10-16 15:21:38 +0000186 namespace['machines'] = machines
187 namespace['args'] = self.args
188 namespace['job'] = self
mbligh6e294382007-11-05 18:11:29 +0000189 test_start_time = int(time.time())
mblighf1c52842007-10-16 15:21:38 +0000190
mbligh87c5d882007-10-29 17:07:24 +0000191 os.chdir(self.resultdir)
192
193 status_log = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000194 try:
mblighf36243d2007-10-30 15:36:16 +0000195 if install_before and machines:
196 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000197 if self.client:
198 namespace['control'] = self.control
199 open('control', 'w').write(self.control)
200 open('control.srv', 'w').write(client_wrapper)
201 server_control = client_wrapper
202 else:
203 open('control.srv', 'w').write(self.control)
204 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000205 exec(preamble + server_control, namespace, namespace)
206
207 finally:
mbligh6e294382007-11-05 18:11:29 +0000208 if machines:
209 namespace['test_start_time'] = test_start_time
210 exec(preamble + crashdumps, namespace,
211 namespace)
mblighf1c52842007-10-16 15:21:38 +0000212 if reboot and machines:
213 exec(preamble + cleanup, namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000214 if install_after and machines:
215 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000216
217
218 def run_test(self, url, *args, **dargs):
219 """Summon a test object and run it.
220
221 tag
222 tag to add to testname
223 url
224 url of the test to run
225 """
226
mblighf1c52842007-10-16 15:21:38 +0000227 (group, testname) = test.testname(url)
228 tag = None
229 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000230
mblighf1c52842007-10-16 15:21:38 +0000231 if dargs.has_key('tag'):
232 tag = dargs['tag']
233 del dargs['tag']
234 if tag:
235 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000236
mbligh43ac5222007-10-16 15:55:01 +0000237 try:
238 test.runtest(self, url, tag, args, dargs)
239 self.record('GOOD', subdir, testname, 'completed successfully')
240 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000241 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000242
243
244 def run_group(self, function, *args, **dargs):
245 """\
246 function:
247 subroutine to run
248 *args:
249 arguments for the function
250 """
251
252 result = None
253 name = function.__name__
254
255 # Allow the tag for the group to be specified.
256 if dargs.has_key('tag'):
257 tag = dargs['tag']
258 del dargs['tag']
259 if tag:
260 name = tag
261
262 # if tag:
263 # name += '.' + tag
264 old_record_prefix = self.record_prefix
265 try:
266 try:
267 self.record('START', None, name)
268 self.record_prefix += '\t'
269 result = function(*args, **dargs)
270 self.record_prefix = old_record_prefix
271 self.record('END GOOD', None, name)
272 except:
273 self.record_prefix = old_record_prefix
274 self.record('END FAIL', None, name, format_error())
275 # We don't want to raise up an error higher if it's just
276 # a TestError - we want to carry on to other tests. Hence
277 # this outer try/except block.
278 except TestError:
279 pass
280 except:
281 raise TestError(name + ' failed\n' + format_error())
282
283 return result
284
285
286 def record(self, status_code, subdir, operation, status = ''):
287 """
288 Record job-level status
289
290 The intent is to make this file both machine parseable and
291 human readable. That involves a little more complexity, but
292 really isn't all that bad ;-)
293
294 Format is <status code>\t<subdir>\t<operation>\t<status>
295
296 status code: (GOOD|WARN|FAIL|ABORT)
297 or START
298 or END (GOOD|WARN|FAIL|ABORT)
299
300 subdir: MUST be a relevant subdirectory in the results,
301 or None, which will be represented as '----'
302
303 operation: description of what you ran (e.g. "dbench", or
304 "mkfs -t foobar /dev/sda9")
305
306 status: error message or "completed sucessfully"
307
308 ------------------------------------------------------------
309
310 Initial tabs indicate indent levels for grouping, and is
311 governed by self.record_prefix
312
313 multiline messages have secondary lines prefaced by a double
314 space (' ')
315 """
316
317 if subdir:
318 if re.match(r'[\n\t]', subdir):
319 raise "Invalid character in subdir string"
320 substr = subdir
321 else:
322 substr = '----'
323
324 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
325 status_code):
326 raise "Invalid status code supplied: %s" % status_code
327 if re.match(r'[\n\t]', operation):
328 raise "Invalid character in operation string"
329 operation = operation.rstrip()
330 status = status.rstrip()
331 status = re.sub(r"\t", " ", status)
332 # Ensure any continuation lines are marked so we can
333 # detect them in the status file to ensure it is parsable.
334 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
335
mbligh30270302007-11-05 20:33:52 +0000336 # Generate timestamps for inclusion in the logs
337 epoch_time = int(time.time()) # seconds since epoch, in UTC
338 local_time = time.localtime(epoch_time)
339 epoch_time_str = "timestamp=%d" % (epoch_time,)
340 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
341 local_time)
342
343 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
344 epoch_time_str, local_time_str,
345 status))
mblighf1c52842007-10-16 15:21:38 +0000346
mbligh31a49de2007-11-05 18:41:19 +0000347 status_file = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000348 print msg
349 open(status_file, "a").write(self.record_prefix + msg + "\n")
350 if subdir:
351 status_file = os.path.join(self.resultdir, subdir, 'status')
352 open(status_file, "a").write(msg + "\n")