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