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 | |
| 14 | import os, sys, re |
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 | |
| 19 | preamble = """\ |
| 20 | import os, sys |
mbligh | 87c5d88 | 2007-10-29 17:07:24 +0000 | [diff] [blame] | 21 | sys.stderr = __stderr |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 22 | |
| 23 | import errors, hosts, autotest, kvm |
| 24 | import source_kernel, rpm_kernel, deb_kernel |
| 25 | from subcommand import * |
| 26 | from utils import run, get_tmp_dir, sh_escape |
| 27 | |
| 28 | """ |
| 29 | |
| 30 | client_wrapper = """ |
| 31 | at = autotest.Autotest() |
| 32 | |
| 33 | def run_client(machine): |
| 34 | host = hosts.SSHHost(machine) |
| 35 | at.run(control, host=host) |
| 36 | |
| 37 | if len(machines) > 1: |
| 38 | parallel_simple(run_client, machines) |
| 39 | else: |
| 40 | run_client(machines[0]) |
| 41 | """ |
| 42 | |
| 43 | cleanup="""\ |
| 44 | def cleanup(machine): |
| 45 | host = hosts.SSHHost(machine, initialize=False) |
| 46 | host.reboot() |
| 47 | |
mbligh | 84c0ab1 | 2007-10-24 21:28:58 +0000 | [diff] [blame] | 48 | parallel_simple(cleanup, machines, log=False) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 49 | """ |
| 50 | |
| 51 | class 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 | |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 69 | def __init__(self, control, args, resultdir, label, user, client=False): |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 70 | """ |
| 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 |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 77 | label |
| 78 | label for the job |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 79 | user |
| 80 | Username for the job (email address) |
| 81 | client |
| 82 | True if a client-side control file |
| 83 | """ |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 84 | path = os.path.dirname(sys.modules['server_job'].__file__) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 85 | self.autodir = os.path.abspath(os.path.join(path, '..')) |
| 86 | self.serverdir = os.path.join(self.autodir, 'server') |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 87 | self.testdir = os.path.join(self.serverdir, 'tests') |
| 88 | self.tmpdir = os.path.join(self.serverdir, 'tmp') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 89 | 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) |
mbligh | 3dcf2c9 | 2007-10-16 22:24:00 +0000 | [diff] [blame] | 95 | self.status = os.path.join(resultdir, 'status') |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 96 | self.label = label |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 97 | self.user = user |
| 98 | self.args = args |
| 99 | self.client = client |
| 100 | self.record_prefix = '' |
| 101 | |
mbligh | 3dcf2c9 | 2007-10-16 22:24:00 +0000 | [diff] [blame] | 102 | if os.path.exists(self.status): |
| 103 | os.unlink(self.status) |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 104 | job_data = { 'label' : label, 'user' : user} |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 105 | write_keyval(self.resultdir, job_data) |
| 106 | |
| 107 | |
| 108 | def run(self, machines, reboot = False, namespace = {}): |
mbligh | 60dbd50 | 2007-10-26 14:59:31 +0000 | [diff] [blame] | 109 | # use a copy so changes don't affect the original dictionary |
| 110 | namespace = namespace.copy() |
| 111 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 112 | namespace['machines'] = machines |
| 113 | namespace['args'] = self.args |
| 114 | namespace['job'] = self |
| 115 | |
mbligh | 87c5d88 | 2007-10-29 17:07:24 +0000 | [diff] [blame] | 116 | os.chdir(self.resultdir) |
| 117 | |
| 118 | status_log = os.path.join(self.resultdir, 'status.log') |
| 119 | namespace['__stderr'] = open(status_log, 'a', 0) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 120 | 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 |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 129 | 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 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 145 | (group, testname) = test.testname(url) |
| 146 | tag = None |
| 147 | subdir = testname |
mbligh | 43ac522 | 2007-10-16 15:55:01 +0000 | [diff] [blame] | 148 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 149 | if dargs.has_key('tag'): |
| 150 | tag = dargs['tag'] |
| 151 | del dargs['tag'] |
| 152 | if tag: |
| 153 | subdir += '.' + tag |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 154 | |
mbligh | 43ac522 | 2007-10-16 15:55:01 +0000 | [diff] [blame] | 155 | try: |
| 156 | test.runtest(self, url, tag, args, dargs) |
| 157 | self.record('GOOD', subdir, testname, 'completed successfully') |
| 158 | except Exception, detail: |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 159 | self.record('FAIL', subdir, testname, format_error()) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 160 | |
| 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') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 257 | 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") |