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, |
Zhongyue Luo | aa85ebf | 2012-09-21 16:38:33 +0800 | [diff] [blame] | 101 | comment_filters= |
| 102 | toList(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 |
Clark Boylan | 9b67090 | 2012-09-28 13:47:56 -0700 | [diff] [blame^] | 481 | if self.sched.config.has_option('zuul', 'status_url'): |
| 482 | msg += "\n" + self.sched.config.get('zuul', 'status_url') |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 483 | ret = self.sched.trigger.report(change, msg, self.start_action) |
| 484 | if ret: |
| 485 | self.log.error("Reporting change start %s received: %s" % |
| 486 | (change, ret)) |
| 487 | except: |
| 488 | self.log.exception("Exception while reporting start:") |
| 489 | |
| 490 | def isChangeReadyToBeEnqueued(self, change): |
| 491 | return True |
| 492 | |
| 493 | def enqueueChangesAhead(self, change): |
| 494 | return True |
| 495 | |
| 496 | def enqueueChangesBehind(self, change): |
| 497 | return True |
| 498 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 499 | def addChange(self, change): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 500 | self.log.debug("Considering adding change %s" % change) |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 501 | if self.isChangeAlreadyInQueue(change): |
| 502 | self.log.debug("Change %s is already in queue, ignoring" % change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 503 | return True |
James E. Blair | 692c6b3 | 2012-07-17 11:16:35 -0700 | [diff] [blame] | 504 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 505 | if not self.isChangeReadyToBeEnqueued(change): |
| 506 | self.log.debug("Change %s is not ready to be enqueued, ignoring" % |
| 507 | change) |
| 508 | return False |
| 509 | |
| 510 | if not self.enqueueChangesAhead(change): |
| 511 | self.log.debug("Failed to enqueue changes ahead of" % change) |
| 512 | return False |
| 513 | |
| 514 | if self.isChangeAlreadyInQueue(change): |
| 515 | self.log.debug("Change %s is already in queue, ignoring" % change) |
| 516 | return True |
| 517 | |
| 518 | change_queue = self.pipeline.getQueue(change.project) |
| 519 | if change_queue: |
| 520 | self.log.debug("Adding change %s to queue %s" % |
| 521 | (change, change_queue)) |
| 522 | if self.start_action: |
| 523 | self.reportStart(change) |
| 524 | change_queue.enqueueChange(change) |
| 525 | self.enqueueChangesBehind(change) |
| 526 | else: |
| 527 | self.log.error("Unable to find change queue for project %s" % |
| 528 | change.project) |
| 529 | return False |
| 530 | self.launchJobs() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 531 | |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 532 | def _launchJobs(self, change, jobs): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 533 | self.log.debug("Launching jobs for change %s" % change) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 534 | ref = change.current_build_set.getRef() |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 535 | if hasattr(change, 'refspec') and not ref: |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 536 | change.current_build_set.setConfiguration() |
| 537 | ref = change.current_build_set.getRef() |
Zhongyue Luo | aa85ebf | 2012-09-21 16:38:33 +0800 | [diff] [blame] | 538 | mode = model.MERGE_IF_NECESSARY |
| 539 | merged = self.sched.merger.mergeChanges([change], ref, mode=mode) |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 540 | if not merged: |
| 541 | self.log.info("Unable to merge change %s" % change) |
| 542 | self.pipeline.setUnableToMerge(change) |
| 543 | self.possiblyReportChange(change) |
| 544 | return |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 545 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 546 | for job in self.pipeline.findJobsToRun(change): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 547 | self.log.debug("Found job %s for change %s" % (job, change)) |
| 548 | try: |
James E. Blair | 03b94ef | 2012-08-20 10:54:29 -0700 | [diff] [blame] | 549 | build = self.sched.launcher.launch(job, change, self.pipeline) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 550 | self.building_jobs[build] = change |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 551 | self.log.debug("Adding build %s of job %s to change %s" % |
| 552 | (build, job, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 553 | change.addBuild(build) |
| 554 | except: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 555 | self.log.exception("Exception while launching job %s " |
| 556 | "for change %s:" % (job, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 557 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 558 | def launchJobs(self, change=None): |
| 559 | if not change: |
| 560 | for change in self.pipeline.getAllChanges(): |
| 561 | self.launchJobs(change) |
| 562 | return |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 563 | jobs = self.pipeline.findJobsToRun(change) |
| 564 | if jobs: |
| 565 | self._launchJobs(change, jobs) |
| 566 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 567 | def updateBuildDescriptions(self, build_set): |
| 568 | for build in build_set.getBuilds(): |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 569 | desc = self.formatDescription(build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 570 | self.sched.launcher.setBuildDescription(build, desc) |
| 571 | |
| 572 | if build_set.previous_build_set: |
| 573 | for build in build_set.previous_build_set.getBuilds(): |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 574 | desc = self.formatDescription(build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 575 | self.sched.launcher.setBuildDescription(build, desc) |
| 576 | |
| 577 | def onBuildStarted(self, build): |
| 578 | self.log.debug("Build %s started" % build) |
| 579 | if build not in self.building_jobs: |
| 580 | self.log.debug("Build %s not found" % (build)) |
| 581 | # Or triggered externally, or triggered before zuul started, |
| 582 | # or restarted |
| 583 | return False |
| 584 | |
| 585 | self.updateBuildDescriptions(build.build_set) |
| 586 | return True |
| 587 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 588 | def handleFailedChange(self, change): |
| 589 | pass |
| 590 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 591 | def onBuildCompleted(self, build): |
| 592 | self.log.debug("Build %s completed" % build) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 593 | if build not in self.building_jobs: |
James E. Blair | c84dd26 | 2012-05-31 10:03:13 -0700 | [diff] [blame] | 594 | self.log.debug("Build %s not found" % (build)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 595 | # Or triggered externally, or triggered before zuul started, |
| 596 | # or restarted |
| 597 | return False |
| 598 | change = self.building_jobs[build] |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 599 | self.log.debug("Found change %s which triggered completed build %s" % |
| 600 | (change, build)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 601 | |
| 602 | del self.building_jobs[build] |
| 603 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 604 | self.pipeline.setResult(change, build) |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 605 | self.log.info("Change %s status is now:\n %s" % |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 606 | (change, self.pipeline.formatStatus(change))) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 607 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 608 | if self.pipeline.didAnyJobFail(change): |
| 609 | self.handleFailedChange(change) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 610 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 611 | self.reportChanges() |
| 612 | self.launchJobs() |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 613 | self.updateBuildDescriptions(build.build_set) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 614 | return True |
| 615 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 616 | def reportChanges(self): |
| 617 | self.log.debug("Searching for changes to report") |
| 618 | reported = False |
| 619 | for change in self.pipeline.getAllChanges(): |
| 620 | self.log.debug(" checking %s" % change) |
| 621 | if self.pipeline.areAllJobsComplete(change): |
| 622 | self.log.debug(" possibly reporting %s" % change) |
| 623 | if self.possiblyReportChange(change): |
| 624 | reported = True |
| 625 | if reported: |
| 626 | self.reportChanges() |
| 627 | self.log.debug("Done searching for changes to report") |
| 628 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 629 | def possiblyReportChange(self, change): |
| 630 | self.log.debug("Possibly reporting change %s" % change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 631 | # Even if a change isn't reportable, keep going so that it |
| 632 | # gets dequeued in the normal manner. |
| 633 | if change.is_reportable and change.reported: |
| 634 | self.log.debug("Change %s already reported" % change) |
| 635 | return False |
| 636 | change_ahead = change.change_ahead |
| 637 | if not change_ahead: |
| 638 | self.log.debug("Change %s is at the front of the queue, " |
| 639 | "reporting" % (change)) |
| 640 | ret = self.reportChange(change) |
| 641 | if self.changes_merge: |
| 642 | succeeded = self.pipeline.didAllJobsSucceed(change) |
| 643 | merged = (not ret) |
| 644 | if merged: |
| 645 | merged = self.sched.trigger.isMerged(change, change.branch) |
| 646 | self.log.info("Reported change %s status: all-succeeded: %s, " |
| 647 | "merged: %s" % (change, succeeded, merged)) |
| 648 | if not (succeeded and merged): |
| 649 | self.log.debug("Reported change %s failed tests or failed " |
| 650 | "to merge" % (change)) |
| 651 | self.handleFailedChange(change) |
| 652 | return True |
| 653 | self.log.debug("Removing reported change %s from queue" % |
| 654 | change) |
| 655 | change_queue = self.pipeline.getQueue(change.project) |
| 656 | change_queue.dequeueChange(change) |
| 657 | return True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 658 | |
| 659 | def reportChange(self, change): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 660 | if not change.is_reportable: |
| 661 | return False |
James E. Blair | 6173fb3 | 2012-07-11 17:23:33 -0700 | [diff] [blame] | 662 | if change.reported: |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 663 | return 0 |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 664 | self.log.debug("Reporting change %s" % change) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 665 | ret = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 666 | if self.pipeline.didAllJobsSucceed(change): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 667 | action = self.success_action |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 668 | change.setReportedResult('SUCCESS') |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 669 | else: |
| 670 | action = self.failure_action |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 671 | change.setReportedResult('FAILURE') |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 672 | report = self.formatReport(change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 673 | change.reported = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 674 | try: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 675 | self.log.info("Reporting change %s, action: %s" % |
| 676 | (change, action)) |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 677 | ret = self.sched.trigger.report(change, report, action) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 678 | if ret: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 679 | self.log.error("Reporting change %s received: %s" % |
| 680 | (change, ret)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 681 | except: |
| 682 | self.log.exception("Exception while reporting:") |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 683 | change.setReportedResult('ERROR') |
| 684 | self.updateBuildDescriptions(change.current_build_set) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 685 | return ret |
| 686 | |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 687 | def formatReport(self, changeish): |
| 688 | ret = '' |
| 689 | if self.pipeline.didAllJobsSucceed(changeish): |
| 690 | ret += 'Build successful\n\n' |
| 691 | else: |
| 692 | ret += 'Build failed\n\n' |
| 693 | |
| 694 | if changeish.dequeued_needing_change: |
| 695 | ret += "This change depends on a change that failed to merge." |
| 696 | elif changeish.current_build_set.unable_to_merge: |
| 697 | ret += "This change was unable to be automatically merged "\ |
| 698 | "with the current state of the repository. Please "\ |
| 699 | "rebase your change and upload a new patchset." |
| 700 | else: |
James E. Blair | a35fcce | 2012-08-24 10:46:01 -0700 | [diff] [blame] | 701 | if self.sched.config.has_option('zuul', 'url_pattern'): |
| 702 | pattern = self.sched.config.get('zuul', 'url_pattern') |
| 703 | else: |
| 704 | pattern = None |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 705 | for job in self.pipeline.getJobs(changeish): |
| 706 | build = changeish.current_build_set.getBuild(job.name) |
| 707 | result = build.result |
| 708 | if result == 'SUCCESS' and job.success_message: |
| 709 | result = job.success_message |
| 710 | elif result == 'FAILURE' and job.failure_message: |
| 711 | result = job.failure_message |
James E. Blair | 7ee88a2 | 2012-09-12 18:59:31 +0200 | [diff] [blame] | 712 | url = None |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 713 | if build.url: |
| 714 | if pattern: |
| 715 | url = pattern.format(change=changeish, |
| 716 | pipeline=self.pipeline, |
| 717 | job=job, |
| 718 | build=build) |
| 719 | else: |
| 720 | url = build.url |
| 721 | if not url: |
| 722 | url = job.name |
| 723 | if not job.voting: |
| 724 | voting = ' (non-voting)' |
| 725 | else: |
| 726 | voting = '' |
| 727 | ret += '- %s : %s%s\n' % (url, result, voting) |
| 728 | return ret |
| 729 | |
| 730 | def formatDescription(self, build): |
| 731 | concurrent_changes = '' |
| 732 | concurrent_builds = '' |
| 733 | other_builds = '' |
| 734 | |
| 735 | for change in build.build_set.other_changes: |
| 736 | concurrent_changes += '<li><a href="{change.url}">\ |
| 737 | {change.number},{change.patchset}</a></li>'.format( |
| 738 | change=change) |
| 739 | |
| 740 | change = build.build_set.change |
| 741 | |
| 742 | for build in build.build_set.getBuilds(): |
| 743 | if build.base_url: |
| 744 | concurrent_builds += """\ |
| 745 | <li> |
| 746 | <a href="{build.base_url}"> |
| 747 | {build.job.name} #{build.number}</a>: {build.result} |
| 748 | </li> |
| 749 | """.format(build=build) |
| 750 | else: |
| 751 | concurrent_builds += """\ |
| 752 | <li> |
| 753 | {build.job.name}: {build.result} |
| 754 | </li>""".format(build=build) |
| 755 | |
| 756 | if build.build_set.previous_build_set: |
| 757 | other_build = build.build_set.previous_build_set.getBuild( |
| 758 | build.job.name) |
| 759 | if other_build: |
| 760 | other_builds += """\ |
| 761 | <li> |
| 762 | Preceded by: <a href="{build.base_url}"> |
| 763 | {build.job.name} #{build.number}</a> |
| 764 | </li> |
| 765 | """.format(build=other_build) |
| 766 | |
| 767 | if build.build_set.next_build_set: |
| 768 | other_build = build.build_set.next_build_set.getBuild( |
| 769 | build.job.name) |
| 770 | if other_build: |
| 771 | other_builds += """\ |
| 772 | <li> |
| 773 | Succeeded by: <a href="{build.base_url}"> |
| 774 | {build.job.name} #{build.number}</a> |
| 775 | </li> |
| 776 | """.format(build=other_build) |
| 777 | |
| 778 | result = build.build_set.result |
| 779 | |
| 780 | if hasattr(change, 'number'): |
| 781 | ret = """\ |
| 782 | <p> |
| 783 | Triggered by change: |
| 784 | <a href="{change.url}">{change.number},{change.patchset}</a><br/> |
| 785 | Branch: <b>{change.branch}</b><br/> |
| 786 | Pipeline: <b>{self.pipeline.name}</b> |
| 787 | </p>""" |
| 788 | else: |
| 789 | ret = """\ |
| 790 | <p> |
| 791 | Triggered by reference: |
| 792 | {change.ref}</a><br/> |
| 793 | Old revision: <b>{change.oldrev}</b><br/> |
| 794 | New revision: <b>{change.newrev}</b><br/> |
| 795 | Pipeline: <b>{self.pipeline.name}</b> |
| 796 | </p>""" |
| 797 | |
| 798 | if concurrent_changes: |
| 799 | ret += """\ |
| 800 | <p> |
| 801 | Other changes tested concurrently with this change: |
| 802 | <ul>{concurrent_changes}</ul> |
| 803 | </p> |
| 804 | """ |
| 805 | if concurrent_builds: |
| 806 | ret += """\ |
| 807 | <p> |
| 808 | All builds for this change set: |
| 809 | <ul>{concurrent_builds}</ul> |
| 810 | </p> |
| 811 | """ |
| 812 | |
| 813 | if other_builds: |
| 814 | ret += """\ |
| 815 | <p> |
| 816 | Other build sets for this change: |
| 817 | <ul>{other_builds}</ul> |
| 818 | </p> |
| 819 | """ |
| 820 | if result: |
| 821 | ret += """\ |
| 822 | <p> |
| 823 | Reported result: <b>{result}</b> |
| 824 | </p> |
| 825 | """ |
| 826 | |
| 827 | ret = ret.format(**locals()) |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 828 | return ret |
| 829 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 830 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 831 | class IndependentPipelineManager(BasePipelineManager): |
| 832 | log = logging.getLogger("zuul.IndependentPipelineManager") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 833 | changes_merge = False |
| 834 | |
| 835 | def _postConfig(self): |
| 836 | super(IndependentPipelineManager, self)._postConfig() |
| 837 | |
| 838 | change_queue = ChangeQueue(self.pipeline, dependent=False) |
| 839 | for project in self.pipeline.getProjects(): |
| 840 | change_queue.addProject(project) |
| 841 | |
| 842 | self.pipeline.addQueue(change_queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 843 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 844 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 845 | class DependentPipelineManager(BasePipelineManager): |
| 846 | log = logging.getLogger("zuul.DependentPipelineManager") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 847 | changes_merge = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 848 | |
| 849 | def __init__(self, *args, **kwargs): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 850 | super(DependentPipelineManager, self).__init__(*args, **kwargs) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 851 | |
| 852 | def _postConfig(self): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 853 | super(DependentPipelineManager, self)._postConfig() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 854 | self.buildChangeQueues() |
| 855 | |
| 856 | def buildChangeQueues(self): |
| 857 | self.log.debug("Building shared change queues") |
| 858 | change_queues = [] |
| 859 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 860 | for project in self.pipeline.getProjects(): |
| 861 | change_queue = ChangeQueue(self.pipeline) |
| 862 | change_queue.addProject(project) |
| 863 | change_queues.append(change_queue) |
| 864 | self.log.debug("Created queue: %s" % change_queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 865 | |
| 866 | self.log.debug("Combining shared queues") |
| 867 | new_change_queues = [] |
| 868 | for a in change_queues: |
| 869 | merged_a = False |
| 870 | for b in new_change_queues: |
| 871 | if not a.getJobs().isdisjoint(b.getJobs()): |
| 872 | self.log.debug("Merging queue %s into %s" % (a, b)) |
| 873 | b.mergeChangeQueue(a) |
| 874 | merged_a = True |
| 875 | break # this breaks out of 'for b' and continues 'for a' |
| 876 | if not merged_a: |
| 877 | self.log.debug("Keeping queue %s" % (a)) |
| 878 | new_change_queues.append(a) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 879 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 880 | self.log.info(" Shared change queues:") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 881 | for queue in new_change_queues: |
| 882 | self.pipeline.addQueue(queue) |
| 883 | self.log.info(" %s" % queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 884 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 885 | def isChangeReadyToBeEnqueued(self, change): |
| 886 | if not self.sched.trigger.canMerge(change, |
| 887 | self.getSubmitAllowNeeds()): |
| 888 | self.log.debug("Change %s can not merge, ignoring" % change) |
| 889 | return False |
| 890 | return True |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 891 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 892 | def enqueueChangesBehind(self, change): |
| 893 | to_enqueue = [] |
| 894 | self.log.debug("Checking for changes needing %s:" % change) |
| 895 | if not hasattr(change, 'needed_by_changes'): |
| 896 | self.log.debug(" Changeish does not support dependencies") |
| 897 | return |
| 898 | for needs in change.needed_by_changes: |
| 899 | if self.sched.trigger.canMerge(needs, |
| 900 | self.getSubmitAllowNeeds()): |
| 901 | self.log.debug(" Change %s needs %s and is ready to merge" % |
| 902 | (needs, change)) |
| 903 | to_enqueue.append(needs) |
| 904 | if not to_enqueue: |
| 905 | self.log.debug(" No changes need %s" % change) |
| 906 | |
| 907 | for other_change in to_enqueue: |
| 908 | self.addChange(other_change) |
| 909 | |
| 910 | def enqueueChangesAhead(self, change): |
| 911 | ret = self.checkForChangesNeededBy(change) |
| 912 | if ret in [True, False]: |
| 913 | return ret |
| 914 | self.log.debug(" Change %s must be merged ahead of %s" % |
| 915 | (ret, change)) |
| 916 | return self.addChange(ret) |
| 917 | |
| 918 | def checkForChangesNeededBy(self, change): |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 919 | self.log.debug("Checking for changes needed by %s:" % change) |
| 920 | # Return true if okay to proceed enqueing this change, |
| 921 | # false if the change should not be enqueued. |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 922 | if not hasattr(change, 'needs_change'): |
| 923 | self.log.debug(" Changeish does not support dependencies") |
| 924 | return True |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 925 | if not change.needs_change: |
| 926 | self.log.debug(" No changes needed") |
| 927 | return True |
| 928 | if change.needs_change.is_merged: |
| 929 | self.log.debug(" Needed change is merged") |
| 930 | return True |
| 931 | if not change.needs_change.is_current_patchset: |
| 932 | self.log.debug(" Needed change is not the current patchset") |
| 933 | return False |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 934 | if self.isChangeAlreadyInQueue(change.needs_change): |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 935 | self.log.debug(" Needed change is already ahead in the queue") |
| 936 | return True |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 937 | if self.sched.trigger.canMerge(change.needs_change, |
| 938 | self.getSubmitAllowNeeds()): |
| 939 | self.log.debug(" Change %s is needed" % |
| 940 | change.needs_change) |
| 941 | return change.needs_change |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 942 | # The needed change can't be merged. |
| 943 | self.log.debug(" Change %s is needed but can not be merged" % |
| 944 | change.needs_change) |
| 945 | return False |
| 946 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 947 | def _getDependentChanges(self, change): |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 948 | orig_change = change |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 949 | changes = [] |
| 950 | while change.change_ahead: |
| 951 | changes.append(change.change_ahead) |
| 952 | change = change.change_ahead |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 953 | self.log.info("Change %s depends on changes %s" % (orig_change, |
| 954 | changes)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 955 | return changes |
| 956 | |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 957 | def _launchJobs(self, change, jobs): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 958 | self.log.debug("Launching jobs for change %s" % change) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 959 | ref = change.current_build_set.getRef() |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 960 | if hasattr(change, 'refspec') and not ref: |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 961 | change.current_build_set.setConfiguration() |
| 962 | ref = change.current_build_set.getRef() |
| 963 | dependent_changes = self._getDependentChanges(change) |
| 964 | dependent_changes.reverse() |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 965 | all_changes = dependent_changes + [change] |
| 966 | merged = self.sched.merger.mergeChanges(all_changes, ref) |
| 967 | if not merged: |
| 968 | self.log.info("Unable to merge changes %s" % all_changes) |
| 969 | self.pipeline.setUnableToMerge(change) |
| 970 | self.possiblyReportChange(change) |
| 971 | return |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 972 | #TODO: remove this line after GERRIT_CHANGES is gone |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 973 | dependent_changes = self._getDependentChanges(change) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 974 | for job in jobs: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 975 | self.log.debug("Found job %s for change %s" % (job, change)) |
| 976 | try: |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 977 | #TODO: remove dependent_changes after GERRIT_CHANGES is gone |
James E. Blair | 03b94ef | 2012-08-20 10:54:29 -0700 | [diff] [blame] | 978 | build = self.sched.launcher.launch(job, change, self.pipeline, |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 979 | dependent_changes) |
| 980 | self.building_jobs[build] = change |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 981 | self.log.debug("Adding build %s of job %s to change %s" % |
| 982 | (build, job, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 983 | change.addBuild(build) |
| 984 | except: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 985 | self.log.exception("Exception while launching job %s " |
| 986 | "for change %s:" % (job, change)) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 987 | |
Clark Boylan | 826ef9e | 2012-07-12 16:44:46 -0700 | [diff] [blame] | 988 | def cancelJobs(self, change, prime=True): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 989 | self.log.debug("Cancel jobs for change %s" % change) |
| 990 | to_remove = [] |
Clark Boylan | 826ef9e | 2012-07-12 16:44:46 -0700 | [diff] [blame] | 991 | if prime: |
| 992 | change.resetAllBuilds() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 993 | for build, build_change in self.building_jobs.items(): |
| 994 | if build_change == change: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 995 | self.log.debug("Found build %s for change %s to cancel" % |
| 996 | (build, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 997 | try: |
| 998 | self.sched.launcher.cancel(build) |
| 999 | except: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 1000 | self.log.exception("Exception while canceling build %s " |
| 1001 | "for change %s" % (build, change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1002 | to_remove.append(build) |
| 1003 | for build in to_remove: |
| 1004 | self.log.debug("Removing build %s from running builds" % build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1005 | build.result = 'CANCELED' |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1006 | del self.building_jobs[build] |
| 1007 | if change.change_behind: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 1008 | self.log.debug("Canceling jobs for change %s, behind change %s" % |
| 1009 | (change.change_behind, change)) |
Clark Boylan | 826ef9e | 2012-07-12 16:44:46 -0700 | [diff] [blame] | 1010 | self.cancelJobs(change.change_behind, prime=prime) |
| 1011 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1012 | def handleFailedChange(self, change): |
| 1013 | # A build failed. All changes behind this change will need to |
| 1014 | # be retested. To free up resources cancel the builds behind |
| 1015 | # this one as they will be rerun anyways. |
| 1016 | change_ahead = change.change_ahead |
| 1017 | change_behind = change.change_behind |
| 1018 | if not change_ahead: |
| 1019 | # If we're at the head of the queue, allow changes to relaunch |
| 1020 | if change_behind: |
James E. Blair | ec59012 | 2012-08-22 15:19:31 -0700 | [diff] [blame] | 1021 | self.log.info("Canceling/relaunching jobs for change %s " |
| 1022 | "behind failed change %s" % |
| 1023 | (change_behind, change)) |
| 1024 | self.cancelJobs(change_behind) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1025 | self.dequeueChange(change) |
Clark Boylan | afd18ac | 2012-08-22 12:59:32 -0700 | [diff] [blame] | 1026 | elif change_behind: |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1027 | self.log.debug("Canceling builds behind change: %s due to " |
| 1028 | "failure." % change) |
| 1029 | self.cancelJobs(change_behind, prime=False) |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 1030 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1031 | def dequeueChange(self, change): |
| 1032 | self.log.debug("Removing change %s from queue" % change) |
| 1033 | change_ahead = change.change_ahead |
| 1034 | change_behind = change.change_behind |
| 1035 | change_queue = self.pipeline.getQueue(change.project) |
| 1036 | change_queue.dequeueChange(change) |
| 1037 | if not change_ahead and not change.reported: |
| 1038 | self.log.debug("Adding %s as a severed head" % change) |
| 1039 | change_queue.addSeveredHead(change) |
| 1040 | self.dequeueDependentChanges(change_behind) |
| 1041 | |
| 1042 | def dequeueDependentChanges(self, change): |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1043 | # When a change is dequeued after failing, dequeue any changes that |
| 1044 | # depend on it. |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1045 | while change: |
| 1046 | change_behind = change.change_behind |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1047 | if self.checkForChangesNeededBy(change) is not True: |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1048 | # 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] | 1049 | self.log.info("Dequeuing change %s because " |
| 1050 | "it can no longer merge" % change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1051 | change_queue = self.pipeline.getQueue(change.project) |
| 1052 | change_queue.dequeueChange(change) |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1053 | self.pipeline.setDequeuedNeedingChange(change) |
| 1054 | self.reportChange(change) |
| 1055 | # We don't need to recurse, because any changes that might |
| 1056 | # be affected by the removal of this change are behind us |
| 1057 | # in the queue, so we can continue walking backwards. |
| 1058 | change = change_behind |