mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 1 | """ |
| 2 | The main job wrapper for the server side. |
| 3 | |
| 4 | This is the core infrastructure. Derived from the client side job.py |
| 5 | |
| 6 | Copyright Martin J. Bligh, Andy Whitcroft 2007 |
| 7 | """ |
| 8 | |
| 9 | __author__ = """ |
| 10 | Martin J. Bligh <mbligh@google.com> |
| 11 | Andy Whitcroft <apw@shadowen.org> |
| 12 | """ |
| 13 | |
mbligh | 6e29438 | 2007-11-05 18:11:29 +0000 | [diff] [blame] | 14 | import os, sys, re, time |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 15 | import test |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 16 | from utils import * |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 17 | from error import * |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 18 | |
mbligh | 3f4bced | 2007-11-05 17:55:53 +0000 | [diff] [blame] | 19 | # this magic incantation should give us access to a client library |
| 20 | server_dir = os.path.dirname(__file__) |
| 21 | client_dir = os.path.join(server_dir, "..", "client", "bin") |
| 22 | sys.path.append(client_dir) |
| 23 | import fd_stack |
| 24 | sys.path.pop() |
| 25 | |
mbligh | ed5a410 | 2007-11-20 00:46:41 +0000 | [diff] [blame^] | 26 | # load up a control segment |
| 27 | # these are all stored in <server_dir>/control_segments |
| 28 | def load_control_segment(name): |
| 29 | server_dir = os.path.dirname(os.path.abspath(__file__)) |
| 30 | script_file = os.path.join(server_dir, |
| 31 | "control_segments", |
| 32 | name) |
| 33 | if os.path.exists(script_file): |
| 34 | return file(script_file).read() |
| 35 | else: |
| 36 | return "" |
| 37 | |
| 38 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 39 | preamble = """\ |
| 40 | import os, sys |
| 41 | |
| 42 | import errors, hosts, autotest, kvm |
| 43 | import source_kernel, rpm_kernel, deb_kernel |
| 44 | from subcommand import * |
| 45 | from utils import run, get_tmp_dir, sh_escape |
| 46 | |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 47 | autotest.Autotest.job = job |
mbligh | 31a49de | 2007-11-05 18:41:19 +0000 | [diff] [blame] | 48 | hosts.SSHHost.job = job |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 49 | """ |
| 50 | |
| 51 | client_wrapper = """ |
| 52 | at = autotest.Autotest() |
| 53 | |
| 54 | def run_client(machine): |
| 55 | host = hosts.SSHHost(machine) |
| 56 | at.run(control, host=host) |
| 57 | |
| 58 | if len(machines) > 1: |
mbligh | 7b32ba3 | 2007-11-05 18:14:20 +0000 | [diff] [blame] | 59 | open('.machines', 'w').write('\\n'.join(machines) + '\\n') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 60 | parallel_simple(run_client, machines) |
| 61 | else: |
| 62 | run_client(machines[0]) |
| 63 | """ |
| 64 | |
mbligh | 303ccac | 2007-11-05 18:07:28 +0000 | [diff] [blame] | 65 | crashdumps = """ |
| 66 | def crashdumps(machine): |
| 67 | host = hosts.SSHHost(machine, initialize=False) |
| 68 | host.get_crashdumps(test_start_time) |
| 69 | |
| 70 | parallel_simple(crashdumps, machines, log=False) |
| 71 | """ |
| 72 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 73 | cleanup="""\ |
| 74 | def cleanup(machine): |
mbligh | 17f0c66 | 2007-11-05 18:28:19 +0000 | [diff] [blame] | 75 | host = hosts.SSHHost(machine, initialize=False) |
| 76 | host.reboot() |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 77 | |
mbligh | 84c0ab1 | 2007-10-24 21:28:58 +0000 | [diff] [blame] | 78 | parallel_simple(cleanup, machines, log=False) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 79 | """ |
| 80 | |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 81 | install="""\ |
| 82 | def install(machine): |
mbligh | 17f0c66 | 2007-11-05 18:28:19 +0000 | [diff] [blame] | 83 | host = hosts.SSHHost(machine, initialize=False) |
| 84 | host.machine_install() |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 85 | |
mbligh | 009b25a | 2007-11-05 18:38:51 +0000 | [diff] [blame] | 86 | parallel_simple(install, machines, log=False) |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 87 | """ |
| 88 | |
mbligh | ed5a410 | 2007-11-20 00:46:41 +0000 | [diff] [blame^] | 89 | # load up the verifier preamble, with an optional site-specific hook |
| 90 | verify = load_control_segment("site_verify") |
| 91 | verify += load_control_segment("verify") |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 92 | |
| 93 | # This is pretty silly. Wait for s/w watchdog. Pray hard. |
| 94 | repair="""\ |
| 95 | def cleanup(machine): |
| 96 | host = hosts.SSHHost(machine, initialize=False) |
| 97 | host.ssh_ping(150*60) # wait for 2.5 hours |
| 98 | |
| 99 | parallel_simple(cleanup, machines, log=False) |
| 100 | """ |
| 101 | |
| 102 | def verify_machines(machines): |
| 103 | namespace = {'machines' : machines, 'job' : None} |
| 104 | exec(preamble + verify, namespace, namespace) |
| 105 | |
| 106 | |
| 107 | def repair_machines(machines): |
| 108 | namespace = {'machines' : machines, 'job' : None} |
| 109 | exec(preamble + repair, namespace, namespace) |
| 110 | |
| 111 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 112 | class server_job: |
| 113 | """The actual job against which we do everything. |
| 114 | |
| 115 | Properties: |
| 116 | autodir |
| 117 | The top level autotest directory (/usr/local/autotest). |
| 118 | serverdir |
| 119 | <autodir>/server/ |
| 120 | clientdir |
| 121 | <autodir>/client/ |
| 122 | conmuxdir |
| 123 | <autodir>/conmux/ |
| 124 | testdir |
| 125 | <autodir>/server/tests/ |
| 126 | control |
| 127 | the control file for this job |
| 128 | """ |
| 129 | |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 130 | def __init__(self, control, args, resultdir, label, user, client=False): |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 131 | """ |
| 132 | control |
| 133 | The control file (pathname of) |
| 134 | args |
| 135 | args to pass to the control file |
| 136 | resultdir |
| 137 | where to throw the results |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 138 | label |
| 139 | label for the job |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 140 | user |
| 141 | Username for the job (email address) |
| 142 | client |
| 143 | True if a client-side control file |
| 144 | """ |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 145 | path = os.path.dirname(sys.modules['server_job'].__file__) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 146 | self.autodir = os.path.abspath(os.path.join(path, '..')) |
| 147 | self.serverdir = os.path.join(self.autodir, 'server') |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 148 | self.testdir = os.path.join(self.serverdir, 'tests') |
| 149 | self.tmpdir = os.path.join(self.serverdir, 'tmp') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 150 | self.conmuxdir = os.path.join(self.autodir, 'conmux') |
| 151 | self.clientdir = os.path.join(self.autodir, 'client') |
| 152 | self.control = re.sub('\r\n', '\n', open(control, 'r').read()) |
| 153 | self.resultdir = resultdir |
| 154 | if not os.path.exists(resultdir): |
| 155 | os.mkdir(resultdir) |
mbligh | 3ccb859 | 2007-11-05 18:13:40 +0000 | [diff] [blame] | 156 | self.debugdir = os.path.join(resultdir, 'debug') |
| 157 | if not os.path.exists(self.debugdir): |
| 158 | os.mkdir(self.debugdir) |
mbligh | 3dcf2c9 | 2007-10-16 22:24:00 +0000 | [diff] [blame] | 159 | self.status = os.path.join(resultdir, 'status') |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 160 | self.label = label |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 161 | self.user = user |
| 162 | self.args = args |
| 163 | self.client = client |
| 164 | self.record_prefix = '' |
| 165 | |
mbligh | 3f4bced | 2007-11-05 17:55:53 +0000 | [diff] [blame] | 166 | self.stdout = fd_stack.fd_stack(1, sys.stdout) |
| 167 | self.stderr = fd_stack.fd_stack(2, sys.stderr) |
| 168 | |
mbligh | 3dcf2c9 | 2007-10-16 22:24:00 +0000 | [diff] [blame] | 169 | if os.path.exists(self.status): |
| 170 | os.unlink(self.status) |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 171 | job_data = { 'label' : label, 'user' : user} |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 172 | write_keyval(self.resultdir, job_data) |
| 173 | |
| 174 | |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 175 | def run(self, machines, reboot = False, install_before = False, |
| 176 | install_after = False, namespace = {}): |
mbligh | 60dbd50 | 2007-10-26 14:59:31 +0000 | [diff] [blame] | 177 | # use a copy so changes don't affect the original dictionary |
| 178 | namespace = namespace.copy() |
| 179 | |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 180 | self.aborted = False |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 181 | namespace['machines'] = machines |
| 182 | namespace['args'] = self.args |
| 183 | namespace['job'] = self |
mbligh | 6e29438 | 2007-11-05 18:11:29 +0000 | [diff] [blame] | 184 | test_start_time = int(time.time()) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 185 | |
mbligh | 87c5d88 | 2007-10-29 17:07:24 +0000 | [diff] [blame] | 186 | os.chdir(self.resultdir) |
| 187 | |
| 188 | status_log = os.path.join(self.resultdir, 'status.log') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 189 | try: |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 190 | if install_before and machines: |
| 191 | exec(preamble + install, namespace, namespace) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 192 | if self.client: |
| 193 | namespace['control'] = self.control |
| 194 | open('control', 'w').write(self.control) |
| 195 | open('control.srv', 'w').write(client_wrapper) |
| 196 | server_control = client_wrapper |
| 197 | else: |
| 198 | open('control.srv', 'w').write(self.control) |
| 199 | server_control = self.control |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 200 | exec(preamble + server_control, namespace, namespace) |
| 201 | |
| 202 | finally: |
mbligh | 6e29438 | 2007-11-05 18:11:29 +0000 | [diff] [blame] | 203 | if machines: |
| 204 | namespace['test_start_time'] = test_start_time |
| 205 | exec(preamble + crashdumps, namespace, |
| 206 | namespace) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 207 | if reboot and machines: |
| 208 | exec(preamble + cleanup, namespace, namespace) |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 209 | if install_after and machines: |
| 210 | exec(preamble + install, namespace, namespace) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | def run_test(self, url, *args, **dargs): |
| 214 | """Summon a test object and run it. |
| 215 | |
| 216 | tag |
| 217 | tag to add to testname |
| 218 | url |
| 219 | url of the test to run |
| 220 | """ |
| 221 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 222 | (group, testname) = test.testname(url) |
| 223 | tag = None |
| 224 | subdir = testname |
mbligh | 43ac522 | 2007-10-16 15:55:01 +0000 | [diff] [blame] | 225 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 226 | if dargs.has_key('tag'): |
| 227 | tag = dargs['tag'] |
| 228 | del dargs['tag'] |
| 229 | if tag: |
| 230 | subdir += '.' + tag |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 231 | |
mbligh | 43ac522 | 2007-10-16 15:55:01 +0000 | [diff] [blame] | 232 | try: |
| 233 | test.runtest(self, url, tag, args, dargs) |
| 234 | self.record('GOOD', subdir, testname, 'completed successfully') |
| 235 | except Exception, detail: |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 236 | self.record('FAIL', subdir, testname, format_error()) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def run_group(self, function, *args, **dargs): |
| 240 | """\ |
| 241 | function: |
| 242 | subroutine to run |
| 243 | *args: |
| 244 | arguments for the function |
| 245 | """ |
| 246 | |
| 247 | result = None |
| 248 | name = function.__name__ |
| 249 | |
| 250 | # Allow the tag for the group to be specified. |
| 251 | if dargs.has_key('tag'): |
| 252 | tag = dargs['tag'] |
| 253 | del dargs['tag'] |
| 254 | if tag: |
| 255 | name = tag |
| 256 | |
| 257 | # if tag: |
| 258 | # name += '.' + tag |
| 259 | old_record_prefix = self.record_prefix |
| 260 | try: |
| 261 | try: |
| 262 | self.record('START', None, name) |
| 263 | self.record_prefix += '\t' |
| 264 | result = function(*args, **dargs) |
| 265 | self.record_prefix = old_record_prefix |
| 266 | self.record('END GOOD', None, name) |
| 267 | except: |
| 268 | self.record_prefix = old_record_prefix |
| 269 | self.record('END FAIL', None, name, format_error()) |
| 270 | # We don't want to raise up an error higher if it's just |
| 271 | # a TestError - we want to carry on to other tests. Hence |
| 272 | # this outer try/except block. |
| 273 | except TestError: |
| 274 | pass |
| 275 | except: |
| 276 | raise TestError(name + ' failed\n' + format_error()) |
| 277 | |
| 278 | return result |
| 279 | |
| 280 | |
| 281 | def record(self, status_code, subdir, operation, status = ''): |
| 282 | """ |
| 283 | Record job-level status |
| 284 | |
| 285 | The intent is to make this file both machine parseable and |
| 286 | human readable. That involves a little more complexity, but |
| 287 | really isn't all that bad ;-) |
| 288 | |
| 289 | Format is <status code>\t<subdir>\t<operation>\t<status> |
| 290 | |
| 291 | status code: (GOOD|WARN|FAIL|ABORT) |
| 292 | or START |
| 293 | or END (GOOD|WARN|FAIL|ABORT) |
| 294 | |
| 295 | subdir: MUST be a relevant subdirectory in the results, |
| 296 | or None, which will be represented as '----' |
| 297 | |
| 298 | operation: description of what you ran (e.g. "dbench", or |
| 299 | "mkfs -t foobar /dev/sda9") |
| 300 | |
| 301 | status: error message or "completed sucessfully" |
| 302 | |
| 303 | ------------------------------------------------------------ |
| 304 | |
| 305 | Initial tabs indicate indent levels for grouping, and is |
| 306 | governed by self.record_prefix |
| 307 | |
| 308 | multiline messages have secondary lines prefaced by a double |
| 309 | space (' ') |
| 310 | """ |
| 311 | |
| 312 | if subdir: |
| 313 | if re.match(r'[\n\t]', subdir): |
| 314 | raise "Invalid character in subdir string" |
| 315 | substr = subdir |
| 316 | else: |
| 317 | substr = '----' |
| 318 | |
| 319 | if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \ |
| 320 | status_code): |
| 321 | raise "Invalid status code supplied: %s" % status_code |
| 322 | if re.match(r'[\n\t]', operation): |
| 323 | raise "Invalid character in operation string" |
| 324 | operation = operation.rstrip() |
| 325 | status = status.rstrip() |
| 326 | status = re.sub(r"\t", " ", status) |
| 327 | # Ensure any continuation lines are marked so we can |
| 328 | # detect them in the status file to ensure it is parsable. |
| 329 | status = re.sub(r"\n", "\n" + self.record_prefix + " ", status) |
| 330 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 331 | # Generate timestamps for inclusion in the logs |
| 332 | epoch_time = int(time.time()) # seconds since epoch, in UTC |
| 333 | local_time = time.localtime(epoch_time) |
| 334 | epoch_time_str = "timestamp=%d" % (epoch_time,) |
| 335 | local_time_str = time.strftime("localtime=%b %d %H:%M:%S", |
| 336 | local_time) |
| 337 | |
| 338 | msg = '\t'.join(str(x) for x in (status_code, substr, operation, |
| 339 | epoch_time_str, local_time_str, |
| 340 | status)) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 341 | |
mbligh | 31a49de | 2007-11-05 18:41:19 +0000 | [diff] [blame] | 342 | status_file = os.path.join(self.resultdir, 'status.log') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 343 | print msg |
| 344 | open(status_file, "a").write(self.record_prefix + msg + "\n") |
| 345 | if subdir: |
| 346 | status_file = os.path.join(self.resultdir, subdir, 'status') |
| 347 | open(status_file, "a").write(msg + "\n") |