blob: 1e81e4cc49e83bf72365ba8405cae29bb2391fc2 [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
mbligh31a49de2007-11-05 18:41:19 +000034hosts.SSHHost.job = job
mblighf1c52842007-10-16 15:21:38 +000035"""
36
37client_wrapper = """
38at = autotest.Autotest()
39
40def run_client(machine):
41 host = hosts.SSHHost(machine)
42 at.run(control, host=host)
43
44if len(machines) > 1:
mbligh7b32ba32007-11-05 18:14:20 +000045 open('.machines', 'w').write('\\n'.join(machines) + '\\n')
mblighf1c52842007-10-16 15:21:38 +000046 parallel_simple(run_client, machines)
47else:
48 run_client(machines[0])
49"""
50
mbligh303ccac2007-11-05 18:07:28 +000051crashdumps = """
52def crashdumps(machine):
53 host = hosts.SSHHost(machine, initialize=False)
54 host.get_crashdumps(test_start_time)
55
56parallel_simple(crashdumps, machines, log=False)
57"""
58
mblighf1c52842007-10-16 15:21:38 +000059cleanup="""\
60def cleanup(machine):
mbligh17f0c662007-11-05 18:28:19 +000061 host = hosts.SSHHost(machine, initialize=False)
62 host.reboot()
mblighf1c52842007-10-16 15:21:38 +000063
mbligh84c0ab12007-10-24 21:28:58 +000064parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000065"""
66
mblighf36243d2007-10-30 15:36:16 +000067install="""\
68def install(machine):
mbligh17f0c662007-11-05 18:28:19 +000069 host = hosts.SSHHost(machine, initialize=False)
70 host.machine_install()
mblighf36243d2007-10-30 15:36:16 +000071
mbligh009b25a2007-11-05 18:38:51 +000072parallel_simple(install, machines, log=False)
mblighf36243d2007-10-30 15:36:16 +000073"""
74
mbligh1d42d4e2007-11-05 22:42:00 +000075# This needs more stuff in it. Check for diskspace, etc. But it's a start.
76verify="""\
77def cleanup(machine):
78 host = hosts.SSHHost(machine, initialize=False)
79 host.ssh_ping()
80
81parallel_simple(cleanup, machines, log=False)
82"""
83
84# This is pretty silly. Wait for s/w watchdog. Pray hard.
85repair="""\
86def cleanup(machine):
87 host = hosts.SSHHost(machine, initialize=False)
88 host.ssh_ping(150*60) # wait for 2.5 hours
89
90parallel_simple(cleanup, machines, log=False)
91"""
92
93def verify_machines(machines):
94 namespace = {'machines' : machines, 'job' : None}
95 exec(preamble + verify, namespace, namespace)
96
97
98def repair_machines(machines):
99 namespace = {'machines' : machines, 'job' : None}
100 exec(preamble + repair, namespace, namespace)
101
102
mblighf1c52842007-10-16 15:21:38 +0000103class server_job:
104 """The actual job against which we do everything.
105
106 Properties:
107 autodir
108 The top level autotest directory (/usr/local/autotest).
109 serverdir
110 <autodir>/server/
111 clientdir
112 <autodir>/client/
113 conmuxdir
114 <autodir>/conmux/
115 testdir
116 <autodir>/server/tests/
117 control
118 the control file for this job
119 """
120
mbligh18420c22007-10-16 22:27:14 +0000121 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +0000122 """
123 control
124 The control file (pathname of)
125 args
126 args to pass to the control file
127 resultdir
128 where to throw the results
mbligh18420c22007-10-16 22:27:14 +0000129 label
130 label for the job
mblighf1c52842007-10-16 15:21:38 +0000131 user
132 Username for the job (email address)
133 client
134 True if a client-side control file
135 """
mbligh05269362007-10-16 16:58:11 +0000136 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +0000137 self.autodir = os.path.abspath(os.path.join(path, '..'))
138 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +0000139 self.testdir = os.path.join(self.serverdir, 'tests')
140 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +0000141 self.conmuxdir = os.path.join(self.autodir, 'conmux')
142 self.clientdir = os.path.join(self.autodir, 'client')
143 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
144 self.resultdir = resultdir
145 if not os.path.exists(resultdir):
146 os.mkdir(resultdir)
mbligh3ccb8592007-11-05 18:13:40 +0000147 self.debugdir = os.path.join(resultdir, 'debug')
148 if not os.path.exists(self.debugdir):
149 os.mkdir(self.debugdir)
mbligh3dcf2c92007-10-16 22:24:00 +0000150 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +0000151 self.label = label
mblighf1c52842007-10-16 15:21:38 +0000152 self.user = user
153 self.args = args
154 self.client = client
155 self.record_prefix = ''
156
mbligh3f4bced2007-11-05 17:55:53 +0000157 self.stdout = fd_stack.fd_stack(1, sys.stdout)
158 self.stderr = fd_stack.fd_stack(2, sys.stderr)
159
mbligh3dcf2c92007-10-16 22:24:00 +0000160 if os.path.exists(self.status):
161 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000162 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000163 write_keyval(self.resultdir, job_data)
164
165
mblighf36243d2007-10-30 15:36:16 +0000166 def run(self, machines, reboot = False, install_before = False,
167 install_after = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000168 # use a copy so changes don't affect the original dictionary
169 namespace = namespace.copy()
170
mblighf1c52842007-10-16 15:21:38 +0000171 namespace['machines'] = machines
172 namespace['args'] = self.args
173 namespace['job'] = self
mbligh6e294382007-11-05 18:11:29 +0000174 test_start_time = int(time.time())
mblighf1c52842007-10-16 15:21:38 +0000175
mbligh87c5d882007-10-29 17:07:24 +0000176 os.chdir(self.resultdir)
177
178 status_log = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000179 try:
mblighf36243d2007-10-30 15:36:16 +0000180 if install_before and machines:
181 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000182 if self.client:
183 namespace['control'] = self.control
184 open('control', 'w').write(self.control)
185 open('control.srv', 'w').write(client_wrapper)
186 server_control = client_wrapper
187 else:
188 open('control.srv', 'w').write(self.control)
189 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000190 exec(preamble + server_control, namespace, namespace)
191
192 finally:
mbligh6e294382007-11-05 18:11:29 +0000193 if machines:
194 namespace['test_start_time'] = test_start_time
195 exec(preamble + crashdumps, namespace,
196 namespace)
mblighf1c52842007-10-16 15:21:38 +0000197 if reboot and machines:
198 exec(preamble + cleanup, namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000199 if install_after and machines:
200 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000201
202
203 def run_test(self, url, *args, **dargs):
204 """Summon a test object and run it.
205
206 tag
207 tag to add to testname
208 url
209 url of the test to run
210 """
211
mblighf1c52842007-10-16 15:21:38 +0000212 (group, testname) = test.testname(url)
213 tag = None
214 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000215
mblighf1c52842007-10-16 15:21:38 +0000216 if dargs.has_key('tag'):
217 tag = dargs['tag']
218 del dargs['tag']
219 if tag:
220 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000221
mbligh43ac5222007-10-16 15:55:01 +0000222 try:
223 test.runtest(self, url, tag, args, dargs)
224 self.record('GOOD', subdir, testname, 'completed successfully')
225 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000226 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000227
228
229 def run_group(self, function, *args, **dargs):
230 """\
231 function:
232 subroutine to run
233 *args:
234 arguments for the function
235 """
236
237 result = None
238 name = function.__name__
239
240 # Allow the tag for the group to be specified.
241 if dargs.has_key('tag'):
242 tag = dargs['tag']
243 del dargs['tag']
244 if tag:
245 name = tag
246
247 # if tag:
248 # name += '.' + tag
249 old_record_prefix = self.record_prefix
250 try:
251 try:
252 self.record('START', None, name)
253 self.record_prefix += '\t'
254 result = function(*args, **dargs)
255 self.record_prefix = old_record_prefix
256 self.record('END GOOD', None, name)
257 except:
258 self.record_prefix = old_record_prefix
259 self.record('END FAIL', None, name, format_error())
260 # We don't want to raise up an error higher if it's just
261 # a TestError - we want to carry on to other tests. Hence
262 # this outer try/except block.
263 except TestError:
264 pass
265 except:
266 raise TestError(name + ' failed\n' + format_error())
267
268 return result
269
270
271 def record(self, status_code, subdir, operation, status = ''):
272 """
273 Record job-level status
274
275 The intent is to make this file both machine parseable and
276 human readable. That involves a little more complexity, but
277 really isn't all that bad ;-)
278
279 Format is <status code>\t<subdir>\t<operation>\t<status>
280
281 status code: (GOOD|WARN|FAIL|ABORT)
282 or START
283 or END (GOOD|WARN|FAIL|ABORT)
284
285 subdir: MUST be a relevant subdirectory in the results,
286 or None, which will be represented as '----'
287
288 operation: description of what you ran (e.g. "dbench", or
289 "mkfs -t foobar /dev/sda9")
290
291 status: error message or "completed sucessfully"
292
293 ------------------------------------------------------------
294
295 Initial tabs indicate indent levels for grouping, and is
296 governed by self.record_prefix
297
298 multiline messages have secondary lines prefaced by a double
299 space (' ')
300 """
301
302 if subdir:
303 if re.match(r'[\n\t]', subdir):
304 raise "Invalid character in subdir string"
305 substr = subdir
306 else:
307 substr = '----'
308
309 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
310 status_code):
311 raise "Invalid status code supplied: %s" % status_code
312 if re.match(r'[\n\t]', operation):
313 raise "Invalid character in operation string"
314 operation = operation.rstrip()
315 status = status.rstrip()
316 status = re.sub(r"\t", " ", status)
317 # Ensure any continuation lines are marked so we can
318 # detect them in the status file to ensure it is parsable.
319 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
320
mbligh30270302007-11-05 20:33:52 +0000321 # Generate timestamps for inclusion in the logs
322 epoch_time = int(time.time()) # seconds since epoch, in UTC
323 local_time = time.localtime(epoch_time)
324 epoch_time_str = "timestamp=%d" % (epoch_time,)
325 local_time_str = time.strftime("localtime=%b %d %H:%M:%S",
326 local_time)
327
328 msg = '\t'.join(str(x) for x in (status_code, substr, operation,
329 epoch_time_str, local_time_str,
330 status))
mblighf1c52842007-10-16 15:21:38 +0000331
mbligh31a49de2007-11-05 18:41:19 +0000332 status_file = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000333 print msg
334 open(status_file, "a").write(self.record_prefix + msg + "\n")
335 if subdir:
336 status_file = os.path.join(self.resultdir, subdir, 'status')
337 open(status_file, "a").write(msg + "\n")