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