mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 1 | """The main job wrapper |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 2 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 3 | This is the core infrastructure. |
| 4 | """ |
| 5 | |
| 6 | __author__ = """Copyright Andy Whitcroft, Martin J. Bligh 2006""" |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 7 | |
mbligh | 8f243ec | 2006-10-10 05:55:49 +0000 | [diff] [blame] | 8 | # standard stuff |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 9 | import os, sys, re, pickle, shutil, time, traceback, types, copy |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 10 | |
mbligh | 8f243ec | 2006-10-10 05:55:49 +0000 | [diff] [blame] | 11 | # autotest stuff |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 12 | from autotest_lib.client.bin import autotest_utils |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame^] | 13 | from autotest_lib.client.common_lib import error, barrier, logging, utils |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 14 | |
| 15 | import parallel, kernel, xen, test, profilers, filesystem, fd_stack, boottool |
| 16 | import harness, config, sysinfo, cpuset |
| 17 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 18 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 19 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 20 | JOB_PREAMBLE = """ |
| 21 | from common.error import * |
| 22 | from autotest_utils import * |
| 23 | """ |
| 24 | |
| 25 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 26 | class StepError(error.AutotestError): |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 27 | pass |
| 28 | |
| 29 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 30 | class base_job: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 31 | """The actual job against which we do everything. |
| 32 | |
| 33 | Properties: |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 34 | autodir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 35 | The top level autotest directory (/usr/local/autotest). |
| 36 | Comes from os.environ['AUTODIR']. |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 37 | bindir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 38 | <autodir>/bin/ |
mbligh | d5a3883 | 2008-01-25 18:15:39 +0000 | [diff] [blame] | 39 | libdir |
| 40 | <autodir>/lib/ |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 41 | testdir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 42 | <autodir>/tests/ |
mbligh | 84bafdb | 2008-01-26 19:43:34 +0000 | [diff] [blame] | 43 | site_testdir |
| 44 | <autodir>/site_tests/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 45 | profdir |
| 46 | <autodir>/profilers/ |
| 47 | tmpdir |
| 48 | <autodir>/tmp/ |
| 49 | resultdir |
| 50 | <autodir>/results/<jobtag> |
| 51 | stdout |
| 52 | fd_stack object for stdout |
| 53 | stderr |
| 54 | fd_stack object for stderr |
| 55 | profilers |
| 56 | the profilers object for this job |
apw | 504a7dd | 2006-10-12 17:18:37 +0000 | [diff] [blame] | 57 | harness |
| 58 | the server harness object for this job |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 59 | config |
| 60 | the job configuration for this job |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 61 | """ |
| 62 | |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 63 | DEFAULT_LOG_FILENAME = "status" |
| 64 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 65 | def __init__(self, control, jobtag, cont, harness_type=None, |
| 66 | use_external_logging = False): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 67 | """ |
| 68 | control |
| 69 | The control file (pathname of) |
| 70 | jobtag |
| 71 | The job tag string (eg "default") |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 72 | cont |
| 73 | If this is the continuation of this job |
apw | e68a713 | 2006-12-01 11:21:37 +0000 | [diff] [blame] | 74 | harness_type |
| 75 | An alternative server harness |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 76 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 77 | self.autodir = os.environ['AUTODIR'] |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 78 | self.bindir = os.path.join(self.autodir, 'bin') |
mbligh | d5a3883 | 2008-01-25 18:15:39 +0000 | [diff] [blame] | 79 | self.libdir = os.path.join(self.autodir, 'lib') |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 80 | self.testdir = os.path.join(self.autodir, 'tests') |
mbligh | 84bafdb | 2008-01-26 19:43:34 +0000 | [diff] [blame] | 81 | self.site_testdir = os.path.join(self.autodir, 'site_tests') |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 82 | self.profdir = os.path.join(self.autodir, 'profilers') |
| 83 | self.tmpdir = os.path.join(self.autodir, 'tmp') |
| 84 | self.resultdir = os.path.join(self.autodir, 'results', jobtag) |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 85 | self.sysinfodir = os.path.join(self.resultdir, 'sysinfo') |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 86 | self.control = os.path.abspath(control) |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 87 | self.state_file = self.control + '.state' |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 88 | self.current_step_ancestry = [] |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 89 | self.next_step_index = 0 |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 90 | self.__load_state() |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 91 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 92 | if not cont: |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 93 | """ |
| 94 | Don't cleanup the tmp dir (which contains the lockfile) |
| 95 | in the constructor, this would be a problem for multiple |
| 96 | jobs starting at the same time on the same client. Instead |
| 97 | do the delete at the server side. We simply create the tmp |
| 98 | directory here if it does not already exist. |
| 99 | """ |
| 100 | if not os.path.exists(self.tmpdir): |
| 101 | os.mkdir(self.tmpdir) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 102 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 103 | results = os.path.join(self.autodir, 'results') |
| 104 | if not os.path.exists(results): |
| 105 | os.mkdir(results) |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 106 | |
apw | f3d2862 | 2007-09-25 16:49:17 +0000 | [diff] [blame] | 107 | download = os.path.join(self.testdir, 'download') |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 108 | if not os.path.exists(download): |
| 109 | os.mkdir(download) |
| 110 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 111 | if os.path.exists(self.resultdir): |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame^] | 112 | utils.system('rm -rf ' |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 113 | + self.resultdir) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 114 | os.mkdir(self.resultdir) |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 115 | os.mkdir(self.sysinfodir) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 116 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 117 | os.mkdir(os.path.join(self.resultdir, 'debug')) |
| 118 | os.mkdir(os.path.join(self.resultdir, 'analysis')) |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 119 | |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 120 | shutil.copyfile(self.control, |
| 121 | os.path.join(self.resultdir, 'control')) |
mbligh | f4ca14f | 2008-03-03 16:03:05 +0000 | [diff] [blame] | 122 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 123 | |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 124 | self.control = control |
mbligh | 2711360 | 2007-10-31 21:07:51 +0000 | [diff] [blame] | 125 | self.jobtag = jobtag |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 126 | self.log_filename = self.DEFAULT_LOG_FILENAME |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 127 | self.container = None |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 128 | |
mbligh | 56f1fbb | 2006-10-01 15:10:56 +0000 | [diff] [blame] | 129 | self.stdout = fd_stack.fd_stack(1, sys.stdout) |
| 130 | self.stderr = fd_stack.fd_stack(2, sys.stderr) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 131 | |
| 132 | self._init_group_level() |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 133 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 134 | self.config = config.config(self) |
| 135 | |
apw | d27e55f | 2006-12-01 11:22:08 +0000 | [diff] [blame] | 136 | self.harness = harness.select(harness_type, self) |
| 137 | |
mbligh | a35553b | 2006-04-23 15:52:25 +0000 | [diff] [blame] | 138 | self.profilers = profilers.profilers(self) |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 139 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 140 | try: |
apw | 90154af | 2006-12-01 11:23:36 +0000 | [diff] [blame] | 141 | tool = self.config_get('boottool.executable') |
| 142 | self.bootloader = boottool.boottool(tool) |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 143 | except: |
| 144 | pass |
| 145 | |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 146 | sysinfo.log_per_reboot_data(self.sysinfodir) |
mbligh | 3a6d6ca | 2006-04-23 15:50:24 +0000 | [diff] [blame] | 147 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 148 | if not cont: |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 149 | self.record('START', None, None) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 150 | self._increment_group_level() |
apw | 357f50f | 2006-12-01 11:22:39 +0000 | [diff] [blame] | 151 | |
apw | f91efaf | 2007-11-24 17:32:13 +0000 | [diff] [blame] | 152 | self.harness.run_start() |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 153 | |
| 154 | if use_external_logging: |
| 155 | self.enable_external_logging() |
apw | f91efaf | 2007-11-24 17:32:13 +0000 | [diff] [blame] | 156 | |
jadmanski | 8415f96 | 2008-05-06 20:38:53 +0000 | [diff] [blame] | 157 | # load the max disk usage rate - default to no monitoring |
| 158 | self.max_disk_usage_rate = self.get_state('__monitor_disk', |
| 159 | default=0.0) |
| 160 | |
| 161 | |
| 162 | def monitor_disk_usage(self, max_rate): |
| 163 | """\ |
| 164 | Signal that the job should monitor disk space usage on / |
| 165 | and generate a warning if a test uses up disk space at a |
| 166 | rate exceeding 'max_rate'. |
| 167 | |
| 168 | Parameters: |
| 169 | max_rate - the maximium allowed rate of disk consumption |
| 170 | during a test, in MB/hour, or 0 to indicate |
| 171 | no limit. |
| 172 | """ |
| 173 | self.set_state('__monitor_disk', max_rate) |
| 174 | self.max_disk_usage_rate = max_rate |
| 175 | |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 176 | |
| 177 | def relative_path(self, path): |
| 178 | """\ |
| 179 | Return a patch relative to the job results directory |
| 180 | """ |
mbligh | 1c250ca | 2007-08-30 16:31:38 +0000 | [diff] [blame] | 181 | head = len(self.resultdir) + 1 # remove the / inbetween |
| 182 | return path[head:] |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 183 | |
| 184 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 185 | def control_get(self): |
| 186 | return self.control |
| 187 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 188 | |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 189 | def control_set(self, control): |
| 190 | self.control = os.path.abspath(control) |
| 191 | |
| 192 | |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 193 | def harness_select(self, which): |
| 194 | self.harness = harness.select(which, self) |
| 195 | |
| 196 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 197 | def config_set(self, name, value): |
| 198 | self.config.set(name, value) |
| 199 | |
| 200 | |
| 201 | def config_get(self, name): |
| 202 | return self.config.get(name) |
| 203 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 204 | def setup_dirs(self, results_dir, tmp_dir): |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 205 | if not tmp_dir: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 206 | tmp_dir = os.path.join(self.tmpdir, 'build') |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 207 | if not os.path.exists(tmp_dir): |
| 208 | os.mkdir(tmp_dir) |
| 209 | if not os.path.isdir(tmp_dir): |
mbligh | 642b03e | 2008-01-14 16:53:15 +0000 | [diff] [blame] | 210 | e_msg = "Temp dir (%s) is not a dir - args backwards?" % self.tmpdir |
| 211 | raise ValueError(e_msg) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 212 | |
| 213 | # We label the first build "build" and then subsequent ones |
| 214 | # as "build.2", "build.3", etc. Whilst this is a little bit |
| 215 | # inconsistent, 99.9% of jobs will only have one build |
| 216 | # (that's not done as kernbench, sparse, or buildtest), |
| 217 | # so it works out much cleaner. One of life's comprimises. |
| 218 | if not results_dir: |
| 219 | results_dir = os.path.join(self.resultdir, 'build') |
| 220 | i = 2 |
| 221 | while os.path.exists(results_dir): |
| 222 | results_dir = os.path.join(self.resultdir, 'build.%d' % i) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 223 | i += 1 |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 224 | if not os.path.exists(results_dir): |
| 225 | os.mkdir(results_dir) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 226 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 227 | return (results_dir, tmp_dir) |
| 228 | |
| 229 | |
| 230 | def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \ |
| 231 | kjob = None ): |
| 232 | """Summon a xen object""" |
| 233 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
| 234 | build_dir = 'xen' |
| 235 | return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob) |
| 236 | |
| 237 | |
| 238 | def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False): |
| 239 | """Summon a kernel object""" |
mbligh | 669caa1 | 2007-11-05 18:32:13 +0000 | [diff] [blame] | 240 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 241 | build_dir = 'linux' |
mbligh | 6ee7ee0 | 2007-11-13 23:49:05 +0000 | [diff] [blame] | 242 | return kernel.auto_kernel(self, base_tree, results_dir, |
| 243 | tmp_dir, build_dir, leave) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 244 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 245 | |
mbligh | 6b504ff | 2007-12-12 21:03:49 +0000 | [diff] [blame] | 246 | def barrier(self, *args, **kwds): |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 247 | """Create a barrier object""" |
mbligh | 6b504ff | 2007-12-12 21:03:49 +0000 | [diff] [blame] | 248 | return barrier.barrier(*args, **kwds) |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 249 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 250 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 251 | def setup_dep(self, deps): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 252 | """Set up the dependencies for this test. |
| 253 | |
| 254 | deps is a list of libraries required for this test. |
| 255 | """ |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 256 | for dep in deps: |
| 257 | try: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 258 | os.chdir(os.path.join(self.autodir, 'deps', dep)) |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame^] | 259 | utils.system('./' + dep + '.py') |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 260 | except: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 261 | err = "setting up dependency " + dep + "\n" |
| 262 | raise error.UnhandledError(err) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 263 | |
| 264 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 265 | def __runtest(self, url, tag, args, dargs): |
| 266 | try: |
mbligh | 53c4150 | 2007-10-23 20:45:04 +0000 | [diff] [blame] | 267 | l = lambda : test.runtest(self, url, tag, args, dargs) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 268 | pid = parallel.fork_start(self.resultdir, l) |
| 269 | parallel.fork_waitfor(self.resultdir, pid) |
| 270 | except error.AutotestError: |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 271 | raise |
jadmanski | cf8c4d6 | 2008-05-27 22:09:14 +0000 | [diff] [blame] | 272 | except Exception, e: |
| 273 | msg = "Unhandled %s error occured during test\n" |
| 274 | msg %= str(e.__class__.__name__) |
| 275 | raise error.UnhandledError(msg) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 276 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 277 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 278 | def run_test(self, url, *args, **dargs): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 279 | """Summon a test object and run it. |
| 280 | |
| 281 | tag |
| 282 | tag to add to testname |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 283 | url |
| 284 | url of the test to run |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 285 | """ |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 286 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 287 | if not url: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 288 | raise TypeError("Test name is invalid. " |
| 289 | "Switched arguments?") |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 290 | (group, testname) = test.testname(url) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 291 | tag = dargs.pop('tag', None) |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 292 | container = dargs.pop('container', None) |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 293 | subdir = testname |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 294 | if tag: |
| 295 | subdir += '.' + tag |
| 296 | |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 297 | if container: |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 298 | cname = container.get('name', None) |
| 299 | if not cname: # get old name |
| 300 | cname = container.get('container_name', None) |
| 301 | mbytes = container.get('mbytes', None) |
| 302 | if not mbytes: # get old name |
| 303 | mbytes = container.get('mem', None) |
| 304 | cpus = container.get('cpus', None) |
| 305 | if not cpus: # get old name |
| 306 | cpus = container.get('cpu', None) |
jadmanski | 87cbc7f | 2008-05-13 18:17:10 +0000 | [diff] [blame] | 307 | root = container.get('root', None) |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 308 | self.new_container(mbytes=mbytes, cpus=cpus, |
| 309 | root=root, name=cname) |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 310 | # We are running in a container now... |
| 311 | |
jadmanski | 8415f96 | 2008-05-06 20:38:53 +0000 | [diff] [blame] | 312 | def log_warning(reason): |
| 313 | self.record("WARN", subdir, testname, reason) |
| 314 | @disk_usage_monitor.watch(log_warning, "/", |
| 315 | self.max_disk_usage_rate) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 316 | def group_func(): |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 317 | try: |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 318 | self.__runtest(url, tag, args, dargs) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 319 | except error.TestNAError, detail: |
| 320 | self.record('TEST_NA', subdir, testname, |
| 321 | str(detail)) |
| 322 | raise |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 323 | except Exception, detail: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 324 | self.record('FAIL', subdir, testname, |
| 325 | str(detail)) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 326 | raise |
| 327 | else: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 328 | self.record('GOOD', subdir, testname, |
| 329 | 'completed successfully') |
jadmanski | 8415f96 | 2008-05-06 20:38:53 +0000 | [diff] [blame] | 330 | |
mbligh | cfc6dd3 | 2007-11-20 00:44:35 +0000 | [diff] [blame] | 331 | result, exc_info = self.__rungroup(subdir, group_func) |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 332 | if container: |
| 333 | self.release_container() |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 334 | if exc_info and isinstance(exc_info[1], error.TestError): |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 335 | return False |
| 336 | elif exc_info: |
mbligh | 71ea249 | 2008-01-15 20:35:52 +0000 | [diff] [blame] | 337 | raise exc_info[0], exc_info[1], exc_info[2] |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 338 | else: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 339 | return True |
| 340 | |
| 341 | |
| 342 | def __rungroup(self, name, function, *args, **dargs): |
| 343 | """\ |
| 344 | name: |
| 345 | name of the group |
| 346 | function: |
| 347 | subroutine to run |
| 348 | *args: |
| 349 | arguments for the function |
| 350 | |
| 351 | Returns a 2-tuple (result, exc_info) where result |
| 352 | is the return value of function, and exc_info is |
| 353 | the sys.exc_info() of the exception thrown by the |
| 354 | function (which may be None). |
| 355 | """ |
| 356 | |
| 357 | result, exc_info = None, None |
| 358 | try: |
| 359 | self.record('START', None, name) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 360 | self._increment_group_level() |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 361 | result = function(*args, **dargs) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 362 | self._decrement_group_level() |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 363 | self.record('END GOOD', None, name) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 364 | except error.TestNAError, e: |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 365 | self._decrement_group_level() |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 366 | self.record('END TEST_NA', None, name, str(e)) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 367 | except Exception, e: |
| 368 | exc_info = sys.exc_info() |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 369 | self._decrement_group_level() |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 370 | err_msg = str(e) + '\n' + traceback.format_exc() |
mbligh | 51144e0 | 2007-11-20 20:38:18 +0000 | [diff] [blame] | 371 | self.record('END FAIL', None, name, err_msg) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 372 | |
| 373 | return result, exc_info |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 374 | |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 375 | |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 376 | def run_group(self, function, *args, **dargs): |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 377 | """\ |
| 378 | function: |
| 379 | subroutine to run |
| 380 | *args: |
| 381 | arguments for the function |
| 382 | """ |
| 383 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 384 | # Allow the tag for the group to be specified |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 385 | name = function.__name__ |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 386 | tag = dargs.pop('tag', None) |
| 387 | if tag: |
| 388 | name = tag |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 389 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 390 | result, exc_info = self.__rungroup(name, function, |
| 391 | *args, **dargs) |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 392 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 393 | # if there was a non-TestError exception, raise it |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 394 | if exc_info and not isinstance(exc_info[1], error.TestError): |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 395 | err = ''.join(traceback.format_exception(*exc_info)) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 396 | raise error.TestError(name + ' failed\n' + err) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 397 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 398 | # pass back the actual return value from the function |
apw | 08403ca | 2007-09-27 17:17:22 +0000 | [diff] [blame] | 399 | return result |
| 400 | |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 401 | |
jadmanski | 87cbc7f | 2008-05-13 18:17:10 +0000 | [diff] [blame] | 402 | def new_container(self, mbytes=None, cpus=None, root=None, name=None): |
mbligh | 8ea61e2 | 2008-05-09 18:09:37 +0000 | [diff] [blame] | 403 | if not autotest_utils.grep('cpuset', '/proc/filesystems'): |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 404 | print "Containers not enabled by latest reboot" |
| 405 | return # containers weren't enabled in this kernel boot |
| 406 | pid = os.getpid() |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 407 | if not name: |
| 408 | name = 'test%d' % pid # make arbitrary unique name |
| 409 | self.container = cpuset.cpuset(name, job_size=mbytes, |
mbligh | 337bb76 | 2008-04-16 21:23:10 +0000 | [diff] [blame] | 410 | job_pid=pid, cpus=cpus, root=root) |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 411 | # This job's python shell is now running in the new container |
| 412 | # and all forked test processes will inherit that container |
| 413 | |
| 414 | |
| 415 | def release_container(self): |
| 416 | if self.container: |
mbligh | 337bb76 | 2008-04-16 21:23:10 +0000 | [diff] [blame] | 417 | self.container.release() |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 418 | self.container = None |
| 419 | |
| 420 | |
| 421 | def cpu_count(self): |
| 422 | if self.container: |
| 423 | return len(self.container.cpus) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 424 | return autotest_utils.count_cpus() # use total system count |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 425 | |
| 426 | |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 427 | # Check the passed kernel identifier against the command line |
| 428 | # and the running kernel, abort the job on missmatch. |
mbligh | 38a4a11 | 2008-03-19 13:11:34 +0000 | [diff] [blame] | 429 | def kernel_check_ident(self, expected_when, expected_id, subdir, |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 430 | type = 'src', patches=[]): |
mbligh | 38a4a11 | 2008-03-19 13:11:34 +0000 | [diff] [blame] | 431 | print (("POST BOOT: checking booted kernel " + |
| 432 | "mark=%d identity='%s' type='%s'") % |
| 433 | (expected_when, expected_id, type)) |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 434 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 435 | running_id = autotest_utils.running_os_ident() |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 436 | |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame^] | 437 | cmdline = utils.read_one_line("/proc/cmdline") |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 438 | |
| 439 | find_sum = re.compile(r'.*IDENT=(\d+)') |
| 440 | m = find_sum.match(cmdline) |
| 441 | cmdline_when = -1 |
| 442 | if m: |
| 443 | cmdline_when = int(m.groups()[0]) |
| 444 | |
| 445 | # We have all the facts, see if they indicate we |
| 446 | # booted the requested kernel or not. |
| 447 | bad = False |
mbligh | da0311e | 2007-10-25 16:03:33 +0000 | [diff] [blame] | 448 | if (type == 'src' and expected_id != running_id or |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 449 | type == 'rpm' and |
| 450 | not running_id.startswith(expected_id + '::')): |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 451 | print "check_kernel_ident: kernel identifier mismatch" |
| 452 | bad = True |
| 453 | if expected_when != cmdline_when: |
| 454 | print "check_kernel_ident: kernel command line mismatch" |
| 455 | bad = True |
| 456 | |
| 457 | if bad: |
| 458 | print " Expected Ident: " + expected_id |
| 459 | print " Running Ident: " + running_id |
| 460 | print " Expected Mark: %d" % (expected_when) |
| 461 | print "Command Line Mark: %d" % (cmdline_when) |
| 462 | print " Command Line: " + cmdline |
| 463 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 464 | raise error.JobError("boot failure", "reboot.verify") |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 465 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 466 | kernel_info = {'kernel': expected_id} |
| 467 | for i, patch in enumerate(patches): |
| 468 | kernel_info["patch%d" % i] = patch |
mbligh | b7fd270 | 2008-03-25 14:57:08 +0000 | [diff] [blame] | 469 | self.record('GOOD', subdir, 'reboot.verify', expected_id) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 470 | self._decrement_group_level() |
| 471 | self.record('END GOOD', subdir, 'reboot', |
| 472 | optional_fields=kernel_info) |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 473 | |
| 474 | |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 475 | def filesystem(self, device, mountpoint = None, loop_size = 0): |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 476 | if not mountpoint: |
| 477 | mountpoint = self.tmpdir |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 478 | return filesystem.filesystem(self, device, mountpoint,loop_size) |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 479 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 480 | |
| 481 | def enable_external_logging(self): |
| 482 | pass |
| 483 | |
| 484 | |
| 485 | def disable_external_logging(self): |
| 486 | pass |
| 487 | |
| 488 | |
| 489 | def reboot_setup(self): |
| 490 | pass |
| 491 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 492 | |
| 493 | def reboot(self, tag='autotest'): |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 494 | self.reboot_setup() |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 495 | self.record('START', None, 'reboot') |
| 496 | self._increment_group_level() |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 497 | self.record('GOOD', None, 'reboot.start') |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 498 | self.harness.run_reboot() |
apw | 11985b7 | 2007-10-04 15:44:47 +0000 | [diff] [blame] | 499 | default = self.config_get('boot.set_default') |
| 500 | if default: |
| 501 | self.bootloader.set_default(tag) |
| 502 | else: |
| 503 | self.bootloader.boot_once(tag) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 504 | cmd = "(sleep 5; reboot) </dev/null >/dev/null 2>&1 &" |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame^] | 505 | utils.system(cmd) |
apw | 0778a2f | 2006-10-06 03:11:40 +0000 | [diff] [blame] | 506 | self.quit() |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 507 | |
| 508 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 509 | def noop(self, text): |
| 510 | print "job: noop: " + text |
| 511 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 512 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 513 | def parallel(self, *tasklist): |
| 514 | """Run tasks in parallel""" |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 515 | |
| 516 | pids = [] |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 517 | old_log_filename = self.log_filename |
| 518 | for i, task in enumerate(tasklist): |
| 519 | self.log_filename = old_log_filename + (".%d" % i) |
| 520 | task_func = lambda: task[0](*task[1:]) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 521 | pids.append(parallel.fork_start(self.resultdir, |
| 522 | task_func)) |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 523 | |
| 524 | old_log_path = os.path.join(self.resultdir, old_log_filename) |
| 525 | old_log = open(old_log_path, "a") |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 526 | exceptions = [] |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 527 | for i, pid in enumerate(pids): |
| 528 | # wait for the task to finish |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 529 | try: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 530 | parallel.fork_waitfor(self.resultdir, pid) |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 531 | except Exception, e: |
| 532 | exceptions.append(e) |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 533 | # copy the logs from the subtask into the main log |
| 534 | new_log_path = old_log_path + (".%d" % i) |
| 535 | if os.path.exists(new_log_path): |
| 536 | new_log = open(new_log_path) |
| 537 | old_log.write(new_log.read()) |
| 538 | new_log.close() |
| 539 | old_log.flush() |
| 540 | os.remove(new_log_path) |
| 541 | old_log.close() |
| 542 | |
| 543 | self.log_filename = old_log_filename |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 544 | |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 545 | # handle any exceptions raised by the parallel tasks |
| 546 | if exceptions: |
| 547 | msg = "%d task(s) failed" % len(exceptions) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 548 | raise error.JobError(msg, str(exceptions), exceptions) |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 549 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 550 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 551 | def quit(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 552 | # XXX: should have a better name. |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 553 | self.harness.run_pause() |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 554 | raise error.JobContinue("more to come") |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 555 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 556 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 557 | def complete(self, status): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 558 | """Clean up and exit""" |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 559 | # We are about to exit 'complete' so clean up the control file. |
| 560 | try: |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 561 | os.unlink(self.state_file) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 562 | except: |
| 563 | pass |
mbligh | c0b10d3 | 2008-03-03 16:03:28 +0000 | [diff] [blame] | 564 | |
mbligh | 61a6c1a | 2006-12-25 01:26:38 +0000 | [diff] [blame] | 565 | self.harness.run_complete() |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 566 | self.disable_external_logging() |
apw | 1b02190 | 2006-04-03 17:02:56 +0000 | [diff] [blame] | 567 | sys.exit(status) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 568 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 569 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 570 | def set_state(self, var, val): |
| 571 | # Deep copies make sure that the state can't be altered |
| 572 | # without it being re-written. Perf wise, deep copies |
| 573 | # are overshadowed by pickling/loading. |
| 574 | self.state[var] = copy.deepcopy(val) |
| 575 | pickle.dump(self.state, open(self.state_file, 'w')) |
| 576 | |
| 577 | |
| 578 | def __load_state(self): |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 579 | assert not hasattr(self, "state") |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 580 | try: |
| 581 | self.state = pickle.load(open(self.state_file, 'r')) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 582 | self.state_existed = True |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 583 | except Exception: |
| 584 | print "Initializing the state engine." |
| 585 | self.state = {} |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 586 | self.set_state('__steps', []) # writes pickle file |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 587 | self.state_existed = False |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 588 | |
| 589 | |
| 590 | def get_state(self, var, default=None): |
| 591 | if var in self.state or default == None: |
| 592 | val = self.state[var] |
| 593 | else: |
| 594 | val = default |
| 595 | return copy.deepcopy(val) |
| 596 | |
| 597 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 598 | def __create_step_tuple(self, fn, args, dargs): |
| 599 | # Legacy code passes in an array where the first arg is |
| 600 | # the function or its name. |
| 601 | if isinstance(fn, list): |
| 602 | assert(len(args) == 0) |
| 603 | assert(len(dargs) == 0) |
| 604 | args = fn[1:] |
| 605 | fn = fn[0] |
| 606 | # Pickling actual functions is harry, thus we have to call |
| 607 | # them by name. Unfortunately, this means only functions |
| 608 | # defined globally can be used as a next step. |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 609 | if callable(fn): |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 610 | fn = fn.__name__ |
| 611 | if not isinstance(fn, types.StringTypes): |
| 612 | raise StepError("Next steps must be functions or " |
| 613 | "strings containing the function name") |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 614 | ancestry = copy.copy(self.current_step_ancestry) |
| 615 | return (ancestry, fn, args, dargs) |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 616 | |
| 617 | |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 618 | def next_step_append(self, fn, *args, **dargs): |
| 619 | """Define the next step and place it at the end""" |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 620 | steps = self.get_state('__steps') |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 621 | steps.append(self.__create_step_tuple(fn, args, dargs)) |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 622 | self.set_state('__steps', steps) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 623 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 624 | |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 625 | def next_step(self, fn, *args, **dargs): |
| 626 | """Create a new step and place it after any steps added |
| 627 | while running the current step but before any steps added in |
| 628 | previous steps""" |
| 629 | steps = self.get_state('__steps') |
| 630 | steps.insert(self.next_step_index, |
| 631 | self.__create_step_tuple(fn, args, dargs)) |
| 632 | self.next_step_index += 1 |
| 633 | self.set_state('__steps', steps) |
| 634 | |
| 635 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 636 | def next_step_prepend(self, fn, *args, **dargs): |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 637 | """Insert a new step, executing first""" |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 638 | steps = self.get_state('__steps') |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 639 | steps.insert(0, self.__create_step_tuple(fn, args, dargs)) |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 640 | self.next_step_index += 1 |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 641 | self.set_state('__steps', steps) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 642 | |
| 643 | |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 644 | def _run_step_fn(self, local_vars, fn, args, dargs): |
| 645 | """Run a (step) function within the given context""" |
| 646 | |
| 647 | local_vars['__args'] = args |
| 648 | local_vars['__dargs'] = dargs |
| 649 | exec('__ret = %s(*__args, **__dargs)' % fn, |
| 650 | local_vars, local_vars) |
| 651 | return local_vars['__ret'] |
| 652 | |
| 653 | |
| 654 | def _create_frame(self, global_vars, ancestry, fn_name): |
| 655 | """Set up the environment like it would have been when this |
| 656 | function was first defined. |
| 657 | |
| 658 | Child step engine 'implementations' must have 'return locals()' |
| 659 | at end end of their steps. Because of this, we can call the |
| 660 | parent function and get back all child functions (i.e. those |
| 661 | defined within it). |
| 662 | |
| 663 | Unfortunately, the call stack of the function calling |
| 664 | job.next_step might have been deeper than the function it |
| 665 | added. In order to make sure that the environment is what it |
| 666 | should be, we need to then pop off the frames we built until |
| 667 | we find the frame where the function was first defined.""" |
| 668 | |
| 669 | # The copies ensure that the parent frames are not modified |
| 670 | # while building child frames. This matters if we then |
| 671 | # pop some frames in the next part of this function. |
| 672 | current_frame = copy.copy(global_vars) |
| 673 | frames = [current_frame] |
| 674 | for steps_fn_name in ancestry: |
| 675 | ret = self._run_step_fn(current_frame, |
| 676 | steps_fn_name, [], {}) |
| 677 | current_frame = copy.copy(ret) |
| 678 | frames.append(current_frame) |
| 679 | |
| 680 | while len(frames) > 2: |
| 681 | if fn_name not in frames[-2]: |
| 682 | break |
| 683 | if frames[-2][fn_name] != frames[-1][fn_name]: |
| 684 | break |
| 685 | frames.pop() |
| 686 | ancestry.pop() |
| 687 | |
| 688 | return (frames[-1], ancestry) |
| 689 | |
| 690 | |
| 691 | def _add_step_init(self, local_vars, current_function): |
| 692 | """If the function returned a dictionary that includes a |
| 693 | function named 'step_init', prepend it to our list of steps. |
| 694 | This will only get run the first time a function with a nested |
| 695 | use of the step engine is run.""" |
| 696 | |
| 697 | if (isinstance(local_vars, dict) and |
| 698 | 'step_init' in local_vars and |
| 699 | callable(local_vars['step_init'])): |
| 700 | # The init step is a child of the function |
| 701 | # we were just running. |
| 702 | self.current_step_ancestry.append(current_function) |
| 703 | self.next_step_prepend('step_init') |
| 704 | |
| 705 | |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 706 | def step_engine(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 707 | """the stepping engine -- if the control file defines |
| 708 | step_init we will be using this engine to drive multiple runs. |
| 709 | """ |
| 710 | """Do the next step""" |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 711 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 712 | # Set up the environment and then interpret the control file. |
| 713 | # Some control files will have code outside of functions, |
| 714 | # which means we need to have our state engine initialized |
| 715 | # before reading in the file. |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 716 | global_control_vars = {'job': self} |
| 717 | exec(JOB_PREAMBLE, global_control_vars, global_control_vars) |
| 718 | execfile(self.control, global_control_vars, global_control_vars) |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 719 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 720 | # If we loaded in a mid-job state file, then we presumably |
| 721 | # know what steps we have yet to run. |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 722 | if not self.state_existed: |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 723 | if global_control_vars.has_key('step_init'): |
| 724 | self.next_step(global_control_vars['step_init']) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 725 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 726 | # Iterate through the steps. If we reboot, we'll simply |
| 727 | # continue iterating on the next step. |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 728 | while len(self.get_state('__steps')) > 0: |
| 729 | steps = self.get_state('__steps') |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 730 | (ancestry, fn_name, args, dargs) = steps.pop(0) |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 731 | self.set_state('__steps', steps) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 732 | |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 733 | self.next_step_index = 0 |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 734 | ret = self._create_frame(global_control_vars, ancestry, |
| 735 | fn_name) |
| 736 | local_vars, self.current_step_ancestry = ret |
| 737 | local_vars = self._run_step_fn(local_vars, fn_name, |
| 738 | args, dargs) |
| 739 | self._add_step_init(local_vars, fn_name) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 740 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 741 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 742 | def _init_group_level(self): |
| 743 | self.group_level = self.get_state("__group_level", default=0) |
| 744 | |
| 745 | |
| 746 | def _increment_group_level(self): |
| 747 | self.group_level += 1 |
| 748 | self.set_state("__group_level", self.group_level) |
| 749 | |
| 750 | |
| 751 | def _decrement_group_level(self): |
| 752 | self.group_level -= 1 |
| 753 | self.set_state("__group_level", self.group_level) |
| 754 | |
| 755 | |
| 756 | def record(self, status_code, subdir, operation, status = '', |
| 757 | optional_fields=None): |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 758 | """ |
| 759 | Record job-level status |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 760 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 761 | The intent is to make this file both machine parseable and |
| 762 | human readable. That involves a little more complexity, but |
| 763 | really isn't all that bad ;-) |
| 764 | |
| 765 | Format is <status code>\t<subdir>\t<operation>\t<status> |
| 766 | |
| 767 | status code: (GOOD|WARN|FAIL|ABORT) |
| 768 | or START |
| 769 | or END (GOOD|WARN|FAIL|ABORT) |
| 770 | |
| 771 | subdir: MUST be a relevant subdirectory in the results, |
| 772 | or None, which will be represented as '----' |
| 773 | |
| 774 | operation: description of what you ran (e.g. "dbench", or |
| 775 | "mkfs -t foobar /dev/sda9") |
| 776 | |
| 777 | status: error message or "completed sucessfully" |
| 778 | |
| 779 | ------------------------------------------------------------ |
| 780 | |
| 781 | Initial tabs indicate indent levels for grouping, and is |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 782 | governed by self.group_level |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 783 | |
| 784 | multiline messages have secondary lines prefaced by a double |
| 785 | space (' ') |
| 786 | """ |
| 787 | |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 788 | if subdir: |
| 789 | if re.match(r'[\n\t]', subdir): |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 790 | raise ValueError("Invalid character in " |
| 791 | "subdir string") |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 792 | substr = subdir |
| 793 | else: |
| 794 | substr = '----' |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 795 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 796 | if not logging.is_valid_status(status_code): |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 797 | raise ValueError("Invalid status code supplied: %s" % |
| 798 | status_code) |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 799 | if not operation: |
| 800 | operation = '----' |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 801 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 802 | if re.match(r'[\n\t]', operation): |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 803 | raise ValueError("Invalid character in " |
| 804 | "operation string") |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 805 | operation = operation.rstrip() |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 806 | |
| 807 | if not optional_fields: |
| 808 | optional_fields = {} |
| 809 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 810 | status = status.rstrip() |
| 811 | status = re.sub(r"\t", " ", status) |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 812 | # Ensure any continuation lines are marked so we can |
| 813 | # detect them in the status file to ensure it is parsable. |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 814 | status = re.sub(r"\n", "\n" + "\t" * self.group_level + " ", |
| 815 | status) |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 816 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 817 | # Generate timestamps for inclusion in the logs |
| 818 | epoch_time = int(time.time()) # seconds since epoch, in UTC |
| 819 | local_time = time.localtime(epoch_time) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 820 | optional_fields["timestamp"] = str(epoch_time) |
| 821 | optional_fields["localtime"] = time.strftime("%b %d %H:%M:%S", |
| 822 | local_time) |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 823 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 824 | fields = [status_code, substr, operation] |
| 825 | fields += ["%s=%s" % x for x in optional_fields.iteritems()] |
| 826 | fields.append(status) |
| 827 | |
| 828 | msg = '\t'.join(str(x) for x in fields) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 829 | msg = '\t' * self.group_level + msg |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 830 | |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 831 | msg_tag = "" |
| 832 | if "." in self.log_filename: |
| 833 | msg_tag = self.log_filename.split(".", 1)[1] |
| 834 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 835 | self.harness.test_status_detail(status_code, substr, |
| 836 | operation, status, msg_tag) |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 837 | self.harness.test_status(msg, msg_tag) |
| 838 | |
| 839 | # log to stdout (if enabled) |
| 840 | #if self.log_filename == self.DEFAULT_LOG_FILENAME: |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 841 | print msg |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 842 | |
| 843 | # log to the "root" status log |
| 844 | status_file = os.path.join(self.resultdir, self.log_filename) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 845 | open(status_file, "a").write(msg + "\n") |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 846 | |
| 847 | # log to the subdir status log (if subdir is set) |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 848 | if subdir: |
mbligh | adff6ca | 2008-01-22 16:38:25 +0000 | [diff] [blame] | 849 | dir = os.path.join(self.resultdir, subdir) |
| 850 | if not os.path.exists(dir): |
| 851 | os.mkdir(dir) |
| 852 | |
| 853 | status_file = os.path.join(dir, |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 854 | self.DEFAULT_LOG_FILENAME) |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 855 | open(status_file, "a").write(msg + "\n") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 856 | |
| 857 | |
jadmanski | 8415f96 | 2008-05-06 20:38:53 +0000 | [diff] [blame] | 858 | class disk_usage_monitor: |
| 859 | def __init__(self, logging_func, device, max_mb_per_hour): |
| 860 | self.func = logging_func |
| 861 | self.device = device |
| 862 | self.max_mb_per_hour = max_mb_per_hour |
| 863 | |
| 864 | |
| 865 | def start(self): |
| 866 | self.initial_space = autotest_utils.freespace(self.device) |
| 867 | self.start_time = time.time() |
| 868 | |
| 869 | |
| 870 | def stop(self): |
| 871 | # if no maximum usage rate was set, we don't need to |
| 872 | # generate any warnings |
| 873 | if not self.max_mb_per_hour: |
| 874 | return |
| 875 | |
| 876 | final_space = autotest_utils.freespace(self.device) |
| 877 | used_space = self.initial_space - final_space |
| 878 | stop_time = time.time() |
| 879 | total_time = stop_time - self.start_time |
| 880 | # round up the time to one minute, to keep extremely short |
| 881 | # tests from generating false positives due to short, badly |
| 882 | # timed bursts of activity |
| 883 | total_time = max(total_time, 60.0) |
| 884 | |
| 885 | # determine the usage rate |
| 886 | bytes_per_sec = used_space / total_time |
| 887 | mb_per_sec = bytes_per_sec / 1024**2 |
| 888 | mb_per_hour = mb_per_sec * 60 * 60 |
| 889 | |
| 890 | if mb_per_hour > self.max_mb_per_hour: |
| 891 | msg = ("disk space on %s was consumed at a rate of " |
| 892 | "%.2f MB/hour") |
| 893 | msg %= (self.device, mb_per_hour) |
| 894 | self.func(msg) |
| 895 | |
| 896 | |
| 897 | @classmethod |
| 898 | def watch(cls, *monitor_args, **monitor_dargs): |
| 899 | """ Generic decorator to wrap a function call with the |
| 900 | standard create-monitor -> start -> call -> stop idiom.""" |
| 901 | def decorator(func): |
| 902 | def watched_func(*args, **dargs): |
| 903 | monitor = cls(*monitor_args, **monitor_dargs) |
| 904 | monitor.start() |
| 905 | try: |
| 906 | func(*args, **dargs) |
| 907 | finally: |
| 908 | monitor.stop() |
| 909 | return watched_func |
| 910 | return decorator |
| 911 | |
| 912 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 913 | def runjob(control, cont = False, tag = "default", harness_type = '', |
| 914 | use_external_logging = False): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 915 | """The main interface to this module |
| 916 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 917 | control |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 918 | The control file to use for this job. |
| 919 | cont |
| 920 | Whether this is the continuation of a previously started job |
| 921 | """ |
mbligh | b4eef24 | 2007-07-23 18:22:49 +0000 | [diff] [blame] | 922 | control = os.path.abspath(control) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 923 | state = control + '.state' |
| 924 | |
| 925 | # instantiate the job object ready for the control file. |
| 926 | myjob = None |
| 927 | try: |
| 928 | # Check that the control file is valid |
| 929 | if not os.path.exists(control): |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 930 | raise error.JobError(control + |
| 931 | ": control file not found") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 932 | |
| 933 | # When continuing, the job is complete when there is no |
| 934 | # state file, ensure we don't try and continue. |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 935 | if cont and not os.path.exists(state): |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 936 | raise error.JobComplete("all done") |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 937 | if cont == False and os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 938 | os.unlink(state) |
| 939 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 940 | myjob = job(control, tag, cont, harness_type, |
| 941 | use_external_logging) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 942 | |
| 943 | # Load in the users control file, may do any one of: |
| 944 | # 1) execute in toto |
| 945 | # 2) define steps, and select the first via next_step() |
| 946 | myjob.step_engine() |
| 947 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 948 | except error.JobContinue: |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 949 | sys.exit(5) |
| 950 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 951 | except error.JobComplete: |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 952 | sys.exit(1) |
| 953 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 954 | except error.JobError, instance: |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 955 | print "JOB ERROR: " + instance.args[0] |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 956 | if myjob: |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 957 | command = None |
| 958 | if len(instance.args) > 1: |
| 959 | command = instance.args[1] |
| 960 | myjob.record('ABORT', None, command, instance.args[0]) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 961 | myjob._decrement_group_level() |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 962 | myjob.record('END ABORT', None, None) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 963 | assert(myjob.group_level == 0) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 964 | myjob.complete(1) |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 965 | else: |
| 966 | sys.exit(1) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 967 | |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 968 | except Exception, e: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 969 | msg = str(e) + '\n' + traceback.format_exc() |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 970 | print "JOB ERROR: " + msg |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 971 | if myjob: |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 972 | myjob.record('ABORT', None, None, msg) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 973 | myjob._decrement_group_level() |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 974 | myjob.record('END ABORT', None, None) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 975 | assert(myjob.group_level == 0) |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 976 | myjob.complete(1) |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 977 | else: |
| 978 | sys.exit(1) |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 979 | |
mbligh | 0144e5a | 2008-03-07 18:17:53 +0000 | [diff] [blame] | 980 | # If we get here, then we assume the job is complete and good. |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 981 | myjob._decrement_group_level() |
mbligh | 0144e5a | 2008-03-07 18:17:53 +0000 | [diff] [blame] | 982 | myjob.record('END GOOD', None, None) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 983 | assert(myjob.group_level == 0) |
mbligh | 0144e5a | 2008-03-07 18:17:53 +0000 | [diff] [blame] | 984 | |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 985 | myjob.complete(0) |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 986 | |
| 987 | |
| 988 | # site_job.py may be non-existant or empty, make sure that an appropriate |
| 989 | # site_job class is created nevertheless |
| 990 | try: |
| 991 | from site_job import site_job |
| 992 | except ImportError: |
| 993 | class site_job(base_job): |
| 994 | pass |
| 995 | |
| 996 | class job(site_job): |
| 997 | pass |
jadmanski | 87cbc7f | 2008-05-13 18:17:10 +0000 | [diff] [blame] | 998 | |