blob: f756a419f927d8e90f8949713cba26ac3756cc07 [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
mblighf1c52842007-10-16 15:21:38 +0000172 namespace['machines'] = machines
173 namespace['args'] = self.args
174 namespace['job'] = self
mbligh6e294382007-11-05 18:11:29 +0000175 test_start_time = int(time.time())
mblighf1c52842007-10-16 15:21:38 +0000176
mbligh87c5d882007-10-29 17:07:24 +0000177 os.chdir(self.resultdir)
178
179 status_log = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000180 try:
mblighf36243d2007-10-30 15:36:16 +0000181 if install_before and machines:
182 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000183 if self.client:
184 namespace['control'] = self.control
185 open('control', 'w').write(self.control)
186 open('control.srv', 'w').write(client_wrapper)
187 server_control = client_wrapper
188 else:
189 open('control.srv', 'w').write(self.control)
190 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000191 exec(preamble + server_control, namespace, namespace)
192
193 finally:
mbligh6e294382007-11-05 18:11:29 +0000194 if machines:
195 namespace['test_start_time'] = test_start_time
196 exec(preamble + crashdumps, namespace,
197 namespace)
mblighf1c52842007-10-16 15:21:38 +0000198 if reboot and machines:
199 exec(preamble + cleanup, namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000200 if install_after and machines:
201 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000202
203
204 def run_test(self, url, *args, **dargs):
205 """Summon a test object and run it.
206
207 tag
208 tag to add to testname
209 url
210 url of the test to run
211 """
212
mblighf1c52842007-10-16 15:21:38 +0000213 (group, testname) = test.testname(url)
214 tag = None
215 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000216
mblighf1c52842007-10-16 15:21:38 +0000217 if dargs.has_key('tag'):
218 tag = dargs['tag']
219 del dargs['tag']
220 if tag:
221 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000222
mbligh43ac5222007-10-16 15:55:01 +0000223 try:
224 test.runtest(self, url, tag, args, dargs)
225 self.record('GOOD', subdir, testname, 'completed successfully')
226 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000227 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000228
229
230 def run_group(self, function, *args, **dargs):
231 """\
232 function:
233 subroutine to run
234 *args:
235 arguments for the function
236 """
237
238 result = None
239 name = function.__name__
240
241 # Allow the tag for the group to be specified.
242 if dargs.has_key('tag'):
243 tag = dargs['tag']
244 del dargs['tag']
245 if tag:
246 name = tag
247
248 # if tag:
249 # name += '.' + tag
250 old_record_prefix = self.record_prefix
251 try:
252 try:
253 self.record('START', None, name)
254 self.record_prefix += '\t'
255 result = function(*args, **dargs)
256 self.record_prefix = old_record_prefix
257 self.record('END GOOD', None, name)
258 except:
259 self.record_prefix = old_record_prefix
260 self.record('END FAIL', None, name, format_error())
261 # We don't want to raise up an error higher if it's just
262 # a TestError - we want to carry on to other tests. Hence
263 # this outer try/except block.
264 except TestError:
265 pass
266 except:
267 raise TestError(name + ' failed\n' + format_error())
268
269 return result
270
271
272 def record(self, status_code, subdir, operation, status = ''):
273 """
274 Record job-level status
275
276 The intent is to make this file both machine parseable and
277 human readable. That involves a little more complexity, but
278 really isn't all that bad ;-)
279
280 Format is <status code>\t<subdir>\t<operation>\t<status>
281
282 status code: (GOOD|WARN|FAIL|ABORT)
283 or START
284 or END (GOOD|WARN|FAIL|ABORT)
285
286 subdir: MUST be a relevant subdirectory in the results,
287 or None, which will be represented as '----'
288
289 operation: description of what you ran (e.g. "dbench", or
290 "mkfs -t foobar /dev/sda9")
291
292 status: error message or "completed sucessfully"
293
294 ------------------------------------------------------------
295
296 Initial tabs indicate indent levels for grouping, and is
297 governed by self.record_prefix
298
299 multiline messages have secondary lines prefaced by a double
300 space (' ')
301 """
302
303 if subdir:
304 if re.match(r'[\n\t]', subdir):
305 raise "Invalid character in subdir string"
306 substr = subdir
307 else:
308 substr = '----'
309
310 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
311 status_code):
312 raise "Invalid status code supplied: %s" % status_code
313 if re.match(r'[\n\t]', operation):
314 raise "Invalid character in operation string"
315 operation = operation.rstrip()
316 status = status.rstrip()
317 status = re.sub(r"\t", " ", status)
318 # Ensure any continuation lines are marked so we can
319 # detect them in the status file to ensure it is parsable.
320 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
321
mbligh30270302007-11-05 20:33:52 +0000322 # Generate timestamps for inclusion in the logs
323 epoch_time = int(time.time()) # seconds since epoch, in UTC
324 local_time = time.localtime(epoch_time)
325 epoch_time_str = "timestamp=%d" % (epoch_time,)
326 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
327 local_time)
328
329 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
330 epoch_time_str, local_time_str,
331 status))
mblighf1c52842007-10-16 15:21:38 +0000332
mbligh31a49de2007-11-05 18:41:19 +0000333 status_file = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000334 print msg
335 open(status_file, "a").write(self.record_prefix + msg + "\n")
336 if subdir:
337 status_file = os.path.join(self.resultdir, subdir, 'status')
338 open(status_file, "a").write(msg + "\n")