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 |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 3 | # Copyright 2013 Antoine "hashar" Musso |
| 4 | # Copyright 2013 Wikimedia Foundation Inc. |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 18 | import extras |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 19 | import json |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 20 | import logging |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 21 | import os |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 22 | import pickle |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 23 | import Queue |
| 24 | import re |
| 25 | import threading |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 26 | import time |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 27 | import yaml |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 28 | |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 29 | import layoutvalidator |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 30 | import model |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 31 | from model import ActionReporter, Pipeline, Project, ChangeQueue, EventFilter |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 32 | import merger |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 33 | |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 34 | statsd = extras.try_import('statsd.statsd') |
| 35 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 36 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 37 | def deep_format(obj, paramdict): |
| 38 | """Apply the paramdict via str.format() to all string objects found within |
| 39 | the supplied obj. Lists and dicts are traversed recursively. |
| 40 | |
| 41 | Borrowed from Jenkins Job Builder project""" |
| 42 | if isinstance(obj, str): |
| 43 | ret = obj.format(**paramdict) |
| 44 | elif isinstance(obj, list): |
| 45 | ret = [] |
| 46 | for item in obj: |
| 47 | ret.append(deep_format(item, paramdict)) |
| 48 | elif isinstance(obj, dict): |
| 49 | ret = {} |
| 50 | for item in obj: |
| 51 | exp_item = item.format(**paramdict) |
| 52 | |
| 53 | ret[exp_item] = deep_format(obj[item], paramdict) |
| 54 | else: |
| 55 | ret = obj |
| 56 | return ret |
| 57 | |
| 58 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 59 | class MergeFailure(Exception): |
| 60 | pass |
| 61 | |
| 62 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 63 | class Scheduler(threading.Thread): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 64 | log = logging.getLogger("zuul.Scheduler") |
| 65 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 66 | def __init__(self): |
| 67 | threading.Thread.__init__(self) |
James E. Blair | 8a6f0c2 | 2013-07-01 12:31:34 -0400 | [diff] [blame] | 68 | self.daemon = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 69 | self.wake_event = threading.Event() |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 70 | self.layout_lock = threading.Lock() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 71 | self.reconfigure_complete_event = threading.Event() |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 72 | self._pause = False |
| 73 | self._reconfigure = False |
| 74 | self._exit = False |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 75 | self._stopped = False |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 76 | self.launcher = None |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 77 | self.triggers = dict() |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 78 | self.reporters = dict() |
James E. Blair | 3c5e5b5 | 2013-04-26 11:17:03 -0700 | [diff] [blame] | 79 | self.config = None |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 80 | self._maintain_trigger_cache = False |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 81 | |
| 82 | self.trigger_event_queue = Queue.Queue() |
| 83 | self.result_event_queue = Queue.Queue() |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 84 | self.layout = model.Layout() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 85 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 86 | def stop(self): |
| 87 | self._stopped = True |
| 88 | self.wake_event.set() |
| 89 | |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 90 | def testConfig(self, config_path): |
James E. Blair | 04948c7 | 2013-07-25 23:03:17 -0700 | [diff] [blame] | 91 | return self._parseConfig(config_path) |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 92 | |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 93 | def _parseConfig(self, config_path): |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 94 | layout = model.Layout() |
| 95 | project_templates = {} |
| 96 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 97 | def toList(item): |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 98 | if not item: |
| 99 | return [] |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 100 | if isinstance(item, list): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 101 | return item |
| 102 | return [item] |
| 103 | |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 104 | if config_path: |
| 105 | config_path = os.path.expanduser(config_path) |
| 106 | if not os.path.exists(config_path): |
| 107 | raise Exception("Unable to read layout config file at %s" % |
| 108 | config_path) |
| 109 | config_file = open(config_path) |
| 110 | data = yaml.load(config_file) |
| 111 | |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 112 | validator = layoutvalidator.LayoutValidator() |
| 113 | validator.validate(data) |
| 114 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 115 | config_env = {} |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 116 | for include in data.get('includes', []): |
| 117 | if 'python-file' in include: |
| 118 | fn = include['python-file'] |
| 119 | if not os.path.isabs(fn): |
| 120 | base = os.path.dirname(config_path) |
| 121 | fn = os.path.join(base, fn) |
| 122 | fn = os.path.expanduser(fn) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 123 | execfile(fn, config_env) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 124 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 125 | for conf_pipeline in data.get('pipelines', []): |
| 126 | pipeline = Pipeline(conf_pipeline['name']) |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 127 | pipeline.description = conf_pipeline.get('description') |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 128 | precedence = model.PRECEDENCE_MAP[conf_pipeline.get('precedence')] |
| 129 | pipeline.precedence = precedence |
James E. Blair | 5637019 | 2013-01-14 15:47:28 -0800 | [diff] [blame] | 130 | pipeline.failure_message = conf_pipeline.get('failure-message', |
| 131 | "Build failed.") |
| 132 | pipeline.success_message = conf_pipeline.get('success-message', |
| 133 | "Build succeeded.") |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 134 | pipeline.dequeue_on_new_patchset = conf_pipeline.get( |
James E. Blair | 6736beb | 2013-07-11 15:18:15 -0700 | [diff] [blame] | 135 | 'dequeue-on-new-patchset', True) |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 136 | |
| 137 | action_reporters = {} |
| 138 | for action in ['start', 'success', 'failure']: |
| 139 | action_reporters[action] = [] |
| 140 | if conf_pipeline.get(action): |
| 141 | for reporter_name, params \ |
| 142 | in conf_pipeline.get(action).items(): |
| 143 | if reporter_name in self.reporters.keys(): |
| 144 | action_reporters[action].append(ActionReporter( |
| 145 | self.reporters[reporter_name], params)) |
| 146 | else: |
| 147 | self.log.error('Invalid reporter name %s' % |
| 148 | reporter_name) |
| 149 | pipeline.start_actions = action_reporters['start'] |
| 150 | pipeline.success_actions = action_reporters['success'] |
| 151 | pipeline.failure_actions = action_reporters['failure'] |
| 152 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 153 | manager = globals()[conf_pipeline['manager']](self, pipeline) |
| 154 | pipeline.setManager(manager) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 155 | layout.pipelines[conf_pipeline['name']] = pipeline |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 156 | |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 157 | # TODO: move this into triggers (may require pluggable |
| 158 | # configuration) |
| 159 | if 'gerrit' in conf_pipeline['trigger']: |
| 160 | pipeline.trigger = self.triggers['gerrit'] |
| 161 | for trigger in toList(conf_pipeline['trigger']['gerrit']): |
| 162 | approvals = {} |
| 163 | for approval_dict in toList(trigger.get('approval')): |
| 164 | for k, v in approval_dict.items(): |
| 165 | approvals[k] = v |
| 166 | f = EventFilter(types=toList(trigger['event']), |
| 167 | branches=toList(trigger.get('branch')), |
| 168 | refs=toList(trigger.get('ref')), |
| 169 | approvals=approvals, |
| 170 | comment_filters= |
| 171 | toList(trigger.get('comment_filter')), |
| 172 | email_filters= |
| 173 | toList(trigger.get('email_filter'))) |
| 174 | manager.event_filters.append(f) |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 175 | elif 'timer' in conf_pipeline['trigger']: |
| 176 | pipeline.trigger = self.triggers['timer'] |
| 177 | for trigger in toList(conf_pipeline['trigger']['timer']): |
| 178 | f = EventFilter(types=['timer'], |
| 179 | timespecs=toList(trigger['time'])) |
| 180 | manager.event_filters.append(f) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 181 | |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 182 | for project_template in data.get('project-templates', []): |
| 183 | # Make sure the template only contains valid pipelines |
| 184 | tpl = dict( |
| 185 | (pipe_name, project_template.get(pipe_name)) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 186 | for pipe_name in layout.pipelines.keys() |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 187 | if pipe_name in project_template |
| 188 | ) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 189 | project_templates[project_template.get('name')] = tpl |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 190 | |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 191 | for config_job in data.get('jobs', []): |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 192 | job = layout.getJob(config_job['name']) |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 193 | # Be careful to only set attributes explicitly present on |
| 194 | # this job, to avoid squashing attributes set by a meta-job. |
| 195 | m = config_job.get('failure-message', None) |
| 196 | if m: |
| 197 | job.failure_message = m |
| 198 | m = config_job.get('success-message', None) |
| 199 | if m: |
| 200 | job.success_message = m |
James E. Blair | 6aea36d | 2012-12-17 13:03:24 -0800 | [diff] [blame] | 201 | m = config_job.get('failure-pattern', None) |
| 202 | if m: |
| 203 | job.failure_pattern = m |
| 204 | m = config_job.get('success-pattern', None) |
| 205 | if m: |
| 206 | job.success_pattern = m |
James E. Blair | 222d498 | 2012-07-16 09:31:19 -0700 | [diff] [blame] | 207 | m = config_job.get('hold-following-changes', False) |
| 208 | if m: |
| 209 | job.hold_following_changes = True |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 210 | m = config_job.get('voting', None) |
| 211 | if m is not None: |
| 212 | job.voting = m |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 213 | fname = config_job.get('parameter-function', None) |
| 214 | if fname: |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 215 | func = config_env.get(fname, None) |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 216 | if not func: |
| 217 | raise Exception("Unable to find function %s" % fname) |
| 218 | job.parameter_function = func |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 219 | branches = toList(config_job.get('branch')) |
| 220 | if branches: |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 221 | job._branches = branches |
| 222 | job.branches = [re.compile(x) for x in branches] |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 223 | files = toList(config_job.get('files')) |
| 224 | if files: |
| 225 | job._files = files |
| 226 | job.files = [re.compile(x) for x in files] |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 227 | |
| 228 | def add_jobs(job_tree, config_jobs): |
| 229 | for job in config_jobs: |
| 230 | if isinstance(job, list): |
| 231 | for x in job: |
| 232 | add_jobs(job_tree, x) |
| 233 | if isinstance(job, dict): |
| 234 | for parent, children in job.items(): |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 235 | parent_tree = job_tree.addJob(layout.getJob(parent)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 236 | add_jobs(parent_tree, children) |
| 237 | if isinstance(job, str): |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 238 | job_tree.addJob(layout.getJob(job)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 239 | |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 240 | for config_project in data.get('projects', []): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 241 | project = Project(config_project['name']) |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 242 | |
| 243 | for requested_template in config_project.get('template', []): |
| 244 | # Fetch the template from 'project-templates' |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 245 | tpl = project_templates.get( |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 246 | requested_template.get('name')) |
| 247 | # Expand it with the project context |
| 248 | expanded = deep_format(tpl, requested_template) |
| 249 | # Finally merge the expansion with whatever has been already |
| 250 | # defined for this project |
| 251 | config_project.update(expanded) |
| 252 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 253 | layout.projects[config_project['name']] = project |
James E. Blair | 19deff2 | 2013-08-25 13:17:35 -0700 | [diff] [blame] | 254 | mode = config_project.get('merge-mode', 'merge-resolve') |
| 255 | project.merge_mode = model.MERGER_MAP[mode] |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 256 | for pipeline in layout.pipelines.values(): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 257 | if pipeline.name in config_project: |
| 258 | job_tree = pipeline.addProject(project) |
| 259 | config_jobs = config_project[pipeline.name] |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 260 | add_jobs(job_tree, config_jobs) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 261 | |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 262 | # All jobs should be defined at this point, get rid of |
| 263 | # metajobs so that getJob isn't doing anything weird. |
James E. Blair | c28d1b0 | 2013-07-19 11:37:06 -0700 | [diff] [blame] | 264 | layout.metajobs = [] |
James E. Blair | b095465 | 2012-06-01 11:32:01 -0700 | [diff] [blame] | 265 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 266 | for pipeline in layout.pipelines.values(): |
| 267 | pipeline.manager._postConfig(layout) |
| 268 | |
| 269 | return layout |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 270 | |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 271 | def _setupMerger(self): |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 272 | if self.config.has_option('zuul', 'git_dir'): |
| 273 | merge_root = self.config.get('zuul', 'git_dir') |
| 274 | else: |
| 275 | merge_root = '/var/lib/zuul/git' |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 276 | |
Paul Belanger | b67aba1 | 2013-05-13 19:22:14 -0400 | [diff] [blame] | 277 | if self.config.has_option('zuul', 'git_user_email'): |
| 278 | merge_email = self.config.get('zuul', 'git_user_email') |
| 279 | else: |
| 280 | merge_email = None |
| 281 | |
| 282 | if self.config.has_option('zuul', 'git_user_name'): |
| 283 | merge_name = self.config.get('zuul', 'git_user_name') |
| 284 | else: |
| 285 | merge_name = None |
| 286 | |
James E. Blair | ceabcbc | 2012-08-17 13:48:46 -0700 | [diff] [blame] | 287 | if self.config.has_option('zuul', 'push_change_refs'): |
| 288 | push_refs = self.config.getboolean('zuul', 'push_change_refs') |
| 289 | else: |
| 290 | push_refs = False |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 291 | |
James E. Blair | ad61501 | 2012-11-30 16:14:21 -0800 | [diff] [blame] | 292 | if self.config.has_option('gerrit', 'sshkey'): |
| 293 | sshkey = self.config.get('gerrit', 'sshkey') |
| 294 | else: |
| 295 | sshkey = None |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 296 | |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 297 | # TODO: The merger should have an upstream repo independent of |
| 298 | # triggers, and then each trigger should provide a fetch |
| 299 | # location. |
| 300 | self.merger = merger.Merger(self.triggers['gerrit'], |
| 301 | merge_root, push_refs, |
Paul Belanger | b67aba1 | 2013-05-13 19:22:14 -0400 | [diff] [blame] | 302 | sshkey, merge_email, merge_name) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 303 | for project in self.layout.projects.values(): |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 304 | url = self.triggers['gerrit'].getGitUrl(project) |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 305 | self.merger.addProject(project, url) |
| 306 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 307 | def setLauncher(self, launcher): |
| 308 | self.launcher = launcher |
| 309 | |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 310 | def registerTrigger(self, trigger, name=None): |
| 311 | if name is None: |
| 312 | name = trigger.name |
| 313 | self.triggers[name] = trigger |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 314 | |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 315 | def registerReporter(self, reporter, name=None): |
| 316 | if name is None: |
| 317 | name = reporter.name |
| 318 | self.reporters[name] = reporter |
| 319 | |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 320 | def getProject(self, name): |
| 321 | self.layout_lock.acquire() |
| 322 | p = None |
| 323 | try: |
| 324 | p = self.layout.projects.get(name) |
| 325 | finally: |
| 326 | self.layout_lock.release() |
| 327 | return p |
| 328 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 329 | def addEvent(self, event): |
| 330 | self.log.debug("Adding trigger event: %s" % event) |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 331 | try: |
| 332 | if statsd: |
| 333 | statsd.incr('gerrit.event.%s' % event.type) |
| 334 | except: |
| 335 | self.log.exception("Exception reporting event stats") |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 336 | self.trigger_event_queue.put(event) |
| 337 | self.wake_event.set() |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 338 | self.log.debug("Done adding trigger event: %s" % event) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 339 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 340 | def onBuildStarted(self, build): |
| 341 | self.log.debug("Adding start event for build: %s" % build) |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 342 | build.start_time = time.time() |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 343 | self.result_event_queue.put(('started', build)) |
| 344 | self.wake_event.set() |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 345 | self.log.debug("Done adding start event for build: %s" % build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 346 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 347 | def onBuildCompleted(self, build): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 348 | self.log.debug("Adding complete event for build: %s" % build) |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 349 | build.end_time = time.time() |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 350 | try: |
James E. Blair | 66eeebf | 2013-07-27 17:44:32 -0700 | [diff] [blame] | 351 | if statsd and build.pipeline: |
| 352 | jobname = build.job.name.replace('.', '_') |
| 353 | key = 'zuul.pipeline.%s.job.%s.%s' % (build.pipeline.name, |
| 354 | jobname, build.result) |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 355 | if build.result in ['SUCCESS', 'FAILURE'] and build.start_time: |
| 356 | dt = int((build.end_time - build.start_time) * 1000) |
| 357 | statsd.timing(key, dt) |
| 358 | statsd.incr(key) |
James E. Blair | 7f4a190 | 2013-08-24 08:20:02 -0700 | [diff] [blame] | 359 | key = 'zuul.pipeline.%s.all_jobs' % build.pipeline.name |
| 360 | statsd.incr(key) |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 361 | except: |
| 362 | self.log.exception("Exception reporting runtime stats") |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 363 | self.result_event_queue.put(('completed', build)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 364 | self.wake_event.set() |
James E. Blair | f62d428 | 2012-12-31 17:01:50 -0800 | [diff] [blame] | 365 | self.log.debug("Done adding complete event for build: %s" % build) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 366 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 367 | def reconfigure(self, config): |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 368 | self.log.debug("Prepare to reconfigure") |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 369 | self.config = config |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 370 | self._reconfigure = True |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 371 | self.wake_event.set() |
| 372 | self.log.debug("Waiting for reconfiguration") |
| 373 | self.reconfigure_complete_event.wait() |
| 374 | self.reconfigure_complete_event.clear() |
| 375 | self.log.debug("Reconfiguration complete") |
| 376 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 377 | def exit(self): |
| 378 | self.log.debug("Prepare to exit") |
| 379 | self._pause = True |
| 380 | self._exit = True |
| 381 | self.wake_event.set() |
| 382 | self.log.debug("Waiting for exit") |
| 383 | |
| 384 | def _get_queue_pickle_file(self): |
James E. Blair | 5a95c86 | 2012-07-09 15:11:17 -0700 | [diff] [blame] | 385 | if self.config.has_option('zuul', 'state_dir'): |
| 386 | state_dir = os.path.expanduser(self.config.get('zuul', |
| 387 | 'state_dir')) |
| 388 | else: |
| 389 | state_dir = '/var/lib/zuul' |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 390 | return os.path.join(state_dir, 'queue.pickle') |
| 391 | |
| 392 | def _save_queue(self): |
| 393 | pickle_file = self._get_queue_pickle_file() |
| 394 | events = [] |
| 395 | while not self.trigger_event_queue.empty(): |
| 396 | events.append(self.trigger_event_queue.get()) |
| 397 | self.log.debug("Queue length is %s" % len(events)) |
| 398 | if events: |
| 399 | self.log.debug("Saving queue") |
| 400 | pickle.dump(events, open(pickle_file, 'wb')) |
| 401 | |
| 402 | def _load_queue(self): |
| 403 | pickle_file = self._get_queue_pickle_file() |
| 404 | if os.path.exists(pickle_file): |
| 405 | self.log.debug("Loading queue") |
| 406 | events = pickle.load(open(pickle_file, 'rb')) |
| 407 | self.log.debug("Queue length is %s" % len(events)) |
| 408 | for event in events: |
| 409 | self.trigger_event_queue.put(event) |
| 410 | else: |
| 411 | self.log.debug("No queue file found") |
| 412 | |
| 413 | def _delete_queue(self): |
| 414 | pickle_file = self._get_queue_pickle_file() |
| 415 | if os.path.exists(pickle_file): |
| 416 | self.log.debug("Deleting saved queue") |
| 417 | os.unlink(pickle_file) |
| 418 | |
| 419 | def resume(self): |
| 420 | try: |
| 421 | self._load_queue() |
| 422 | except: |
| 423 | self.log.exception("Unable to load queue") |
| 424 | try: |
| 425 | self._delete_queue() |
| 426 | except: |
| 427 | self.log.exception("Unable to delete saved queue") |
| 428 | self.log.debug("Resuming queue processing") |
| 429 | self.wake_event.set() |
| 430 | |
| 431 | def _doPauseEvent(self): |
| 432 | if self._exit: |
| 433 | self.log.debug("Exiting") |
| 434 | self._save_queue() |
| 435 | os._exit(0) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 436 | |
| 437 | def _doReconfigureEvent(self): |
| 438 | # This is called in the scheduler loop after another thread sets |
| 439 | # the reconfigure flag |
| 440 | self.layout_lock.acquire() |
| 441 | try: |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 442 | self.log.debug("Performing reconfiguration") |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 443 | layout = self._parseConfig( |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 444 | self.config.get('zuul', 'layout_config')) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 445 | for name, new_pipeline in layout.pipelines.items(): |
| 446 | old_pipeline = self.layout.pipelines.get(name) |
| 447 | if not old_pipeline: |
| 448 | if self.layout.pipelines: |
| 449 | # Don't emit this warning on startup |
| 450 | self.log.warning("No old pipeline matching %s found " |
| 451 | "when reconfiguring" % name) |
| 452 | continue |
| 453 | self.log.debug("Re-enqueueing changes for pipeline %s" % |
| 454 | name) |
| 455 | items_to_remove = [] |
| 456 | for shared_queue in old_pipeline.queues: |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 457 | for item in shared_queue.queue: |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 458 | item.item_ahead = None |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 459 | item.items_behind = [] |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 460 | item.pipeline = None |
| 461 | project = layout.projects.get(item.change.project.name) |
| 462 | if not project: |
| 463 | self.log.warning("Unable to find project for " |
| 464 | "change %s while reenqueueing" % |
| 465 | item.change) |
| 466 | item.change.project = None |
| 467 | items_to_remove.append(item) |
| 468 | continue |
| 469 | item.change.project = project |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 470 | if not new_pipeline.manager.reEnqueueItem(item): |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 471 | items_to_remove.append(item) |
| 472 | builds_to_remove = [] |
| 473 | for build, item in old_pipeline.manager.building_jobs.items(): |
| 474 | if item in items_to_remove: |
| 475 | builds_to_remove.append(build) |
| 476 | self.log.warning("Deleting running build %s for " |
| 477 | "change %s while reenqueueing" % ( |
| 478 | build, item.change)) |
| 479 | for build in builds_to_remove: |
| 480 | del old_pipeline.manager.building_jobs[build] |
| 481 | new_pipeline.manager.building_jobs = \ |
| 482 | old_pipeline.manager.building_jobs |
| 483 | self.layout = layout |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 484 | self._setupMerger() |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 485 | for trigger in self.triggers.values(): |
| 486 | trigger.postConfig() |
James E. Blair | 3cb1070 | 2013-08-24 08:56:03 -0700 | [diff] [blame] | 487 | if statsd: |
| 488 | try: |
| 489 | for pipeline in self.layout.pipelines.values(): |
| 490 | items = len(pipeline.getAllItems()) |
| 491 | # stats.gauges.zuul.pipeline.NAME.current_changes |
| 492 | key = 'zuul.pipeline.%s' % pipeline.name |
| 493 | statsd.gauge(key + '.current_changes', items) |
| 494 | except Exception: |
| 495 | self.log.exception("Exception reporting initial " |
| 496 | "pipeline stats:") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 497 | self._reconfigure = False |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 498 | self.reconfigure_complete_event.set() |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 499 | finally: |
| 500 | self.layout_lock.release() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 501 | |
| 502 | def _areAllBuildsComplete(self): |
| 503 | self.log.debug("Checking if all builds are complete") |
| 504 | waiting = False |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 505 | for pipeline in self.layout.pipelines.values(): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 506 | for build in pipeline.manager.building_jobs.keys(): |
| 507 | self.log.debug("%s waiting on %s" % (pipeline.manager, build)) |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 508 | waiting = True |
| 509 | if not waiting: |
| 510 | self.log.debug("All builds are complete") |
| 511 | return True |
| 512 | self.log.debug("All builds are not complete") |
| 513 | return False |
| 514 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 515 | def run(self): |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 516 | if statsd: |
| 517 | self.log.debug("Statsd enabled") |
| 518 | else: |
| 519 | self.log.debug("Statsd disabled because python statsd " |
| 520 | "package not found") |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 521 | while True: |
| 522 | self.log.debug("Run handler sleeping") |
| 523 | self.wake_event.wait() |
| 524 | self.wake_event.clear() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 525 | if self._stopped: |
| 526 | return |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 527 | self.log.debug("Run handler awake") |
| 528 | try: |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 529 | if self._reconfigure: |
| 530 | self._doReconfigureEvent() |
| 531 | |
James E. Blair | 263fba9 | 2013-02-27 13:07:19 -0800 | [diff] [blame] | 532 | # Give result events priority -- they let us stop builds, |
| 533 | # whereas trigger evensts cause us to launch builds. |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 534 | if not self.result_event_queue.empty(): |
| 535 | self.process_result_queue() |
James E. Blair | 263fba9 | 2013-02-27 13:07:19 -0800 | [diff] [blame] | 536 | elif not self._pause: |
| 537 | if not self.trigger_event_queue.empty(): |
| 538 | self.process_event_queue() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 539 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 540 | if self._pause and self._areAllBuildsComplete(): |
| 541 | self._doPauseEvent() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 542 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 543 | if not self._pause: |
James E. Blair | 4baa94c | 2012-06-07 17:04:21 -0700 | [diff] [blame] | 544 | if not (self.trigger_event_queue.empty() and |
| 545 | self.result_event_queue.empty()): |
| 546 | self.wake_event.set() |
| 547 | else: |
| 548 | if not self.result_event_queue.empty(): |
| 549 | self.wake_event.set() |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 550 | |
| 551 | if self._maintain_trigger_cache: |
| 552 | self.maintainTriggerCache() |
| 553 | self._maintain_trigger_cache = False |
| 554 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 555 | except: |
| 556 | self.log.exception("Exception in run handler:") |
| 557 | |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 558 | def maintainTriggerCache(self): |
| 559 | relevant = set() |
| 560 | for pipeline in self.layout.pipelines.values(): |
James E. Blair | fadc6e1 | 2013-08-21 18:23:15 -0700 | [diff] [blame] | 561 | self.log.debug("Start maintain trigger cache for: %s" % pipeline) |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 562 | for item in pipeline.getAllItems(): |
| 563 | relevant.add(item.change) |
| 564 | relevant.update(item.change.getRelatedChanges()) |
James E. Blair | fadc6e1 | 2013-08-21 18:23:15 -0700 | [diff] [blame] | 565 | self.log.debug("End maintain trigger cache for: %s" % pipeline) |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 566 | self.log.debug("Trigger cache size: %s" % len(relevant)) |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 567 | for trigger in self.triggers.values(): |
| 568 | trigger.maintainCache(relevant) |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 569 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 570 | def process_event_queue(self): |
| 571 | self.log.debug("Fetching trigger event") |
| 572 | event = self.trigger_event_queue.get() |
| 573 | self.log.debug("Processing trigger event %s" % event) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 574 | project = self.layout.projects.get(event.project_name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 575 | if not project: |
| 576 | self.log.warning("Project %s not found" % event.project_name) |
James E. Blair | ff79197 | 2013-01-09 11:45:43 -0800 | [diff] [blame] | 577 | self.trigger_event_queue.task_done() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 578 | return |
| 579 | |
Antoine Musso | feba967 | 2013-01-17 13:44:59 +0100 | [diff] [blame] | 580 | # Preprocessing for ref-update events |
James E. Blair | 312df9a | 2013-08-27 17:17:02 -0700 | [diff] [blame] | 581 | if event.ref: |
Antoine Musso | feba967 | 2013-01-17 13:44:59 +0100 | [diff] [blame] | 582 | # Make sure the local git repo is up-to-date with the remote one. |
| 583 | # We better have the new ref before enqueuing the changes. |
| 584 | # This is done before enqueuing the changes to avoid calling an |
| 585 | # update per pipeline accepting the change. |
James E. Blair | b34e926 | 2013-08-27 17:12:31 -0700 | [diff] [blame] | 586 | self.log.info("Fetching references for %s" % project) |
| 587 | self.merger.updateRepo(project) |
Antoine Musso | feba967 | 2013-01-17 13:44:59 +0100 | [diff] [blame] | 588 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 589 | for pipeline in self.layout.pipelines.values(): |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 590 | change = event.getChange(project, |
| 591 | self.triggers.get(event.trigger_name)) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 592 | if event.type == 'patchset-created': |
| 593 | pipeline.manager.removeOldVersionsOfChange(change) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 594 | if pipeline.manager.eventMatches(event): |
| 595 | self.log.info("Adding %s, %s to %s" % |
| 596 | (project, change, pipeline)) |
| 597 | pipeline.manager.addChange(change) |
| 598 | while pipeline.manager.processQueue(): |
| 599 | pass |
| 600 | |
James E. Blair | ff79197 | 2013-01-09 11:45:43 -0800 | [diff] [blame] | 601 | self.trigger_event_queue.task_done() |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 602 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 603 | def process_result_queue(self): |
| 604 | self.log.debug("Fetching result event") |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 605 | event_type, build = self.result_event_queue.get() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 606 | self.log.debug("Processing result event %s" % build) |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 607 | for pipeline in self.layout.pipelines.values(): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 608 | if event_type == 'started': |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 609 | if pipeline.manager.onBuildStarted(build): |
James E. Blair | ff79197 | 2013-01-09 11:45:43 -0800 | [diff] [blame] | 610 | self.result_event_queue.task_done() |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 611 | return |
| 612 | elif event_type == 'completed': |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 613 | if pipeline.manager.onBuildCompleted(build): |
James E. Blair | ff79197 | 2013-01-09 11:45:43 -0800 | [diff] [blame] | 614 | self.result_event_queue.task_done() |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 615 | return |
James E. Blair | c84dd26 | 2012-05-31 10:03:13 -0700 | [diff] [blame] | 616 | 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] | 617 | self.result_event_queue.task_done() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 618 | |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 619 | def formatStatusHTML(self): |
| 620 | ret = '<html><pre>' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 621 | if self._pause: |
| 622 | ret += '<p><b>Queue only mode:</b> preparing to ' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 623 | if self._exit: |
| 624 | ret += 'exit' |
| 625 | ret += ', queue length: %s' % self.trigger_event_queue.qsize() |
| 626 | ret += '</p>' |
| 627 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 628 | keys = self.layout.pipelines.keys() |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 629 | for key in keys: |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 630 | pipeline = self.layout.pipelines[key] |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 631 | s = 'Pipeline: %s' % pipeline.name |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 632 | ret += s + '\n' |
| 633 | ret += '-' * len(s) + '\n' |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 634 | ret += pipeline.formatStatusHTML() |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 635 | ret += '\n' |
| 636 | ret += '</pre></html>' |
| 637 | return ret |
| 638 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 639 | def formatStatusJSON(self): |
| 640 | data = {} |
| 641 | if self._pause: |
| 642 | ret = '<p><b>Queue only mode:</b> preparing to ' |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 643 | if self._exit: |
| 644 | ret += 'exit' |
| 645 | ret += ', queue length: %s' % self.trigger_event_queue.qsize() |
| 646 | ret += '</p>' |
| 647 | data['message'] = ret |
| 648 | |
James E. Blair | fb682cc | 2013-02-26 15:23:27 -0800 | [diff] [blame] | 649 | data['trigger_event_queue'] = {} |
| 650 | data['trigger_event_queue']['length'] = \ |
| 651 | self.trigger_event_queue.qsize() |
| 652 | data['result_event_queue'] = {} |
| 653 | data['result_event_queue']['length'] = \ |
| 654 | self.result_event_queue.qsize() |
| 655 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 656 | pipelines = [] |
| 657 | data['pipelines'] = pipelines |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 658 | keys = self.layout.pipelines.keys() |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 659 | for key in keys: |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 660 | pipeline = self.layout.pipelines[key] |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 661 | pipelines.append(pipeline.formatStatusJSON()) |
| 662 | return json.dumps(data) |
| 663 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 664 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 665 | class BasePipelineManager(object): |
| 666 | log = logging.getLogger("zuul.BasePipelineManager") |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 667 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 668 | def __init__(self, sched, pipeline): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 669 | self.sched = sched |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 670 | self.pipeline = pipeline |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 671 | self.building_jobs = {} |
| 672 | self.event_filters = [] |
James E. Blair | 3c5e5b5 | 2013-04-26 11:17:03 -0700 | [diff] [blame] | 673 | if self.sched.config and self.sched.config.has_option( |
| 674 | 'zuul', 'report_times'): |
James E. Blair | 0ac6c01 | 2013-04-26 09:04:23 -0700 | [diff] [blame] | 675 | self.report_times = self.sched.config.getboolean( |
| 676 | 'zuul', 'report_times') |
| 677 | else: |
| 678 | self.report_times = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 679 | |
| 680 | def __str__(self): |
James E. Blair | 93cc8d4 | 2012-08-07 10:46:51 -0700 | [diff] [blame] | 681 | return "<%s %s>" % (self.__class__.__name__, self.pipeline.name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 682 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 683 | def _postConfig(self, layout): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 684 | self.log.info("Configured Pipeline Manager %s" % self.pipeline.name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 685 | self.log.info(" Events:") |
| 686 | for e in self.event_filters: |
| 687 | self.log.info(" %s" % e) |
| 688 | self.log.info(" Projects:") |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 689 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 690 | def log_jobs(tree, indent=0): |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 691 | istr = ' ' + ' ' * indent |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 692 | if tree.job: |
| 693 | efilters = '' |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 694 | for b in tree.job._branches: |
| 695 | efilters += str(b) |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 696 | for f in tree.job._files: |
| 697 | efilters += str(f) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 698 | if efilters: |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 699 | efilters = ' ' + efilters |
James E. Blair | 222d498 | 2012-07-16 09:31:19 -0700 | [diff] [blame] | 700 | hold = '' |
| 701 | if tree.job.hold_following_changes: |
| 702 | hold = ' [hold]' |
James E. Blair | 4ec821f | 2012-08-23 15:28:28 -0700 | [diff] [blame] | 703 | voting = '' |
| 704 | if not tree.job.voting: |
| 705 | voting = ' [nonvoting]' |
| 706 | self.log.info("%s%s%s%s%s" % (istr, repr(tree.job), |
| 707 | efilters, hold, voting)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 708 | for x in tree.job_trees: |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 709 | log_jobs(x, indent + 2) |
| 710 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 711 | for p in layout.projects.values(): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 712 | tree = self.pipeline.getJobTree(p) |
| 713 | if tree: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 714 | self.log.info(" %s" % p) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 715 | log_jobs(tree) |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 716 | self.log.info(" On start:") |
| 717 | self.log.info(" %s" % self.pipeline.start_actions) |
| 718 | self.log.info(" On success:") |
| 719 | self.log.info(" %s" % self.pipeline.success_actions) |
| 720 | self.log.info(" On failure:") |
| 721 | self.log.info(" %s" % self.pipeline.failure_actions) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 722 | |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 723 | def getSubmitAllowNeeds(self): |
| 724 | # Get a list of code review labels that are allowed to be |
| 725 | # "needed" in the submit records for a change, with respect |
| 726 | # to this queue. In other words, the list of review labels |
| 727 | # this queue itself is likely to set before submitting. |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 728 | allow_needs = set() |
| 729 | for action_reporter in self.pipeline.success_actions: |
| 730 | allow_needs.update(action_reporter.getSubmitAllowNeeds()) |
| 731 | return allow_needs |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 732 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 733 | def eventMatches(self, event): |
| 734 | for ef in self.event_filters: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 735 | if ef.matches(event): |
| 736 | return True |
| 737 | return False |
| 738 | |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 739 | def isChangeAlreadyInQueue(self, change): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 740 | for c in self.pipeline.getChangesInQueue(): |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 741 | if change.equals(c): |
| 742 | return True |
| 743 | return False |
| 744 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 745 | def reportStart(self, change): |
| 746 | try: |
| 747 | self.log.info("Reporting start, action %s change %s" % |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 748 | (self.pipeline.start_actions, change)) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 749 | msg = "Starting %s jobs." % self.pipeline.name |
Clark Boylan | 9b67090 | 2012-09-28 13:47:56 -0700 | [diff] [blame] | 750 | if self.sched.config.has_option('zuul', 'status_url'): |
| 751 | msg += "\n" + self.sched.config.get('zuul', 'status_url') |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 752 | ret = self.sendReport(self.pipeline.start_actions, |
| 753 | change, msg) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 754 | if ret: |
| 755 | self.log.error("Reporting change start %s received: %s" % |
| 756 | (change, ret)) |
| 757 | except: |
| 758 | self.log.exception("Exception while reporting start:") |
| 759 | |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 760 | def sendReport(self, action_reporters, change, message): |
| 761 | """Sends the built message off to configured reporters. |
| 762 | |
| 763 | Takes the action_reporters, change, message and extra options and |
| 764 | sends them to the pluggable reporters. |
| 765 | """ |
| 766 | report_errors = [] |
| 767 | if len(action_reporters) > 0: |
| 768 | if not change.number: |
| 769 | self.log.debug("Not reporting change %s: No number present." |
| 770 | % change) |
| 771 | return |
| 772 | for action_reporter in action_reporters: |
| 773 | ret = action_reporter.report(change, message) |
| 774 | if ret: |
| 775 | report_errors.append(ret) |
| 776 | if len(report_errors) == 0: |
| 777 | return |
| 778 | return report_errors |
| 779 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 780 | def isChangeReadyToBeEnqueued(self, change): |
| 781 | return True |
| 782 | |
| 783 | def enqueueChangesAhead(self, change): |
| 784 | return True |
| 785 | |
| 786 | def enqueueChangesBehind(self, change): |
| 787 | return True |
| 788 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 789 | def checkForChangesNeededBy(self, change): |
| 790 | return True |
| 791 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 792 | def getFailingDependentItem(self, item): |
| 793 | return None |
| 794 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 795 | def getDependentItems(self, item): |
| 796 | orig_item = item |
| 797 | items = [] |
| 798 | while item.item_ahead: |
| 799 | items.append(item.item_ahead) |
| 800 | item = item.item_ahead |
| 801 | self.log.info("Change %s depends on changes %s" % |
| 802 | (orig_item.change, |
| 803 | [x.change for x in items])) |
| 804 | return items |
| 805 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 806 | def getItemForChange(self, change): |
| 807 | for item in self.pipeline.getAllItems(): |
| 808 | if item.change.equals(change): |
| 809 | return item |
| 810 | return None |
| 811 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 812 | def findOldVersionOfChangeAlreadyInQueue(self, change): |
| 813 | for c in self.pipeline.getChangesInQueue(): |
| 814 | if change.isUpdateOf(c): |
| 815 | return c |
| 816 | return None |
| 817 | |
| 818 | def removeOldVersionsOfChange(self, change): |
| 819 | if not self.pipeline.dequeue_on_new_patchset: |
| 820 | return |
| 821 | old_change = self.findOldVersionOfChangeAlreadyInQueue(change) |
| 822 | if old_change: |
| 823 | self.log.debug("Change %s is a new version of %s, removing %s" % |
| 824 | (change, old_change, old_change)) |
| 825 | self.removeChange(old_change) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 826 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 827 | def reEnqueueItem(self, item): |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 828 | change_queue = self.pipeline.getQueue(item.change.project) |
| 829 | if change_queue: |
| 830 | self.log.debug("Re-enqueing change %s in queue %s" % |
| 831 | (item.change, change_queue)) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 832 | change_queue.enqueueItem(item) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 833 | self.reportStats(item) |
| 834 | return True |
| 835 | else: |
| 836 | self.log.error("Unable to find change queue for project %s" % |
| 837 | item.change.project) |
| 838 | return False |
| 839 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 840 | def addChange(self, change): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 841 | self.log.debug("Considering adding change %s" % change) |
James E. Blair | 0dc8ba9 | 2012-07-16 14:23:52 -0700 | [diff] [blame] | 842 | if self.isChangeAlreadyInQueue(change): |
| 843 | self.log.debug("Change %s is already in queue, ignoring" % change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 844 | return True |
James E. Blair | 692c6b3 | 2012-07-17 11:16:35 -0700 | [diff] [blame] | 845 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 846 | if not self.isChangeReadyToBeEnqueued(change): |
| 847 | self.log.debug("Change %s is not ready to be enqueued, ignoring" % |
| 848 | change) |
| 849 | return False |
| 850 | |
| 851 | if not self.enqueueChangesAhead(change): |
James E. Blair | 1490eba | 2013-03-06 19:14:00 -0800 | [diff] [blame] | 852 | self.log.debug("Failed to enqueue changes ahead of %s" % change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 853 | return False |
| 854 | |
| 855 | if self.isChangeAlreadyInQueue(change): |
| 856 | self.log.debug("Change %s is already in queue, ignoring" % change) |
| 857 | return True |
| 858 | |
| 859 | change_queue = self.pipeline.getQueue(change.project) |
| 860 | if change_queue: |
| 861 | self.log.debug("Adding change %s to queue %s" % |
| 862 | (change, change_queue)) |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 863 | if len(self.pipeline.start_actions) > 0: |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 864 | self.reportStart(change) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 865 | item = change_queue.enqueueChange(change) |
| 866 | self.reportStats(item) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 867 | self.enqueueChangesBehind(change) |
| 868 | else: |
| 869 | self.log.error("Unable to find change queue for project %s" % |
| 870 | change.project) |
| 871 | return False |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 872 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 873 | def dequeueItem(self, item): |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 874 | self.log.debug("Removing change %s from queue" % item.change) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 875 | change_queue = self.pipeline.getQueue(item.change.project) |
| 876 | change_queue.dequeueItem(item) |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 877 | self.sched._maintain_trigger_cache = True |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 878 | |
| 879 | def removeChange(self, change): |
| 880 | # Remove a change from the queue, probably because it has been |
| 881 | # superceded by another change. |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 882 | for item in self.pipeline.getAllItems(): |
| 883 | if item.change == change: |
| 884 | self.log.debug("Canceling builds behind change: %s " |
| 885 | "because it is being removed." % item.change) |
| 886 | self.cancelJobs(item) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 887 | self.dequeueItem(item) |
James E. Blair | 9423556 | 2013-08-26 18:12:31 -0700 | [diff] [blame] | 888 | self.reportStats(item) |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 889 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 890 | def prepareRef(self, item): |
| 891 | # Returns False on success. |
| 892 | # Returns True if we were unable to prepare the ref. |
| 893 | ref = item.current_build_set.ref |
| 894 | if hasattr(item.change, 'refspec') and not ref: |
| 895 | self.log.debug("Preparing ref for: %s" % item.change) |
| 896 | item.current_build_set.setConfiguration() |
| 897 | ref = item.current_build_set.ref |
| 898 | dependent_items = self.getDependentItems(item) |
| 899 | dependent_items.reverse() |
| 900 | all_items = dependent_items + [item] |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 901 | commit = self.sched.merger.mergeChanges(all_items, ref) |
| 902 | item.current_build_set.commit = commit |
James E. Blair | 81515ad | 2012-10-01 18:29:08 -0700 | [diff] [blame] | 903 | if not commit: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 904 | self.log.info("Unable to merge change %s" % item.change) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 905 | msg = ("This change was unable to be automatically merged " |
| 906 | "with the current state of the repository. Please " |
| 907 | "rebase your change and upload a new patchset.") |
James E. Blair | 6736beb | 2013-07-11 15:18:15 -0700 | [diff] [blame] | 908 | self.pipeline.setUnableToMerge(item, msg) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 909 | return True |
| 910 | return False |
| 911 | |
| 912 | def _launchJobs(self, item, jobs): |
| 913 | self.log.debug("Launching jobs for change %s" % item.change) |
| 914 | dependent_items = self.getDependentItems(item) |
| 915 | for job in jobs: |
| 916 | self.log.debug("Found job %s for change %s" % (job, item.change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 917 | try: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 918 | build = self.sched.launcher.launch(job, item, |
| 919 | self.pipeline, |
| 920 | dependent_items) |
| 921 | self.building_jobs[build] = item |
| 922 | self.log.debug("Adding build %s of job %s to item %s" % |
| 923 | (build, job, item)) |
| 924 | item.addBuild(build) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 925 | except: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 926 | self.log.exception("Exception while launching job %s " |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 927 | "for change %s:" % (job, item.change)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 928 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 929 | def launchJobs(self, item): |
| 930 | jobs = self.pipeline.findJobsToRun(item) |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 931 | if jobs: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 932 | self._launchJobs(item, jobs) |
| 933 | |
| 934 | def cancelJobs(self, item, prime=True): |
| 935 | self.log.debug("Cancel jobs for change %s" % item.change) |
| 936 | canceled = False |
| 937 | to_remove = [] |
| 938 | if prime and item.current_build_set.builds: |
| 939 | item.resetAllBuilds() |
| 940 | for build, build_item in self.building_jobs.items(): |
| 941 | if build_item == item: |
| 942 | self.log.debug("Found build %s for change %s to cancel" % |
| 943 | (build, item.change)) |
| 944 | try: |
| 945 | self.sched.launcher.cancel(build) |
| 946 | except: |
| 947 | self.log.exception("Exception while canceling build %s " |
| 948 | "for change %s" % (build, item.change)) |
| 949 | to_remove.append(build) |
| 950 | canceled = True |
| 951 | for build in to_remove: |
| 952 | self.log.debug("Removing build %s from running builds" % build) |
| 953 | build.result = 'CANCELED' |
| 954 | del self.building_jobs[build] |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 955 | for item_behind in item.items_behind: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 956 | self.log.debug("Canceling jobs for change %s, behind change %s" % |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 957 | (item_behind.change, item.change)) |
| 958 | if self.cancelJobs(item_behind, prime=prime): |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 959 | canceled = True |
| 960 | return canceled |
| 961 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 962 | def _processOneItem(self, item, nnfi): |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 963 | changed = False |
| 964 | item_ahead = item.item_ahead |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 965 | change_queue = self.pipeline.getQueue(item.change.project) |
| 966 | failing_reasons = [] # Reasons this item is failing |
| 967 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 968 | if self.checkForChangesNeededBy(item.change) is not True: |
| 969 | # It's not okay to enqueue this change, we should remove it. |
| 970 | self.log.info("Dequeuing change %s because " |
| 971 | "it can no longer merge" % item.change) |
| 972 | self.cancelJobs(item) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 973 | self.dequeueItem(item) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 974 | self.pipeline.setDequeuedNeedingChange(item) |
| 975 | try: |
| 976 | self.reportItem(item) |
| 977 | except MergeFailure: |
| 978 | pass |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 979 | return (True, nnfi) |
| 980 | dep_item = self.getFailingDependentItem(item) |
| 981 | if dep_item: |
| 982 | failing_reasons.append('a needed change is failing') |
| 983 | self.cancelJobs(item, prime=False) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 984 | else: |
James E. Blair | fef7163 | 2013-09-23 11:15:47 -0700 | [diff] [blame] | 985 | item_ahead_merged = False |
| 986 | if ((item_ahead and item_ahead.change.is_merged) or |
| 987 | not change_queue.dependent): |
| 988 | item_ahead_merged = True |
| 989 | if (item_ahead != nnfi and not item_ahead_merged): |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 990 | # Our current base is different than what we expected, |
| 991 | # and it's not because our current base merged. Something |
| 992 | # ahead must have failed. |
| 993 | self.log.info("Resetting builds for change %s because the " |
| 994 | "item ahead, %s, is not the nearest non-failing " |
| 995 | "item, %s" % (item.change, item_ahead, nnfi)) |
| 996 | change_queue.moveItem(item, nnfi) |
| 997 | changed = True |
| 998 | self.cancelJobs(item) |
| 999 | self.prepareRef(item) |
| 1000 | if item.current_build_set.unable_to_merge: |
James E. Blair | 062c4fb | 2013-09-26 07:46:00 -0700 | [diff] [blame^] | 1001 | failing_reasons.append("it has a merge conflict") |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1002 | if self.launchJobs(item): |
| 1003 | changed = True |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1004 | if self.pipeline.didAnyJobFail(item): |
| 1005 | failing_reasons.append("at least one job failed") |
| 1006 | if (not item_ahead) and self.pipeline.areAllJobsComplete(item): |
| 1007 | try: |
| 1008 | self.reportItem(item) |
| 1009 | except MergeFailure: |
James E. Blair | 062c4fb | 2013-09-26 07:46:00 -0700 | [diff] [blame^] | 1010 | failing_reasons.append("it did not merge") |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1011 | for item_behind in item.items_behind: |
| 1012 | self.log.info("Resetting builds for change %s because the " |
| 1013 | "item ahead, %s, failed to merge" % |
| 1014 | (item_behind.change, item)) |
| 1015 | self.cancelJobs(item_behind) |
| 1016 | self.dequeueItem(item) |
| 1017 | changed = True |
| 1018 | elif not failing_reasons: |
| 1019 | nnfi = item |
| 1020 | item.current_build_set.failing_reasons = failing_reasons |
| 1021 | if failing_reasons: |
| 1022 | self.log.debug("%s is a failing item because %s" % |
| 1023 | (item, failing_reasons)) |
| 1024 | return (changed, nnfi) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1025 | |
| 1026 | def processQueue(self): |
| 1027 | # Do whatever needs to be done for each change in the queue |
| 1028 | self.log.debug("Starting queue processor: %s" % self.pipeline.name) |
| 1029 | changed = False |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1030 | for queue in self.pipeline.queues: |
| 1031 | queue_changed = False |
| 1032 | nnfi = None # Nearest non-failing item |
| 1033 | for item in queue.queue[:]: |
| 1034 | item_changed, nnfi = self._processOneItem(item, nnfi) |
| 1035 | if item_changed: |
| 1036 | queue_changed = True |
| 1037 | self.reportStats(item) |
| 1038 | if queue_changed: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1039 | changed = True |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1040 | status = '' |
| 1041 | for item in queue.queue: |
| 1042 | status += self.pipeline.formatStatus(item) |
| 1043 | if status: |
| 1044 | self.log.debug("Queue %s status is now:\n %s" % |
| 1045 | (queue.name, status)) |
James E. Blair | fadc6e1 | 2013-08-21 18:23:15 -0700 | [diff] [blame] | 1046 | self.log.debug("Finished queue processor: %s (changed: %s)" % |
| 1047 | (self.pipeline.name, changed)) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1048 | return changed |
James E. Blair | daabed2 | 2012-08-15 15:38:57 -0700 | [diff] [blame] | 1049 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1050 | def updateBuildDescriptions(self, build_set): |
| 1051 | for build in build_set.getBuilds(): |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1052 | desc = self.formatDescription(build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1053 | self.sched.launcher.setBuildDescription(build, desc) |
| 1054 | |
| 1055 | if build_set.previous_build_set: |
| 1056 | for build in build_set.previous_build_set.getBuilds(): |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1057 | desc = self.formatDescription(build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1058 | self.sched.launcher.setBuildDescription(build, desc) |
| 1059 | |
| 1060 | def onBuildStarted(self, build): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1061 | if build not in self.building_jobs: |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1062 | # Or triggered externally, or triggered before zuul started, |
| 1063 | # or restarted |
| 1064 | return False |
| 1065 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1066 | self.log.debug("Build %s started" % build) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1067 | self.updateBuildDescriptions(build.build_set) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1068 | while self.processQueue(): |
| 1069 | pass |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1070 | return True |
| 1071 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1072 | def onBuildCompleted(self, build): |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1073 | if build not in self.building_jobs: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1074 | # Or triggered externally, or triggered before zuul started, |
| 1075 | # or restarted |
| 1076 | return False |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1077 | |
| 1078 | self.log.debug("Build %s completed" % build) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1079 | change = self.building_jobs[build] |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 1080 | self.log.debug("Found change %s which triggered completed build %s" % |
| 1081 | (change, build)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1082 | |
| 1083 | del self.building_jobs[build] |
| 1084 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1085 | self.pipeline.setResult(change, build) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1086 | self.log.debug("Change %s status is now:\n %s" % |
| 1087 | (change, self.pipeline.formatStatus(change))) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1088 | self.updateBuildDescriptions(build.build_set) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1089 | while self.processQueue(): |
| 1090 | pass |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1091 | return True |
| 1092 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1093 | def reportItem(self, item): |
| 1094 | if item.change.is_reportable and item.reported: |
| 1095 | raise Exception("Already reported change %s" % item.change) |
| 1096 | ret = self._reportItem(item) |
| 1097 | if self.changes_merge: |
| 1098 | succeeded = self.pipeline.didAllJobsSucceed(item) |
| 1099 | merged = (not ret) |
| 1100 | if merged: |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1101 | merged = self.pipeline.trigger.isMerged(item.change, |
| 1102 | item.change.branch) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1103 | self.log.info("Reported change %s status: all-succeeded: %s, " |
| 1104 | "merged: %s" % (item.change, succeeded, merged)) |
| 1105 | if not (succeeded and merged): |
| 1106 | self.log.debug("Reported change %s failed tests or failed " |
| 1107 | "to merge" % (item.change)) |
| 1108 | raise MergeFailure("Change %s failed to merge" % item.change) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1109 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1110 | def _reportItem(self, item): |
| 1111 | if not item.change.is_reportable: |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1112 | return False |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1113 | if item.change.is_reportable and item.reported: |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 1114 | return 0 |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1115 | self.log.debug("Reporting change %s" % item.change) |
James E. Blair | b98fcdb | 2013-08-26 18:23:09 -0700 | [diff] [blame] | 1116 | ret = True # Means error as returned by trigger.report |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1117 | if self.pipeline.didAllJobsSucceed(item): |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 1118 | self.log.debug("success %s %s" % (self.pipeline.success_actions, |
| 1119 | self.pipeline.failure_actions)) |
| 1120 | actions = self.pipeline.success_actions |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1121 | item.setReportedResult('SUCCESS') |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1122 | else: |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 1123 | actions = self.pipeline.failure_actions |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1124 | item.setReportedResult('FAILURE') |
| 1125 | report = self.formatReport(item) |
| 1126 | item.reported = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1127 | try: |
Joshua Hesketh | 1879cf7 | 2013-08-19 14:13:15 +1000 | [diff] [blame] | 1128 | self.log.info("Reporting change %s, actions: %s" % |
| 1129 | (item.change, actions)) |
| 1130 | ret = self.sendReport(actions, item.change, report) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1131 | if ret: |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 1132 | self.log.error("Reporting change %s received: %s" % |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1133 | (item.change, ret)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1134 | except: |
| 1135 | self.log.exception("Exception while reporting:") |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1136 | item.setReportedResult('ERROR') |
| 1137 | self.updateBuildDescriptions(item.current_build_set) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1138 | return ret |
| 1139 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1140 | def formatReport(self, item): |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1141 | ret = '' |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1142 | if self.pipeline.didAllJobsSucceed(item): |
James E. Blair | 5637019 | 2013-01-14 15:47:28 -0800 | [diff] [blame] | 1143 | ret += self.pipeline.success_message + '\n\n' |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1144 | else: |
James E. Blair | 5637019 | 2013-01-14 15:47:28 -0800 | [diff] [blame] | 1145 | ret += self.pipeline.failure_message + '\n\n' |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1146 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1147 | if item.dequeued_needing_change: |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1148 | ret += "This change depends on a change that failed to merge." |
James E. Blair | 6736beb | 2013-07-11 15:18:15 -0700 | [diff] [blame] | 1149 | elif item.current_build_set.unable_to_merge_message: |
| 1150 | ret += item.current_build_set.unable_to_merge_message |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1151 | else: |
James E. Blair | a35fcce | 2012-08-24 10:46:01 -0700 | [diff] [blame] | 1152 | if self.sched.config.has_option('zuul', 'url_pattern'): |
James E. Blair | 6aea36d | 2012-12-17 13:03:24 -0800 | [diff] [blame] | 1153 | url_pattern = self.sched.config.get('zuul', 'url_pattern') |
James E. Blair | a35fcce | 2012-08-24 10:46:01 -0700 | [diff] [blame] | 1154 | else: |
James E. Blair | 6aea36d | 2012-12-17 13:03:24 -0800 | [diff] [blame] | 1155 | url_pattern = None |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1156 | for job in self.pipeline.getJobs(item.change): |
| 1157 | build = item.current_build_set.getBuild(job.name) |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1158 | result = build.result |
James E. Blair | 6aea36d | 2012-12-17 13:03:24 -0800 | [diff] [blame] | 1159 | pattern = url_pattern |
| 1160 | if result == 'SUCCESS': |
| 1161 | if job.success_message: |
| 1162 | result = job.success_message |
| 1163 | if job.success_pattern: |
| 1164 | pattern = job.success_pattern |
| 1165 | elif result == 'FAILURE': |
| 1166 | if job.failure_message: |
| 1167 | result = job.failure_message |
| 1168 | if job.failure_pattern: |
| 1169 | pattern = job.failure_pattern |
Ori Livneh | 7191ee8 | 2013-05-02 19:13:53 -0700 | [diff] [blame] | 1170 | if pattern: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1171 | url = pattern.format(change=item.change, |
Ori Livneh | 7191ee8 | 2013-05-02 19:13:53 -0700 | [diff] [blame] | 1172 | pipeline=self.pipeline, |
| 1173 | job=job, |
| 1174 | build=build) |
| 1175 | else: |
| 1176 | url = build.url or job.name |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1177 | if not job.voting: |
| 1178 | voting = ' (non-voting)' |
| 1179 | else: |
| 1180 | voting = '' |
James E. Blair | 0ac6c01 | 2013-04-26 09:04:23 -0700 | [diff] [blame] | 1181 | if self.report_times and build.end_time and build.start_time: |
| 1182 | dt = int(build.end_time - build.start_time) |
| 1183 | m, s = divmod(dt, 60) |
| 1184 | h, m = divmod(m, 60) |
Sean Dague | 51fd119 | 2013-05-03 07:09:53 -0400 | [diff] [blame] | 1185 | if h: |
| 1186 | elapsed = ' in %dh %02dm %02ds' % (h, m, s) |
| 1187 | elif m: |
| 1188 | elapsed = ' in %dm %02ds' % (m, s) |
| 1189 | else: |
| 1190 | elapsed = ' in %ds' % (s) |
James E. Blair | 0ac6c01 | 2013-04-26 09:04:23 -0700 | [diff] [blame] | 1191 | else: |
| 1192 | elapsed = '' |
James E. Blair | 754e31e | 2013-08-18 13:15:15 -0700 | [diff] [blame] | 1193 | name = '' |
James E. Blair | 8fef52b | 2013-08-17 17:32:50 -0700 | [diff] [blame] | 1194 | if self.sched.config.has_option('zuul', 'job_name_in_report'): |
| 1195 | if self.sched.config.getboolean('zuul', |
| 1196 | 'job_name_in_report'): |
| 1197 | name = job.name + ' ' |
James E. Blair | 8fef52b | 2013-08-17 17:32:50 -0700 | [diff] [blame] | 1198 | ret += '- %s%s : %s%s%s\n' % (name, url, result, elapsed, |
| 1199 | voting) |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1200 | return ret |
| 1201 | |
| 1202 | def formatDescription(self, build): |
| 1203 | concurrent_changes = '' |
| 1204 | concurrent_builds = '' |
| 1205 | other_builds = '' |
| 1206 | |
| 1207 | for change in build.build_set.other_changes: |
| 1208 | concurrent_changes += '<li><a href="{change.url}">\ |
| 1209 | {change.number},{change.patchset}</a></li>'.format( |
| 1210 | change=change) |
| 1211 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1212 | change = build.build_set.item.change |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1213 | |
| 1214 | for build in build.build_set.getBuilds(): |
Ori Livneh | 7191ee8 | 2013-05-02 19:13:53 -0700 | [diff] [blame] | 1215 | if build.url: |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1216 | concurrent_builds += """\ |
| 1217 | <li> |
Ori Livneh | 7191ee8 | 2013-05-02 19:13:53 -0700 | [diff] [blame] | 1218 | <a href="{build.url}"> |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1219 | {build.job.name} #{build.number}</a>: {build.result} |
| 1220 | </li> |
| 1221 | """.format(build=build) |
| 1222 | else: |
| 1223 | concurrent_builds += """\ |
| 1224 | <li> |
| 1225 | {build.job.name}: {build.result} |
| 1226 | </li>""".format(build=build) |
| 1227 | |
| 1228 | if build.build_set.previous_build_set: |
| 1229 | other_build = build.build_set.previous_build_set.getBuild( |
| 1230 | build.job.name) |
| 1231 | if other_build: |
| 1232 | other_builds += """\ |
| 1233 | <li> |
Ori Livneh | 7191ee8 | 2013-05-02 19:13:53 -0700 | [diff] [blame] | 1234 | Preceded by: <a href="{build.url}"> |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1235 | {build.job.name} #{build.number}</a> |
| 1236 | </li> |
| 1237 | """.format(build=other_build) |
| 1238 | |
| 1239 | if build.build_set.next_build_set: |
| 1240 | other_build = build.build_set.next_build_set.getBuild( |
| 1241 | build.job.name) |
| 1242 | if other_build: |
| 1243 | other_builds += """\ |
| 1244 | <li> |
Ori Livneh | 7191ee8 | 2013-05-02 19:13:53 -0700 | [diff] [blame] | 1245 | Succeeded by: <a href="{build.url}"> |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1246 | {build.job.name} #{build.number}</a> |
| 1247 | </li> |
| 1248 | """.format(build=other_build) |
| 1249 | |
| 1250 | result = build.build_set.result |
| 1251 | |
| 1252 | if hasattr(change, 'number'): |
| 1253 | ret = """\ |
| 1254 | <p> |
| 1255 | Triggered by change: |
| 1256 | <a href="{change.url}">{change.number},{change.patchset}</a><br/> |
| 1257 | Branch: <b>{change.branch}</b><br/> |
| 1258 | Pipeline: <b>{self.pipeline.name}</b> |
| 1259 | </p>""" |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 1260 | elif hasattr(change, 'ref'): |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1261 | ret = """\ |
| 1262 | <p> |
| 1263 | Triggered by reference: |
| 1264 | {change.ref}</a><br/> |
| 1265 | Old revision: <b>{change.oldrev}</b><br/> |
| 1266 | New revision: <b>{change.newrev}</b><br/> |
| 1267 | Pipeline: <b>{self.pipeline.name}</b> |
| 1268 | </p>""" |
James E. Blair | 63bb0ef | 2013-07-29 17:14:51 -0700 | [diff] [blame] | 1269 | else: |
| 1270 | ret = "" |
James E. Blair | 8b0d4c4 | 2012-08-23 16:03:05 -0700 | [diff] [blame] | 1271 | |
| 1272 | if concurrent_changes: |
| 1273 | ret += """\ |
| 1274 | <p> |
| 1275 | Other changes tested concurrently with this change: |
| 1276 | <ul>{concurrent_changes}</ul> |
| 1277 | </p> |
| 1278 | """ |
| 1279 | if concurrent_builds: |
| 1280 | ret += """\ |
| 1281 | <p> |
| 1282 | All builds for this change set: |
| 1283 | <ul>{concurrent_builds}</ul> |
| 1284 | </p> |
| 1285 | """ |
| 1286 | |
| 1287 | if other_builds: |
| 1288 | ret += """\ |
| 1289 | <p> |
| 1290 | Other build sets for this change: |
| 1291 | <ul>{other_builds}</ul> |
| 1292 | </p> |
| 1293 | """ |
| 1294 | if result: |
| 1295 | ret += """\ |
| 1296 | <p> |
| 1297 | Reported result: <b>{result}</b> |
| 1298 | </p> |
| 1299 | """ |
| 1300 | |
| 1301 | ret = ret.format(**locals()) |
James E. Blair | 268d934 | 2012-06-13 18:24:29 -0700 | [diff] [blame] | 1302 | return ret |
| 1303 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1304 | def reportStats(self, item): |
James E. Blair | 8fa1697 | 2013-01-15 16:57:20 -0800 | [diff] [blame] | 1305 | if not statsd: |
| 1306 | return |
| 1307 | try: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1308 | # Update the gauge on enqueue and dequeue, but timers only |
James E. Blair | 8fa1697 | 2013-01-15 16:57:20 -0800 | [diff] [blame] | 1309 | # when dequeing. |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1310 | if item.dequeue_time: |
| 1311 | dt = int((item.dequeue_time - item.enqueue_time) * 1000) |
James E. Blair | 8fa1697 | 2013-01-15 16:57:20 -0800 | [diff] [blame] | 1312 | else: |
| 1313 | dt = None |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1314 | items = len(self.pipeline.getAllItems()) |
James E. Blair | 8fa1697 | 2013-01-15 16:57:20 -0800 | [diff] [blame] | 1315 | |
| 1316 | # stats.timers.zuul.pipeline.NAME.resident_time |
| 1317 | # stats_counts.zuul.pipeline.NAME.total_changes |
| 1318 | # stats.gauges.zuul.pipeline.NAME.current_changes |
| 1319 | key = 'zuul.pipeline.%s' % self.pipeline.name |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1320 | statsd.gauge(key + '.current_changes', items) |
James E. Blair | 8fa1697 | 2013-01-15 16:57:20 -0800 | [diff] [blame] | 1321 | if dt: |
| 1322 | statsd.timing(key + '.resident_time', dt) |
| 1323 | statsd.incr(key + '.total_changes') |
| 1324 | |
| 1325 | # stats.timers.zuul.pipeline.NAME.ORG.PROJECT.resident_time |
| 1326 | # stats_counts.zuul.pipeline.NAME.ORG.PROJECT.total_changes |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1327 | project_name = item.change.project.name.replace('/', '.') |
James E. Blair | 8fa1697 | 2013-01-15 16:57:20 -0800 | [diff] [blame] | 1328 | key += '.%s' % project_name |
| 1329 | if dt: |
| 1330 | statsd.timing(key + '.resident_time', dt) |
| 1331 | statsd.incr(key + '.total_changes') |
| 1332 | except: |
| 1333 | self.log.exception("Exception reporting pipeline stats") |
| 1334 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1335 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1336 | class IndependentPipelineManager(BasePipelineManager): |
| 1337 | log = logging.getLogger("zuul.IndependentPipelineManager") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1338 | changes_merge = False |
| 1339 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1340 | def _postConfig(self, layout): |
| 1341 | super(IndependentPipelineManager, self)._postConfig(layout) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1342 | |
| 1343 | change_queue = ChangeQueue(self.pipeline, dependent=False) |
| 1344 | for project in self.pipeline.getProjects(): |
| 1345 | change_queue.addProject(project) |
| 1346 | |
| 1347 | self.pipeline.addQueue(change_queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1348 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1349 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1350 | class DependentPipelineManager(BasePipelineManager): |
| 1351 | log = logging.getLogger("zuul.DependentPipelineManager") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1352 | changes_merge = True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1353 | |
| 1354 | def __init__(self, *args, **kwargs): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1355 | super(DependentPipelineManager, self).__init__(*args, **kwargs) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1356 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 1357 | def _postConfig(self, layout): |
| 1358 | super(DependentPipelineManager, self)._postConfig(layout) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1359 | self.buildChangeQueues() |
| 1360 | |
| 1361 | def buildChangeQueues(self): |
| 1362 | self.log.debug("Building shared change queues") |
| 1363 | change_queues = [] |
| 1364 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1365 | for project in self.pipeline.getProjects(): |
| 1366 | change_queue = ChangeQueue(self.pipeline) |
| 1367 | change_queue.addProject(project) |
| 1368 | change_queues.append(change_queue) |
| 1369 | self.log.debug("Created queue: %s" % change_queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1370 | |
| 1371 | self.log.debug("Combining shared queues") |
| 1372 | new_change_queues = [] |
| 1373 | for a in change_queues: |
| 1374 | merged_a = False |
| 1375 | for b in new_change_queues: |
| 1376 | if not a.getJobs().isdisjoint(b.getJobs()): |
| 1377 | self.log.debug("Merging queue %s into %s" % (a, b)) |
| 1378 | b.mergeChangeQueue(a) |
| 1379 | merged_a = True |
| 1380 | break # this breaks out of 'for b' and continues 'for a' |
| 1381 | if not merged_a: |
| 1382 | self.log.debug("Keeping queue %s" % (a)) |
| 1383 | new_change_queues.append(a) |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1384 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1385 | self.log.info(" Shared change queues:") |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1386 | for queue in new_change_queues: |
| 1387 | self.pipeline.addQueue(queue) |
| 1388 | self.log.info(" %s" % queue) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1389 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1390 | def isChangeReadyToBeEnqueued(self, change): |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1391 | if not self.pipeline.trigger.canMerge(change, |
| 1392 | self.getSubmitAllowNeeds()): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1393 | self.log.debug("Change %s can not merge, ignoring" % change) |
| 1394 | return False |
| 1395 | return True |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1396 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1397 | def enqueueChangesBehind(self, change): |
| 1398 | to_enqueue = [] |
| 1399 | self.log.debug("Checking for changes needing %s:" % change) |
| 1400 | if not hasattr(change, 'needed_by_changes'): |
| 1401 | self.log.debug(" Changeish does not support dependencies") |
| 1402 | return |
| 1403 | for needs in change.needed_by_changes: |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1404 | if self.pipeline.trigger.canMerge(needs, |
| 1405 | self.getSubmitAllowNeeds()): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1406 | self.log.debug(" Change %s needs %s and is ready to merge" % |
| 1407 | (needs, change)) |
| 1408 | to_enqueue.append(needs) |
| 1409 | if not to_enqueue: |
| 1410 | self.log.debug(" No changes need %s" % change) |
| 1411 | |
| 1412 | for other_change in to_enqueue: |
| 1413 | self.addChange(other_change) |
| 1414 | |
| 1415 | def enqueueChangesAhead(self, change): |
| 1416 | ret = self.checkForChangesNeededBy(change) |
| 1417 | if ret in [True, False]: |
| 1418 | return ret |
| 1419 | self.log.debug(" Change %s must be merged ahead of %s" % |
| 1420 | (ret, change)) |
| 1421 | return self.addChange(ret) |
| 1422 | |
| 1423 | def checkForChangesNeededBy(self, change): |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 1424 | self.log.debug("Checking for changes needed by %s:" % change) |
| 1425 | # Return true if okay to proceed enqueing this change, |
| 1426 | # false if the change should not be enqueued. |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1427 | if not hasattr(change, 'needs_change'): |
| 1428 | self.log.debug(" Changeish does not support dependencies") |
| 1429 | return True |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 1430 | if not change.needs_change: |
| 1431 | self.log.debug(" No changes needed") |
| 1432 | return True |
| 1433 | if change.needs_change.is_merged: |
| 1434 | self.log.debug(" Needed change is merged") |
| 1435 | return True |
| 1436 | if not change.needs_change.is_current_patchset: |
| 1437 | self.log.debug(" Needed change is not the current patchset") |
| 1438 | return False |
James E. Blair | 127bc18 | 2012-08-28 15:55:15 -0700 | [diff] [blame] | 1439 | if self.isChangeAlreadyInQueue(change.needs_change): |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 1440 | self.log.debug(" Needed change is already ahead in the queue") |
| 1441 | return True |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 1442 | if self.pipeline.trigger.canMerge(change.needs_change, |
| 1443 | self.getSubmitAllowNeeds()): |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 1444 | self.log.debug(" Change %s is needed" % |
| 1445 | change.needs_change) |
| 1446 | return change.needs_change |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 1447 | # The needed change can't be merged. |
| 1448 | self.log.debug(" Change %s is needed but can not be merged" % |
| 1449 | change.needs_change) |
| 1450 | return False |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1451 | |
| 1452 | def getFailingDependentItem(self, item): |
| 1453 | if not hasattr(item.change, 'needs_change'): |
| 1454 | return None |
| 1455 | if not item.change.needs_change: |
| 1456 | return None |
| 1457 | needs_item = self.getItemForChange(item.change.needs_change) |
| 1458 | if not needs_item: |
| 1459 | return None |
| 1460 | if needs_item.current_build_set.failing_reasons: |
| 1461 | return needs_item |
| 1462 | return None |