blob: 39ab3525cf2083bd6b33b670d2f26f92670d552d [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
34"""
35
36client_wrapper = """
37at = autotest.Autotest()
38
39def run_client(machine):
40 host = hosts.SSHHost(machine)
41 at.run(control, host=host)
42
43if len(machines) > 1:
mbligh7b32ba32007-11-05 18:14:20 +000044 open('.machines', 'w').write('\\n'.join(machines) + '\\n')
mblighf1c52842007-10-16 15:21:38 +000045 parallel_simple(run_client, machines)
46else:
47 run_client(machines[0])
48"""
49
mbligh303ccac2007-11-05 18:07:28 +000050crashdumps = """
51def crashdumps(machine):
52 host = hosts.SSHHost(machine, initialize=False)
53 host.get_crashdumps(test_start_time)
54
55parallel_simple(crashdumps, machines, log=False)
56"""
57
mblighf1c52842007-10-16 15:21:38 +000058cleanup="""\
59def cleanup(machine):
mbligh17f0c662007-11-05 18:28:19 +000060 host = hosts.SSHHost(machine, initialize=False)
61 host.reboot()
mblighf1c52842007-10-16 15:21:38 +000062
mbligh84c0ab12007-10-24 21:28:58 +000063parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000064"""
65
mblighf36243d2007-10-30 15:36:16 +000066install="""\
67def install(machine):
mbligh17f0c662007-11-05 18:28:19 +000068 host = hosts.SSHHost(machine, initialize=False)
69 host.machine_install()
mblighf36243d2007-10-30 15:36:16 +000070
71parallel_simple(cleanup, machines, log=False)
72"""
73
mblighf1c52842007-10-16 15:21:38 +000074class server_job:
75 """The actual job against which we do everything.
76
77 Properties:
78 autodir
79 The top level autotest directory (/usr/local/autotest).
80 serverdir
81 <autodir>/server/
82 clientdir
83 <autodir>/client/
84 conmuxdir
85 <autodir>/conmux/
86 testdir
87 <autodir>/server/tests/
88 control
89 the control file for this job
90 """
91
mbligh18420c22007-10-16 22:27:14 +000092 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +000093 """
94 control
95 The control file (pathname of)
96 args
97 args to pass to the control file
98 resultdir
99 where to throw the results
mbligh18420c22007-10-16 22:27:14 +0000100 label
101 label for the job
mblighf1c52842007-10-16 15:21:38 +0000102 user
103 Username for the job (email address)
104 client
105 True if a client-side control file
106 """
mbligh05269362007-10-16 16:58:11 +0000107 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +0000108 self.autodir = os.path.abspath(os.path.join(path, '..'))
109 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +0000110 self.testdir = os.path.join(self.serverdir, 'tests')
111 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +0000112 self.conmuxdir = os.path.join(self.autodir, 'conmux')
113 self.clientdir = os.path.join(self.autodir, 'client')
114 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
115 self.resultdir = resultdir
116 if not os.path.exists(resultdir):
117 os.mkdir(resultdir)
mbligh3ccb8592007-11-05 18:13:40 +0000118 self.debugdir = os.path.join(resultdir, 'debug')
119 if not os.path.exists(self.debugdir):
120 os.mkdir(self.debugdir)
mbligh3dcf2c92007-10-16 22:24:00 +0000121 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +0000122 self.label = label
mblighf1c52842007-10-16 15:21:38 +0000123 self.user = user
124 self.args = args
125 self.client = client
126 self.record_prefix = ''
127
mbligh3f4bced2007-11-05 17:55:53 +0000128 self.stdout = fd_stack.fd_stack(1, sys.stdout)
129 self.stderr = fd_stack.fd_stack(2, sys.stderr)
130
mbligh3dcf2c92007-10-16 22:24:00 +0000131 if os.path.exists(self.status):
132 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000133 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000134 write_keyval(self.resultdir, job_data)
135
136
mblighf36243d2007-10-30 15:36:16 +0000137 def run(self, machines, reboot = False, install_before = False,
138 install_after = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000139 # use a copy so changes don't affect the original dictionary
140 namespace = namespace.copy()
141
mblighf1c52842007-10-16 15:21:38 +0000142 namespace['machines'] = machines
143 namespace['args'] = self.args
144 namespace['job'] = self
mbligh6e294382007-11-05 18:11:29 +0000145 test_start_time = int(time.time())
mblighf1c52842007-10-16 15:21:38 +0000146
mbligh87c5d882007-10-29 17:07:24 +0000147 os.chdir(self.resultdir)
148
149 status_log = os.path.join(self.resultdir, 'status.log')
mblighf1c52842007-10-16 15:21:38 +0000150 try:
mblighf36243d2007-10-30 15:36:16 +0000151 if install_before and machines:
152 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000153 if self.client:
154 namespace['control'] = self.control
155 open('control', 'w').write(self.control)
156 open('control.srv', 'w').write(client_wrapper)
157 server_control = client_wrapper
158 else:
159 open('control.srv', 'w').write(self.control)
160 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000161 exec(preamble + server_control, namespace, namespace)
162
163 finally:
mbligh6e294382007-11-05 18:11:29 +0000164 if machines:
165 namespace['test_start_time'] = test_start_time
166 exec(preamble + crashdumps, namespace,
167 namespace)
mblighf1c52842007-10-16 15:21:38 +0000168 if reboot and machines:
169 exec(preamble + cleanup, namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000170 if install_after and machines:
171 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000172
173
174 def run_test(self, url, *args, **dargs):
175 """Summon a test object and run it.
176
177 tag
178 tag to add to testname
179 url
180 url of the test to run
181 """
182
mblighf1c52842007-10-16 15:21:38 +0000183 (group, testname) = test.testname(url)
184 tag = None
185 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000186
mblighf1c52842007-10-16 15:21:38 +0000187 if dargs.has_key('tag'):
188 tag = dargs['tag']
189 del dargs['tag']
190 if tag:
191 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000192
mbligh43ac5222007-10-16 15:55:01 +0000193 try:
194 test.runtest(self, url, tag, args, dargs)
195 self.record('GOOD', subdir, testname, 'completed successfully')
196 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000197 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000198
199
200 def run_group(self, function, *args, **dargs):
201 """\
202 function:
203 subroutine to run
204 *args:
205 arguments for the function
206 """
207
208 result = None
209 name = function.__name__
210
211 # Allow the tag for the group to be specified.
212 if dargs.has_key('tag'):
213 tag = dargs['tag']
214 del dargs['tag']
215 if tag:
216 name = tag
217
218 # if tag:
219 # name += '.' + tag
220 old_record_prefix = self.record_prefix
221 try:
222 try:
223 self.record('START', None, name)
224 self.record_prefix += '\t'
225 result = function(*args, **dargs)
226 self.record_prefix = old_record_prefix
227 self.record('END GOOD', None, name)
228 except:
229 self.record_prefix = old_record_prefix
230 self.record('END FAIL', None, name, format_error())
231 # We don't want to raise up an error higher if it's just
232 # a TestError - we want to carry on to other tests. Hence
233 # this outer try/except block.
234 except TestError:
235 pass
236 except:
237 raise TestError(name + ' failed\n' + format_error())
238
239 return result
240
241
242 def record(self, status_code, subdir, operation, status = ''):
243 """
244 Record job-level status
245
246 The intent is to make this file both machine parseable and
247 human readable. That involves a little more complexity, but
248 really isn't all that bad ;-)
249
250 Format is <status code>\t<subdir>\t<operation>\t<status>
251
252 status code: (GOOD|WARN|FAIL|ABORT)
253 or START
254 or END (GOOD|WARN|FAIL|ABORT)
255
256 subdir: MUST be a relevant subdirectory in the results,
257 or None, which will be represented as '----'
258
259 operation: description of what you ran (e.g. "dbench", or
260 "mkfs -t foobar /dev/sda9")
261
262 status: error message or "completed sucessfully"
263
264 ------------------------------------------------------------
265
266 Initial tabs indicate indent levels for grouping, and is
267 governed by self.record_prefix
268
269 multiline messages have secondary lines prefaced by a double
270 space (' ')
271 """
272
273 if subdir:
274 if re.match(r'[\n\t]', subdir):
275 raise "Invalid character in subdir string"
276 substr = subdir
277 else:
278 substr = '----'
279
280 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
281 status_code):
282 raise "Invalid status code supplied: %s" % status_code
283 if re.match(r'[\n\t]', operation):
284 raise "Invalid character in operation string"
285 operation = operation.rstrip()
286 status = status.rstrip()
287 status = re.sub(r"\t", " ", status)
288 # Ensure any continuation lines are marked so we can
289 # detect them in the status file to ensure it is parsable.
290 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
291
292 msg = '%s\t%s\t%s\t%s' %(status_code, substr, operation, status)
293
294 status_file = os.path.join(self.resultdir, 'status')
mblighf1c52842007-10-16 15:21:38 +0000295 print msg
296 open(status_file, "a").write(self.record_prefix + msg + "\n")
297 if subdir:
298 status_file = os.path.join(self.resultdir, subdir, 'status')
299 open(status_file, "a").write(msg + "\n")