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