blob: d7506e95d48de41f1aa28f3fb7aeef253a204476 [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
mbligh05269362007-10-16 16:58:11 +000015import test
mblighf1c52842007-10-16 15:21:38 +000016from utils import *
mbligh05269362007-10-16 16:58:11 +000017from 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
mblighf1c52842007-10-16 15:21:38 +000026preamble = """\
27import os, sys
28
29import errors, hosts, autotest, kvm
30import source_kernel, rpm_kernel, deb_kernel
31from subcommand import *
32from utils import run, get_tmp_dir, sh_escape
33
mbligh119c12a2007-11-12 22:13:44 +000034autotest.Autotest.job = job
mbligh31a49de2007-11-05 18:41:19 +000035hosts.SSHHost.job = job
mblighf1c52842007-10-16 15:21:38 +000036"""
37
38client_wrapper = """
39at = autotest.Autotest()
40
41def run_client(machine):
42 host = hosts.SSHHost(machine)
43 at.run(control, host=host)
44
45if len(machines) > 1:
mbligh7b32ba32007-11-05 18:14:20 +000046 open('.machines', 'w').write('\\n'.join(machines) + '\\n')
mblighf1c52842007-10-16 15:21:38 +000047 parallel_simple(run_client, machines)
48else:
49 run_client(machines[0])
50"""
51
mbligh303ccac2007-11-05 18:07:28 +000052crashdumps = """
53def crashdumps(machine):
54 host = hosts.SSHHost(machine, initialize=False)
55 host.get_crashdumps(test_start_time)
56
57parallel_simple(crashdumps, machines, log=False)
58"""
59
mblighf1c52842007-10-16 15:21:38 +000060cleanup="""\
61def cleanup(machine):
mbligh17f0c662007-11-05 18:28:19 +000062 host = hosts.SSHHost(machine, initialize=False)
63 host.reboot()
mblighf1c52842007-10-16 15:21:38 +000064
mbligh84c0ab12007-10-24 21:28:58 +000065parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000066"""
67
mblighf36243d2007-10-30 15:36:16 +000068install="""\
69def install(machine):
mbligh17f0c662007-11-05 18:28:19 +000070 host = hosts.SSHHost(machine, initialize=False)
71 host.machine_install()
mblighf36243d2007-10-30 15:36:16 +000072
mbligh009b25a2007-11-05 18:38:51 +000073parallel_simple(install, machines, log=False)
mblighf36243d2007-10-30 15:36:16 +000074"""
75
mbligh1d42d4e2007-11-05 22:42:00 +000076# This needs more stuff in it. Check for diskspace, etc. But it's a start.
77verify="""\
78def cleanup(machine):
79 host = hosts.SSHHost(machine, initialize=False)
80 host.ssh_ping()
81
82parallel_simple(cleanup, machines, log=False)
83"""
84
85# This is pretty silly. Wait for s/w watchdog. Pray hard.
86repair="""\
87def cleanup(machine):
88 host = hosts.SSHHost(machine, initialize=False)
89 host.ssh_ping(150*60) # wait for 2.5 hours
90
91parallel_simple(cleanup, machines, log=False)
92"""
93
94def verify_machines(machines):
95 namespace = {'machines' : machines, 'job' : None}
96 exec(preamble + verify, namespace, namespace)
97
98
99def repair_machines(machines):
100 namespace = {'machines' : machines, 'job' : None}
101 exec(preamble + repair, namespace, namespace)
102
103
mblighf1c52842007-10-16 15:21:38 +0000104class server_job:
105 """The actual job against which we do everything.
106
107 Properties:
108 autodir
109 The top level autotest directory (/usr/local/autotest).
110 serverdir
111 <autodir>/server/
112 clientdir
113 <autodir>/client/
114 conmuxdir
115 <autodir>/conmux/
116 testdir
117 <autodir>/server/tests/
118 control
119 the control file for this job
120 """
121
mbligh18420c22007-10-16 22:27:14 +0000122 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +0000123 """
124 control
125 The control file (pathname of)
126 args
127 args to pass to the control file
128 resultdir
129 where to throw the results
mbligh18420c22007-10-16 22:27:14 +0000130 label
131 label for the job
mblighf1c52842007-10-16 15:21:38 +0000132 user
133 Username for the job (email address)
134 client
135 True if a client-side control file
136 """
mbligh05269362007-10-16 16:58:11 +0000137 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +0000138 self.autodir = os.path.abspath(os.path.join(path, '..'))
139 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +0000140 self.testdir = os.path.join(self.serverdir, 'tests')
141 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +0000142 self.conmuxdir = os.path.join(self.autodir, 'conmux')
143 self.clientdir = os.path.join(self.autodir, 'client')
144 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
145 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
155 self.client = client
156 self.record_prefix = ''
157
mbligh3f4bced2007-11-05 17:55:53 +0000158 self.stdout = fd_stack.fd_stack(1, sys.stdout)
159 self.stderr = fd_stack.fd_stack(2, sys.stderr)
160
mbligh3dcf2c92007-10-16 22:24:00 +0000161 if os.path.exists(self.status):
162 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000163 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000164 write_keyval(self.resultdir, job_data)
165
166
mblighf36243d2007-10-30 15:36:16 +0000167 def run(self, machines, reboot = False, install_before = False,
168 install_after = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000169 # use a copy so changes don't affect the original dictionary
170 namespace = namespace.copy()
171
mblighfaf0cd42007-11-19 16:00:24 +0000172 self.aborted = False
mblighf1c52842007-10-16 15:21:38 +0000173 namespace['machines'] = machines
174 namespace['args'] = self.args
175 namespace['job'] = self
mbligh6e294382007-11-05 18:11:29 +0000176 test_start_time = int(time.time())
mblighf1c52842007-10-16 15:21:38 +0000177
mbligh87c5d882007-10-29 17:07:24 +0000178 os.chdir(self.resultdir)
179
180 status_log = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000181 try:
mblighf36243d2007-10-30 15:36:16 +0000182 if install_before and machines:
183 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000184 if self.client:
185 namespace['control'] = self.control
186 open('control', 'w').write(self.control)
187 open('control.srv', 'w').write(client_wrapper)
188 server_control = client_wrapper
189 else:
190 open('control.srv', 'w').write(self.control)
191 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000192 exec(preamble + server_control, namespace, namespace)
193
194 finally:
mbligh6e294382007-11-05 18:11:29 +0000195 if machines:
196 namespace['test_start_time'] = test_start_time
197 exec(preamble + crashdumps, namespace,
198 namespace)
mblighf1c52842007-10-16 15:21:38 +0000199 if reboot and machines:
200 exec(preamble + cleanup, namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000201 if install_after and machines:
202 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000203
204
205 def run_test(self, url, *args, **dargs):
206 """Summon a test object and run it.
207
208 tag
209 tag to add to testname
210 url
211 url of the test to run
212 """
213
mblighf1c52842007-10-16 15:21:38 +0000214 (group, testname) = test.testname(url)
215 tag = None
216 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000217
mblighf1c52842007-10-16 15:21:38 +0000218 if dargs.has_key('tag'):
219 tag = dargs['tag']
220 del dargs['tag']
221 if tag:
222 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000223
mbligh43ac5222007-10-16 15:55:01 +0000224 try:
225 test.runtest(self, url, tag, args, dargs)
226 self.record('GOOD', subdir, testname, 'completed successfully')
227 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000228 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000229
230
231 def run_group(self, function, *args, **dargs):
232 """\
233 function:
234 subroutine to run
235 *args:
236 arguments for the function
237 """
238
239 result = None
240 name = function.__name__
241
242 # Allow the tag for the group to be specified.
243 if dargs.has_key('tag'):
244 tag = dargs['tag']
245 del dargs['tag']
246 if tag:
247 name = tag
248
249 # if tag:
250 # name += '.' + tag
251 old_record_prefix = self.record_prefix
252 try:
253 try:
254 self.record('START', None, name)
255 self.record_prefix += '\t'
256 result = function(*args, **dargs)
257 self.record_prefix = old_record_prefix
258 self.record('END GOOD', None, name)
259 except:
260 self.record_prefix = old_record_prefix
261 self.record('END FAIL', None, name, format_error())
262 # We don't want to raise up an error higher if it's just
263 # a TestError - we want to carry on to other tests. Hence
264 # this outer try/except block.
265 except TestError:
266 pass
267 except:
268 raise TestError(name + ' failed\n' + format_error())
269
270 return result
271
272
273 def record(self, status_code, subdir, operation, status = ''):
274 """
275 Record job-level status
276
277 The intent is to make this file both machine parseable and
278 human readable. That involves a little more complexity, but
279 really isn't all that bad ;-)
280
281 Format is <status code>\t<subdir>\t<operation>\t<status>
282
283 status code: (GOOD|WARN|FAIL|ABORT)
284 or START
285 or END (GOOD|WARN|FAIL|ABORT)
286
287 subdir: MUST be a relevant subdirectory in the results,
288 or None, which will be represented as '----'
289
290 operation: description of what you ran (e.g. "dbench", or
291 "mkfs -t foobar /dev/sda9")
292
293 status: error message or "completed sucessfully"
294
295 ------------------------------------------------------------
296
297 Initial tabs indicate indent levels for grouping, and is
298 governed by self.record_prefix
299
300 multiline messages have secondary lines prefaced by a double
301 space (' ')
302 """
303
304 if subdir:
305 if re.match(r'[\n\t]', subdir):
306 raise "Invalid character in subdir string"
307 substr = subdir
308 else:
309 substr = '----'
310
311 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
312 status_code):
313 raise "Invalid status code supplied: %s" % status_code
314 if re.match(r'[\n\t]', operation):
315 raise "Invalid character in operation string"
316 operation = operation.rstrip()
317 status = status.rstrip()
318 status = re.sub(r"\t", " ", status)
319 # Ensure any continuation lines are marked so we can
320 # detect them in the status file to ensure it is parsable.
321 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
322
mbligh30270302007-11-05 20:33:52 +0000323 # Generate timestamps for inclusion in the logs
324 epoch_time = int(time.time()) # seconds since epoch, in UTC
325 local_time = time.localtime(epoch_time)
326 epoch_time_str = "timestamp=%d" % (epoch_time,)
327 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
328 local_time)
329
330 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
331 epoch_time_str, local_time_str,
332 status))
mblighf1c52842007-10-16 15:21:38 +0000333
mbligh31a49de2007-11-05 18:41:19 +0000334 status_file = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000335 print msg
336 open(status_file, "a").write(self.record_prefix + msg + "\n")
337 if subdir:
338 status_file = os.path.join(self.resultdir, subdir, 'status')
339 open(status_file, "a").write(msg + "\n")