James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1 | # Copyright 2012 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 15 | import logging |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 16 | import os |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 17 | import pickle |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 18 | import Queue |
| 19 | import re |
| 20 | import threading |
| 21 | import yaml |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 22 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 23 | import model |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 24 | from model import Pipeline, Job, Project, ChangeQueue, EventFilter |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 25 | import merger |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 26 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 27 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 28 | class Scheduler(threading.Thread): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 29 | log = logging.getLogger("zuul.Scheduler") |
| 30 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 31 | def __init__(self): |
| 32 | threading.Thread.__init__(self) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 33 | self.wake_event = threading.Event() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 34 | self.reconfigure_complete_event = threading.Event() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 35 | self.queue_lock = threading.Lock() |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 36 | self._pause = False |
| 37 | self._reconfigure = False |
| 38 | self._exit = False |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 39 | self._stopped = False |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 40 | self.launcher = None |
| 41 | self.trigger = None |
| 42 | |
| 43 | self.trigger_event_queue = Queue.Queue() |
| 44 | self.result_event_queue = Queue.Queue() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 45 | self._init() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 46 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 47 | def _init(self): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 48 | self.pipelines = {} |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 49 | self.jobs = {} |
| 50 | self.projects = {} |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 51 | self.metajobs = {} |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 52 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 53 | def stop(self): |
| 54 | self._stopped = True |
| 55 | self.wake_event.set() |
| 56 | |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 57 | def _parseConfig(self, config_path): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 58 | def toList(item): |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 59 | if not item: |
| 60 | return [] |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 61 | if isinstance(item, list): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 62 | return item |
| 63 | return [item] |
| 64 | |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 65 | if config_path: |
| 66 | config_path = os.path.expanduser(config_path) |
| 67 | if not os.path.exists(config_path): |
| 68 | raise Exception("Unable to read layout config file at %s" % |
| 69 | config_path) |
| 70 | config_file = open(config_path) |
| 71 | data = yaml.load(config_file) |
| 72 | |
| 73 | self._config_env = {} |
| 74 | for include in data.get('includes', []): |
| 75 | if 'python-file' in include: |
| 76 | fn = include['python-file'] |
| 77 | if not os.path.isabs(fn): |
| 78 | base = os.path.dirname(config_path) |
| 79 | fn = os.path.join(base, fn) |
| 80 | fn = os.path.expanduser(fn) |
| 81 | execfile(fn, self._config_env) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 82 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 83 | for conf_pipeline in data.get('pipelines', []): |
| 84 | pipeline = Pipeline(conf_pipeline['name']) |
| 85 | manager = globals()[conf_pipeline['manager']](self, pipeline) |
| 86 | pipeline.setManager(manager) |
| 87 | |
| 88 | self.pipelines[conf_pipeline['name']] = pipeline |
| 89 | manager.success_action = conf_pipeline.get('success') |
| 90 | manager.failure_action = conf_pipeline.get('failure') |
| 91 | manager.start_action = conf_pipeline.get('start') |
| 92 | for trigger in toList(conf_pipeline['trigger']): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 93 | approvals = {} |
| 94 | for approval_dict in toList(trigger.get('approval')): |
| 95 | for k, v in approval_dict.items(): |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 96 | approvals[k] = v |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 97 | f = EventFilter(types=toList(trigger['event']), |
| 98 | branches=toList(trigger.get('branch')), |
| 99 | refs=toList(trigger.get('ref')), |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 100 | approvals=approvals, |
| 101 | comment_filters=toList( |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 102 | trigger.get('comment_filter'))) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 103 | manager.event_filters.append(f) |
| 104 | |
| 105 | for config_job in data['jobs']: |
| 106 | job = self.getJob(config_job['name']) |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 107 | # Be careful to only set attributes explicitly present on |
| 108 | # this job, to avoid squashing attributes set by a meta-job. |
| 109 | m = config_job.get('failure-message', None) |
| 110 | if m: |
| 111 | job.failure_message = m |
| 112 | m = config_job.get('success-message', None) |
| 113 | if m: |
| 114 | job.success_message = m |
James E. Blair | 222d498 | 2012-07-16 09:31:19 -0700 | [diff] [blame] | 115 | m = config_job.get('hold-following-changes', False) |
| 116 | if m: |
| 117 | job.hold_following_changes = True |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 118 | m = config_job.get('voting', None) |
| 119 | if m is not None: |
| 120 | job.voting = m |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 121 | fname = config_job.get('parameter-function', None) |
| 122 | if fname: |
| 123 | func = self._config_env.get(fname, None) |
| 124 | if not func: |
| 125 | raise Exception("Unable to find function %s" % fname) |
| 126 | job.parameter_function = func |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 127 | branches = toList(config_job.get('branch')) |
| 128 | if branches: |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 129 | job._branches = branches |
| 130 | job.branches = [re.compile(x) for x in branches] |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 131 | |
| 132 | def add_jobs(job_tree, config_jobs): |
| 133 | for job in config_jobs: |
| 134 | if isinstance(job, list): |
| 135 | for x in job: |
| 136 | add_jobs(job_tree, x) |
| 137 | if isinstance(job, dict): |
| 138 | for parent, children in job.items(): |
| 139 | parent_tree = job_tree.addJob(self.getJob(parent)) |
| 140 | add_jobs(parent_tree, children) |
| 141 | if isinstance(job, str): |
| 142 | job_tree.addJob(self.getJob(job)) |
| 143 | |
| 144 | for config_project in data['projects']: |
| 145 | project = Project(config_project['name']) |
| 146 | self.projects[config_project['name']] = project |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 147 | mode = config_project.get('merge-mode') |
| 148 | if mode and mode == 'cherry-pick': |
| 149 | project.merge_mode = model.CHERRY_PICK |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 150 | for pipeline in self.pipelines.values(): |
| 151 | if pipeline.name in config_project: |
| 152 | job_tree = pipeline.addProject(project) |
| 153 | config_jobs = config_project[pipeline.name] |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 154 | add_jobs(job_tree, config_jobs) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 155 | |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 156 | # All jobs should be defined at this point, get rid of |
| 157 | # metajobs so that getJob isn't doing anything weird. |
| 158 | self.metajobs = {} |
| 159 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 160 | # TODO(jeblair): check that we don't end up with jobs like |
| 161 | # "foo - bar" because a ':' is missing in the yaml for a dependent job |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 162 | for pipeline in self.pipelines.values(): |
| 163 | pipeline.manager._postConfig() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 164 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 165 | if self.config.has_option('zuul', 'git_dir'): |
| 166 | merge_root = self.config.get('zuul', 'git_dir') |
| 167 | else: |
| 168 | merge_root = '/var/lib/zuul/git' |
James E. Blair | ceabcbc | 2012-08-17 13:48:46 -0700 | [diff] [blame] | 169 | if self.config.has_option('zuul', 'push_change_refs'): |
| 170 | push_refs = self.config.getboolean('zuul', 'push_change_refs') |
| 171 | else: |
| 172 | push_refs = False |
| 173 | self.merger = merger.Merger(self.trigger, merge_root, push_refs) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 174 | for project in self.projects.values(): |
| 175 | url = self.trigger.getGitUrl(project) |
| 176 | self.merger.addProject(project, url) |
| 177 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 178 | def getJob(self, name): |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 179 | if name in self.jobs: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 180 | return self.jobs[name] |
| 181 | job = Job(name) |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 182 | if name.startswith('^'): |
| 183 | # This is a meta-job |
| 184 | regex = re.compile(name) |
| 185 | self.metajobs[regex] = job |
| 186 | else: |
| 187 | # Apply attributes from matching meta-jobs |
| 188 | for regex, metajob in self.metajobs.items(): |
| 189 | if regex.match(name): |
| 190 | job.copy(metajob) |
| 191 | self.jobs[name] = job |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 192 | return job |
| 193 | |
| 194 | def setLauncher(self, launcher): |
| 195 | self.launcher = launcher |
| 196 | |
| 197 | def setTrigger(self, trigger): |
| 198 | self.trigger = trigger |
| 199 | |
| 200 | def addEvent(self, event): |
| 201 | self.log.debug("Adding trigger event: %s" % event) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 202 | self.queue_lock.acquire() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 203 | self.trigger_event_queue.put(event) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 204 | self.queue_lock.release() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 205 | self.wake_event.set() |
| 206 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 207 | def onBuildStarted(self, build): |
| 208 | self.log.debug("Adding start event for build: %s" % build) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 209 | self.queue_lock.acquire() |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 210 | self.result_event_queue.put(('started', build)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 211 | self.queue_lock.release() |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 212 | self.wake_event.set() |
| 213 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 214 | def onBuildCompleted(self, build): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 215 | self.log.debug("Adding complete event for build: %s" % build) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 216 | self.queue_lock.acquire() |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 217 | self.result_event_queue.put(('completed', build)) |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 218 | self.queue_lock.release() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 219 | self.wake_event.set() |
| 220 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 221 | def reconfigure(self, config): |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 222 | self.log.debug("Prepare to reconfigure") |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 223 | self.config = config |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 224 | self._pause = True |
| 225 | self._reconfigure = True |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 226 | self.wake_event.set() |
| 227 | self.log.debug("Waiting for reconfiguration") |
| 228 | self.reconfigure_complete_event.wait() |
| 229 | self.reconfigure_complete_event.clear() |
| 230 | self.log.debug("Reconfiguration complete") |
| 231 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 232 | def exit(self): |
| 233 | self.log.debug("Prepare to exit") |
| 234 | self._pause = True |
| 235 | self._exit = True |
| 236 | self.wake_event.set() |
| 237 | self.log.debug("Waiting for exit") |
| 238 | |
| 239 | def _get_queue_pickle_file(self): |
James E. Blair | 5a95c86 | 2012-07-09 15:11:17 -0700 | [diff] [blame] | 240 | if self.config.has_option('zuul', 'state_dir'): |
| 241 | state_dir = os.path.expanduser(self.config.get('zuul', |
| 242 | 'state_dir')) |
| 243 | else: |
| 244 | state_dir = '/var/lib/zuul' |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 245 | return os.path.join(state_dir, 'queue.pickle') |
| 246 | |
| 247 | def _save_queue(self): |
| 248 | pickle_file = self._get_queue_pickle_file() |
| 249 | events = [] |
| 250 | while not self.trigger_event_queue.empty(): |
| 251 | events.append(self.trigger_event_queue.get()) |
| 252 | self.log.debug("Queue length is %s" % len(events)) |
| 253 | if events: |
| 254 | self.log.debug("Saving queue") |
| 255 | pickle.dump(events, open(pickle_file, 'wb')) |
| 256 | |
| 257 | def _load_queue(self): |
| 258 | pickle_file = self._get_queue_pickle_file() |
| 259 | if os.path.exists(pickle_file): |
| 260 | self.log.debug("Loading queue") |
| 261 | events = pickle.load(open(pickle_file, 'rb')) |
| 262 | self.log.debug("Queue length is %s" % len(events)) |
| 263 | for event in events: |
| 264 | self.trigger_event_queue.put(event) |
| 265 | else: |
| 266 | self.log.debug("No queue file found") |
| 267 | |
| 268 | def _delete_queue(self): |
| 269 | pickle_file = self._get_queue_pickle_file() |
| 270 | if os.path.exists(pickle_file): |
| 271 | self.log.debug("Deleting saved queue") |
| 272 | os.unlink(pickle_file) |
| 273 | |
| 274 | def resume(self): |
| 275 | try: |
| 276 | self._load_queue() |
| 277 | except: |
| 278 | self.log.exception("Unable to load queue") |
| 279 | try: |
| 280 | self._delete_queue() |
| 281 | except: |
| 282 | self.log.exception("Unable to delete saved queue") |
| 283 | self.log.debug("Resuming queue processing") |
| 284 | self.wake_event.set() |
| 285 | |
| 286 | def _doPauseEvent(self): |
| 287 | if self._exit: |
| 288 | self.log.debug("Exiting") |
| 289 | self._save_queue() |
| 290 | os._exit(0) |
| 291 | if self._reconfigure: |
| 292 | self.log.debug("Performing reconfiguration") |
| 293 | self._init() |
| 294 | self._parseConfig(self.config.get('zuul', 'layout_config')) |
| 295 | self._pause = False |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 296 | self._reconfigure = False |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 297 | self.reconfigure_complete_event.set() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 298 | |
| 299 | def _areAllBuildsComplete(self): |
| 300 | self.log.debug("Checking if all builds are complete") |
| 301 | waiting = False |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 302 | for pipeline in self.pipelines.values(): |
| 303 | for build in pipeline.manager.building_jobs.keys(): |
| 304 | self.log.debug("%s waiting on %s" % (pipeline.manager, build)) |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 305 | waiting = True |
| 306 | if not waiting: |
| 307 | self.log.debug("All builds are complete") |
| 308 | return True |
| 309 | self.log.debug("All builds are not complete") |
| 310 | return False |
| 311 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 312 | def run(self): |
| 313 | while True: |
| 314 | self.log.debug("Run handler sleeping") |
| 315 | self.wake_event.wait() |
| 316 | self.wake_event.clear() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 317 | if self._stopped: |
| 318 | return |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 319 | self.log.debug("Run handler awake") |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 320 | self.queue_lock.acquire() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 321 | try: |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 322 | if not self._pause: |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 323 | if not self.trigger_event_queue.empty(): |
| 324 | self.process_event_queue() |
| 325 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 326 | if not self.result_event_queue.empty(): |
| 327 | self.process_result_queue() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 328 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 329 | if self._pause and self._areAllBuildsComplete(): |
| 330 | self._doPauseEvent() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 331 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 332 | if not self._pause: |
James E. Blair | 4baa94c | 2012-06-07 17:04:21 -0700 | [diff] [blame] | 333 | if not (self.trigger_event_queue.empty() and |
| 334 | self.result_event_queue.empty()): |
| 335 | self.wake_event.set() |
| 336 | else: |
| 337 | if not self.result_event_queue.empty(): |
| 338 | self.wake_event.set() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 339 | except: |
| 340 | self.log.exception("Exception in run handler:") |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 341 | self.queue_lock.release() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 342 | |
| 343 | def process_event_queue(self): |
| 344 | self.log.debug("Fetching trigger event") |
| 345 | event = self.trigger_event_queue.get() |
| 346 | self.log.debug("Processing trigger event %s" % event) |
| 347 | project = self.projects.get(event.project_name) |
| 348 | if not project: |
| 349 | self.log.warning("Project %s not found" % event.project_name) |
| 350 | return |
| 351 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 352 | for pipeline in self.pipelines.values(): |
| 353 | if not pipeline.manager.eventMatches(event): |
| 354 | self.log.debug("Event %s ignored by %s" % (event, pipeline)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 355 | continue |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 356 | change = event.getChange(project, self.trigger) |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 357 | self.log.info("Adding %s, %s to %s" % |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 358 | (project, change, pipeline)) |
| 359 | pipeline.manager.addChange(change) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 360 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 361 | def process_result_queue(self): |
| 362 | self.log.debug("Fetching result event") |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 363 | event_type, build = self.result_event_queue.get() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 364 | self.log.debug("Processing result event %s" % build) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 365 | for pipeline in self.pipelines.values(): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 366 | if event_type == 'started': |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 367 | if pipeline.manager.onBuildStarted(build): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 368 | return |
| 369 | elif event_type == 'completed': |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 370 | if pipeline.manager.onBuildCompleted(build): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 371 | return |
James E. Blair | c84dd26 | 2012-05-31 10:03:13 -0700 | [diff] [blame] | 372 | self.log.warning("Build %s not found by any queue manager" % (build)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 373 | |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 374 | def formatStatusHTML(self): |
| 375 | ret = '<html><pre>' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 376 | if self._pause: |
| 377 | ret += '<p><b>Queue only mode:</b> preparing to ' |
| 378 | if self._reconfigure: |
| 379 | ret += 'reconfigure' |
| 380 | if self._exit: |
| 381 | ret += 'exit' |
| 382 | ret += ', queue length: %s' % self.trigger_event_queue.qsize() |
| 383 | ret += '</p>' |
| 384 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 385 | keys = self.pipelines.keys() |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 386 | keys.sort() |
| 387 | for key in keys: |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 388 | pipeline = self.pipelines[key] |
| 389 | s = 'Pipeline: %s' % pipeline.name |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 390 | ret += s + '\n' |
| 391 | ret += '-' * len(s) + '\n' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 392 | ret += pipeline.formatStatusHTML() |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 393 | ret += '\n' |
| 394 | ret += '</pre></html>' |
| 395 | return ret |
| 396 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 397 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 398 | class BasePipelineManager(object): |
| 399 | log = logging.getLogger("zuul.BasePipelineManager") |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 400 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 401 | def __init__(self, sched, pipeline): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 402 | self.sched = sched |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 403 | self.pipeline = pipeline |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 404 | self.building_jobs = {} |
| 405 | self.event_filters = [] |
| 406 | self.success_action = {} |
| 407 | self.failure_action = {} |
James E. Blair | dc25386 | 2012-06-13 17:12:42 -0700 | [diff] [blame] | 408 | self.start_action = {} |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 409 | |
| 410 | def __str__(self): |
James E. Blair | 93cc8d4 | 2012-08-07 10:46:51 -0700 | [diff] [blame] | 411 | return "<%s %s>" % (self.__class__.__name__, self.pipeline.name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 412 | |
| 413 | def _postConfig(self): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 414 | self.log.info("Configured Pipeline Manager %s" % self.pipeline.name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 415 | self.log.info(" Events:") |
| 416 | for e in self.event_filters: |
| 417 | self.log.info(" %s" % e) |
| 418 | self.log.info(" Projects:") |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 419 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 420 | def log_jobs(tree, indent=0): |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 421 | istr = ' ' + ' ' * indent |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 422 | if tree.job: |
| 423 | efilters = '' |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 424 | for b in tree.job._branches: |
| 425 | efilters += str(b) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 426 | if efilters: |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 427 | efilters = ' ' + efilters |
James E. Blair | 222d498 | 2012-07-16 09:31:19 -0700 | [diff] [blame] | 428 | hold = '' |
| 429 | if tree.job.hold_following_changes: |
| 430 | hold = ' [hold]' |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 431 | voting = '' |
| 432 | if not tree.job.voting: |
| 433 | voting = ' [nonvoting]' |
| 434 | self.log.info("%s%s%s%s%s" % (istr, repr(tree.job), |
| 435 | efilters, hold, voting)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 436 | for x in tree.job_trees: |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 437 | log_jobs(x, indent + 2) |
| 438 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 439 | for p in self.sched.projects.values(): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 440 | tree = self.pipeline.getJobTree(p) |
| 441 | if tree: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 442 | self.log.info(" %s" % p) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 443 | log_jobs(tree) |
James E. Blair | dc25386 | 2012-06-13 17:12:42 -0700 | [diff] [blame] | 444 | if self.start_action: |
| 445 | self.log.info(" On start:") |
| 446 | self.log.info(" %s" % self.start_action) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 447 | if self.success_action: |
| 448 | self.log.info(" On success:") |
| 449 | self.log.info(" %s" % self.success_action) |
| 450 | if self.failure_action: |
| 451 | self.log.info(" On failure:") |
| 452 | self.log.info(" %s" % self.failure_action) |
| 453 | |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 454 | def getSubmitAllowNeeds(self): |
| 455 | # Get a list of code review labels that are allowed to be |
| 456 | # "needed" in the submit records for a change, with respect |
| 457 | # to this queue. In other words, the list of review labels |
| 458 | # this queue itself is likely to set before submitting. |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 459 | if self.success_action: |
| 460 | return self.success_action.keys() |
| 461 | else: |
| 462 | return {} |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 463 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 464 | def eventMatches(self, event): |
| 465 | for ef in self.event_filters: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 466 | if ef.matches(event): |
| 467 | return True |
| 468 | return False |
| 469 | |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 470 | def isChangeAlreadyInQueue(self, change): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 471 | for c in self.pipeline.getChangesInQueue(): |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 472 | if change.equals(c): |
| 473 | return True |
| 474 | return False |
| 475 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 476 | def reportStart(self, change): |
| 477 | try: |
| 478 | self.log.info("Reporting start, action %s change %s" % |
| 479 | (self.start_action, change)) |
| 480 | msg = "Starting %s jobs." % self.pipeline.name |
| 481 | ret = self.sched.trigger.report(change, msg, self.start_action) |
| 482 | if ret: |
| 483 | self.log.error("Reporting change start %s received: %s" % |
| 484 | (change, ret)) |
| 485 | except: |
| 486 | self.log.exception("Exception while reporting start:") |
| 487 | |
| 488 | def isChangeReadyToBeEnqueued(self, change): |
| 489 | return True |
| 490 | |
| 491 | def enqueueChangesAhead(self, change): |
| 492 | return True |
| 493 | |
| 494 | def enqueueChangesBehind(self, change): |
| 495 | return True |
| 496 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 497 | def addChange(self, change): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 498 | self.log.debug("Considering adding change %s" % change) |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 499 | if self.isChangeAlreadyInQueue(change): |
| 500 | self.log.debug("Change %s is already in queue, ignoring" % change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 501 | return True |
James E. Blair | 692c6b3 | 2012-07-17 11:16:35 -0700 | [diff] [blame] | 502 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 503 | if not self.isChangeReadyToBeEnqueued(change): |
| 504 | self.log.debug("Change %s is not ready to be enqueued, ignoring" % |
| 505 | change) |
| 506 | return False |
| 507 | |
| 508 | if not self.enqueueChangesAhead(change): |
| 509 | self.log.debug("Failed to enqueue changes ahead of" % change) |
| 510 | return False |
| 511 | |
| 512 | if self.isChangeAlreadyInQueue(change): |
| 513 | self.log.debug("Change %s is already in queue, ignoring" % change) |
| 514 | return True |
| 515 | |
| 516 | change_queue = self.pipeline.getQueue(change.project) |
| 517 | if change_queue: |
| 518 | self.log.debug("Adding change %s to queue %s" % |
| 519 | (change, change_queue)) |
| 520 | if self.start_action: |
| 521 | self.reportStart(change) |
| 522 | change_queue.enqueueChange(change) |
| 523 | self.enqueueChangesBehind(change) |
| 524 | else: |
| 525 | self.log.error("Unable to find change queue for project %s" % |
| 526 | change.project) |
| 527 | return False |
| 528 | self.launchJobs() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 529 | |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 530 | def _launchJobs(self, change, jobs): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 531 | self.log.debug("Launching jobs for change %s" % change) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 532 | ref = change.current_build_set.getRef() |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 533 | if hasattr(change, 'refspec') and not ref: |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 534 | change.current_build_set.setConfiguration() |
| 535 | ref = change.current_build_set.getRef() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 536 | merged = self.sched.merger.mergeChanges([change], ref, |
| 537 | mode=model.MERGE_IF_NECESSARY) |
| 538 | if not merged: |
| 539 | self.log.info("Unable to merge change %s" % change) |
| 540 | self.pipeline.setUnableToMerge(change) |
| 541 | self.possiblyReportChange(change) |
| 542 | return |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 543 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 544 | for job in self.pipeline.findJobsToRun(change): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 545 | self.log.debug("Found job %s for change %s" % (job, change)) |
| 546 | try: |
James E. Blair | 03b94ef | 2012-08-20 10:54:29 -0700 | [diff] [blame] | 547 | build = self.sched.launcher.launch(job, change, self.pipeline) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 548 | self.building_jobs[build] = change |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 549 | self.log.debug("Adding build %s of job %s to change %s" % |
| 550 | (build, job, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 551 | change.addBuild(build) |
| 552 | except: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 553 | self.log.exception("Exception while launching job %s " |
| 554 | "for change %s:" % (job, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 555 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 556 | def launchJobs(self, change=None): |
| 557 | if not change: |
| 558 | for change in self.pipeline.getAllChanges(): |
| 559 | self.launchJobs(change) |
| 560 | return |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 561 | jobs = self.pipeline.findJobsToRun(change) |
| 562 | if jobs: |
| 563 | self._launchJobs(change, jobs) |
| 564 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 565 | def updateBuildDescriptions(self, build_set): |
| 566 | for build in build_set.getBuilds(): |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 567 | desc = self.formatDescription(build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 568 | self.sched.launcher.setBuildDescription(build, desc) |
| 569 | |
| 570 | if build_set.previous_build_set: |
| 571 | for build in build_set.previous_build_set.getBuilds(): |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 572 | desc = self.formatDescription(build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 573 | self.sched.launcher.setBuildDescription(build, desc) |
| 574 | |
| 575 | def onBuildStarted(self, build): |
| 576 | self.log.debug("Build %s started" % build) |
| 577 | if build not in self.building_jobs: |
| 578 | self.log.debug("Build %s not found" % (build)) |
| 579 | # Or triggered externally, or triggered before zuul started, |
| 580 | # or restarted |
| 581 | return False |
| 582 | |
| 583 | self.updateBuildDescriptions(build.build_set) |
| 584 | return True |
| 585 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 586 | def handleFailedChange(self, change): |
| 587 | pass |
| 588 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 589 | def onBuildCompleted(self, build): |
| 590 | self.log.debug("Build %s completed" % build) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 591 | if build not in self.building_jobs: |
James E. Blair | c84dd26 | 2012-05-31 10:03:13 -0700 | [diff] [blame] | 592 | self.log.debug("Build %s not found" % (build)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 593 | # Or triggered externally, or triggered before zuul started, |
| 594 | # or restarted |
| 595 | return False |
| 596 | change = self.building_jobs[build] |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 597 | self.log.debug("Found change %s which triggered completed build %s" % |
| 598 | (change, build)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 599 | |
| 600 | del self.building_jobs[build] |
| 601 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 602 | self.pipeline.setResult(change, build) |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 603 | self.log.info("Change %s status is now:\n %s" % |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 604 | (change, self.pipeline.formatStatus(change))) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 605 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 606 | if self.pipeline.didAnyJobFail(change): |
| 607 | self.handleFailedChange(change) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 608 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 609 | self.reportChanges() |
| 610 | self.launchJobs() |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 611 | self.updateBuildDescriptions(build.build_set) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 612 | return True |
| 613 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 614 | def reportChanges(self): |
| 615 | self.log.debug("Searching for changes to report") |
| 616 | reported = False |
| 617 | for change in self.pipeline.getAllChanges(): |
| 618 | self.log.debug(" checking %s" % change) |
| 619 | if self.pipeline.areAllJobsComplete(change): |
| 620 | self.log.debug(" possibly reporting %s" % change) |
| 621 | if self.possiblyReportChange(change): |
| 622 | reported = True |
| 623 | if reported: |
| 624 | self.reportChanges() |
| 625 | self.log.debug("Done searching for changes to report") |
| 626 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 627 | def possiblyReportChange(self, change): |
| 628 | self.log.debug("Possibly reporting change %s" % change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 629 | # Even if a change isn't reportable, keep going so that it |
| 630 | # gets dequeued in the normal manner. |
| 631 | if change.is_reportable and change.reported: |
| 632 | self.log.debug("Change %s already reported" % change) |
| 633 | return False |
| 634 | change_ahead = change.change_ahead |
| 635 | if not change_ahead: |
| 636 | self.log.debug("Change %s is at the front of the queue, " |
| 637 | "reporting" % (change)) |
| 638 | ret = self.reportChange(change) |
| 639 | if self.changes_merge: |
| 640 | succeeded = self.pipeline.didAllJobsSucceed(change) |
| 641 | merged = (not ret) |
| 642 | if merged: |
| 643 | merged = self.sched.trigger.isMerged(change, change.branch) |
| 644 | self.log.info("Reported change %s status: all-succeeded: %s, " |
| 645 | "merged: %s" % (change, succeeded, merged)) |
| 646 | if not (succeeded and merged): |
| 647 | self.log.debug("Reported change %s failed tests or failed " |
| 648 | "to merge" % (change)) |
| 649 | self.handleFailedChange(change) |
| 650 | return True |
| 651 | self.log.debug("Removing reported change %s from queue" % |
| 652 | change) |
| 653 | change_queue = self.pipeline.getQueue(change.project) |
| 654 | change_queue.dequeueChange(change) |
| 655 | return True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 656 | |
| 657 | def reportChange(self, change): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 658 | if not change.is_reportable: |
| 659 | return False |
James E. Blair | 6173fb3 | 2012-07-11 17:23:33 -0700 | [diff] [blame] | 660 | if change.reported: |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 661 | return 0 |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 662 | self.log.debug("Reporting change %s" % change) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 663 | ret = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 664 | if self.pipeline.didAllJobsSucceed(change): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 665 | action = self.success_action |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 666 | change.setReportedResult('SUCCESS') |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 667 | else: |
| 668 | action = self.failure_action |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 669 | change.setReportedResult('FAILURE') |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 670 | report = self.formatReport(change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 671 | change.reported = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 672 | try: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 673 | self.log.info("Reporting change %s, action: %s" % |
| 674 | (change, action)) |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 675 | ret = self.sched.trigger.report(change, report, action) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 676 | if ret: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 677 | self.log.error("Reporting change %s received: %s" % |
| 678 | (change, ret)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 679 | except: |
| 680 | self.log.exception("Exception while reporting:") |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 681 | change.setReportedResult('ERROR') |
| 682 | self.updateBuildDescriptions(change.current_build_set) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 683 | return ret |
| 684 | |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 685 | def formatReport(self, changeish): |
| 686 | ret = '' |
| 687 | if self.pipeline.didAllJobsSucceed(changeish): |
| 688 | ret += 'Build successful\n\n' |
| 689 | else: |
| 690 | ret += 'Build failed\n\n' |
| 691 | |
| 692 | if changeish.dequeued_needing_change: |
| 693 | ret += "This change depends on a change that failed to merge." |
| 694 | elif changeish.current_build_set.unable_to_merge: |
| 695 | ret += "This change was unable to be automatically merged "\ |
| 696 | "with the current state of the repository. Please "\ |
| 697 | "rebase your change and upload a new patchset." |
| 698 | else: |
James E. Blair | a35fcce | 2012-08-24 10:46:01 -0700 | [diff] [blame] | 699 | if self.sched.config.has_option('zuul', 'url_pattern'): |
| 700 | pattern = self.sched.config.get('zuul', 'url_pattern') |
| 701 | else: |
| 702 | pattern = None |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 703 | for job in self.pipeline.getJobs(changeish): |
| 704 | build = changeish.current_build_set.getBuild(job.name) |
| 705 | result = build.result |
| 706 | if result == 'SUCCESS' and job.success_message: |
| 707 | result = job.success_message |
| 708 | elif result == 'FAILURE' and job.failure_message: |
| 709 | result = job.failure_message |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame^] | 710 | url = None |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 711 | if build.url: |
| 712 | if pattern: |
| 713 | url = pattern.format(change=changeish, |
| 714 | pipeline=self.pipeline, |
| 715 | job=job, |
| 716 | build=build) |
| 717 | else: |
| 718 | url = build.url |
| 719 | if not url: |
| 720 | url = job.name |
| 721 | if not job.voting: |
| 722 | voting = ' (non-voting)' |
| 723 | else: |
| 724 | voting = '' |
| 725 | ret += '- %s : %s%s\n' % (url, result, voting) |
| 726 | return ret |
| 727 | |
| 728 | def formatDescription(self, build): |
| 729 | concurrent_changes = '' |
| 730 | concurrent_builds = '' |
| 731 | other_builds = '' |
| 732 | |
| 733 | for change in build.build_set.other_changes: |
| 734 | concurrent_changes += '<li><a href="{change.url}">\ |
| 735 | {change.number},{change.patchset}</a></li>'.format( |
| 736 | change=change) |
| 737 | |
| 738 | change = build.build_set.change |
| 739 | |
| 740 | for build in build.build_set.getBuilds(): |
| 741 | if build.base_url: |
| 742 | concurrent_builds += """\ |
| 743 | <li> |
| 744 | <a href="{build.base_url}"> |
| 745 | {build.job.name} #{build.number}</a>: {build.result} |
| 746 | </li> |
| 747 | """.format(build=build) |
| 748 | else: |
| 749 | concurrent_builds += """\ |
| 750 | <li> |
| 751 | {build.job.name}: {build.result} |
| 752 | </li>""".format(build=build) |
| 753 | |
| 754 | if build.build_set.previous_build_set: |
| 755 | other_build = build.build_set.previous_build_set.getBuild( |
| 756 | build.job.name) |
| 757 | if other_build: |
| 758 | other_builds += """\ |
| 759 | <li> |
| 760 | Preceded by: <a href="{build.base_url}"> |
| 761 | {build.job.name} #{build.number}</a> |
| 762 | </li> |
| 763 | """.format(build=other_build) |
| 764 | |
| 765 | if build.build_set.next_build_set: |
| 766 | other_build = build.build_set.next_build_set.getBuild( |
| 767 | build.job.name) |
| 768 | if other_build: |
| 769 | other_builds += """\ |
| 770 | <li> |
| 771 | Succeeded by: <a href="{build.base_url}"> |
| 772 | {build.job.name} #{build.number}</a> |
| 773 | </li> |
| 774 | """.format(build=other_build) |
| 775 | |
| 776 | result = build.build_set.result |
| 777 | |
| 778 | if hasattr(change, 'number'): |
| 779 | ret = """\ |
| 780 | <p> |
| 781 | Triggered by change: |
| 782 | <a href="{change.url}">{change.number},{change.patchset}</a><br/> |
| 783 | Branch: <b>{change.branch}</b><br/> |
| 784 | Pipeline: <b>{self.pipeline.name}</b> |
| 785 | </p>""" |
| 786 | else: |
| 787 | ret = """\ |
| 788 | <p> |
| 789 | Triggered by reference: |
| 790 | {change.ref}</a><br/> |
| 791 | Old revision: <b>{change.oldrev}</b><br/> |
| 792 | New revision: <b>{change.newrev}</b><br/> |
| 793 | Pipeline: <b>{self.pipeline.name}</b> |
| 794 | </p>""" |
| 795 | |
| 796 | if concurrent_changes: |
| 797 | ret += """\ |
| 798 | <p> |
| 799 | Other changes tested concurrently with this change: |
| 800 | <ul>{concurrent_changes}</ul> |
| 801 | </p> |
| 802 | """ |
| 803 | if concurrent_builds: |
| 804 | ret += """\ |
| 805 | <p> |
| 806 | All builds for this change set: |
| 807 | <ul>{concurrent_builds}</ul> |
| 808 | </p> |
| 809 | """ |
| 810 | |
| 811 | if other_builds: |
| 812 | ret += """\ |
| 813 | <p> |
| 814 | Other build sets for this change: |
| 815 | <ul>{other_builds}</ul> |
| 816 | </p> |
| 817 | """ |
| 818 | if result: |
| 819 | ret += """\ |
| 820 | <p> |
| 821 | Reported result: <b>{result}</b> |
| 822 | </p> |
| 823 | """ |
| 824 | |
| 825 | ret = ret.format(**locals()) |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 826 | return ret |
| 827 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 828 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 829 | class IndependentPipelineManager(BasePipelineManager): |
| 830 | log = logging.getLogger("zuul.IndependentPipelineManager") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 831 | changes_merge = False |
| 832 | |
| 833 | def _postConfig(self): |
| 834 | super(IndependentPipelineManager, self)._postConfig() |
| 835 | |
| 836 | change_queue = ChangeQueue(self.pipeline, dependent=False) |
| 837 | for project in self.pipeline.getProjects(): |
| 838 | change_queue.addProject(project) |
| 839 | |
| 840 | self.pipeline.addQueue(change_queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 841 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 842 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 843 | class DependentPipelineManager(BasePipelineManager): |
| 844 | log = logging.getLogger("zuul.DependentPipelineManager") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 845 | changes_merge = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 846 | |
| 847 | def __init__(self, *args, **kwargs): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 848 | super(DependentPipelineManager, self).__init__(*args, **kwargs) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 849 | |
| 850 | def _postConfig(self): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 851 | super(DependentPipelineManager, self)._postConfig() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 852 | self.buildChangeQueues() |
| 853 | |
| 854 | def buildChangeQueues(self): |
| 855 | self.log.debug("Building shared change queues") |
| 856 | change_queues = [] |
| 857 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 858 | for project in self.pipeline.getProjects(): |
| 859 | change_queue = ChangeQueue(self.pipeline) |
| 860 | change_queue.addProject(project) |
| 861 | change_queues.append(change_queue) |
| 862 | self.log.debug("Created queue: %s" % change_queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 863 | |
| 864 | self.log.debug("Combining shared queues") |
| 865 | new_change_queues = [] |
| 866 | for a in change_queues: |
| 867 | merged_a = False |
| 868 | for b in new_change_queues: |
| 869 | if not a.getJobs().isdisjoint(b.getJobs()): |
| 870 | self.log.debug("Merging queue %s into %s" % (a, b)) |
| 871 | b.mergeChangeQueue(a) |
| 872 | merged_a = True |
| 873 | break # this breaks out of 'for b' and continues 'for a' |
| 874 | if not merged_a: |
| 875 | self.log.debug("Keeping queue %s" % (a)) |
| 876 | new_change_queues.append(a) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 877 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 878 | self.log.info(" Shared change queues:") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 879 | for queue in new_change_queues: |
| 880 | self.pipeline.addQueue(queue) |
| 881 | self.log.info(" %s" % queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 882 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 883 | def isChangeReadyToBeEnqueued(self, change): |
| 884 | if not self.sched.trigger.canMerge(change, |
| 885 | self.getSubmitAllowNeeds()): |
| 886 | self.log.debug("Change %s can not merge, ignoring" % change) |
| 887 | return False |
| 888 | return True |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 889 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 890 | def enqueueChangesBehind(self, change): |
| 891 | to_enqueue = [] |
| 892 | self.log.debug("Checking for changes needing %s:" % change) |
| 893 | if not hasattr(change, 'needed_by_changes'): |
| 894 | self.log.debug(" Changeish does not support dependencies") |
| 895 | return |
| 896 | for needs in change.needed_by_changes: |
| 897 | if self.sched.trigger.canMerge(needs, |
| 898 | self.getSubmitAllowNeeds()): |
| 899 | self.log.debug(" Change %s needs %s and is ready to merge" % |
| 900 | (needs, change)) |
| 901 | to_enqueue.append(needs) |
| 902 | if not to_enqueue: |
| 903 | self.log.debug(" No changes need %s" % change) |
| 904 | |
| 905 | for other_change in to_enqueue: |
| 906 | self.addChange(other_change) |
| 907 | |
| 908 | def enqueueChangesAhead(self, change): |
| 909 | ret = self.checkForChangesNeededBy(change) |
| 910 | if ret in [True, False]: |
| 911 | return ret |
| 912 | self.log.debug(" Change %s must be merged ahead of %s" % |
| 913 | (ret, change)) |
| 914 | return self.addChange(ret) |
| 915 | |
| 916 | def checkForChangesNeededBy(self, change): |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 917 | self.log.debug("Checking for changes needed by %s:" % change) |
| 918 | # Return true if okay to proceed enqueing this change, |
| 919 | # false if the change should not be enqueued. |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 920 | if not hasattr(change, 'needs_change'): |
| 921 | self.log.debug(" Changeish does not support dependencies") |
| 922 | return True |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 923 | if not change.needs_change: |
| 924 | self.log.debug(" No changes needed") |
| 925 | return True |
| 926 | if change.needs_change.is_merged: |
| 927 | self.log.debug(" Needed change is merged") |
| 928 | return True |
| 929 | if not change.needs_change.is_current_patchset: |
| 930 | self.log.debug(" Needed change is not the current patchset") |
| 931 | return False |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 932 | if self.isChangeAlreadyInQueue(change.needs_change): |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 933 | self.log.debug(" Needed change is already ahead in the queue") |
| 934 | return True |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 935 | if self.sched.trigger.canMerge(change.needs_change, |
| 936 | self.getSubmitAllowNeeds()): |
| 937 | self.log.debug(" Change %s is needed" % |
| 938 | change.needs_change) |
| 939 | return change.needs_change |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 940 | # The needed change can't be merged. |
| 941 | self.log.debug(" Change %s is needed but can not be merged" % |
| 942 | change.needs_change) |
| 943 | return False |
| 944 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 945 | def _getDependentChanges(self, change): |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 946 | orig_change = change |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 947 | changes = [] |
| 948 | while change.change_ahead: |
| 949 | changes.append(change.change_ahead) |
| 950 | change = change.change_ahead |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 951 | self.log.info("Change %s depends on changes %s" % (orig_change, |
| 952 | changes)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 953 | return changes |
| 954 | |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 955 | def _launchJobs(self, change, jobs): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 956 | self.log.debug("Launching jobs for change %s" % change) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 957 | ref = change.current_build_set.getRef() |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 958 | if hasattr(change, 'refspec') and not ref: |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 959 | change.current_build_set.setConfiguration() |
| 960 | ref = change.current_build_set.getRef() |
| 961 | dependent_changes = self._getDependentChanges(change) |
| 962 | dependent_changes.reverse() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 963 | all_changes = dependent_changes + [change] |
| 964 | merged = self.sched.merger.mergeChanges(all_changes, ref) |
| 965 | if not merged: |
| 966 | self.log.info("Unable to merge changes %s" % all_changes) |
| 967 | self.pipeline.setUnableToMerge(change) |
| 968 | self.possiblyReportChange(change) |
| 969 | return |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 970 | #TODO: remove this line after GERRIT_CHANGES is gone |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 971 | dependent_changes = self._getDependentChanges(change) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 972 | for job in jobs: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 973 | self.log.debug("Found job %s for change %s" % (job, change)) |
| 974 | try: |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 975 | #TODO: remove dependent_changes after GERRIT_CHANGES is gone |
James E. Blair | 03b94ef | 2012-08-20 10:54:29 -0700 | [diff] [blame] | 976 | build = self.sched.launcher.launch(job, change, self.pipeline, |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 977 | dependent_changes) |
| 978 | self.building_jobs[build] = change |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 979 | self.log.debug("Adding build %s of job %s to change %s" % |
| 980 | (build, job, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 981 | change.addBuild(build) |
| 982 | except: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 983 | self.log.exception("Exception while launching job %s " |
| 984 | "for change %s:" % (job, change)) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 985 | |
Clark Boylan | 826ef9e | 2012-07-12 16:44:46 -0700 | [diff] [blame] | 986 | def cancelJobs(self, change, prime=True): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 987 | self.log.debug("Cancel jobs for change %s" % change) |
| 988 | to_remove = [] |
Clark Boylan | 826ef9e | 2012-07-12 16:44:46 -0700 | [diff] [blame] | 989 | if prime: |
| 990 | change.resetAllBuilds() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 991 | for build, build_change in self.building_jobs.items(): |
| 992 | if build_change == change: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 993 | self.log.debug("Found build %s for change %s to cancel" % |
| 994 | (build, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 995 | try: |
| 996 | self.sched.launcher.cancel(build) |
| 997 | except: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 998 | self.log.exception("Exception while canceling build %s " |
| 999 | "for change %s" % (build, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1000 | to_remove.append(build) |
| 1001 | for build in to_remove: |
| 1002 | self.log.debug("Removing build %s from running builds" % build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1003 | build.result = 'CANCELED' |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1004 | del self.building_jobs[build] |
| 1005 | if change.change_behind: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 1006 | self.log.debug("Canceling jobs for change %s, behind change %s" % |
| 1007 | (change.change_behind, change)) |
Clark Boylan | 826ef9e | 2012-07-12 16:44:46 -0700 | [diff] [blame] | 1008 | self.cancelJobs(change.change_behind, prime=prime) |
| 1009 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1010 | def handleFailedChange(self, change): |
| 1011 | # A build failed. All changes behind this change will need to |
| 1012 | # be retested. To free up resources cancel the builds behind |
| 1013 | # this one as they will be rerun anyways. |
| 1014 | change_ahead = change.change_ahead |
| 1015 | change_behind = change.change_behind |
| 1016 | if not change_ahead: |
| 1017 | # If we're at the head of the queue, allow changes to relaunch |
| 1018 | if change_behind: |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1019 | self.log.info("Canceling/relaunching jobs for change %s " |
| 1020 | "behind failed change %s" % |
| 1021 | (change_behind, change)) |
| 1022 | self.cancelJobs(change_behind) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1023 | self.dequeueChange(change) |
Clark Boylan | afd18ac | 2012-08-22 12:59:32 -0700 | [diff] [blame] | 1024 | elif change_behind: |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1025 | self.log.debug("Canceling builds behind change: %s due to " |
| 1026 | "failure." % change) |
| 1027 | self.cancelJobs(change_behind, prime=False) |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 1028 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1029 | def dequeueChange(self, change): |
| 1030 | self.log.debug("Removing change %s from queue" % change) |
| 1031 | change_ahead = change.change_ahead |
| 1032 | change_behind = change.change_behind |
| 1033 | change_queue = self.pipeline.getQueue(change.project) |
| 1034 | change_queue.dequeueChange(change) |
| 1035 | if not change_ahead and not change.reported: |
| 1036 | self.log.debug("Adding %s as a severed head" % change) |
| 1037 | change_queue.addSeveredHead(change) |
| 1038 | self.dequeueDependentChanges(change_behind) |
| 1039 | |
| 1040 | def dequeueDependentChanges(self, change): |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1041 | # When a change is dequeued after failing, dequeue any changes that |
| 1042 | # depend on it. |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1043 | while change: |
| 1044 | change_behind = change.change_behind |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1045 | if self.checkForChangesNeededBy(change) is not True: |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1046 | # It's not okay to enqueue this change, we should remove it. |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1047 | self.log.info("Dequeuing change %s because " |
| 1048 | "it can no longer merge" % change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1049 | change_queue = self.pipeline.getQueue(change.project) |
| 1050 | change_queue.dequeueChange(change) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1051 | self.pipeline.setDequeuedNeedingChange(change) |
| 1052 | self.reportChange(change) |
| 1053 | # We don't need to recurse, because any changes that might |
| 1054 | # be affected by the removal of this change are behind us |
| 1055 | # in the queue, so we can continue walking backwards. |
| 1056 | change = change_behind |