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