blob: 2a06c3e60a5c219b07dfcda162ed50bb4df19314 [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
19preamble = """\
20import os, sys
mbligh87c5d882007-10-29 17:07:24 +000021sys.stderr = __stderr
mblighf1c52842007-10-16 15:21:38 +000022
23import errors, hosts, autotest, kvm
24import source_kernel, rpm_kernel, deb_kernel
25from subcommand import *
26from utils import run, get_tmp_dir, sh_escape
27
28"""
29
30client_wrapper = """
31at = autotest.Autotest()
32
33def run_client(machine):
34 host = hosts.SSHHost(machine)
35 at.run(control, host=host)
36
37if len(machines) > 1:
38 parallel_simple(run_client, machines)
39else:
40 run_client(machines[0])
41"""
42
43cleanup="""\
44def cleanup(machine):
45 host = hosts.SSHHost(machine, initialize=False)
46 host.reboot()
47
mbligh84c0ab12007-10-24 21:28:58 +000048parallel_simple(cleanup, machines, log=False)
mblighf1c52842007-10-16 15:21:38 +000049"""
50
51class server_job:
52 """The actual job against which we do everything.
53
54 Properties:
55 autodir
56 The top level autotest directory (/usr/local/autotest).
57 serverdir
58 <autodir>/server/
59 clientdir
60 <autodir>/client/
61 conmuxdir
62 <autodir>/conmux/
63 testdir
64 <autodir>/server/tests/
65 control
66 the control file for this job
67 """
68
mbligh18420c22007-10-16 22:27:14 +000069 def __init__(self, control, args, resultdir, label, user, client=False):
mblighf1c52842007-10-16 15:21:38 +000070 """
71 control
72 The control file (pathname of)
73 args
74 args to pass to the control file
75 resultdir
76 where to throw the results
mbligh18420c22007-10-16 22:27:14 +000077 label
78 label for the job
mblighf1c52842007-10-16 15:21:38 +000079 user
80 Username for the job (email address)
81 client
82 True if a client-side control file
83 """
mbligh05269362007-10-16 16:58:11 +000084 path = os.path.dirname(sys.modules['server_job'].__file__)
mblighf1c52842007-10-16 15:21:38 +000085 self.autodir = os.path.abspath(os.path.join(path, '..'))
86 self.serverdir = os.path.join(self.autodir, 'server')
mbligh05269362007-10-16 16:58:11 +000087 self.testdir = os.path.join(self.serverdir, 'tests')
88 self.tmpdir = os.path.join(self.serverdir, 'tmp')
mblighf1c52842007-10-16 15:21:38 +000089 self.conmuxdir = os.path.join(self.autodir, 'conmux')
90 self.clientdir = os.path.join(self.autodir, 'client')
91 self.control = re.sub('\r\n', '\n', open(control, 'r').read())
92 self.resultdir = resultdir
93 if not os.path.exists(resultdir):
94 os.mkdir(resultdir)
mbligh3dcf2c92007-10-16 22:24:00 +000095 self.status = os.path.join(resultdir, 'status')
mbligh18420c22007-10-16 22:27:14 +000096 self.label = label
mblighf1c52842007-10-16 15:21:38 +000097 self.user = user
98 self.args = args
99 self.client = client
100 self.record_prefix = ''
101
mbligh3dcf2c92007-10-16 22:24:00 +0000102 if os.path.exists(self.status):
103 os.unlink(self.status)
mbligh18420c22007-10-16 22:27:14 +0000104 job_data = { 'label' : label, 'user' : user}
mblighf1c52842007-10-16 15:21:38 +0000105 write_keyval(self.resultdir, job_data)
106
107
108 def run(self, machines, reboot = False, namespace = {}):
mbligh60dbd502007-10-26 14:59:31 +0000109 # use a copy so changes don't affect the original dictionary
110 namespace = namespace.copy()
111
mblighf1c52842007-10-16 15:21:38 +0000112 namespace['machines'] = machines
113 namespace['args'] = self.args
114 namespace['job'] = self
115
mbligh87c5d882007-10-29 17:07:24 +0000116 os.chdir(self.resultdir)
117
118 status_log = os.path.join(self.resultdir, 'status.log')
119 namespace['__stderr'] = open(status_log, 'a', 0)
mblighf1c52842007-10-16 15:21:38 +0000120 try:
121 if self.client:
122 namespace['control'] = self.control
123 open('control', 'w').write(self.control)
124 open('control.srv', 'w').write(client_wrapper)
125 server_control = client_wrapper
126 else:
127 open('control.srv', 'w').write(self.control)
128 server_control = self.control
mblighf1c52842007-10-16 15:21:38 +0000129 exec(preamble + server_control, namespace, namespace)
130
131 finally:
132 if reboot and machines:
133 exec(preamble + cleanup, namespace, namespace)
134
135
136 def run_test(self, url, *args, **dargs):
137 """Summon a test object and run it.
138
139 tag
140 tag to add to testname
141 url
142 url of the test to run
143 """
144
mblighf1c52842007-10-16 15:21:38 +0000145 (group, testname) = test.testname(url)
146 tag = None
147 subdir = testname
mbligh43ac5222007-10-16 15:55:01 +0000148
mblighf1c52842007-10-16 15:21:38 +0000149 if dargs.has_key('tag'):
150 tag = dargs['tag']
151 del dargs['tag']
152 if tag:
153 subdir += '.' + tag
mblighf1c52842007-10-16 15:21:38 +0000154
mbligh43ac5222007-10-16 15:55:01 +0000155 try:
156 test.runtest(self, url, tag, args, dargs)
157 self.record('GOOD', subdir, testname, 'completed successfully')
158 except Exception, detail:
mbligh05269362007-10-16 16:58:11 +0000159 self.record('FAIL', subdir, testname, format_error())
mblighf1c52842007-10-16 15:21:38 +0000160
161
162 def run_group(self, function, *args, **dargs):
163 """\
164 function:
165 subroutine to run
166 *args:
167 arguments for the function
168 """
169
170 result = None
171 name = function.__name__
172
173 # Allow the tag for the group to be specified.
174 if dargs.has_key('tag'):
175 tag = dargs['tag']
176 del dargs['tag']
177 if tag:
178 name = tag
179
180 # if tag:
181 # name += '.' + tag
182 old_record_prefix = self.record_prefix
183 try:
184 try:
185 self.record('START', None, name)
186 self.record_prefix += '\t'
187 result = function(*args, **dargs)
188 self.record_prefix = old_record_prefix
189 self.record('END GOOD', None, name)
190 except:
191 self.record_prefix = old_record_prefix
192 self.record('END FAIL', None, name, format_error())
193 # We don't want to raise up an error higher if it's just
194 # a TestError - we want to carry on to other tests. Hence
195 # this outer try/except block.
196 except TestError:
197 pass
198 except:
199 raise TestError(name + ' failed\n' + format_error())
200
201 return result
202
203
204 def record(self, status_code, subdir, operation, status = ''):
205 """
206 Record job-level status
207
208 The intent is to make this file both machine parseable and
209 human readable. That involves a little more complexity, but
210 really isn't all that bad ;-)
211
212 Format is <status code>\t<subdir>\t<operation>\t<status>
213
214 status code: (GOOD|WARN|FAIL|ABORT)
215 or START
216 or END (GOOD|WARN|FAIL|ABORT)
217
218 subdir: MUST be a relevant subdirectory in the results,
219 or None, which will be represented as '----'
220
221 operation: description of what you ran (e.g. "dbench", or
222 "mkfs -t foobar /dev/sda9")
223
224 status: error message or "completed sucessfully"
225
226 ------------------------------------------------------------
227
228 Initial tabs indicate indent levels for grouping, and is
229 governed by self.record_prefix
230
231 multiline messages have secondary lines prefaced by a double
232 space (' ')
233 """
234
235 if subdir:
236 if re.match(r'[\n\t]', subdir):
237 raise "Invalid character in subdir string"
238 substr = subdir
239 else:
240 substr = '----'
241
242 if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \
243 status_code):
244 raise "Invalid status code supplied: %s" % status_code
245 if re.match(r'[\n\t]', operation):
246 raise "Invalid character in operation string"
247 operation = operation.rstrip()
248 status = status.rstrip()
249 status = re.sub(r"\t", " ", status)
250 # Ensure any continuation lines are marked so we can
251 # detect them in the status file to ensure it is parsable.
252 status = re.sub(r"\n", "\n" + self.record_prefix + " ", status)
253
254 msg = '%s\t%s\t%s\t%s' %(status_code, substr, operation, status)
255
256 status_file = os.path.join(self.resultdir, 'status')
mblighf1c52842007-10-16 15:21:38 +0000257 print msg
258 open(status_file, "a").write(self.record_prefix + msg + "\n")
259 if subdir:
260 status_file = os.path.join(self.resultdir, subdir, 'status')
261 open(status_file, "a").write(msg + "\n")