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