blob: 8cc78ff4ccf1f40824d0a248a8d2a2bcbab99701 [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
14import os, sys, re
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
mbligh87c5d882007-10-29 17:07:24 +000028sys.stderr = __stderr
mblighf1c52842007-10-16 15:21:38 +000029
30import errors, hosts, autotest, kvm
31import source_kernel, rpm_kernel, deb_kernel
32from subcommand import *
33from utils import run, get_tmp_dir, sh_escape
34
35"""
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:
45 parallel_simple(run_client, machines)
46else:
47 run_client(machines[0])
48"""
49
50cleanup="""\
51def cleanup(machine):
52 host = hosts.SSHHost(machine, initialize=False)
53 host.reboot()
54
mbligh84c0ab12007-10-24 21:28:58 +000055parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000056"""
57
mblighf36243d2007-10-30 15:36:16 +000058install="""\
59def install(machine):
60 host = hosts.SSHHost(machine, initialize=False)
61 host.machine_install()
62
63parallel_simple(cleanup, machines, log=False)
64"""
65
mblighf1c52842007-10-16 15:21:38 +000066class server_job:
67 """The actual job against which we do everything.
68
69 Properties:
70 autodir
71 The top level autotest directory (/usr/local/autotest).
72 serverdir
73 <autodir>/server/
74 clientdir
75 <autodir>/client/
76 conmuxdir
77 <autodir>/conmux/
78 testdir
79 <autodir>/server/tests/
80 control
81 the control file for this job
82 """
83
mbligh18420c22007-10-16 22:27:14 +000084 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +000085 """
86 control
87 The control file (pathname of)
88 args
89 args to pass to the control file
90 resultdir
91 where to throw the results
mbligh18420c22007-10-16 22:27:14 +000092 label
93 label for the job
mblighf1c52842007-10-16 15:21:38 +000094 user
95 Username for the job (email address)
96 client
97 True if a client-side control file
98 """
mbligh05269362007-10-16 16:58:11 +000099 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +0000100 self.autodir = os.path.abspath(os.path.join(path, '..'))
101 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +0000102 self.testdir = os.path.join(self.serverdir, 'tests')
103 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +0000104 self.conmuxdir = os.path.join(self.autodir, 'conmux')
105 self.clientdir = os.path.join(self.autodir, 'client')
106 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
107 self.resultdir = resultdir
108 if not os.path.exists(resultdir):
109 os.mkdir(resultdir)
mbligh3dcf2c92007-10-16 22:24:00 +0000110 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +0000111 self.label = label
mblighf1c52842007-10-16 15:21:38 +0000112 self.user = user
113 self.args = args
114 self.client = client
115 self.record_prefix = ''
116
mbligh3f4bced2007-11-05 17:55:53 +0000117 self.stdout = fd_stack.fd_stack(1, sys.stdout)
118 self.stderr = fd_stack.fd_stack(2, sys.stderr)
119
mbligh3dcf2c92007-10-16 22:24:00 +0000120 if os.path.exists(self.status):
121 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000122 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000123 write_keyval(self.resultdir, job_data)
124
125
mblighf36243d2007-10-30 15:36:16 +0000126 def run(self, machines, reboot = False, install_before = False,
127 install_after = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000128 # use a copy so changes don't affect the original dictionary
129 namespace = namespace.copy()
130
mblighf1c52842007-10-16 15:21:38 +0000131 namespace['machines'] = machines
132 namespace['args'] = self.args
133 namespace['job'] = self
134
mbligh87c5d882007-10-29 17:07:24 +0000135 os.chdir(self.resultdir)
136
137 status_log = os.path.join(self.resultdir, 'status.log')
138 namespace['__stderr'] = open(status_log, 'a', 0)
mblighf1c52842007-10-16 15:21:38 +0000139 try:
mblighf36243d2007-10-30 15:36:16 +0000140 if install_before and machines:
141 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000142 if self.client:
143 namespace['control'] = self.control
144 open('control', 'w').write(self.control)
145 open('control.srv', 'w').write(client_wrapper)
146 server_control = client_wrapper
147 else:
148 open('control.srv', 'w').write(self.control)
149 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000150 exec(preamble + server_control, namespace, namespace)
151
152 finally:
153 if reboot and machines:
154 exec(preamble + cleanup, namespace, namespace)
mblighf36243d2007-10-30 15:36:16 +0000155 if install_after and machines:
156 exec(preamble + install, namespace, namespace)
mblighf1c52842007-10-16 15:21:38 +0000157
158
159 def run_test(self, url, *args, **dargs):
160 """Summon a test object and run it.
161
162 tag
163 tag to add to testname
164 url
165 url of the test to run
166 """
167
mblighf1c52842007-10-16 15:21:38 +0000168 (group, testname) = test.testname(url)
169 tag = None
170 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000171
mblighf1c52842007-10-16 15:21:38 +0000172 if dargs.has_key('tag'):
173 tag = dargs['tag']
174 del dargs['tag']
175 if tag:
176 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000177
mbligh43ac5222007-10-16 15:55:01 +0000178 try:
179 test.runtest(self, url, tag, args, dargs)
180 self.record('GOOD', subdir, testname, 'completed successfully')
181 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000182 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000183
184
185 def run_group(self, function, *args, **dargs):
186 """\
187 function:
188 subroutine to run
189 *args:
190 arguments for the function
191 """
192
193 result = None
194 name = function.__name__
195
196 # Allow the tag for the group to be specified.
197 if dargs.has_key('tag'):
198 tag = dargs['tag']
199 del dargs['tag']
200 if tag:
201 name = tag
202
203 # if tag:
204 # name += '.' + tag
205 old_record_prefix = self.record_prefix
206 try:
207 try:
208 self.record('START', None, name)
209 self.record_prefix += '\t'
210 result = function(*args, **dargs)
211 self.record_prefix = old_record_prefix
212 self.record('END GOOD', None, name)
213 except:
214 self.record_prefix = old_record_prefix
215 self.record('END FAIL', None, name, format_error())
216 # We don't want to raise up an error higher if it's just
217 # a TestError - we want to carry on to other tests. Hence
218 # this outer try/except block.
219 except TestError:
220 pass
221 except:
222 raise TestError(name + ' failed\n' + format_error())
223
224 return result
225
226
227 def record(self, status_code, subdir, operation, status = ''):
228 """
229 Record job-level status
230
231 The intent is to make this file both machine parseable and
232 human readable. That involves a little more complexity, but
233 really isn't all that bad ;-)
234
235 Format is <status code>\t<subdir>\t<operation>\t<status>
236
237 status code: (GOOD|WARN|FAIL|ABORT)
238 or START
239 or END (GOOD|WARN|FAIL|ABORT)
240
241 subdir: MUST be a relevant subdirectory in the results,
242 or None, which will be represented as '----'
243
244 operation: description of what you ran (e.g. "dbench", or
245 "mkfs -t foobar /dev/sda9")
246
247 status: error message or "completed sucessfully"
248
249 ------------------------------------------------------------
250
251 Initial tabs indicate indent levels for grouping, and is
252 governed by self.record_prefix
253
254 multiline messages have secondary lines prefaced by a double
255 space (' ')
256 """
257
258 if subdir:
259 if re.match(r'[\n\t]', subdir):
260 raise "Invalid character in subdir string"
261 substr = subdir
262 else:
263 substr = '----'
264
265 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
266 status_code):
267 raise "Invalid status code supplied: %s" % status_code
268 if re.match(r'[\n\t]', operation):
269 raise "Invalid character in operation string"
270 operation = operation.rstrip()
271 status = status.rstrip()
272 status = re.sub(r"\t", " ", status)
273 # Ensure any continuation lines are marked so we can
274 # detect them in the status file to ensure it is parsable.
275 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
276
277 msg = '%s\t%s\t%s\t%s' %(status_code, substr, operation, status)
278
279 status_file = os.path.join(self.resultdir, 'status')
mblighf1c52842007-10-16 15:21:38 +0000280 print msg
281 open(status_file, "a").write(self.record_prefix + msg + "\n")
282 if subdir:
283 status_file = os.path.join(self.resultdir, subdir, 'status')
284 open(status_file, "a").write(msg + "\n")