James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1 | # Copyright 2012 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 15 | import abc |
Tristan Cacqueray | e7410af | 2017-06-19 04:32:08 +0000 | [diff] [blame] | 16 | from collections import OrderedDict |
James E. Blair | 1b26531 | 2014-06-24 09:35:21 -0700 | [diff] [blame] | 17 | import copy |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 18 | import logging |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 19 | import os |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 20 | import struct |
James E. Blair | ff986a1 | 2012-05-30 14:56:51 -0700 | [diff] [blame] | 21 | import time |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 22 | from uuid import uuid4 |
James E. Blair | 88e79c0 | 2017-07-07 13:36:54 -0700 | [diff] [blame] | 23 | import urllib.parse |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 24 | import textwrap |
James E. Blair | 5a9918a | 2013-08-27 10:06:27 -0700 | [diff] [blame] | 25 | |
James E. Blair | e74f571 | 2017-09-29 15:14:31 -0700 | [diff] [blame] | 26 | from zuul import change_matcher |
| 27 | |
James E. Blair | 19deff2 | 2013-08-25 13:17:35 -0700 | [diff] [blame] | 28 | MERGER_MERGE = 1 # "git merge" |
| 29 | MERGER_MERGE_RESOLVE = 2 # "git merge -s resolve" |
| 30 | MERGER_CHERRY_PICK = 3 # "git cherry-pick" |
| 31 | |
| 32 | MERGER_MAP = { |
| 33 | 'merge': MERGER_MERGE, |
| 34 | 'merge-resolve': MERGER_MERGE_RESOLVE, |
| 35 | 'cherry-pick': MERGER_CHERRY_PICK, |
| 36 | } |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 37 | |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 38 | PRECEDENCE_NORMAL = 0 |
| 39 | PRECEDENCE_LOW = 1 |
| 40 | PRECEDENCE_HIGH = 2 |
| 41 | |
| 42 | PRECEDENCE_MAP = { |
| 43 | None: PRECEDENCE_NORMAL, |
| 44 | 'low': PRECEDENCE_LOW, |
| 45 | 'normal': PRECEDENCE_NORMAL, |
| 46 | 'high': PRECEDENCE_HIGH, |
| 47 | } |
| 48 | |
Monty Taylor | 6dc5bc1 | 2017-09-29 15:47:31 -0500 | [diff] [blame] | 49 | PRIORITY_MAP = { |
| 50 | PRECEDENCE_NORMAL: 200, |
| 51 | PRECEDENCE_LOW: 300, |
| 52 | PRECEDENCE_HIGH: 100, |
| 53 | } |
| 54 | |
James E. Blair | 803e94f | 2017-01-06 09:18:59 -0800 | [diff] [blame] | 55 | # Request states |
| 56 | STATE_REQUESTED = 'requested' |
| 57 | STATE_PENDING = 'pending' |
| 58 | STATE_FULFILLED = 'fulfilled' |
| 59 | STATE_FAILED = 'failed' |
| 60 | REQUEST_STATES = set([STATE_REQUESTED, |
| 61 | STATE_PENDING, |
| 62 | STATE_FULFILLED, |
| 63 | STATE_FAILED]) |
| 64 | |
| 65 | # Node states |
| 66 | STATE_BUILDING = 'building' |
| 67 | STATE_TESTING = 'testing' |
| 68 | STATE_READY = 'ready' |
| 69 | STATE_IN_USE = 'in-use' |
| 70 | STATE_USED = 'used' |
| 71 | STATE_HOLD = 'hold' |
| 72 | STATE_DELETING = 'deleting' |
| 73 | NODE_STATES = set([STATE_BUILDING, |
| 74 | STATE_TESTING, |
| 75 | STATE_READY, |
| 76 | STATE_IN_USE, |
| 77 | STATE_USED, |
| 78 | STATE_HOLD, |
| 79 | STATE_DELETING]) |
| 80 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 81 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 82 | class NoMatchingParentError(Exception): |
| 83 | """A job referenced a parent, but that parent had no variants which |
| 84 | matched the current change.""" |
| 85 | pass |
| 86 | |
| 87 | |
Joshua Hesketh | 58419cb | 2017-02-24 13:09:22 -0500 | [diff] [blame] | 88 | class Attributes(object): |
| 89 | """A class to hold attributes for string formatting.""" |
| 90 | |
| 91 | def __init__(self, **kw): |
| 92 | setattr(self, '__dict__', kw) |
| 93 | |
| 94 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 95 | class Pipeline(object): |
James E. Blair | 6053de4 | 2017-04-05 11:27:11 -0700 | [diff] [blame] | 96 | """A configuration that ties together triggers, reporters and managers |
Monty Taylor | 82dfd41 | 2016-07-29 12:01:28 -0700 | [diff] [blame] | 97 | |
| 98 | Trigger |
| 99 | A description of which events should be processed |
| 100 | |
| 101 | Manager |
| 102 | Responsible for enqueing and dequeing Changes |
| 103 | |
| 104 | Reporter |
| 105 | Communicates success and failure results somewhere |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 106 | """ |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 107 | def __init__(self, name, layout): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 108 | self.name = name |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 109 | self.layout = layout |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 110 | self.description = None |
James E. Blair | 5637019 | 2013-01-14 15:47:28 -0800 | [diff] [blame] | 111 | self.failure_message = None |
Joshua Hesketh | b717977 | 2014-01-30 23:30:46 +1100 | [diff] [blame] | 112 | self.merge_failure_message = None |
James E. Blair | 5637019 | 2013-01-14 15:47:28 -0800 | [diff] [blame] | 113 | self.success_message = None |
Joshua Hesketh | 3979e3e | 2014-03-04 11:21:10 +1100 | [diff] [blame] | 114 | self.footer_message = None |
James E. Blair | 60af7f4 | 2016-03-11 16:11:06 -0800 | [diff] [blame] | 115 | self.start_message = None |
James E. Blair | 8eb564a | 2017-08-10 09:21:41 -0700 | [diff] [blame] | 116 | self.post_review = False |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 117 | self.dequeue_on_new_patchset = True |
James E. Blair | 17dd677 | 2015-02-09 14:45:18 -0800 | [diff] [blame] | 118 | self.ignore_dependencies = False |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 119 | self.manager = None |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 120 | self.queues = [] |
James E. Blair | 64ed6f2 | 2013-07-10 14:07:23 -0700 | [diff] [blame] | 121 | self.precedence = PRECEDENCE_NORMAL |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 122 | self.triggers = [] |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 123 | self.start_actions = [] |
| 124 | self.success_actions = [] |
| 125 | self.failure_actions = [] |
| 126 | self.merge_failure_actions = [] |
| 127 | self.disabled_actions = [] |
Joshua Hesketh | 89e829d | 2015-02-10 16:29:45 +1100 | [diff] [blame] | 128 | self.disable_at = None |
| 129 | self._consecutive_failures = 0 |
| 130 | self._disabled = False |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 131 | self.window = None |
| 132 | self.window_floor = None |
| 133 | self.window_increase_type = None |
| 134 | self.window_increase_factor = None |
| 135 | self.window_decrease_type = None |
| 136 | self.window_decrease_factor = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 137 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 138 | @property |
| 139 | def actions(self): |
| 140 | return ( |
| 141 | self.start_actions + |
| 142 | self.success_actions + |
| 143 | self.failure_actions + |
| 144 | self.merge_failure_actions + |
| 145 | self.disabled_actions |
| 146 | ) |
| 147 | |
James E. Blair | d09c17a | 2012-08-07 09:23:14 -0700 | [diff] [blame] | 148 | def __repr__(self): |
| 149 | return '<Pipeline %s>' % self.name |
| 150 | |
Joshua Hesketh | cd96ec0 | 2017-03-28 08:05:56 +1100 | [diff] [blame] | 151 | def getSafeAttributes(self): |
| 152 | return Attributes(name=self.name) |
| 153 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 154 | def setManager(self, manager): |
| 155 | self.manager = manager |
| 156 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 157 | def addQueue(self, queue): |
| 158 | self.queues.append(queue) |
| 159 | |
| 160 | def getQueue(self, project): |
| 161 | for queue in self.queues: |
| 162 | if project in queue.projects: |
| 163 | return queue |
| 164 | return None |
| 165 | |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 166 | def removeQueue(self, queue): |
Tobias Henkel | 6b9390f | 2017-03-28 11:23:21 +0200 | [diff] [blame] | 167 | if queue in self.queues: |
| 168 | self.queues.remove(queue) |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 169 | |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 170 | def getChangesInQueue(self): |
| 171 | changes = [] |
| 172 | for shared_queue in self.queues: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 173 | changes.extend([x.change for x in shared_queue.queue]) |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 174 | return changes |
| 175 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 176 | def getAllItems(self): |
| 177 | items = [] |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 178 | for shared_queue in self.queues: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 179 | items.extend(shared_queue.queue) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 180 | return items |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 181 | |
Tobias Henkel | b4407fc | 2017-07-07 13:52:56 +0200 | [diff] [blame] | 182 | def formatStatusJSON(self, websocket_url=None): |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 183 | j_pipeline = dict(name=self.name, |
| 184 | description=self.description) |
| 185 | j_queues = [] |
| 186 | j_pipeline['change_queues'] = j_queues |
| 187 | for queue in self.queues: |
| 188 | j_queue = dict(name=queue.name) |
| 189 | j_queues.append(j_queue) |
| 190 | j_queue['heads'] = [] |
Clark Boylan | af2476f | 2014-01-23 14:47:36 -0800 | [diff] [blame] | 191 | j_queue['window'] = queue.window |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 192 | |
| 193 | j_changes = [] |
| 194 | for e in queue.queue: |
| 195 | if not e.item_ahead: |
| 196 | if j_changes: |
| 197 | j_queue['heads'].append(j_changes) |
| 198 | j_changes = [] |
Tobias Henkel | b4407fc | 2017-07-07 13:52:56 +0200 | [diff] [blame] | 199 | j_changes.append(e.formatJSON(websocket_url)) |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 200 | if (len(j_changes) > 1 and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 201 | (j_changes[-2]['remaining_time'] is not None) and |
| 202 | (j_changes[-1]['remaining_time'] is not None)): |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 203 | j_changes[-1]['remaining_time'] = max( |
| 204 | j_changes[-2]['remaining_time'], |
| 205 | j_changes[-1]['remaining_time']) |
| 206 | if j_changes: |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 207 | j_queue['heads'].append(j_changes) |
| 208 | return j_pipeline |
| 209 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 210 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 211 | class ChangeQueue(object): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 212 | """A ChangeQueue contains Changes to be processed related projects. |
| 213 | |
Monty Taylor | 82dfd41 | 2016-07-29 12:01:28 -0700 | [diff] [blame] | 214 | A Pipeline with a DependentPipelineManager has multiple parallel |
| 215 | ChangeQueues shared by different projects. For instance, there may a |
| 216 | ChangeQueue shared by interrelated projects foo and bar, and a second queue |
| 217 | for independent project baz. |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 218 | |
Monty Taylor | 82dfd41 | 2016-07-29 12:01:28 -0700 | [diff] [blame] | 219 | A Pipeline with an IndependentPipelineManager puts every Change into its |
| 220 | own ChangeQueue |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 221 | |
| 222 | The ChangeQueue Window is inspired by TCP windows and controlls how many |
| 223 | Changes in a given ChangeQueue will be considered active and ready to |
| 224 | be processed. If a Change succeeds, the Window is increased by |
| 225 | `window_increase_factor`. If a Change fails, the Window is decreased by |
| 226 | `window_decrease_factor`. |
Jesse Keating | 78f544a | 2017-07-13 14:27:40 -0700 | [diff] [blame] | 227 | |
| 228 | A ChangeQueue may be a dynamically created queue, which may be removed |
| 229 | from a DependentPipelineManager once empty. |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 230 | """ |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 231 | def __init__(self, pipeline, window=0, window_floor=1, |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 232 | window_increase_type='linear', window_increase_factor=1, |
James E. Blair | 0dcef7a | 2016-08-19 09:35:17 -0700 | [diff] [blame] | 233 | window_decrease_type='exponential', window_decrease_factor=2, |
Jesse Keating | 78f544a | 2017-07-13 14:27:40 -0700 | [diff] [blame] | 234 | name=None, dynamic=False): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 235 | self.pipeline = pipeline |
James E. Blair | 0dcef7a | 2016-08-19 09:35:17 -0700 | [diff] [blame] | 236 | if name: |
| 237 | self.name = name |
| 238 | else: |
| 239 | self.name = '' |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 240 | self.projects = [] |
| 241 | self._jobs = set() |
| 242 | self.queue = [] |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 243 | self.window = window |
| 244 | self.window_floor = window_floor |
| 245 | self.window_increase_type = window_increase_type |
| 246 | self.window_increase_factor = window_increase_factor |
| 247 | self.window_decrease_type = window_decrease_type |
| 248 | self.window_decrease_factor = window_decrease_factor |
Jesse Keating | 78f544a | 2017-07-13 14:27:40 -0700 | [diff] [blame] | 249 | self.dynamic = dynamic |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 250 | |
James E. Blair | 9f9667e | 2012-06-12 17:51:08 -0700 | [diff] [blame] | 251 | def __repr__(self): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 252 | return '<ChangeQueue %s: %s>' % (self.pipeline.name, self.name) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 253 | |
| 254 | def getJobs(self): |
| 255 | return self._jobs |
| 256 | |
| 257 | def addProject(self, project): |
| 258 | if project not in self.projects: |
| 259 | self.projects.append(project) |
James E. Blair | c8a1e05 | 2014-02-25 09:29:26 -0800 | [diff] [blame] | 260 | |
James E. Blair | 0dcef7a | 2016-08-19 09:35:17 -0700 | [diff] [blame] | 261 | if not self.name: |
| 262 | self.name = project.name |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 263 | |
| 264 | def enqueueChange(self, change): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 265 | item = QueueItem(self, change) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 266 | self.enqueueItem(item) |
| 267 | item.enqueue_time = time.time() |
| 268 | return item |
| 269 | |
| 270 | def enqueueItem(self, item): |
James E. Blair | 4a035d9 | 2014-01-23 13:10:48 -0800 | [diff] [blame] | 271 | item.pipeline = self.pipeline |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 272 | item.queue = self |
| 273 | if self.queue: |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 274 | item.item_ahead = self.queue[-1] |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 275 | item.item_ahead.items_behind.append(item) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 276 | self.queue.append(item) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 277 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 278 | def dequeueItem(self, item): |
| 279 | if item in self.queue: |
| 280 | self.queue.remove(item) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 281 | if item.item_ahead: |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 282 | item.item_ahead.items_behind.remove(item) |
| 283 | for item_behind in item.items_behind: |
| 284 | if item.item_ahead: |
| 285 | item.item_ahead.items_behind.append(item_behind) |
| 286 | item_behind.item_ahead = item.item_ahead |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 287 | item.item_ahead = None |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 288 | item.items_behind = [] |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 289 | item.dequeue_time = time.time() |
James E. Blair | e048707 | 2012-08-29 17:38:31 -0700 | [diff] [blame] | 290 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 291 | def moveItem(self, item, item_ahead): |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 292 | if item.item_ahead == item_ahead: |
| 293 | return False |
| 294 | # Remove from current location |
| 295 | if item.item_ahead: |
| 296 | item.item_ahead.items_behind.remove(item) |
| 297 | for item_behind in item.items_behind: |
| 298 | if item.item_ahead: |
| 299 | item.item_ahead.items_behind.append(item_behind) |
| 300 | item_behind.item_ahead = item.item_ahead |
| 301 | # Add to new location |
| 302 | item.item_ahead = item_ahead |
James E. Blair | 0045126 | 2013-09-20 11:40:17 -0700 | [diff] [blame] | 303 | item.items_behind = [] |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 304 | if item.item_ahead: |
| 305 | item.item_ahead.items_behind.append(item) |
| 306 | return True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 307 | |
Clark Boylan | 3d2f7a7 | 2014-01-23 11:07:42 -0800 | [diff] [blame] | 308 | def isActionable(self, item): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 309 | if self.window: |
Clark Boylan | 3d2f7a7 | 2014-01-23 11:07:42 -0800 | [diff] [blame] | 310 | return item in self.queue[:self.window] |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 311 | else: |
Clark Boylan | 3d2f7a7 | 2014-01-23 11:07:42 -0800 | [diff] [blame] | 312 | return True |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 313 | |
| 314 | def increaseWindowSize(self): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 315 | if self.window: |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 316 | if self.window_increase_type == 'linear': |
| 317 | self.window += self.window_increase_factor |
| 318 | elif self.window_increase_type == 'exponential': |
| 319 | self.window *= self.window_increase_factor |
| 320 | |
| 321 | def decreaseWindowSize(self): |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 322 | if self.window: |
Clark Boylan | 7603a37 | 2014-01-21 11:43:20 -0800 | [diff] [blame] | 323 | if self.window_decrease_type == 'linear': |
| 324 | self.window = max( |
| 325 | self.window_floor, |
| 326 | self.window - self.window_decrease_factor) |
| 327 | elif self.window_decrease_type == 'exponential': |
| 328 | self.window = max( |
| 329 | self.window_floor, |
Morgan Fainberg | fdae225 | 2016-06-07 20:13:20 -0700 | [diff] [blame] | 330 | int(self.window / self.window_decrease_factor)) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 331 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 332 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 333 | class Project(object): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 334 | """A Project represents a git repository such as openstack/nova.""" |
| 335 | |
James E. Blair | cf440a2 | 2016-07-15 09:11:58 -0700 | [diff] [blame] | 336 | # NOTE: Projects should only be instantiated via a Source object |
| 337 | # so that they are associated with and cached by their Connection. |
| 338 | # This makes a Project instance a unique identifier for a given |
| 339 | # project from a given source. |
| 340 | |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 341 | def __init__(self, name, source, foreign=False): |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 342 | self.name = name |
James E. Blair | 8a395f9 | 2017-03-30 11:15:33 -0700 | [diff] [blame] | 343 | self.source = source |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 344 | self.connection_name = source.connection.connection_name |
| 345 | self.canonical_hostname = source.canonical_hostname |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 346 | self.canonical_name = source.canonical_hostname + '/' + name |
Evgeny Antyshev | 0deaaad | 2015-08-03 20:22:56 +0000 | [diff] [blame] | 347 | # foreign projects are those referenced in dependencies |
| 348 | # of layout projects, this should matter |
| 349 | # when deciding whether to enqueue their changes |
James E. Blair | cf440a2 | 2016-07-15 09:11:58 -0700 | [diff] [blame] | 350 | # TODOv3 (jeblair): re-add support for foreign projects if needed |
Evgeny Antyshev | 0deaaad | 2015-08-03 20:22:56 +0000 | [diff] [blame] | 351 | self.foreign = foreign |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 352 | self.unparsed_config = None |
James E. Blair | e316202 | 2017-02-20 16:47:27 -0500 | [diff] [blame] | 353 | self.unparsed_branch_config = {} # branch -> UnparsedTenantConfig |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 354 | |
| 355 | def __str__(self): |
| 356 | return self.name |
| 357 | |
| 358 | def __repr__(self): |
| 359 | return '<Project %s>' % (self.name) |
| 360 | |
Clark Boylan | a7f724c | 2017-10-25 11:35:19 -0700 | [diff] [blame] | 361 | def getSafeAttributes(self): |
| 362 | return Attributes(name=self.name) |
| 363 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 364 | |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 365 | class Node(object): |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 366 | """A single node for use by a job. |
| 367 | |
| 368 | This may represent a request for a node, or an actual node |
| 369 | provided by Nodepool. |
| 370 | """ |
| 371 | |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 372 | def __init__(self, name, label): |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 373 | self.name = name |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 374 | self.label = label |
James E. Blair | cbf4367 | 2017-01-04 14:33:41 -0800 | [diff] [blame] | 375 | self.id = None |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 376 | self.lock = None |
David Shrewsbury | ffab07a | 2017-07-24 12:45:07 -0400 | [diff] [blame] | 377 | self.hold_job = None |
David Shrewsbury | f9af9df | 2017-08-01 15:19:26 -0400 | [diff] [blame] | 378 | self.comment = None |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 379 | # Attributes from Nodepool |
| 380 | self._state = 'unknown' |
| 381 | self.state_time = time.time() |
Monty Taylor | 56f6133 | 2017-04-11 05:38:12 -0500 | [diff] [blame] | 382 | self.interface_ip = None |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 383 | self.public_ipv4 = None |
| 384 | self.private_ipv4 = None |
| 385 | self.public_ipv6 = None |
Tristan Cacqueray | 8095440 | 2017-05-28 00:33:55 +0000 | [diff] [blame] | 386 | self.ssh_port = 22 |
James E. Blair | cacdf2b | 2017-01-04 13:14:37 -0800 | [diff] [blame] | 387 | self._keys = [] |
Paul Belanger | 30ba93a | 2017-03-16 16:28:10 -0400 | [diff] [blame] | 388 | self.az = None |
| 389 | self.provider = None |
| 390 | self.region = None |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 391 | |
| 392 | @property |
| 393 | def state(self): |
| 394 | return self._state |
| 395 | |
| 396 | @state.setter |
| 397 | def state(self, value): |
James E. Blair | 803e94f | 2017-01-06 09:18:59 -0800 | [diff] [blame] | 398 | if value not in NODE_STATES: |
| 399 | raise TypeError("'%s' is not a valid state" % value) |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 400 | self._state = value |
| 401 | self.state_time = time.time() |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 402 | |
| 403 | def __repr__(self): |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 404 | return '<Node %s %s:%s>' % (self.id, self.name, self.label) |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 405 | |
James E. Blair | 0d95215 | 2017-02-07 17:14:44 -0800 | [diff] [blame] | 406 | def __ne__(self, other): |
| 407 | return not self.__eq__(other) |
| 408 | |
| 409 | def __eq__(self, other): |
| 410 | if not isinstance(other, Node): |
| 411 | return False |
| 412 | return (self.name == other.name and |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 413 | self.label == other.label and |
James E. Blair | 0d95215 | 2017-02-07 17:14:44 -0800 | [diff] [blame] | 414 | self.id == other.id) |
| 415 | |
James E. Blair | cacdf2b | 2017-01-04 13:14:37 -0800 | [diff] [blame] | 416 | def toDict(self): |
| 417 | d = {} |
| 418 | d['state'] = self.state |
David Shrewsbury | ffab07a | 2017-07-24 12:45:07 -0400 | [diff] [blame] | 419 | d['hold_job'] = self.hold_job |
David Shrewsbury | f9af9df | 2017-08-01 15:19:26 -0400 | [diff] [blame] | 420 | d['comment'] = self.comment |
James E. Blair | cacdf2b | 2017-01-04 13:14:37 -0800 | [diff] [blame] | 421 | for k in self._keys: |
| 422 | d[k] = getattr(self, k) |
| 423 | return d |
| 424 | |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 425 | def updateFromDict(self, data): |
| 426 | self._state = data['state'] |
James E. Blair | cacdf2b | 2017-01-04 13:14:37 -0800 | [diff] [blame] | 427 | keys = [] |
| 428 | for k, v in data.items(): |
| 429 | if k == 'state': |
| 430 | continue |
| 431 | keys.append(k) |
| 432 | setattr(self, k, v) |
| 433 | self._keys = keys |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 434 | |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 435 | |
Monty Taylor | 7b19ba7 | 2017-05-24 07:42:54 -0500 | [diff] [blame] | 436 | class Group(object): |
| 437 | """A logical group of nodes for use by a job. |
| 438 | |
| 439 | A Group is a named set of node names that will be provided to |
| 440 | jobs in the inventory to describe logical units where some subset of tasks |
| 441 | run. |
| 442 | """ |
| 443 | |
| 444 | def __init__(self, name, nodes): |
| 445 | self.name = name |
| 446 | self.nodes = nodes |
| 447 | |
| 448 | def __repr__(self): |
| 449 | return '<Group %s %s>' % (self.name, str(self.nodes)) |
| 450 | |
| 451 | def __ne__(self, other): |
| 452 | return not self.__eq__(other) |
| 453 | |
| 454 | def __eq__(self, other): |
| 455 | if not isinstance(other, Group): |
| 456 | return False |
| 457 | return (self.name == other.name and |
| 458 | self.nodes == other.nodes) |
| 459 | |
| 460 | def toDict(self): |
| 461 | return { |
| 462 | 'name': self.name, |
| 463 | 'nodes': self.nodes |
| 464 | } |
| 465 | |
| 466 | |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 467 | class NodeSet(object): |
| 468 | """A set of nodes. |
| 469 | |
| 470 | In configuration, NodeSets are attributes of Jobs indicating that |
| 471 | a Job requires nodes matching this description. |
| 472 | |
| 473 | They may appear as top-level configuration objects and be named, |
| 474 | or they may appears anonymously in in-line job definitions. |
| 475 | """ |
| 476 | |
| 477 | def __init__(self, name=None): |
| 478 | self.name = name or '' |
| 479 | self.nodes = OrderedDict() |
Monty Taylor | 7b19ba7 | 2017-05-24 07:42:54 -0500 | [diff] [blame] | 480 | self.groups = OrderedDict() |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 481 | |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 482 | def __ne__(self, other): |
| 483 | return not self.__eq__(other) |
| 484 | |
| 485 | def __eq__(self, other): |
| 486 | if not isinstance(other, NodeSet): |
| 487 | return False |
| 488 | return (self.name == other.name and |
| 489 | self.nodes == other.nodes) |
| 490 | |
James E. Blair | cbf4367 | 2017-01-04 14:33:41 -0800 | [diff] [blame] | 491 | def copy(self): |
| 492 | n = NodeSet(self.name) |
| 493 | for name, node in self.nodes.items(): |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 494 | n.addNode(Node(node.name, node.label)) |
Monty Taylor | 7b19ba7 | 2017-05-24 07:42:54 -0500 | [diff] [blame] | 495 | for name, group in self.groups.items(): |
| 496 | n.addGroup(Group(group.name, group.nodes[:])) |
James E. Blair | cbf4367 | 2017-01-04 14:33:41 -0800 | [diff] [blame] | 497 | return n |
| 498 | |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 499 | def addNode(self, node): |
| 500 | if node.name in self.nodes: |
| 501 | raise Exception("Duplicate node in %s" % (self,)) |
| 502 | self.nodes[node.name] = node |
| 503 | |
James E. Blair | 0eaad55 | 2016-09-02 12:09:54 -0700 | [diff] [blame] | 504 | def getNodes(self): |
Clint Byrum | a4471d1 | 2017-05-10 20:57:40 -0400 | [diff] [blame] | 505 | return list(self.nodes.values()) |
James E. Blair | 0eaad55 | 2016-09-02 12:09:54 -0700 | [diff] [blame] | 506 | |
Monty Taylor | 7b19ba7 | 2017-05-24 07:42:54 -0500 | [diff] [blame] | 507 | def addGroup(self, group): |
| 508 | if group.name in self.groups: |
| 509 | raise Exception("Duplicate group in %s" % (self,)) |
| 510 | self.groups[group.name] = group |
| 511 | |
| 512 | def getGroups(self): |
| 513 | return list(self.groups.values()) |
| 514 | |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 515 | def __repr__(self): |
| 516 | if self.name: |
| 517 | name = self.name + ' ' |
| 518 | else: |
| 519 | name = '' |
Monty Taylor | 7b19ba7 | 2017-05-24 07:42:54 -0500 | [diff] [blame] | 520 | return '<NodeSet %s%s%s>' % (name, self.nodes, self.groups) |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 521 | |
Tristan Cacqueray | 82f864b | 2017-08-01 05:54:42 +0000 | [diff] [blame] | 522 | def __len__(self): |
| 523 | return len(self.nodes) |
| 524 | |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 525 | |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 526 | class NodeRequest(object): |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 527 | """A request for a set of nodes.""" |
| 528 | |
James E. Blair | 8b2a147 | 2017-02-19 15:33:55 -0800 | [diff] [blame] | 529 | def __init__(self, requestor, build_set, job, nodeset): |
| 530 | self.requestor = requestor |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 531 | self.build_set = build_set |
| 532 | self.job = job |
James E. Blair | 0eaad55 | 2016-09-02 12:09:54 -0700 | [diff] [blame] | 533 | self.nodeset = nodeset |
James E. Blair | 803e94f | 2017-01-06 09:18:59 -0800 | [diff] [blame] | 534 | self._state = STATE_REQUESTED |
James E. Blair | 4f1731b | 2017-10-10 18:11:42 -0700 | [diff] [blame] | 535 | self.requested_time = time.time() |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 536 | self.state_time = time.time() |
| 537 | self.stat = None |
| 538 | self.uid = uuid4().hex |
James E. Blair | cbf4367 | 2017-01-04 14:33:41 -0800 | [diff] [blame] | 539 | self.id = None |
James E. Blair | cbbce0d | 2017-05-19 07:28:29 -0700 | [diff] [blame] | 540 | # Zuul internal flags (not stored in ZK so they are not |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 541 | # overwritten). |
| 542 | self.failed = False |
James E. Blair | cbbce0d | 2017-05-19 07:28:29 -0700 | [diff] [blame] | 543 | self.canceled = False |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 544 | |
| 545 | @property |
Monty Taylor | 6dc5bc1 | 2017-09-29 15:47:31 -0500 | [diff] [blame] | 546 | def priority(self): |
Monty Taylor | b588205 | 2017-09-29 19:12:52 -0500 | [diff] [blame] | 547 | if self.build_set: |
| 548 | precedence = self.build_set.item.pipeline.precedence |
| 549 | else: |
| 550 | precedence = PRECEDENCE_NORMAL |
| 551 | return PRIORITY_MAP[precedence] |
Monty Taylor | 6dc5bc1 | 2017-09-29 15:47:31 -0500 | [diff] [blame] | 552 | |
| 553 | @property |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 554 | def fulfilled(self): |
| 555 | return (self._state == STATE_FULFILLED) and not self.failed |
| 556 | |
| 557 | @property |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 558 | def state(self): |
| 559 | return self._state |
| 560 | |
| 561 | @state.setter |
| 562 | def state(self, value): |
James E. Blair | 803e94f | 2017-01-06 09:18:59 -0800 | [diff] [blame] | 563 | if value not in REQUEST_STATES: |
| 564 | raise TypeError("'%s' is not a valid state" % value) |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 565 | self._state = value |
| 566 | self.state_time = time.time() |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 567 | |
| 568 | def __repr__(self): |
James E. Blair | cbf4367 | 2017-01-04 14:33:41 -0800 | [diff] [blame] | 569 | return '<NodeRequest %s %s>' % (self.id, self.nodeset) |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 570 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 571 | def toDict(self): |
| 572 | d = {} |
James E. Blair | 16d96a0 | 2017-06-08 11:32:56 -0700 | [diff] [blame] | 573 | nodes = [n.label for n in self.nodeset.getNodes()] |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 574 | d['node_types'] = nodes |
James E. Blair | 8b2a147 | 2017-02-19 15:33:55 -0800 | [diff] [blame] | 575 | d['requestor'] = self.requestor |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 576 | d['state'] = self.state |
| 577 | d['state_time'] = self.state_time |
| 578 | return d |
| 579 | |
| 580 | def updateFromDict(self, data): |
| 581 | self._state = data['state'] |
| 582 | self.state_time = data['state_time'] |
| 583 | |
James E. Blair | 34776ee | 2016-08-25 13:53:54 -0700 | [diff] [blame] | 584 | |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 585 | class Secret(object): |
| 586 | """A collection of private data. |
| 587 | |
| 588 | In configuration, Secrets are collections of private data in |
| 589 | key-value pair format. They are defined as top-level |
| 590 | configuration objects and then referenced by Jobs. |
| 591 | |
| 592 | """ |
| 593 | |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 594 | def __init__(self, name, source_context): |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 595 | self.name = name |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 596 | self.source_context = source_context |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 597 | # The secret data may or may not be encrypted. This attribute |
| 598 | # is named 'secret_data' to make it easy to search for and |
| 599 | # spot where it is directly used. |
| 600 | self.secret_data = {} |
| 601 | |
| 602 | def __ne__(self, other): |
| 603 | return not self.__eq__(other) |
| 604 | |
| 605 | def __eq__(self, other): |
| 606 | if not isinstance(other, Secret): |
| 607 | return False |
| 608 | return (self.name == other.name and |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 609 | self.source_context == other.source_context and |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 610 | self.secret_data == other.secret_data) |
| 611 | |
| 612 | def __repr__(self): |
| 613 | return '<Secret %s>' % (self.name,) |
| 614 | |
James E. Blair | 18f86a3 | 2017-03-15 14:43:26 -0700 | [diff] [blame] | 615 | def decrypt(self, private_key): |
| 616 | """Return a copy of this secret with any encrypted data decrypted. |
| 617 | Note that the original remains encrypted.""" |
| 618 | |
| 619 | r = copy.deepcopy(self) |
| 620 | decrypted_secret_data = {} |
| 621 | for k, v in r.secret_data.items(): |
| 622 | if hasattr(v, 'decrypt'): |
| 623 | decrypted_secret_data[k] = v.decrypt(private_key) |
| 624 | else: |
| 625 | decrypted_secret_data[k] = v |
| 626 | r.secret_data = decrypted_secret_data |
| 627 | return r |
| 628 | |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 629 | |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 630 | class SourceContext(object): |
| 631 | """A reference to the branch of a project in configuration. |
| 632 | |
| 633 | Jobs and playbooks reference this to keep track of where they |
| 634 | originate.""" |
| 635 | |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 636 | def __init__(self, project, branch, path, trusted): |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 637 | self.project = project |
| 638 | self.branch = branch |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 639 | self.path = path |
Monty Taylor | e6562aa | 2017-02-20 07:37:39 -0500 | [diff] [blame] | 640 | self.trusted = trusted |
James E. Blair | 7edc25f | 2017-10-26 10:47:14 -0700 | [diff] [blame] | 641 | self.implied_branch_matchers = None |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 642 | |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 643 | def __str__(self): |
| 644 | return '%s/%s@%s' % (self.project, self.path, self.branch) |
| 645 | |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 646 | def __repr__(self): |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 647 | return '<SourceContext %s trusted:%s>' % (str(self), |
| 648 | self.trusted) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 649 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 650 | def __deepcopy__(self, memo): |
| 651 | return self.copy() |
| 652 | |
| 653 | def copy(self): |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 654 | return self.__class__(self.project, self.branch, self.path, |
| 655 | self.trusted) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 656 | |
Tristan Cacqueray | e50af2e | 2017-09-19 14:18:28 +0000 | [diff] [blame] | 657 | def isSameProject(self, other): |
| 658 | if not isinstance(other, SourceContext): |
| 659 | return False |
| 660 | return (self.project == other.project and |
| 661 | self.branch == other.branch and |
| 662 | self.trusted == other.trusted) |
| 663 | |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 664 | def __ne__(self, other): |
| 665 | return not self.__eq__(other) |
| 666 | |
| 667 | def __eq__(self, other): |
| 668 | if not isinstance(other, SourceContext): |
| 669 | return False |
| 670 | return (self.project == other.project and |
| 671 | self.branch == other.branch and |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 672 | self.path == other.path and |
Monty Taylor | e6562aa | 2017-02-20 07:37:39 -0500 | [diff] [blame] | 673 | self.trusted == other.trusted) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 674 | |
| 675 | |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 676 | class PlaybookContext(object): |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 677 | |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 678 | """A reference to a playbook in the context of a project. |
| 679 | |
| 680 | Jobs refer to objects of this class for their main, pre, and post |
| 681 | playbooks so that we can keep track of which repos and security |
James E. Blair | 74a82cf | 2017-07-12 17:23:08 -0700 | [diff] [blame] | 682 | contexts are needed in order to run them. |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 683 | |
James E. Blair | 74a82cf | 2017-07-12 17:23:08 -0700 | [diff] [blame] | 684 | We also keep a list of roles so that playbooks only run with the |
| 685 | roles which were defined at the point the playbook was defined. |
| 686 | |
| 687 | """ |
| 688 | |
James E. Blair | 892cca6 | 2017-08-09 11:36:58 -0700 | [diff] [blame] | 689 | def __init__(self, source_context, path, roles, secrets): |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 690 | self.source_context = source_context |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 691 | self.path = path |
James E. Blair | 74a82cf | 2017-07-12 17:23:08 -0700 | [diff] [blame] | 692 | self.roles = roles |
James E. Blair | 892cca6 | 2017-08-09 11:36:58 -0700 | [diff] [blame] | 693 | self.secrets = secrets |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 694 | |
| 695 | def __repr__(self): |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 696 | return '<PlaybookContext %s %s>' % (self.source_context, |
| 697 | self.path) |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 698 | |
| 699 | def __ne__(self, other): |
| 700 | return not self.__eq__(other) |
| 701 | |
| 702 | def __eq__(self, other): |
| 703 | if not isinstance(other, PlaybookContext): |
| 704 | return False |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 705 | return (self.source_context == other.source_context and |
James E. Blair | 74a82cf | 2017-07-12 17:23:08 -0700 | [diff] [blame] | 706 | self.path == other.path and |
James E. Blair | 892cca6 | 2017-08-09 11:36:58 -0700 | [diff] [blame] | 707 | self.roles == other.roles and |
| 708 | self.secrets == other.secrets) |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 709 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 710 | def copy(self): |
| 711 | r = PlaybookContext(self.source_context, |
| 712 | self.path, |
| 713 | self.roles, |
| 714 | self.secrets) |
| 715 | return r |
| 716 | |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 717 | def toDict(self): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 718 | # Render to a dict to use in passing json to the executor |
James E. Blair | 892cca6 | 2017-08-09 11:36:58 -0700 | [diff] [blame] | 719 | secrets = {} |
| 720 | for secret in self.secrets: |
| 721 | secret_data = copy.deepcopy(secret.secret_data) |
| 722 | secrets[secret.name] = secret_data |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 723 | return dict( |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 724 | connection=self.source_context.project.connection_name, |
| 725 | project=self.source_context.project.name, |
| 726 | branch=self.source_context.branch, |
Monty Taylor | e6562aa | 2017-02-20 07:37:39 -0500 | [diff] [blame] | 727 | trusted=self.source_context.trusted, |
James E. Blair | 74a82cf | 2017-07-12 17:23:08 -0700 | [diff] [blame] | 728 | roles=[r.toDict() for r in self.roles], |
James E. Blair | 892cca6 | 2017-08-09 11:36:58 -0700 | [diff] [blame] | 729 | secrets=secrets, |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 730 | path=self.path) |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 731 | |
| 732 | |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 733 | class Role(object, metaclass=abc.ABCMeta): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 734 | """A reference to an ansible role.""" |
| 735 | |
| 736 | def __init__(self, target_name): |
| 737 | self.target_name = target_name |
| 738 | |
| 739 | @abc.abstractmethod |
| 740 | def __repr__(self): |
| 741 | pass |
| 742 | |
| 743 | def __ne__(self, other): |
| 744 | return not self.__eq__(other) |
| 745 | |
| 746 | @abc.abstractmethod |
| 747 | def __eq__(self, other): |
| 748 | if not isinstance(other, Role): |
| 749 | return False |
| 750 | return (self.target_name == other.target_name) |
| 751 | |
| 752 | @abc.abstractmethod |
| 753 | def toDict(self): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 754 | # Render to a dict to use in passing json to the executor |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 755 | return dict(target_name=self.target_name) |
| 756 | |
| 757 | |
| 758 | class ZuulRole(Role): |
| 759 | """A reference to an ansible role in a Zuul project.""" |
| 760 | |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 761 | def __init__(self, target_name, connection_name, project_name, |
| 762 | implicit=False): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 763 | super(ZuulRole, self).__init__(target_name) |
| 764 | self.connection_name = connection_name |
| 765 | self.project_name = project_name |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 766 | self.implicit = implicit |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 767 | |
| 768 | def __repr__(self): |
| 769 | return '<ZuulRole %s %s>' % (self.project_name, self.target_name) |
| 770 | |
Clint Byrum | af7438f | 2017-05-10 17:26:57 -0400 | [diff] [blame] | 771 | __hash__ = object.__hash__ |
| 772 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 773 | def __eq__(self, other): |
| 774 | if not isinstance(other, ZuulRole): |
| 775 | return False |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 776 | # Implicit is not consulted for equality so that we can handle |
| 777 | # implicit to explicit conversions. |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 778 | return (super(ZuulRole, self).__eq__(other) and |
James E. Blair | 1b27f6a | 2017-07-14 14:09:07 -0700 | [diff] [blame] | 779 | self.connection_name == other.connection_name and |
James E. Blair | 6563e4b | 2017-04-28 08:14:48 -0700 | [diff] [blame] | 780 | self.project_name == other.project_name) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 781 | |
| 782 | def toDict(self): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 783 | # Render to a dict to use in passing json to the executor |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 784 | d = super(ZuulRole, self).toDict() |
| 785 | d['type'] = 'zuul' |
| 786 | d['connection'] = self.connection_name |
| 787 | d['project'] = self.project_name |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 788 | d['implicit'] = self.implicit |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 789 | return d |
| 790 | |
| 791 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 792 | class Job(object): |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 793 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 794 | """A Job represents the defintion of actions to perform. |
| 795 | |
James E. Blair | d4ade8c | 2017-02-19 15:25:46 -0800 | [diff] [blame] | 796 | A Job is an abstract configuration concept. It describes what, |
| 797 | where, and under what circumstances something should be run |
| 798 | (contrast this with Build which is a concrete single execution of |
| 799 | a Job). |
| 800 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 801 | NB: Do not modify attributes of this class, set them directly |
| 802 | (e.g., "job.run = ..." rather than "job.run.append(...)"). |
| 803 | """ |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 804 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 805 | BASE_JOB_MARKER = object() |
| 806 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 807 | def __init__(self, name): |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 808 | # These attributes may override even the final form of a job |
| 809 | # in the context of a project-pipeline. They can not affect |
| 810 | # the execution of the job, but only whether the job is run |
| 811 | # and how it is reported. |
| 812 | self.context_attributes = dict( |
| 813 | voting=True, |
| 814 | hold_following_changes=False, |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 815 | failure_message=None, |
| 816 | success_message=None, |
| 817 | failure_url=None, |
| 818 | success_url=None, |
| 819 | # Matchers. These are separate so they can be individually |
| 820 | # overidden. |
| 821 | branch_matcher=None, |
| 822 | file_matcher=None, |
| 823 | irrelevant_file_matcher=None, # skip-if |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 824 | tags=frozenset(), |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 825 | dependencies=frozenset(), |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 826 | ) |
| 827 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 828 | # These attributes affect how the job is actually run and more |
| 829 | # care must be taken when overriding them. If a job is |
| 830 | # declared "final", these may not be overriden in a |
| 831 | # project-pipeline. |
| 832 | self.execution_attributes = dict( |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 833 | parent=None, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 834 | timeout=None, |
James E. Blair | 490cf04 | 2017-02-24 23:07:21 -0500 | [diff] [blame] | 835 | variables={}, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 836 | nodeset=NodeSet(), |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 837 | workspace=None, |
| 838 | pre_run=(), |
| 839 | post_run=(), |
| 840 | run=(), |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 841 | semaphore=None, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 842 | attempts=3, |
| 843 | final=False, |
James E. Blair | 5fc8192 | 2017-07-12 13:19:37 -0700 | [diff] [blame] | 844 | roles=(), |
James E. Blair | 912322f | 2017-05-23 13:11:25 -0700 | [diff] [blame] | 845 | required_projects={}, |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame] | 846 | allowed_projects=None, |
James E. Blair | 7391ecb | 2017-05-23 13:32:40 -0700 | [diff] [blame] | 847 | override_branch=None, |
James E. Blair | edff2c2 | 2017-10-30 14:04:48 -0700 | [diff] [blame] | 848 | override_checkout=None, |
James E. Blair | 8eb564a | 2017-08-10 09:21:41 -0700 | [diff] [blame] | 849 | post_review=None, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 850 | ) |
| 851 | |
| 852 | # These are generally internal attributes which are not |
| 853 | # accessible via configuration. |
| 854 | self.other_attributes = dict( |
| 855 | name=None, |
| 856 | source_context=None, |
James E. Blair | 167d6cd | 2017-09-29 14:24:42 -0700 | [diff] [blame] | 857 | source_line=None, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 858 | inheritance_path=(), |
James E. Blair | 698703c | 2017-09-15 20:58:30 -0600 | [diff] [blame] | 859 | parent_data=None, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 860 | ) |
| 861 | |
| 862 | self.inheritable_attributes = {} |
| 863 | self.inheritable_attributes.update(self.context_attributes) |
| 864 | self.inheritable_attributes.update(self.execution_attributes) |
| 865 | self.attributes = {} |
| 866 | self.attributes.update(self.inheritable_attributes) |
| 867 | self.attributes.update(self.other_attributes) |
| 868 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 869 | self.name = name |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 870 | |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 871 | def __ne__(self, other): |
| 872 | return not self.__eq__(other) |
| 873 | |
Paul Belanger | e22baea | 2016-11-03 16:59:27 -0400 | [diff] [blame] | 874 | def __eq__(self, other): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 875 | # Compare the name and all inheritable attributes to determine |
| 876 | # whether two jobs with the same name are identically |
| 877 | # configured. Useful upon reconfiguration. |
| 878 | if not isinstance(other, Job): |
| 879 | return False |
| 880 | if self.name != other.name: |
| 881 | return False |
| 882 | for k, v in self.attributes.items(): |
| 883 | if getattr(self, k) != getattr(other, k): |
| 884 | return False |
| 885 | return True |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 886 | |
Clint Byrum | af7438f | 2017-05-10 17:26:57 -0400 | [diff] [blame] | 887 | __hash__ = object.__hash__ |
| 888 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 889 | def __str__(self): |
| 890 | return self.name |
| 891 | |
| 892 | def __repr__(self): |
James E. Blair | 167d6cd | 2017-09-29 14:24:42 -0700 | [diff] [blame] | 893 | return '<Job %s branches: %s source: %s#%s>' % ( |
| 894 | self.name, |
| 895 | self.branch_matcher, |
| 896 | self.source_context, |
| 897 | self.source_line) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 898 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 899 | def __getattr__(self, name): |
| 900 | v = self.__dict__.get(name) |
| 901 | if v is None: |
James E. Blair | af8b208 | 2017-10-03 15:38:27 -0700 | [diff] [blame] | 902 | return self.attributes[name] |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 903 | return v |
| 904 | |
| 905 | def _get(self, name): |
| 906 | return self.__dict__.get(name) |
| 907 | |
Joshua Hesketh | cd96ec0 | 2017-03-28 08:05:56 +1100 | [diff] [blame] | 908 | def getSafeAttributes(self): |
| 909 | return Attributes(name=self.name) |
| 910 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 911 | def isBase(self): |
| 912 | return self.parent is self.BASE_JOB_MARKER |
| 913 | |
| 914 | def setBase(self): |
| 915 | self.inheritance_path = self.inheritance_path + (repr(self),) |
| 916 | |
James E. Blair | 5fc8192 | 2017-07-12 13:19:37 -0700 | [diff] [blame] | 917 | def addRoles(self, roles): |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 918 | newroles = [] |
| 919 | # Start with a copy of the existing roles, but if any of them |
| 920 | # are implicit roles which are identified as explicit in the |
| 921 | # new roles list, replace them with the explicit version. |
| 922 | changed = False |
| 923 | for existing_role in self.roles: |
| 924 | if existing_role in roles: |
| 925 | new_role = roles[roles.index(existing_role)] |
| 926 | else: |
| 927 | new_role = None |
| 928 | if (new_role and |
| 929 | isinstance(new_role, ZuulRole) and |
| 930 | isinstance(existing_role, ZuulRole) and |
| 931 | existing_role.implicit and not new_role.implicit): |
| 932 | newroles.append(new_role) |
| 933 | changed = True |
| 934 | else: |
| 935 | newroles.append(existing_role) |
| 936 | # Now add the new roles. |
James E. Blair | 4eec828 | 2017-07-12 17:33:26 -0700 | [diff] [blame] | 937 | for role in reversed(roles): |
James E. Blair | 5fc8192 | 2017-07-12 13:19:37 -0700 | [diff] [blame] | 938 | if role not in newroles: |
James E. Blair | 4eec828 | 2017-07-12 17:33:26 -0700 | [diff] [blame] | 939 | newroles.insert(0, role) |
James E. Blair | bb94dfa | 2017-07-11 07:45:19 -0700 | [diff] [blame] | 940 | changed = True |
| 941 | if changed: |
| 942 | self.roles = tuple(newroles) |
James E. Blair | 5fc8192 | 2017-07-12 13:19:37 -0700 | [diff] [blame] | 943 | |
James E. Blair | e74f571 | 2017-09-29 15:14:31 -0700 | [diff] [blame] | 944 | def setBranchMatcher(self, branches): |
| 945 | # Set the branch matcher to match any of the supplied branches |
| 946 | matchers = [] |
| 947 | for branch in branches: |
| 948 | matchers.append(change_matcher.BranchMatcher(branch)) |
| 949 | self.branch_matcher = change_matcher.MatchAny(matchers) |
| 950 | |
James E. Blair | 490cf04 | 2017-02-24 23:07:21 -0500 | [diff] [blame] | 951 | def updateVariables(self, other_vars): |
James E. Blair | af8b208 | 2017-10-03 15:38:27 -0700 | [diff] [blame] | 952 | v = copy.deepcopy(self.variables) |
James E. Blair | 490cf04 | 2017-02-24 23:07:21 -0500 | [diff] [blame] | 953 | Job._deepUpdate(v, other_vars) |
| 954 | self.variables = v |
| 955 | |
James E. Blair | 698703c | 2017-09-15 20:58:30 -0600 | [diff] [blame] | 956 | def updateParentData(self, other_vars): |
| 957 | # Update variables, but give the current values priority (used |
| 958 | # for job return data which is lower precedence than defined |
| 959 | # job vars). |
| 960 | v = self.parent_data or {} |
| 961 | Job._deepUpdate(v, other_vars) |
| 962 | # To avoid running afoul of checks that jobs don't set zuul |
| 963 | # variables, remove them from parent data here. |
| 964 | if 'zuul' in v: |
| 965 | del v['zuul'] |
| 966 | self.parent_data = v |
| 967 | v = copy.deepcopy(self.parent_data) |
| 968 | Job._deepUpdate(v, self.variables) |
| 969 | self.variables = v |
| 970 | |
James E. Blair | 912322f | 2017-05-23 13:11:25 -0700 | [diff] [blame] | 971 | def updateProjects(self, other_projects): |
James E. Blair | af8b208 | 2017-10-03 15:38:27 -0700 | [diff] [blame] | 972 | required_projects = self.required_projects.copy() |
| 973 | required_projects.update(other_projects) |
James E. Blair | 912322f | 2017-05-23 13:11:25 -0700 | [diff] [blame] | 974 | self.required_projects = required_projects |
James E. Blair | 27f3dfc | 2017-05-23 13:07:28 -0700 | [diff] [blame] | 975 | |
James E. Blair | 490cf04 | 2017-02-24 23:07:21 -0500 | [diff] [blame] | 976 | @staticmethod |
| 977 | def _deepUpdate(a, b): |
| 978 | # Merge nested dictionaries if possible, otherwise, overwrite |
| 979 | # the value in 'a' with the value in 'b'. |
| 980 | for k, bv in b.items(): |
| 981 | av = a.get(k) |
| 982 | if isinstance(av, dict) and isinstance(bv, dict): |
| 983 | Job._deepUpdate(av, bv) |
| 984 | else: |
| 985 | a[k] = bv |
| 986 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 987 | def copy(self): |
| 988 | job = Job(self.name) |
| 989 | for k in self.attributes: |
| 990 | if self._get(k) is not None: |
| 991 | setattr(job, k, copy.deepcopy(self._get(k))) |
| 992 | return job |
| 993 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 994 | def freezePlaybooks(self, pblist): |
| 995 | """Take a list of playbooks, and return a copy of it updated with this |
| 996 | job's roles. |
| 997 | |
| 998 | """ |
| 999 | |
| 1000 | ret = [] |
| 1001 | for old_pb in pblist: |
| 1002 | pb = old_pb.copy() |
| 1003 | pb.roles = self.roles |
| 1004 | ret.append(pb) |
| 1005 | return tuple(ret) |
| 1006 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 1007 | def applyVariant(self, other): |
| 1008 | """Copy the attributes which have been set on the other job to this |
| 1009 | job.""" |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1010 | if not isinstance(other, Job): |
| 1011 | raise Exception("Job unable to inherit from %s" % (other,)) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 1012 | |
| 1013 | for k in self.execution_attributes: |
| 1014 | if (other._get(k) is not None and |
| 1015 | k not in set(['final'])): |
| 1016 | if self.final: |
| 1017 | raise Exception("Unable to modify final job %s attribute " |
| 1018 | "%s=%s with variant %s" % ( |
| 1019 | repr(self), k, other._get(k), |
| 1020 | repr(other))) |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 1021 | if k not in set(['pre_run', 'run', 'post_run', 'roles', |
| 1022 | 'variables', 'required_projects']): |
| 1023 | # TODO(jeblair): determine if deepcopy is required |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 1024 | setattr(self, k, copy.deepcopy(other._get(k))) |
| 1025 | |
| 1026 | # Don't set final above so that we don't trip an error halfway |
| 1027 | # through assignment. |
| 1028 | if other.final != self.attributes['final']: |
| 1029 | self.final = other.final |
| 1030 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 1031 | # We must update roles before any playbook contexts |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 1032 | if other._get('roles') is not None: |
James E. Blair | 5fc8192 | 2017-07-12 13:19:37 -0700 | [diff] [blame] | 1033 | self.addRoles(other.roles) |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 1034 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 1035 | if other._get('run') is not None: |
| 1036 | other_run = self.freezePlaybooks(other.run) |
| 1037 | self.run = other_run |
| 1038 | if other._get('pre_run') is not None: |
| 1039 | other_pre_run = self.freezePlaybooks(other.pre_run) |
| 1040 | self.pre_run = self.pre_run + other_pre_run |
| 1041 | if other._get('post_run') is not None: |
| 1042 | other_post_run = self.freezePlaybooks(other.post_run) |
| 1043 | self.post_run = other_post_run + self.post_run |
James E. Blair | 490cf04 | 2017-02-24 23:07:21 -0500 | [diff] [blame] | 1044 | if other._get('variables') is not None: |
| 1045 | self.updateVariables(other.variables) |
James E. Blair | 912322f | 2017-05-23 13:11:25 -0700 | [diff] [blame] | 1046 | if other._get('required_projects') is not None: |
| 1047 | self.updateProjects(other.required_projects) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 1048 | |
| 1049 | for k in self.context_attributes: |
| 1050 | if (other._get(k) is not None and |
| 1051 | k not in set(['tags'])): |
| 1052 | setattr(self, k, copy.deepcopy(other._get(k))) |
| 1053 | |
| 1054 | if other._get('tags') is not None: |
| 1055 | self.tags = self.tags.union(other.tags) |
| 1056 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 1057 | self.inheritance_path = self.inheritance_path + (repr(other),) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1058 | |
James E. Blair | e421a23 | 2012-07-25 16:59:21 -0700 | [diff] [blame] | 1059 | def changeMatches(self, change): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1060 | if self.branch_matcher and not self.branch_matcher.matches(change): |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 1061 | return False |
| 1062 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1063 | if self.file_matcher and not self.file_matcher.matches(change): |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 1064 | return False |
| 1065 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1066 | # NB: This is a negative match. |
| 1067 | if (self.irrelevant_file_matcher and |
| 1068 | self.irrelevant_file_matcher.matches(change)): |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1069 | return False |
| 1070 | |
James E. Blair | 70c7158 | 2013-03-06 08:50:50 -0800 | [diff] [blame] | 1071 | return True |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 1072 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1073 | |
James E. Blair | 912322f | 2017-05-23 13:11:25 -0700 | [diff] [blame] | 1074 | class JobProject(object): |
James E. Blair | 27f3dfc | 2017-05-23 13:07:28 -0700 | [diff] [blame] | 1075 | """ A reference to a project from a job. """ |
| 1076 | |
James E. Blair | edff2c2 | 2017-10-30 14:04:48 -0700 | [diff] [blame] | 1077 | def __init__(self, project_name, override_branch=None, |
| 1078 | override_checkout=None): |
James E. Blair | 27f3dfc | 2017-05-23 13:07:28 -0700 | [diff] [blame] | 1079 | self.project_name = project_name |
| 1080 | self.override_branch = override_branch |
James E. Blair | edff2c2 | 2017-10-30 14:04:48 -0700 | [diff] [blame] | 1081 | self.override_checkout = override_checkout |
James E. Blair | 27f3dfc | 2017-05-23 13:07:28 -0700 | [diff] [blame] | 1082 | |
| 1083 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1084 | class JobList(object): |
| 1085 | """ A list of jobs in a project's pipeline. """ |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 1086 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1087 | def __init__(self): |
| 1088 | self.jobs = OrderedDict() # job.name -> [job, ...] |
James E. Blair | 12748b5 | 2017-02-07 17:17:53 -0800 | [diff] [blame] | 1089 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1090 | def addJob(self, job): |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1091 | if job.name in self.jobs: |
| 1092 | self.jobs[job.name].append(job) |
| 1093 | else: |
| 1094 | self.jobs[job.name] = [job] |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1095 | |
James E. Blair | e74f571 | 2017-09-29 15:14:31 -0700 | [diff] [blame] | 1096 | def inheritFrom(self, other, implied_branch): |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1097 | for jobname, jobs in other.jobs.items(): |
James E. Blair | e74f571 | 2017-09-29 15:14:31 -0700 | [diff] [blame] | 1098 | joblist = self.jobs.setdefault(jobname, []) |
| 1099 | for job in jobs: |
| 1100 | if not job.branch_matcher and implied_branch: |
| 1101 | job = job.copy() |
| 1102 | job.setBranchMatcher([implied_branch]) |
James E. Blair | 2a66450 | 2017-10-27 11:39:33 -0700 | [diff] [blame] | 1103 | if job not in joblist: |
| 1104 | joblist.append(job) |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1105 | |
| 1106 | |
| 1107 | class JobGraph(object): |
| 1108 | """ A JobGraph represents the dependency graph between Job.""" |
| 1109 | |
| 1110 | def __init__(self): |
| 1111 | self.jobs = OrderedDict() # job_name -> Job |
| 1112 | self._dependencies = {} # dependent_job_name -> set(parent_job_names) |
| 1113 | |
| 1114 | def __repr__(self): |
| 1115 | return '<JobGraph %s>' % (self.jobs) |
| 1116 | |
| 1117 | def addJob(self, job): |
| 1118 | # A graph must be created after the job list is frozen, |
| 1119 | # therefore we should only get one job with the same name. |
| 1120 | if job.name in self.jobs: |
| 1121 | raise Exception("Job %s already added" % (job.name,)) |
| 1122 | self.jobs[job.name] = job |
| 1123 | # Append the dependency information |
| 1124 | self._dependencies.setdefault(job.name, set()) |
| 1125 | try: |
| 1126 | for dependency in job.dependencies: |
| 1127 | # Make sure a circular dependency is never created |
| 1128 | ancestor_jobs = self._getParentJobNamesRecursively( |
| 1129 | dependency, soft=True) |
| 1130 | ancestor_jobs.add(dependency) |
| 1131 | if any((job.name == anc_job) for anc_job in ancestor_jobs): |
| 1132 | raise Exception("Dependency cycle detected in job %s" % |
| 1133 | (job.name,)) |
| 1134 | self._dependencies[job.name].add(dependency) |
| 1135 | except Exception: |
| 1136 | del self.jobs[job.name] |
| 1137 | del self._dependencies[job.name] |
| 1138 | raise |
| 1139 | |
| 1140 | def getJobs(self): |
Clint Byrum | a4471d1 | 2017-05-10 20:57:40 -0400 | [diff] [blame] | 1141 | return list(self.jobs.values()) # Report in the order of layout cfg |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1142 | |
| 1143 | def _getDirectDependentJobs(self, parent_job): |
| 1144 | ret = set() |
| 1145 | for dependent_name, parent_names in self._dependencies.items(): |
| 1146 | if parent_job in parent_names: |
| 1147 | ret.add(dependent_name) |
| 1148 | return ret |
| 1149 | |
| 1150 | def getDependentJobsRecursively(self, parent_job): |
| 1151 | all_dependent_jobs = set() |
| 1152 | jobs_to_iterate = set([parent_job]) |
| 1153 | while len(jobs_to_iterate) > 0: |
| 1154 | current_job = jobs_to_iterate.pop() |
| 1155 | current_dependent_jobs = self._getDirectDependentJobs(current_job) |
| 1156 | new_dependent_jobs = current_dependent_jobs - all_dependent_jobs |
| 1157 | jobs_to_iterate |= new_dependent_jobs |
| 1158 | all_dependent_jobs |= new_dependent_jobs |
| 1159 | return [self.jobs[name] for name in all_dependent_jobs] |
| 1160 | |
| 1161 | def getParentJobsRecursively(self, dependent_job): |
| 1162 | return [self.jobs[name] for name in |
| 1163 | self._getParentJobNamesRecursively(dependent_job)] |
| 1164 | |
| 1165 | def _getParentJobNamesRecursively(self, dependent_job, soft=False): |
| 1166 | all_parent_jobs = set() |
| 1167 | jobs_to_iterate = set([dependent_job]) |
| 1168 | while len(jobs_to_iterate) > 0: |
| 1169 | current_job = jobs_to_iterate.pop() |
| 1170 | current_parent_jobs = self._dependencies.get(current_job) |
| 1171 | if current_parent_jobs is None: |
| 1172 | if soft: |
| 1173 | current_parent_jobs = set() |
| 1174 | else: |
| 1175 | raise Exception("Dependent job %s not found: " % |
| 1176 | (dependent_job,)) |
| 1177 | new_parent_jobs = current_parent_jobs - all_parent_jobs |
| 1178 | jobs_to_iterate |= new_parent_jobs |
| 1179 | all_parent_jobs |= new_parent_jobs |
| 1180 | return all_parent_jobs |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 1181 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1182 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1183 | class Build(object): |
James E. Blair | d4ade8c | 2017-02-19 15:25:46 -0800 | [diff] [blame] | 1184 | """A Build is an instance of a single execution of a Job. |
| 1185 | |
| 1186 | While a Job describes what to run, a Build describes an actual |
| 1187 | execution of that Job. Each build is associated with exactly one |
| 1188 | Job (related builds are grouped together in a BuildSet). |
| 1189 | """ |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 1190 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1191 | def __init__(self, job, uuid): |
| 1192 | self.job = job |
| 1193 | self.uuid = uuid |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1194 | self.url = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1195 | self.result = None |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 1196 | self.result_data = {} |
James E. Blair | 6f69973 | 2017-07-18 14:19:11 -0700 | [diff] [blame] | 1197 | self.error_detail = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 1198 | self.build_set = None |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1199 | self.execute_time = time.time() |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 1200 | self.start_time = None |
| 1201 | self.end_time = None |
James E. Blair | bea9ef1 | 2013-07-15 11:52:23 -0700 | [diff] [blame] | 1202 | self.estimated_time = None |
James E. Blair | 0aac487 | 2013-08-23 14:02:38 -0700 | [diff] [blame] | 1203 | self.canceled = False |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 1204 | self.retry = False |
James E. Blair | d78576a | 2013-07-09 10:39:17 -0700 | [diff] [blame] | 1205 | self.parameters = {} |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 1206 | self.worker = Worker() |
Timothy Chavez | b233208 | 2015-08-07 20:08:04 -0500 | [diff] [blame] | 1207 | self.node_labels = [] |
| 1208 | self.node_name = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1209 | |
| 1210 | def __repr__(self): |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 1211 | return ('<Build %s of %s on %s>' % |
| 1212 | (self.uuid, self.job.name, self.worker)) |
| 1213 | |
James E. Blair | 3a098dd | 2017-10-04 14:37:29 -0700 | [diff] [blame] | 1214 | @property |
| 1215 | def pipeline(self): |
| 1216 | return self.build_set.item.pipeline |
| 1217 | |
Joshua Hesketh | cd96ec0 | 2017-03-28 08:05:56 +1100 | [diff] [blame] | 1218 | def getSafeAttributes(self): |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 1219 | return Attributes(uuid=self.uuid, |
| 1220 | result=self.result, |
James E. Blair | 6f69973 | 2017-07-18 14:19:11 -0700 | [diff] [blame] | 1221 | error_detail=self.error_detail, |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 1222 | result_data=self.result_data) |
Joshua Hesketh | cd96ec0 | 2017-03-28 08:05:56 +1100 | [diff] [blame] | 1223 | |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 1224 | |
| 1225 | class Worker(object): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 1226 | """Information about the specific worker executing a Build.""" |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 1227 | def __init__(self): |
| 1228 | self.name = "Unknown" |
| 1229 | self.hostname = None |
Monty Taylor | 0dbe159 | 2017-06-11 10:57:27 -0500 | [diff] [blame] | 1230 | self.log_port = None |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 1231 | |
| 1232 | def updateFromData(self, data): |
| 1233 | """Update worker information if contained in the WORK_DATA response.""" |
| 1234 | self.name = data.get('worker_name', self.name) |
| 1235 | self.hostname = data.get('worker_hostname', self.hostname) |
Monty Taylor | 0dbe159 | 2017-06-11 10:57:27 -0500 | [diff] [blame] | 1236 | self.log_port = data.get('worker_log_port', self.log_port) |
Joshua Hesketh | ba8776a | 2014-01-12 14:35:40 +0800 | [diff] [blame] | 1237 | |
| 1238 | def __repr__(self): |
| 1239 | return '<Worker %s>' % self.name |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1240 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 1241 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1242 | class RepoFiles(object): |
James E. Blair | d4ade8c | 2017-02-19 15:25:46 -0800 | [diff] [blame] | 1243 | """RepoFiles holds config-file content for per-project job config. |
| 1244 | |
| 1245 | When Zuul asks a merger to prepare a future multiple-repo state |
| 1246 | and collect Zuul configuration files so that we can dynamically |
| 1247 | load our configuration, this class provides cached access to that |
| 1248 | data for use by the Change which updated the config files and any |
| 1249 | changes that follow it in a ChangeQueue. |
| 1250 | |
| 1251 | It is attached to a BuildSet since the content of Zuul |
| 1252 | configuration files can change with each new BuildSet. |
| 1253 | """ |
| 1254 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1255 | def __init__(self): |
James E. Blair | 2a53567 | 2017-04-27 12:03:15 -0700 | [diff] [blame] | 1256 | self.connections = {} |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1257 | |
| 1258 | def __repr__(self): |
James E. Blair | 2a53567 | 2017-04-27 12:03:15 -0700 | [diff] [blame] | 1259 | return '<RepoFiles %s>' % self.connections |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1260 | |
| 1261 | def setFiles(self, items): |
James E. Blair | 2a53567 | 2017-04-27 12:03:15 -0700 | [diff] [blame] | 1262 | self.hostnames = {} |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1263 | for item in items: |
James E. Blair | 2a53567 | 2017-04-27 12:03:15 -0700 | [diff] [blame] | 1264 | connection = self.connections.setdefault( |
| 1265 | item['connection'], {}) |
| 1266 | project = connection.setdefault(item['project'], {}) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1267 | branch = project.setdefault(item['branch'], {}) |
| 1268 | branch.update(item['files']) |
| 1269 | |
James E. Blair | 2a53567 | 2017-04-27 12:03:15 -0700 | [diff] [blame] | 1270 | def getFile(self, connection_name, project_name, branch, fn): |
| 1271 | host = self.connections.get(connection_name, {}) |
| 1272 | return host.get(project_name, {}).get(branch, {}).get(fn) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1273 | |
| 1274 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 1275 | class BuildSet(object): |
James E. Blair | d4ade8c | 2017-02-19 15:25:46 -0800 | [diff] [blame] | 1276 | """A collection of Builds for one specific potential future repository |
| 1277 | state. |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 1278 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1279 | When Zuul executes Builds for a change, it creates a Build to |
James E. Blair | d4ade8c | 2017-02-19 15:25:46 -0800 | [diff] [blame] | 1280 | represent each execution of each job and a BuildSet to keep track |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1281 | of all the Builds running for that Change. When Zuul re-executes |
James E. Blair | d4ade8c | 2017-02-19 15:25:46 -0800 | [diff] [blame] | 1282 | Builds for a Change with a different configuration, all of the |
| 1283 | running Builds in the BuildSet for that change are aborted, and a |
| 1284 | new BuildSet is created to hold the Builds for the Jobs being |
| 1285 | run with the new configuration. |
| 1286 | |
| 1287 | A BuildSet also holds the UUID used to produce the Zuul Ref that |
| 1288 | builders check out. |
| 1289 | |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 1290 | """ |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 1291 | # Merge states: |
| 1292 | NEW = 1 |
| 1293 | PENDING = 2 |
| 1294 | COMPLETE = 3 |
| 1295 | |
Antoine Musso | 9b22928 | 2014-08-18 23:45:43 +0200 | [diff] [blame] | 1296 | states_map = { |
| 1297 | 1: 'NEW', |
| 1298 | 2: 'PENDING', |
| 1299 | 3: 'COMPLETE', |
| 1300 | } |
| 1301 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1302 | def __init__(self, item): |
| 1303 | self.item = item |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 1304 | self.builds = {} |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1305 | self.result = None |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 1306 | self.uuid = None |
James E. Blair | 81515ad | 2012-10-01 18:29:08 -0700 | [diff] [blame] | 1307 | self.commit = None |
James E. Blair | 9e5b811 | 2017-10-19 08:12:24 -0700 | [diff] [blame] | 1308 | self.dependent_changes = None |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 1309 | self.merger_items = None |
James E. Blair | 973721f | 2012-08-15 10:19:43 -0700 | [diff] [blame] | 1310 | self.unable_to_merge = False |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 1311 | self.config_error = None # None or an error message string. |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1312 | self.failing_reasons = [] |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 1313 | self.merge_state = self.NEW |
James E. Blair | 0eaad55 | 2016-09-02 12:09:54 -0700 | [diff] [blame] | 1314 | self.nodesets = {} # job -> nodeset |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 1315 | self.node_requests = {} # job -> reqs |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1316 | self.files = RepoFiles() |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 1317 | self.repo_state = {} |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 1318 | self.tries = {} |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 1319 | |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 1320 | @property |
| 1321 | def ref(self): |
| 1322 | # NOTE(jamielennox): The concept of buildset ref is to be removed and a |
| 1323 | # buildset UUID identifier available instead. Currently the ref is |
| 1324 | # checked to see if the BuildSet has been configured. |
| 1325 | return 'Z' + self.uuid if self.uuid else None |
| 1326 | |
Antoine Musso | 9b22928 | 2014-08-18 23:45:43 +0200 | [diff] [blame] | 1327 | def __repr__(self): |
| 1328 | return '<BuildSet item: %s #builds: %s merge state: %s>' % ( |
| 1329 | self.item, |
| 1330 | len(self.builds), |
| 1331 | self.getStateName(self.merge_state)) |
| 1332 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1333 | def setConfiguration(self): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1334 | # The change isn't enqueued until after it's created |
| 1335 | # so we don't know what the other changes ahead will be |
| 1336 | # until jobs start. |
James E. Blair | 9e5b811 | 2017-10-19 08:12:24 -0700 | [diff] [blame] | 1337 | if not self.uuid: |
| 1338 | self.uuid = uuid4().hex |
| 1339 | if self.dependent_changes is None: |
| 1340 | items = [self.item] |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1341 | next_item = self.item.item_ahead |
| 1342 | while next_item: |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 1343 | items.append(next_item) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1344 | next_item = next_item.item_ahead |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 1345 | items.reverse() |
James E. Blair | 9e5b811 | 2017-10-19 08:12:24 -0700 | [diff] [blame] | 1346 | self.dependent_changes = [i.change.toDict() for i in items] |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 1347 | self.merger_items = [i.makeMergerItem() for i in items] |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1348 | |
Antoine Musso | 9b22928 | 2014-08-18 23:45:43 +0200 | [diff] [blame] | 1349 | def getStateName(self, state_num): |
| 1350 | return self.states_map.get( |
| 1351 | state_num, 'UNKNOWN (%s)' % state_num) |
| 1352 | |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1353 | def addBuild(self, build): |
| 1354 | self.builds[build.job.name] = build |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 1355 | if build.job.name not in self.tries: |
James E. Blair | 5aed111 | 2016-12-15 09:18:05 -0800 | [diff] [blame] | 1356 | self.tries[build.job.name] = 1 |
James E. Blair | 4886cc1 | 2012-07-18 15:39:41 -0700 | [diff] [blame] | 1357 | build.build_set = self |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1358 | |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 1359 | def removeBuild(self, build): |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 1360 | self.tries[build.job.name] += 1 |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 1361 | del self.builds[build.job.name] |
| 1362 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 1363 | def getBuild(self, job_name): |
| 1364 | return self.builds.get(job_name) |
| 1365 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1366 | def getBuilds(self): |
Clint Byrum | 1d0c7d1 | 2017-05-10 19:40:53 -0700 | [diff] [blame] | 1367 | keys = list(self.builds.keys()) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1368 | keys.sort() |
| 1369 | return [self.builds.get(x) for x in keys] |
| 1370 | |
James E. Blair | 0eaad55 | 2016-09-02 12:09:54 -0700 | [diff] [blame] | 1371 | def getJobNodeSet(self, job_name): |
| 1372 | # Return None if not provisioned; empty NodeSet if no nodes |
| 1373 | # required |
| 1374 | return self.nodesets.get(job_name) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 1375 | |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 1376 | def removeJobNodeSet(self, job_name): |
| 1377 | if job_name not in self.nodesets: |
| 1378 | raise Exception("No job set for %s" % (job_name)) |
| 1379 | del self.nodesets[job_name] |
| 1380 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 1381 | def setJobNodeRequest(self, job_name, req): |
| 1382 | if job_name in self.node_requests: |
| 1383 | raise Exception("Prior node request for %s" % (job_name)) |
| 1384 | self.node_requests[job_name] = req |
| 1385 | |
| 1386 | def getJobNodeRequest(self, job_name): |
| 1387 | return self.node_requests.get(job_name) |
| 1388 | |
James E. Blair | 0eaad55 | 2016-09-02 12:09:54 -0700 | [diff] [blame] | 1389 | def jobNodeRequestComplete(self, job_name, req, nodeset): |
| 1390 | if job_name in self.nodesets: |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 1391 | raise Exception("Prior node request for %s" % (job_name)) |
James E. Blair | 0eaad55 | 2016-09-02 12:09:54 -0700 | [diff] [blame] | 1392 | self.nodesets[job_name] = nodeset |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 1393 | del self.node_requests[job_name] |
| 1394 | |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 1395 | def getTries(self, job_name): |
Clint Byrum | 804073b | 2017-05-10 17:47:45 -0400 | [diff] [blame] | 1396 | return self.tries.get(job_name, 0) |
Paul Belanger | 71d9817 | 2016-11-08 10:56:31 -0500 | [diff] [blame] | 1397 | |
James E. Blair | 0ffa010 | 2017-03-30 13:11:33 -0700 | [diff] [blame] | 1398 | def getMergeMode(self): |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 1399 | # We may be called before this build set has a shadow layout |
| 1400 | # (ie, we are called to perform the merge to create that |
| 1401 | # layout). It's possible that the change we are merging will |
| 1402 | # update the merge-mode for the project, but there's not much |
| 1403 | # we can do about that here. Instead, do the best we can by |
| 1404 | # using the nearest shadow layout to determine the merge mode, |
| 1405 | # or if that fails, the current live layout, or if that fails, |
| 1406 | # use the default: merge-resolve. |
| 1407 | item = self.item |
| 1408 | layout = None |
| 1409 | while item: |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 1410 | layout = item.layout |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 1411 | if layout: |
| 1412 | break |
| 1413 | item = item.item_ahead |
| 1414 | if not layout: |
| 1415 | layout = self.item.pipeline.layout |
| 1416 | if layout: |
James E. Blair | 0ffa010 | 2017-03-30 13:11:33 -0700 | [diff] [blame] | 1417 | project = self.item.change.project |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 1418 | project_config = layout.project_configs.get( |
James E. Blair | 0ffa010 | 2017-03-30 13:11:33 -0700 | [diff] [blame] | 1419 | project.canonical_name) |
| 1420 | if project_config: |
| 1421 | return project_config.merge_mode |
| 1422 | return MERGER_MERGE_RESOLVE |
Adam Gandelman | 8bd5710 | 2016-12-02 12:58:42 -0800 | [diff] [blame] | 1423 | |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 1424 | def getSafeAttributes(self): |
| 1425 | return Attributes(uuid=self.uuid) |
| 1426 | |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 1427 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1428 | class QueueItem(object): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 1429 | """Represents the position of a Change in a ChangeQueue. |
| 1430 | |
| 1431 | All Changes are enqueued into ChangeQueue in a QueueItem. The QueueItem |
| 1432 | holds the current `BuildSet` as well as all previous `BuildSets` that were |
| 1433 | produced for this `QueueItem`. |
| 1434 | """ |
Joshua Hesketh | c0c012b | 2017-03-28 08:45:50 +1100 | [diff] [blame] | 1435 | log = logging.getLogger("zuul.QueueItem") |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 1436 | |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 1437 | def __init__(self, queue, change): |
| 1438 | self.pipeline = queue.pipeline |
| 1439 | self.queue = queue |
Monty Taylor | 55d0f56 | 2017-05-17 14:30:21 -0500 | [diff] [blame] | 1440 | self.change = change # a ref |
James E. Blair | caec0c5 | 2012-08-22 14:52:22 -0700 | [diff] [blame] | 1441 | self.dequeued_needing_change = False |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1442 | self.current_build_set = BuildSet(self) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1443 | self.item_ahead = None |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1444 | self.items_behind = [] |
James E. Blair | 8fa1697 | 2013-01-15 16:57:20 -0800 | [diff] [blame] | 1445 | self.enqueue_time = None |
| 1446 | self.dequeue_time = None |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1447 | self.reported = False |
Tobias Henkel | 9842bd7 | 2017-05-16 13:40:03 +0200 | [diff] [blame] | 1448 | self.reported_start = False |
| 1449 | self.quiet = False |
James E. Blair | bfb8e04 | 2014-12-30 17:01:44 -0800 | [diff] [blame] | 1450 | self.active = False # Whether an item is within an active window |
| 1451 | self.live = True # Whether an item is intended to be processed at all |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 1452 | self.layout = None |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1453 | self.job_graph = None |
James E. Blair | e5a847f | 2012-07-10 15:29:14 -0700 | [diff] [blame] | 1454 | |
James E. Blair | 972e3c7 | 2013-08-29 12:04:55 -0700 | [diff] [blame] | 1455 | def __repr__(self): |
| 1456 | if self.pipeline: |
| 1457 | pipeline = self.pipeline.name |
| 1458 | else: |
| 1459 | pipeline = None |
| 1460 | return '<QueueItem 0x%x for %s in %s>' % ( |
| 1461 | id(self), self.change, pipeline) |
| 1462 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1463 | def resetAllBuilds(self): |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 1464 | self.current_build_set = BuildSet(self) |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 1465 | self.layout = None |
James E. Blair | c945500 | 2017-09-06 09:22:19 -0700 | [diff] [blame] | 1466 | self.job_graph = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1467 | |
| 1468 | def addBuild(self, build): |
James E. Blair | 7e530ad | 2012-07-03 16:12:28 -0700 | [diff] [blame] | 1469 | self.current_build_set.addBuild(build) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 1470 | |
James E. Blair | 4a28a88 | 2013-08-23 15:17:33 -0700 | [diff] [blame] | 1471 | def removeBuild(self, build): |
| 1472 | self.current_build_set.removeBuild(build) |
| 1473 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1474 | def setReportedResult(self, result): |
| 1475 | self.current_build_set.result = result |
| 1476 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1477 | def freezeJobGraph(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1478 | """Find or create actual matching jobs for this item's change and |
| 1479 | store the resulting job tree.""" |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 1480 | job_graph = self.layout.createJobGraph(self) |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1481 | for job in job_graph.getJobs(): |
| 1482 | # Ensure that each jobs's dependencies are fully |
| 1483 | # accessible. This will raise an exception if not. |
| 1484 | job_graph.getParentJobsRecursively(job.name) |
| 1485 | self.job_graph = job_graph |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1486 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1487 | def hasJobGraph(self): |
| 1488 | """Returns True if the item has a job graph.""" |
| 1489 | return self.job_graph is not None |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1490 | |
| 1491 | def getJobs(self): |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1492 | if not self.live or not self.job_graph: |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1493 | return [] |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1494 | return self.job_graph.getJobs() |
| 1495 | |
| 1496 | def getJob(self, name): |
| 1497 | if not self.job_graph: |
| 1498 | return None |
| 1499 | return self.job_graph.jobs.get(name) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 1500 | |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1501 | def haveAllJobsStarted(self): |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1502 | if not self.hasJobGraph(): |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1503 | return False |
| 1504 | for job in self.getJobs(): |
| 1505 | build = self.current_build_set.getBuild(job.name) |
| 1506 | if not build or not build.start_time: |
| 1507 | return False |
| 1508 | return True |
| 1509 | |
| 1510 | def areAllJobsComplete(self): |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 1511 | if (self.current_build_set.config_error or |
| 1512 | self.current_build_set.unable_to_merge): |
| 1513 | return True |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1514 | if not self.hasJobGraph(): |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1515 | return False |
| 1516 | for job in self.getJobs(): |
| 1517 | build = self.current_build_set.getBuild(job.name) |
| 1518 | if not build or not build.result: |
| 1519 | return False |
| 1520 | return True |
| 1521 | |
| 1522 | def didAllJobsSucceed(self): |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1523 | if not self.hasJobGraph(): |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1524 | return False |
| 1525 | for job in self.getJobs(): |
| 1526 | if not job.voting: |
| 1527 | continue |
| 1528 | build = self.current_build_set.getBuild(job.name) |
| 1529 | if not build: |
| 1530 | return False |
| 1531 | if build.result != 'SUCCESS': |
| 1532 | return False |
| 1533 | return True |
| 1534 | |
| 1535 | def didAnyJobFail(self): |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1536 | if not self.hasJobGraph(): |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1537 | return False |
| 1538 | for job in self.getJobs(): |
| 1539 | if not job.voting: |
| 1540 | continue |
| 1541 | build = self.current_build_set.getBuild(job.name) |
| 1542 | if build and build.result and (build.result != 'SUCCESS'): |
| 1543 | return True |
| 1544 | return False |
| 1545 | |
| 1546 | def didMergerFail(self): |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 1547 | return self.current_build_set.unable_to_merge |
| 1548 | |
| 1549 | def getConfigError(self): |
| 1550 | return self.current_build_set.config_error |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1551 | |
James E. Blair | 0d3e83b | 2017-06-05 13:51:57 -0700 | [diff] [blame] | 1552 | def wasDequeuedNeedingChange(self): |
| 1553 | return self.dequeued_needing_change |
| 1554 | |
James E. Blair | 8c2d581 | 2017-10-06 09:29:21 -0700 | [diff] [blame] | 1555 | def includesConfigUpdates(self): |
| 1556 | includes_trusted = False |
| 1557 | includes_untrusted = False |
| 1558 | tenant = self.pipeline.layout.tenant |
| 1559 | item = self |
| 1560 | while item: |
| 1561 | if item.change.updatesConfig(): |
| 1562 | (trusted, project) = tenant.getProject( |
| 1563 | item.change.project.canonical_name) |
| 1564 | if trusted: |
| 1565 | includes_trusted = True |
| 1566 | else: |
| 1567 | includes_untrusted = True |
| 1568 | if includes_trusted and includes_untrusted: |
| 1569 | # We're done early |
| 1570 | return (includes_trusted, includes_untrusted) |
| 1571 | item = item.item_ahead |
| 1572 | return (includes_trusted, includes_untrusted) |
| 1573 | |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1574 | def isHoldingFollowingChanges(self): |
| 1575 | if not self.live: |
| 1576 | return False |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1577 | if not self.hasJobGraph(): |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1578 | return False |
| 1579 | for job in self.getJobs(): |
| 1580 | if not job.hold_following_changes: |
| 1581 | continue |
| 1582 | build = self.current_build_set.getBuild(job.name) |
| 1583 | if not build: |
| 1584 | return True |
| 1585 | if build.result != 'SUCCESS': |
| 1586 | return True |
| 1587 | |
| 1588 | if not self.item_ahead: |
| 1589 | return False |
| 1590 | return self.item_ahead.isHoldingFollowingChanges() |
| 1591 | |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 1592 | def findJobsToRun(self, semaphore_handler): |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1593 | torun = [] |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1594 | if not self.live: |
| 1595 | return [] |
| 1596 | if not self.job_graph: |
| 1597 | return [] |
James E. Blair | 791b539 | 2016-08-03 11:25:56 -0700 | [diff] [blame] | 1598 | if self.item_ahead: |
| 1599 | # Only run jobs if any 'hold' jobs on the change ahead |
| 1600 | # have completed successfully. |
| 1601 | if self.item_ahead.isHoldingFollowingChanges(): |
| 1602 | return [] |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1603 | |
| 1604 | successful_job_names = set() |
| 1605 | jobs_not_started = set() |
| 1606 | for job in self.job_graph.getJobs(): |
| 1607 | build = self.current_build_set.getBuild(job.name) |
| 1608 | if build: |
| 1609 | if build.result == 'SUCCESS': |
| 1610 | successful_job_names.add(job.name) |
| 1611 | else: |
| 1612 | jobs_not_started.add(job) |
| 1613 | |
James E. Blair | 698703c | 2017-09-15 20:58:30 -0600 | [diff] [blame] | 1614 | # Attempt to run jobs in the order they appear in |
| 1615 | # configuration. |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1616 | for job in self.job_graph.getJobs(): |
| 1617 | if job not in jobs_not_started: |
| 1618 | continue |
| 1619 | all_parent_jobs_successful = True |
Tobias Henkel | a96c9b3 | 2017-10-22 12:38:06 +0200 | [diff] [blame] | 1620 | parent_builds_with_data = {} |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1621 | for parent_job in self.job_graph.getParentJobsRecursively( |
| 1622 | job.name): |
| 1623 | if parent_job.name not in successful_job_names: |
| 1624 | all_parent_jobs_successful = False |
| 1625 | break |
James E. Blair | 698703c | 2017-09-15 20:58:30 -0600 | [diff] [blame] | 1626 | parent_build = self.current_build_set.getBuild(parent_job.name) |
| 1627 | if parent_build.result_data: |
Tobias Henkel | a96c9b3 | 2017-10-22 12:38:06 +0200 | [diff] [blame] | 1628 | parent_builds_with_data[parent_job.name] = parent_build |
| 1629 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1630 | if all_parent_jobs_successful: |
Tobias Henkel | a96c9b3 | 2017-10-22 12:38:06 +0200 | [diff] [blame] | 1631 | # Iterate in reverse order over all jobs of the graph (which is |
| 1632 | # in sorted config order) and apply parent data of the jobs we |
| 1633 | # already found. |
| 1634 | if len(parent_builds_with_data) > 0: |
| 1635 | for parent_job in reversed(self.job_graph.getJobs()): |
| 1636 | parent_build = parent_builds_with_data.get( |
| 1637 | parent_job.name) |
| 1638 | if parent_build: |
| 1639 | job.updateParentData(parent_build.result_data) |
| 1640 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1641 | nodeset = self.current_build_set.getJobNodeSet(job.name) |
| 1642 | if nodeset is None: |
| 1643 | # The nodes for this job are not ready, skip |
| 1644 | # it for now. |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1645 | continue |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 1646 | if semaphore_handler.acquire(self, job): |
| 1647 | # If this job needs a semaphore, either acquire it or |
| 1648 | # make sure that we have it before running the job. |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1649 | torun.append(job) |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1650 | return torun |
| 1651 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1652 | def findJobsToRequest(self): |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 1653 | build_set = self.current_build_set |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1654 | toreq = [] |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1655 | if not self.live: |
| 1656 | return [] |
| 1657 | if not self.job_graph: |
| 1658 | return [] |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 1659 | if self.item_ahead: |
| 1660 | if self.item_ahead.isHoldingFollowingChanges(): |
| 1661 | return [] |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1662 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1663 | successful_job_names = set() |
| 1664 | jobs_not_requested = set() |
| 1665 | for job in self.job_graph.getJobs(): |
| 1666 | build = build_set.getBuild(job.name) |
| 1667 | if build and build.result == 'SUCCESS': |
| 1668 | successful_job_names.add(job.name) |
| 1669 | else: |
| 1670 | nodeset = build_set.getJobNodeSet(job.name) |
| 1671 | if nodeset is None: |
| 1672 | req = build_set.getJobNodeRequest(job.name) |
| 1673 | if req is None: |
| 1674 | jobs_not_requested.add(job) |
| 1675 | |
| 1676 | # Attempt to request nodes for jobs in the order jobs appear |
| 1677 | # in configuration. |
| 1678 | for job in self.job_graph.getJobs(): |
| 1679 | if job not in jobs_not_requested: |
| 1680 | continue |
| 1681 | all_parent_jobs_successful = True |
| 1682 | for parent_job in self.job_graph.getParentJobsRecursively( |
| 1683 | job.name): |
| 1684 | if parent_job.name not in successful_job_names: |
| 1685 | all_parent_jobs_successful = False |
| 1686 | break |
| 1687 | if all_parent_jobs_successful: |
| 1688 | toreq.append(job) |
| 1689 | return toreq |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1690 | |
| 1691 | def setResult(self, build): |
| 1692 | if build.retry: |
| 1693 | self.removeBuild(build) |
| 1694 | elif build.result != 'SUCCESS': |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 1695 | for job in self.job_graph.getDependentJobsRecursively( |
| 1696 | build.job.name): |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1697 | fakebuild = Build(job, None) |
| 1698 | fakebuild.result = 'SKIPPED' |
| 1699 | self.addBuild(fakebuild) |
| 1700 | |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 1701 | def setNodeRequestFailure(self, job): |
| 1702 | fakebuild = Build(job, None) |
| 1703 | self.addBuild(fakebuild) |
| 1704 | fakebuild.result = 'NODE_FAILURE' |
| 1705 | self.setResult(fakebuild) |
| 1706 | |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1707 | def setDequeuedNeedingChange(self): |
| 1708 | self.dequeued_needing_change = True |
| 1709 | self._setAllJobsSkipped() |
| 1710 | |
| 1711 | def setUnableToMerge(self): |
| 1712 | self.current_build_set.unable_to_merge = True |
| 1713 | self._setAllJobsSkipped() |
| 1714 | |
James E. Blair | e53250c | 2017-03-01 14:34:36 -0800 | [diff] [blame] | 1715 | def setConfigError(self, error): |
| 1716 | self.current_build_set.config_error = error |
| 1717 | self._setAllJobsSkipped() |
| 1718 | |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1719 | def _setAllJobsSkipped(self): |
| 1720 | for job in self.getJobs(): |
| 1721 | fakebuild = Build(job, None) |
| 1722 | fakebuild.result = 'SKIPPED' |
| 1723 | self.addBuild(fakebuild) |
| 1724 | |
Joshua Hesketh | c0c012b | 2017-03-28 08:45:50 +1100 | [diff] [blame] | 1725 | def formatUrlPattern(self, url_pattern, job=None, build=None): |
| 1726 | url = None |
| 1727 | # Produce safe versions of objects which may be useful in |
| 1728 | # result formatting, but don't allow users to crawl through |
| 1729 | # the entire data structure where they might be able to access |
| 1730 | # secrets, etc. |
| 1731 | safe_change = self.change.getSafeAttributes() |
| 1732 | safe_pipeline = self.pipeline.getSafeAttributes() |
Jamie Lennox | bf997c8 | 2017-05-10 09:58:40 +1000 | [diff] [blame] | 1733 | safe_tenant = self.pipeline.layout.tenant.getSafeAttributes() |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 1734 | safe_buildset = self.current_build_set.getSafeAttributes() |
K Jonathan Harker | a7f390c | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 1735 | safe_job = job.getSafeAttributes() if job else {} |
| 1736 | safe_build = build.getSafeAttributes() if build else {} |
Joshua Hesketh | c0c012b | 2017-03-28 08:45:50 +1100 | [diff] [blame] | 1737 | try: |
| 1738 | url = url_pattern.format(change=safe_change, |
| 1739 | pipeline=safe_pipeline, |
Jamie Lennox | bf997c8 | 2017-05-10 09:58:40 +1000 | [diff] [blame] | 1740 | tenant=safe_tenant, |
Jamie Lennox | 3f16de5 | 2017-05-09 14:24:11 +1000 | [diff] [blame] | 1741 | buildset=safe_buildset, |
Joshua Hesketh | c0c012b | 2017-03-28 08:45:50 +1100 | [diff] [blame] | 1742 | job=safe_job, |
| 1743 | build=safe_build) |
| 1744 | except KeyError as e: |
| 1745 | self.log.error("Error while formatting url for job %s: unknown " |
| 1746 | "key %s in pattern %s" |
Clint Byrum | e0093db | 2017-05-10 20:53:43 -0700 | [diff] [blame] | 1747 | % (job, e.args[0], url_pattern)) |
Joshua Hesketh | c0c012b | 2017-03-28 08:45:50 +1100 | [diff] [blame] | 1748 | except AttributeError as e: |
| 1749 | self.log.error("Error while formatting url for job %s: unknown " |
| 1750 | "attribute %s in pattern %s" |
Clint Byrum | e0093db | 2017-05-10 20:53:43 -0700 | [diff] [blame] | 1751 | % (job, e.args[0], url_pattern)) |
Joshua Hesketh | c0c012b | 2017-03-28 08:45:50 +1100 | [diff] [blame] | 1752 | except Exception: |
| 1753 | self.log.exception("Error while formatting url for job %s with " |
| 1754 | "pattern %s:" % (job, url_pattern)) |
| 1755 | |
| 1756 | return url |
| 1757 | |
James E. Blair | 800e7ff | 2017-03-17 16:06:52 -0700 | [diff] [blame] | 1758 | def formatJobResult(self, job): |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1759 | build = self.current_build_set.getBuild(job.name) |
| 1760 | result = build.result |
James E. Blair | 800e7ff | 2017-03-17 16:06:52 -0700 | [diff] [blame] | 1761 | pattern = None |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1762 | if result == 'SUCCESS': |
| 1763 | if job.success_message: |
| 1764 | result = job.success_message |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 1765 | if job.success_url: |
| 1766 | pattern = job.success_url |
Tobias Henkel | 077f2f3 | 2017-05-30 20:16:46 +0200 | [diff] [blame] | 1767 | else: |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1768 | if job.failure_message: |
| 1769 | result = job.failure_message |
James E. Blair | a5dba23 | 2016-08-08 15:53:24 -0700 | [diff] [blame] | 1770 | if job.failure_url: |
| 1771 | pattern = job.failure_url |
James E. Blair | 88e79c0 | 2017-07-07 13:36:54 -0700 | [diff] [blame] | 1772 | url = None # The final URL |
| 1773 | default_url = build.result_data.get('zuul', {}).get('log_url') |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1774 | if pattern: |
James E. Blair | 88e79c0 | 2017-07-07 13:36:54 -0700 | [diff] [blame] | 1775 | job_url = self.formatUrlPattern(pattern, job, build) |
| 1776 | else: |
| 1777 | job_url = None |
| 1778 | try: |
| 1779 | if job_url: |
| 1780 | u = urllib.parse.urlparse(job_url) |
| 1781 | if u.scheme: |
| 1782 | # The job success or failure url is absolute, so it's |
| 1783 | # our final url. |
| 1784 | url = job_url |
| 1785 | else: |
| 1786 | # We have a relative job url. Combine it with our |
| 1787 | # default url. |
| 1788 | if default_url: |
| 1789 | url = urllib.parse.urljoin(default_url, job_url) |
| 1790 | except Exception: |
| 1791 | self.log.exception("Error while parsing url for job %s:" |
| 1792 | % (job,)) |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1793 | if not url: |
James E. Blair | 88e79c0 | 2017-07-07 13:36:54 -0700 | [diff] [blame] | 1794 | url = default_url or build.url or job.name |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1795 | return (result, url) |
| 1796 | |
Tobias Henkel | b4407fc | 2017-07-07 13:52:56 +0200 | [diff] [blame] | 1797 | def formatJSON(self, websocket_url=None): |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1798 | ret = {} |
| 1799 | ret['active'] = self.active |
James E. Blair | 107c385 | 2015-02-07 08:23:10 -0800 | [diff] [blame] | 1800 | ret['live'] = self.live |
Monty Taylor | 55d0f56 | 2017-05-17 14:30:21 -0500 | [diff] [blame] | 1801 | if hasattr(self.change, 'url') and self.change.url is not None: |
| 1802 | ret['url'] = self.change.url |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1803 | else: |
| 1804 | ret['url'] = None |
Monty Taylor | 55d0f56 | 2017-05-17 14:30:21 -0500 | [diff] [blame] | 1805 | ret['id'] = self.change._id() |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1806 | if self.item_ahead: |
| 1807 | ret['item_ahead'] = self.item_ahead.change._id() |
| 1808 | else: |
| 1809 | ret['item_ahead'] = None |
| 1810 | ret['items_behind'] = [i.change._id() for i in self.items_behind] |
| 1811 | ret['failing_reasons'] = self.current_build_set.failing_reasons |
| 1812 | ret['zuul_ref'] = self.current_build_set.ref |
Monty Taylor | 55d0f56 | 2017-05-17 14:30:21 -0500 | [diff] [blame] | 1813 | if self.change.project: |
| 1814 | ret['project'] = self.change.project.name |
Ramy Asselin | 07cc33c | 2015-06-12 14:06:34 -0700 | [diff] [blame] | 1815 | else: |
| 1816 | # For cross-project dependencies with the depends-on |
| 1817 | # project not known to zuul, the project is None |
| 1818 | # Set it to a static value |
| 1819 | ret['project'] = "Unknown Project" |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1820 | ret['enqueue_time'] = int(self.enqueue_time * 1000) |
| 1821 | ret['jobs'] = [] |
Monty Taylor | 55d0f56 | 2017-05-17 14:30:21 -0500 | [diff] [blame] | 1822 | if hasattr(self.change, 'owner'): |
| 1823 | ret['owner'] = self.change.owner |
Davanum Srinivas | b6bfbcc | 2014-11-18 13:26:52 -0500 | [diff] [blame] | 1824 | else: |
| 1825 | ret['owner'] = None |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1826 | max_remaining = 0 |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1827 | for job in self.getJobs(): |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1828 | now = time.time() |
| 1829 | build = self.current_build_set.getBuild(job.name) |
| 1830 | elapsed = None |
| 1831 | remaining = None |
| 1832 | result = None |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1833 | build_url = None |
Tobias Henkel | b4407fc | 2017-07-07 13:52:56 +0200 | [diff] [blame] | 1834 | finger_url = None |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1835 | report_url = None |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1836 | worker = None |
| 1837 | if build: |
| 1838 | result = build.result |
Tobias Henkel | b4407fc | 2017-07-07 13:52:56 +0200 | [diff] [blame] | 1839 | finger_url = build.url |
| 1840 | # TODO(tobiash): add support for custom web root |
| 1841 | urlformat = 'static/stream.html?' \ |
| 1842 | 'uuid={build.uuid}&' \ |
| 1843 | 'logfile=console.log' |
| 1844 | if websocket_url: |
| 1845 | urlformat += '&websocket_url={websocket_url}' |
| 1846 | build_url = urlformat.format( |
| 1847 | build=build, websocket_url=websocket_url) |
James E. Blair | 800e7ff | 2017-03-17 16:06:52 -0700 | [diff] [blame] | 1848 | (unused, report_url) = self.formatJobResult(job) |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1849 | if build.start_time: |
| 1850 | if build.end_time: |
| 1851 | elapsed = int((build.end_time - |
| 1852 | build.start_time) * 1000) |
| 1853 | remaining = 0 |
| 1854 | else: |
| 1855 | elapsed = int((now - build.start_time) * 1000) |
| 1856 | if build.estimated_time: |
| 1857 | remaining = max( |
| 1858 | int(build.estimated_time * 1000) - elapsed, |
| 1859 | 0) |
| 1860 | worker = { |
| 1861 | 'name': build.worker.name, |
| 1862 | 'hostname': build.worker.hostname, |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1863 | } |
| 1864 | if remaining and remaining > max_remaining: |
| 1865 | max_remaining = remaining |
| 1866 | |
| 1867 | ret['jobs'].append({ |
| 1868 | 'name': job.name, |
Tobias Henkel | 65639f8 | 2017-07-10 10:25:42 +0200 | [diff] [blame] | 1869 | 'dependencies': list(job.dependencies), |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1870 | 'elapsed_time': elapsed, |
| 1871 | 'remaining_time': remaining, |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1872 | 'url': build_url, |
Tobias Henkel | b4407fc | 2017-07-07 13:52:56 +0200 | [diff] [blame] | 1873 | 'finger_url': finger_url, |
James E. Blair | b7273ef | 2016-04-19 08:58:51 -0700 | [diff] [blame] | 1874 | 'report_url': report_url, |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1875 | 'result': result, |
| 1876 | 'voting': job.voting, |
| 1877 | 'uuid': build.uuid if build else None, |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 1878 | 'execute_time': build.execute_time if build else None, |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1879 | 'start_time': build.start_time if build else None, |
| 1880 | 'end_time': build.end_time if build else None, |
| 1881 | 'estimated_time': build.estimated_time if build else None, |
| 1882 | 'pipeline': build.pipeline.name if build else None, |
| 1883 | 'canceled': build.canceled if build else None, |
| 1884 | 'retry': build.retry if build else None, |
Timothy Chavez | b233208 | 2015-08-07 20:08:04 -0500 | [diff] [blame] | 1885 | 'node_labels': build.node_labels if build else [], |
| 1886 | 'node_name': build.node_name if build else None, |
| 1887 | 'worker': worker, |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1888 | }) |
| 1889 | |
James E. Blair | dbfd328 | 2016-07-21 10:46:19 -0700 | [diff] [blame] | 1890 | if self.haveAllJobsStarted(): |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1891 | ret['remaining_time'] = max_remaining |
| 1892 | else: |
| 1893 | ret['remaining_time'] = None |
| 1894 | return ret |
| 1895 | |
| 1896 | def formatStatus(self, indent=0, html=False): |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1897 | indent_str = ' ' * indent |
| 1898 | ret = '' |
Monty Taylor | 55d0f56 | 2017-05-17 14:30:21 -0500 | [diff] [blame] | 1899 | if html and getattr(self.change, 'url', None) is not None: |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1900 | ret += '%sProject %s change <a href="%s">%s</a>\n' % ( |
| 1901 | indent_str, |
Monty Taylor | 55d0f56 | 2017-05-17 14:30:21 -0500 | [diff] [blame] | 1902 | self.change.project.name, |
| 1903 | self.change.url, |
| 1904 | self.change._id()) |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1905 | else: |
| 1906 | ret += '%sProject %s change %s based on %s\n' % ( |
| 1907 | indent_str, |
Monty Taylor | 55d0f56 | 2017-05-17 14:30:21 -0500 | [diff] [blame] | 1908 | self.change.project.name, |
| 1909 | self.change._id(), |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1910 | self.item_ahead) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 1911 | for job in self.getJobs(): |
Joshua Hesketh | 85af4e9 | 2014-02-21 08:28:58 -0800 | [diff] [blame] | 1912 | build = self.current_build_set.getBuild(job.name) |
| 1913 | if build: |
| 1914 | result = build.result |
| 1915 | else: |
| 1916 | result = None |
| 1917 | job_name = job.name |
| 1918 | if not job.voting: |
| 1919 | voting = ' (non-voting)' |
| 1920 | else: |
| 1921 | voting = '' |
| 1922 | if html: |
| 1923 | if build: |
| 1924 | url = build.url |
| 1925 | else: |
| 1926 | url = None |
| 1927 | if url is not None: |
| 1928 | job_name = '<a href="%s">%s</a>' % (url, job_name) |
| 1929 | ret += '%s %s: %s%s' % (indent_str, job_name, result, voting) |
| 1930 | ret += '\n' |
| 1931 | return ret |
| 1932 | |
James E. Blair | a04b079 | 2017-04-27 09:59:06 -0700 | [diff] [blame] | 1933 | def makeMergerItem(self): |
| 1934 | # Create a dictionary with all info about the item needed by |
| 1935 | # the merger. |
| 1936 | number = None |
| 1937 | patchset = None |
| 1938 | oldrev = None |
| 1939 | newrev = None |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 1940 | branch = None |
James E. Blair | a04b079 | 2017-04-27 09:59:06 -0700 | [diff] [blame] | 1941 | if hasattr(self.change, 'number'): |
| 1942 | number = self.change.number |
| 1943 | patchset = self.change.patchset |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 1944 | if hasattr(self.change, 'newrev'): |
James E. Blair | a04b079 | 2017-04-27 09:59:06 -0700 | [diff] [blame] | 1945 | oldrev = self.change.oldrev |
| 1946 | newrev = self.change.newrev |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 1947 | if hasattr(self.change, 'branch'): |
| 1948 | branch = self.change.branch |
| 1949 | |
James E. Blair | a04b079 | 2017-04-27 09:59:06 -0700 | [diff] [blame] | 1950 | source = self.change.project.source |
| 1951 | connection_name = source.connection.connection_name |
James E. Blair | 2a53567 | 2017-04-27 12:03:15 -0700 | [diff] [blame] | 1952 | project = self.change.project |
James E. Blair | a04b079 | 2017-04-27 09:59:06 -0700 | [diff] [blame] | 1953 | |
James E. Blair | 2a53567 | 2017-04-27 12:03:15 -0700 | [diff] [blame] | 1954 | return dict(project=project.name, |
| 1955 | connection=connection_name, |
James E. Blair | a04b079 | 2017-04-27 09:59:06 -0700 | [diff] [blame] | 1956 | merge_mode=self.current_build_set.getMergeMode(), |
James E. Blair | 247cab7 | 2017-07-20 16:52:36 -0700 | [diff] [blame] | 1957 | ref=self.change.ref, |
James E. Blair | a04b079 | 2017-04-27 09:59:06 -0700 | [diff] [blame] | 1958 | branch=branch, |
James E. Blair | 247cab7 | 2017-07-20 16:52:36 -0700 | [diff] [blame] | 1959 | buildset_uuid=self.current_build_set.uuid, |
James E. Blair | a04b079 | 2017-04-27 09:59:06 -0700 | [diff] [blame] | 1960 | number=number, |
| 1961 | patchset=patchset, |
| 1962 | oldrev=oldrev, |
| 1963 | newrev=newrev, |
| 1964 | ) |
| 1965 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1966 | |
Clint Byrum | f8cc990 | 2017-03-22 22:38:25 -0700 | [diff] [blame] | 1967 | class Ref(object): |
| 1968 | """An existing state of a Project.""" |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1969 | |
| 1970 | def __init__(self, project): |
| 1971 | self.project = project |
Clint Byrum | f8cc990 | 2017-03-22 22:38:25 -0700 | [diff] [blame] | 1972 | self.ref = None |
| 1973 | self.oldrev = None |
| 1974 | self.newrev = None |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 1975 | self.files = [] |
| 1976 | |
Clint Byrum | f8cc990 | 2017-03-22 22:38:25 -0700 | [diff] [blame] | 1977 | def _id(self): |
| 1978 | return self.newrev |
| 1979 | |
| 1980 | def __repr__(self): |
| 1981 | rep = None |
| 1982 | if self.newrev == '0000000000000000000000000000000000000000': |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 1983 | rep = '<%s 0x%x deletes %s from %s' % ( |
| 1984 | type(self).__name__, |
| 1985 | id(self), self.ref, self.oldrev) |
Clint Byrum | f8cc990 | 2017-03-22 22:38:25 -0700 | [diff] [blame] | 1986 | elif self.oldrev == '0000000000000000000000000000000000000000': |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 1987 | rep = '<%s 0x%x creates %s on %s>' % ( |
| 1988 | type(self).__name__, |
| 1989 | id(self), self.ref, self.newrev) |
Clint Byrum | f8cc990 | 2017-03-22 22:38:25 -0700 | [diff] [blame] | 1990 | else: |
| 1991 | # Catch all |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 1992 | rep = '<%s 0x%x %s updated %s..%s>' % ( |
| 1993 | type(self).__name__, |
| 1994 | id(self), self.ref, self.oldrev, self.newrev) |
Clint Byrum | f8cc990 | 2017-03-22 22:38:25 -0700 | [diff] [blame] | 1995 | return rep |
| 1996 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 1997 | def equals(self, other): |
Clint Byrum | f8cc990 | 2017-03-22 22:38:25 -0700 | [diff] [blame] | 1998 | if (self.project == other.project |
| 1999 | and self.ref == other.ref |
| 2000 | and self.newrev == other.newrev): |
| 2001 | return True |
| 2002 | return False |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 2003 | |
| 2004 | def isUpdateOf(self, other): |
Clint Byrum | f8cc990 | 2017-03-22 22:38:25 -0700 | [diff] [blame] | 2005 | return False |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 2006 | |
| 2007 | def filterJobs(self, jobs): |
| 2008 | return filter(lambda job: job.changeMatches(self), jobs) |
| 2009 | |
| 2010 | def getRelatedChanges(self): |
| 2011 | return set() |
| 2012 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2013 | def updatesConfig(self): |
Tristan Cacqueray | 829e617 | 2017-06-13 06:49:36 +0000 | [diff] [blame] | 2014 | if 'zuul.yaml' in self.files or '.zuul.yaml' in self.files or \ |
| 2015 | [True for fn in self.files if fn.startswith("zuul.d/") or |
| 2016 | fn.startswith(".zuul.d/")]: |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 2017 | return True |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2018 | return False |
| 2019 | |
Joshua Hesketh | 58419cb | 2017-02-24 13:09:22 -0500 | [diff] [blame] | 2020 | def getSafeAttributes(self): |
| 2021 | return Attributes(project=self.project, |
| 2022 | ref=self.ref, |
| 2023 | oldrev=self.oldrev, |
| 2024 | newrev=self.newrev) |
| 2025 | |
James E. Blair | 9e5b811 | 2017-10-19 08:12:24 -0700 | [diff] [blame] | 2026 | def toDict(self): |
| 2027 | # Render to a dict to use in passing json to the executor |
| 2028 | d = dict() |
| 2029 | d['project'] = dict( |
| 2030 | name=self.project.name, |
| 2031 | short_name=self.project.name.split('/')[-1], |
| 2032 | canonical_hostname=self.project.canonical_hostname, |
| 2033 | canonical_name=self.project.canonical_name, |
| 2034 | src_dir=os.path.join('src', self.project.canonical_name), |
| 2035 | ) |
| 2036 | return d |
| 2037 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 2038 | |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 2039 | class Branch(Ref): |
| 2040 | """An existing branch state for a Project.""" |
| 2041 | def __init__(self, project): |
| 2042 | super(Branch, self).__init__(project) |
| 2043 | self.branch = None |
| 2044 | |
James E. Blair | 9e5b811 | 2017-10-19 08:12:24 -0700 | [diff] [blame] | 2045 | def toDict(self): |
| 2046 | # Render to a dict to use in passing json to the executor |
| 2047 | d = super(Branch, self).toDict() |
| 2048 | d['branch'] = self.branch |
| 2049 | return d |
| 2050 | |
James E. Blair | 2103778 | 2017-07-19 11:56:55 -0700 | [diff] [blame] | 2051 | |
| 2052 | class Tag(Ref): |
| 2053 | """An existing tag state for a Project.""" |
| 2054 | def __init__(self, project): |
| 2055 | super(Tag, self).__init__(project) |
| 2056 | self.tag = None |
| 2057 | |
| 2058 | |
| 2059 | class Change(Branch): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 2060 | """A proposed new state for a Project.""" |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2061 | def __init__(self, project): |
| 2062 | super(Change, self).__init__(project) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2063 | self.number = None |
| 2064 | self.url = None |
| 2065 | self.patchset = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2066 | |
James E. Blair | 6965a4b | 2014-12-16 17:19:04 -0800 | [diff] [blame] | 2067 | self.needs_changes = [] |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2068 | self.needed_by_changes = [] |
| 2069 | self.is_current_patchset = True |
| 2070 | self.can_merge = False |
| 2071 | self.is_merged = False |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 2072 | self.failed_to_merge = False |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 2073 | self.open = None |
| 2074 | self.status = None |
Davanum Srinivas | b6bfbcc | 2014-11-18 13:26:52 -0500 | [diff] [blame] | 2075 | self.owner = None |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2076 | |
Jan Hruban | 3b41592 | 2016-02-03 13:10:22 +0100 | [diff] [blame] | 2077 | self.source_event = None |
| 2078 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2079 | def _id(self): |
James E. Blair | be765db | 2012-08-07 08:36:20 -0700 | [diff] [blame] | 2080 | return '%s,%s' % (self.number, self.patchset) |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2081 | |
| 2082 | def __repr__(self): |
| 2083 | return '<Change 0x%x %s>' % (id(self), self._id()) |
| 2084 | |
| 2085 | def equals(self, other): |
Zhongyue Luo | aa85ebf | 2012-09-21 16:38:33 +0800 | [diff] [blame] | 2086 | if self.number == other.number and self.patchset == other.patchset: |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2087 | return True |
| 2088 | return False |
| 2089 | |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2090 | def isUpdateOf(self, other): |
Clark Boylan | 0197624 | 2013-02-17 18:41:48 -0800 | [diff] [blame] | 2091 | if ((hasattr(other, 'number') and self.number == other.number) and |
James E. Blair | 7a192e4 | 2013-07-11 14:10:36 -0700 | [diff] [blame] | 2092 | (hasattr(other, 'patchset') and |
| 2093 | self.patchset is not None and |
| 2094 | other.patchset is not None and |
| 2095 | int(self.patchset) > int(other.patchset))): |
James E. Blair | 2fa5096 | 2013-01-30 21:50:41 -0800 | [diff] [blame] | 2096 | return True |
| 2097 | return False |
| 2098 | |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 2099 | def getRelatedChanges(self): |
| 2100 | related = set() |
James E. Blair | 6965a4b | 2014-12-16 17:19:04 -0800 | [diff] [blame] | 2101 | for c in self.needs_changes: |
| 2102 | related.add(c) |
James E. Blair | fee8d65 | 2013-06-07 08:57:52 -0700 | [diff] [blame] | 2103 | for c in self.needed_by_changes: |
| 2104 | related.add(c) |
| 2105 | related.update(c.getRelatedChanges()) |
| 2106 | return related |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2107 | |
Joshua Hesketh | 58419cb | 2017-02-24 13:09:22 -0500 | [diff] [blame] | 2108 | def getSafeAttributes(self): |
| 2109 | return Attributes(project=self.project, |
| 2110 | number=self.number, |
| 2111 | patchset=self.patchset) |
| 2112 | |
James E. Blair | 9e5b811 | 2017-10-19 08:12:24 -0700 | [diff] [blame] | 2113 | def toDict(self): |
| 2114 | # Render to a dict to use in passing json to the executor |
| 2115 | d = super(Change, self).toDict() |
| 2116 | d['change'] = str(self.number) |
| 2117 | d['change_url'] = self.url |
| 2118 | d['patchset'] = str(self.patchset) |
| 2119 | return d |
| 2120 | |
James E. Blair | 4aea70c | 2012-07-26 14:23:24 -0700 | [diff] [blame] | 2121 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2122 | class TriggerEvent(object): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 2123 | """Incoming event from an external system.""" |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2124 | def __init__(self): |
James E. Blair | aad3ae2 | 2017-05-18 14:11:29 -0700 | [diff] [blame] | 2125 | # TODO(jeblair): further reduce this list |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2126 | self.data = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 2127 | # common |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2128 | self.type = None |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 2129 | self.branch_updated = False |
James E. Blair | 72facdc | 2017-08-17 10:29:12 -0700 | [diff] [blame] | 2130 | self.branch_created = False |
| 2131 | self.branch_deleted = False |
James E. Blair | 247cab7 | 2017-07-20 16:52:36 -0700 | [diff] [blame] | 2132 | self.ref = None |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 2133 | # For management events (eg: enqueue / promote) |
| 2134 | self.tenant_name = None |
James E. Blair | 6f284b4 | 2017-03-31 14:14:41 -0700 | [diff] [blame] | 2135 | self.project_hostname = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2136 | self.project_name = None |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 2137 | self.trigger_name = None |
Antoine Musso | b4e809e | 2012-12-06 16:58:06 +0100 | [diff] [blame] | 2138 | # Representation of the user account that performed the event. |
| 2139 | self.account = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 2140 | # patchset-created, comment-added, etc. |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2141 | self.change_number = None |
Clark Boylan | fc56df3 | 2012-06-28 15:25:57 -0700 | [diff] [blame] | 2142 | self.change_url = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2143 | self.patch_number = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2144 | self.branch = None |
Clark Boylan | b9bcb40 | 2012-06-29 17:44:05 -0700 | [diff] [blame] | 2145 | self.comment = None |
Jesse Keating | 5c05a9f | 2017-01-12 14:44:58 -0800 | [diff] [blame] | 2146 | self.state = None |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 2147 | # ref-updated |
James E. Blair | 3266340 | 2012-06-01 10:04:18 -0700 | [diff] [blame] | 2148 | self.oldrev = None |
James E. Blair | 89cae0f | 2012-07-18 11:18:32 -0700 | [diff] [blame] | 2149 | self.newrev = None |
James E. Blair | ad28e91 | 2013-11-27 10:43:22 -0800 | [diff] [blame] | 2150 | # For events that arrive with a destination pipeline (eg, from |
| 2151 | # an admin command, etc): |
| 2152 | self.forced_pipeline = None |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2153 | |
James E. Blair | 6f284b4 | 2017-03-31 14:14:41 -0700 | [diff] [blame] | 2154 | @property |
| 2155 | def canonical_project_name(self): |
| 2156 | return self.project_hostname + '/' + self.project_name |
| 2157 | |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 2158 | def isPatchsetCreated(self): |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 2159 | return False |
| 2160 | |
| 2161 | def isChangeAbandoned(self): |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 2162 | return False |
| 2163 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 2164 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 2165 | class BaseFilter(object): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 2166 | """Base Class for filtering which Changes and Events to process.""" |
James E. Blair | aad3ae2 | 2017-05-18 14:11:29 -0700 | [diff] [blame] | 2167 | pass |
Joshua Hesketh | 66c8e52 | 2014-06-26 15:30:08 +1000 | [diff] [blame] | 2168 | |
James E. Blair | 9c17dbf | 2014-06-23 14:21:58 -0700 | [diff] [blame] | 2169 | |
| 2170 | class EventFilter(BaseFilter): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 2171 | """Allows a Pipeline to only respond to certain events.""" |
James E. Blair | aad3ae2 | 2017-05-18 14:11:29 -0700 | [diff] [blame] | 2172 | def __init__(self, trigger): |
| 2173 | super(EventFilter, self).__init__() |
James E. Blair | c0dedf8 | 2014-08-06 09:37:52 -0700 | [diff] [blame] | 2174 | self.trigger = trigger |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2175 | |
James E. Blair | aad3ae2 | 2017-05-18 14:11:29 -0700 | [diff] [blame] | 2176 | def matches(self, event, ref): |
| 2177 | # TODO(jeblair): consider removing ref argument |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 2178 | return True |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 2179 | |
| 2180 | |
James E. Blair | aad3ae2 | 2017-05-18 14:11:29 -0700 | [diff] [blame] | 2181 | class RefFilter(BaseFilter): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 2182 | """Allows a Manager to only enqueue Changes that meet certain criteria.""" |
Jesse Keating | 8c2eb57 | 2017-05-30 17:31:45 -0700 | [diff] [blame] | 2183 | def __init__(self, connection_name): |
James E. Blair | aad3ae2 | 2017-05-18 14:11:29 -0700 | [diff] [blame] | 2184 | super(RefFilter, self).__init__() |
Jesse Keating | 8c2eb57 | 2017-05-30 17:31:45 -0700 | [diff] [blame] | 2185 | self.connection_name = connection_name |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 2186 | |
| 2187 | def matches(self, change): |
James E. Blair | 11041d2 | 2014-05-02 14:49:53 -0700 | [diff] [blame] | 2188 | return True |
| 2189 | |
| 2190 | |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2191 | class ProjectPipelineConfig(object): |
| 2192 | # Represents a project cofiguration in the context of a pipeline |
| 2193 | def __init__(self): |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 2194 | self.job_list = JobList() |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2195 | self.queue_name = None |
Adam Gandelman | 8bd5710 | 2016-12-02 12:58:42 -0800 | [diff] [blame] | 2196 | self.merge_mode = None |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2197 | |
| 2198 | |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2199 | class TenantProjectConfig(object): |
| 2200 | """A project in the context of a tenant. |
| 2201 | |
| 2202 | A Project is globally unique in the system, however, when used in |
| 2203 | a tenant, some metadata about the project local to the tenant is |
| 2204 | stored in a TenantProjectConfig. |
| 2205 | """ |
| 2206 | |
| 2207 | def __init__(self, project): |
| 2208 | self.project = project |
| 2209 | self.load_classes = set() |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 2210 | self.shadow_projects = set() |
James E. Blair | daaf326 | 2017-10-23 13:51:48 -0700 | [diff] [blame] | 2211 | self.branches = [] |
Tobias Henkel | eca4620 | 2017-08-02 20:27:10 +0200 | [diff] [blame] | 2212 | # The tenant's default setting of exclude_unprotected_branches will |
| 2213 | # be overridden by this one if not None. |
| 2214 | self.exclude_unprotected_branches = None |
| 2215 | |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2216 | |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2217 | class ProjectConfig(object): |
| 2218 | # Represents a project cofiguration |
James E. Blair | 2a66450 | 2017-10-27 11:39:33 -0700 | [diff] [blame] | 2219 | def __init__(self, name, source_context=None): |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2220 | self.name = name |
James E. Blair | 2a66450 | 2017-10-27 11:39:33 -0700 | [diff] [blame] | 2221 | # If this is a template, it will have a source_context, but |
| 2222 | # not if it is a project definition. |
| 2223 | self.source_context = source_context |
Adam Gandelman | 8bd5710 | 2016-12-02 12:58:42 -0800 | [diff] [blame] | 2224 | self.merge_mode = None |
James E. Blair | e74f571 | 2017-09-29 15:14:31 -0700 | [diff] [blame] | 2225 | # The default branch for the project (usually master). |
James E. Blair | 040b650 | 2017-05-23 10:18:21 -0700 | [diff] [blame] | 2226 | self.default_branch = None |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2227 | self.pipelines = {} |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 2228 | self.private_key_file = None |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2229 | |
| 2230 | |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2231 | class ConfigItemNotListError(Exception): |
| 2232 | def __init__(self): |
| 2233 | message = textwrap.dedent("""\ |
| 2234 | Configuration file is not a list. Each zuul.yaml configuration |
| 2235 | file must be a list of items, for example: |
| 2236 | |
| 2237 | - job: |
| 2238 | name: foo |
| 2239 | |
| 2240 | - project: |
| 2241 | name: bar |
| 2242 | |
| 2243 | Ensure that every item starts with "- " so that it is parsed as a |
| 2244 | YAML list. |
| 2245 | """) |
| 2246 | super(ConfigItemNotListError, self).__init__(message) |
| 2247 | |
| 2248 | |
| 2249 | class ConfigItemNotDictError(Exception): |
| 2250 | def __init__(self): |
| 2251 | message = textwrap.dedent("""\ |
| 2252 | Configuration item is not a dictionary. Each zuul.yaml |
| 2253 | configuration file must be a list of dictionaries, for |
| 2254 | example: |
| 2255 | |
| 2256 | - job: |
| 2257 | name: foo |
| 2258 | |
| 2259 | - project: |
| 2260 | name: bar |
| 2261 | |
| 2262 | Ensure that every item in the list is a dictionary with one |
| 2263 | key (in this example, 'job' and 'project'). |
| 2264 | """) |
| 2265 | super(ConfigItemNotDictError, self).__init__(message) |
| 2266 | |
| 2267 | |
| 2268 | class ConfigItemMultipleKeysError(Exception): |
| 2269 | def __init__(self): |
| 2270 | message = textwrap.dedent("""\ |
| 2271 | Configuration item has more than one key. Each zuul.yaml |
| 2272 | configuration file must be a list of dictionaries with a |
| 2273 | single key, for example: |
| 2274 | |
| 2275 | - job: |
| 2276 | name: foo |
| 2277 | |
| 2278 | - project: |
| 2279 | name: bar |
| 2280 | |
| 2281 | Ensure that every item in the list is a dictionary with only |
| 2282 | one key (in this example, 'job' and 'project'). This error |
| 2283 | may be caused by insufficient indentation of the keys under |
| 2284 | the configuration item ('name' in this example). |
| 2285 | """) |
| 2286 | super(ConfigItemMultipleKeysError, self).__init__(message) |
| 2287 | |
| 2288 | |
| 2289 | class ConfigItemUnknownError(Exception): |
| 2290 | def __init__(self): |
| 2291 | message = textwrap.dedent("""\ |
| 2292 | Configuration item not recognized. Each zuul.yaml |
| 2293 | configuration file must be a list of dictionaries, for |
| 2294 | example: |
| 2295 | |
| 2296 | - job: |
| 2297 | name: foo |
| 2298 | |
| 2299 | - project: |
| 2300 | name: bar |
| 2301 | |
| 2302 | The dictionary keys must match one of the configuration item |
| 2303 | types recognized by zuul (for example, 'job' or 'project'). |
| 2304 | """) |
| 2305 | super(ConfigItemUnknownError, self).__init__(message) |
| 2306 | |
| 2307 | |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2308 | class UnparsedAbideConfig(object): |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2309 | |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 2310 | """A collection of yaml lists that has not yet been parsed into objects. |
| 2311 | |
| 2312 | An Abide is a collection of tenants. |
| 2313 | """ |
| 2314 | |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2315 | def __init__(self): |
| 2316 | self.tenants = [] |
| 2317 | |
| 2318 | def extend(self, conf): |
| 2319 | if isinstance(conf, UnparsedAbideConfig): |
| 2320 | self.tenants.extend(conf.tenants) |
| 2321 | return |
| 2322 | |
| 2323 | if not isinstance(conf, list): |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2324 | raise ConfigItemNotListError() |
| 2325 | |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2326 | for item in conf: |
| 2327 | if not isinstance(item, dict): |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2328 | raise ConfigItemNotDictError() |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2329 | if len(item.keys()) > 1: |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2330 | raise ConfigItemMultipleKeysError() |
Clint Byrum | 1d0c7d1 | 2017-05-10 19:40:53 -0700 | [diff] [blame] | 2331 | key, value = list(item.items())[0] |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2332 | if key == 'tenant': |
| 2333 | self.tenants.append(value) |
| 2334 | else: |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2335 | raise ConfigItemUnknownError() |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2336 | |
| 2337 | |
| 2338 | class UnparsedTenantConfig(object): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 2339 | """A collection of yaml lists that has not yet been parsed into objects.""" |
| 2340 | |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2341 | def __init__(self): |
James E. Blair | 7edc25f | 2017-10-26 10:47:14 -0700 | [diff] [blame] | 2342 | self.pragmas = [] |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2343 | self.pipelines = [] |
| 2344 | self.jobs = [] |
| 2345 | self.project_templates = [] |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 2346 | self.projects = {} |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 2347 | self.nodesets = [] |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 2348 | self.secrets = [] |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2349 | self.semaphores = [] |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2350 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2351 | def copy(self): |
| 2352 | r = UnparsedTenantConfig() |
James E. Blair | 7edc25f | 2017-10-26 10:47:14 -0700 | [diff] [blame] | 2353 | r.pragmas = copy.deepcopy(self.pragmas) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2354 | r.pipelines = copy.deepcopy(self.pipelines) |
| 2355 | r.jobs = copy.deepcopy(self.jobs) |
| 2356 | r.project_templates = copy.deepcopy(self.project_templates) |
| 2357 | r.projects = copy.deepcopy(self.projects) |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 2358 | r.nodesets = copy.deepcopy(self.nodesets) |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 2359 | r.secrets = copy.deepcopy(self.secrets) |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2360 | r.semaphores = copy.deepcopy(self.semaphores) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2361 | return r |
| 2362 | |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 2363 | def extend(self, conf): |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2364 | if isinstance(conf, UnparsedTenantConfig): |
James E. Blair | 7edc25f | 2017-10-26 10:47:14 -0700 | [diff] [blame] | 2365 | self.pragmas.extend(conf.pragmas) |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2366 | self.pipelines.extend(conf.pipelines) |
| 2367 | self.jobs.extend(conf.jobs) |
| 2368 | self.project_templates.extend(conf.project_templates) |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 2369 | for k, v in conf.projects.items(): |
| 2370 | self.projects.setdefault(k, []).extend(v) |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 2371 | self.nodesets.extend(conf.nodesets) |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 2372 | self.secrets.extend(conf.secrets) |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2373 | self.semaphores.extend(conf.semaphores) |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2374 | return |
| 2375 | |
| 2376 | if not isinstance(conf, list): |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2377 | raise ConfigItemNotListError() |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 2378 | |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2379 | for item in conf: |
| 2380 | if not isinstance(item, dict): |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2381 | raise ConfigItemNotDictError() |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2382 | if len(item.keys()) > 1: |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2383 | raise ConfigItemMultipleKeysError() |
Clint Byrum | 1d0c7d1 | 2017-05-10 19:40:53 -0700 | [diff] [blame] | 2384 | key, value = list(item.items())[0] |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 2385 | if key == 'project': |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 2386 | name = value['name'] |
| 2387 | self.projects.setdefault(name, []).append(value) |
James E. Blair | 66b274e | 2017-01-31 14:47:52 -0800 | [diff] [blame] | 2388 | elif key == 'job': |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2389 | self.jobs.append(value) |
| 2390 | elif key == 'project-template': |
| 2391 | self.project_templates.append(value) |
| 2392 | elif key == 'pipeline': |
| 2393 | self.pipelines.append(value) |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 2394 | elif key == 'nodeset': |
| 2395 | self.nodesets.append(value) |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 2396 | elif key == 'secret': |
| 2397 | self.secrets.append(value) |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2398 | elif key == 'semaphore': |
| 2399 | self.semaphores.append(value) |
James E. Blair | 7edc25f | 2017-10-26 10:47:14 -0700 | [diff] [blame] | 2400 | elif key == 'pragma': |
| 2401 | self.pragmas.append(value) |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2402 | else: |
James E. Blair | 9704388 | 2017-09-06 15:51:17 -0700 | [diff] [blame] | 2403 | raise ConfigItemUnknownError() |
James E. Blair | d8e778f | 2015-12-22 14:09:20 -0800 | [diff] [blame] | 2404 | |
| 2405 | |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 2406 | class Layout(object): |
Monty Taylor | a42a55b | 2016-07-29 07:53:33 -0700 | [diff] [blame] | 2407 | """Holds all of the Pipelines.""" |
| 2408 | |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 2409 | def __init__(self, tenant): |
James E. Blair | 8fe53b4 | 2017-10-18 16:58:38 -0700 | [diff] [blame] | 2410 | self.uuid = uuid4().hex |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 2411 | self.tenant = tenant |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2412 | self.project_configs = {} |
| 2413 | self.project_templates = {} |
James E. Blair | 5a9918a | 2013-08-27 10:06:27 -0700 | [diff] [blame] | 2414 | self.pipelines = OrderedDict() |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2415 | # This is a dictionary of name -> [jobs]. The first element |
| 2416 | # of the list is the first job added with that name. It is |
| 2417 | # the reference definition for a given job. Subsequent |
| 2418 | # elements are aspects of that job with different matchers |
| 2419 | # that override some attribute of the job. These aspects all |
| 2420 | # inherit from the reference definition. |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 2421 | noop = Job('noop') |
| 2422 | noop.parent = noop.BASE_JOB_MARKER |
James E. Blair | 0029267 | 2017-10-26 15:29:39 -0700 | [diff] [blame] | 2423 | noop.run = 'noop.yaml' |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 2424 | self.jobs = {'noop': [noop]} |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 2425 | self.nodesets = {} |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 2426 | self.secrets = {} |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2427 | self.semaphores = {} |
James E. Blair | eff8816 | 2013-07-01 12:44:14 -0400 | [diff] [blame] | 2428 | |
| 2429 | def getJob(self, name): |
| 2430 | if name in self.jobs: |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2431 | return self.jobs[name][0] |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2432 | raise Exception("Job %s not defined" % (name,)) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2433 | |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 2434 | def hasJob(self, name): |
| 2435 | return name in self.jobs |
| 2436 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2437 | def getJobs(self, name): |
| 2438 | return self.jobs.get(name, []) |
| 2439 | |
| 2440 | def addJob(self, job): |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 2441 | # We can have multiple variants of a job all with the same |
| 2442 | # name, but these variants must all be defined in the same repo. |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 2443 | prior_jobs = [j for j in self.getJobs(job.name) if |
| 2444 | j.source_context.project != |
| 2445 | job.source_context.project] |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 2446 | # Unless the repo is permitted to shadow another. If so, and |
| 2447 | # the job we are adding is from a repo that is permitted to |
| 2448 | # shadow the one with the older jobs, skip adding this job. |
| 2449 | job_project = job.source_context.project |
| 2450 | job_tpc = self.tenant.project_configs[job_project.canonical_name] |
| 2451 | skip_add = False |
| 2452 | for prior_job in prior_jobs[:]: |
| 2453 | prior_project = prior_job.source_context.project |
| 2454 | if prior_project in job_tpc.shadow_projects: |
| 2455 | prior_jobs.remove(prior_job) |
| 2456 | skip_add = True |
| 2457 | |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 2458 | if prior_jobs: |
| 2459 | raise Exception("Job %s in %s is not permitted to shadow " |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 2460 | "job %s in %s" % ( |
| 2461 | job, |
| 2462 | job.source_context.project, |
| 2463 | prior_jobs[0], |
| 2464 | prior_jobs[0].source_context.project)) |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 2465 | if skip_add: |
| 2466 | return False |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2467 | if job.name in self.jobs: |
| 2468 | self.jobs[job.name].append(job) |
| 2469 | else: |
| 2470 | self.jobs[job.name] = [job] |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 2471 | return True |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2472 | |
James E. Blair | a98340f | 2016-09-02 11:33:49 -0700 | [diff] [blame] | 2473 | def addNodeSet(self, nodeset): |
| 2474 | if nodeset.name in self.nodesets: |
| 2475 | raise Exception("NodeSet %s already defined" % (nodeset.name,)) |
| 2476 | self.nodesets[nodeset.name] = nodeset |
| 2477 | |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 2478 | def addSecret(self, secret): |
| 2479 | if secret.name in self.secrets: |
| 2480 | raise Exception("Secret %s already defined" % (secret.name,)) |
| 2481 | self.secrets[secret.name] = secret |
| 2482 | |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2483 | def addSemaphore(self, semaphore): |
| 2484 | if semaphore.name in self.semaphores: |
| 2485 | raise Exception("Semaphore %s already defined" % (semaphore.name,)) |
| 2486 | self.semaphores[semaphore.name] = semaphore |
| 2487 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 2488 | def addPipeline(self, pipeline): |
| 2489 | self.pipelines[pipeline.name] = pipeline |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2490 | |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2491 | def addProjectTemplate(self, project_template): |
James E. Blair | 2a66450 | 2017-10-27 11:39:33 -0700 | [diff] [blame] | 2492 | template = self.project_templates.get(project_template.name) |
| 2493 | if template: |
| 2494 | if (project_template.source_context.project != |
| 2495 | template.source_context.project): |
| 2496 | raise Exception("Project template %s is already defined" % |
| 2497 | (project_template.name,)) |
| 2498 | for pipeline in project_template.pipelines: |
| 2499 | template.pipelines[pipeline].job_list.\ |
| 2500 | inheritFrom(project_template.pipelines[pipeline].job_list, |
| 2501 | None) |
| 2502 | else: |
| 2503 | self.project_templates[project_template.name] = project_template |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2504 | |
James E. Blair | f59f3cf | 2017-02-19 14:50:26 -0800 | [diff] [blame] | 2505 | def addProjectConfig(self, project_config): |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2506 | self.project_configs[project_config.name] = project_config |
James E. Blair | b97ed80 | 2015-12-21 15:55:35 -0800 | [diff] [blame] | 2507 | |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 2508 | def collectJobs(self, jobname, change, path=None, jobs=None, stack=None): |
| 2509 | if stack is None: |
| 2510 | stack = [] |
| 2511 | if jobs is None: |
| 2512 | jobs = [] |
| 2513 | if path is None: |
| 2514 | path = [] |
| 2515 | path.append(jobname) |
| 2516 | matched = False |
| 2517 | for variant in self.getJobs(jobname): |
| 2518 | if not variant.changeMatches(change): |
| 2519 | continue |
| 2520 | if not variant.isBase(): |
| 2521 | parent = variant.parent |
| 2522 | if not jobs and parent is None: |
| 2523 | parent = self.tenant.default_base_job |
| 2524 | else: |
| 2525 | parent = None |
| 2526 | if parent and parent not in path: |
| 2527 | if parent in stack: |
| 2528 | raise Exception("Dependency cycle in jobs: %s" % stack) |
| 2529 | self.collectJobs(parent, change, path, jobs, stack + [jobname]) |
| 2530 | matched = True |
| 2531 | jobs.append(variant) |
| 2532 | if not matched: |
| 2533 | raise NoMatchingParentError() |
| 2534 | return jobs |
| 2535 | |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 2536 | def _createJobGraph(self, item, job_list, job_graph): |
| 2537 | change = item.change |
| 2538 | pipeline = item.pipeline |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 2539 | for jobname in job_list.jobs: |
| 2540 | # This is the final job we are constructing |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 2541 | frozen_job = None |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 2542 | try: |
| 2543 | variants = self.collectJobs(jobname, change) |
| 2544 | except NoMatchingParentError: |
| 2545 | variants = None |
| 2546 | if not variants: |
James E. Blair | 6e85c2b | 2016-11-21 16:47:01 -0800 | [diff] [blame] | 2547 | # A change must match at least one defined job variant |
| 2548 | # (that is to say that it must match more than just |
| 2549 | # the job that is defined in the tree). |
| 2550 | continue |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 2551 | for variant in variants: |
| 2552 | if frozen_job is None: |
| 2553 | frozen_job = variant.copy() |
| 2554 | frozen_job.setBase() |
| 2555 | else: |
| 2556 | frozen_job.applyVariant(variant) |
| 2557 | frozen_job.name = variant.name |
James E. Blair | c32a835 | 2017-10-11 16:27:50 -0700 | [diff] [blame] | 2558 | frozen_job.name = jobname |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 2559 | # Whether the change matches any of the project pipeline |
| 2560 | # variants |
| 2561 | matched = False |
| 2562 | for variant in job_list.jobs[jobname]: |
| 2563 | if variant.changeMatches(change): |
| 2564 | frozen_job.applyVariant(variant) |
| 2565 | matched = True |
| 2566 | if not matched: |
| 2567 | # A change must match at least one project pipeline |
| 2568 | # job variant. |
| 2569 | continue |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame] | 2570 | if (frozen_job.allowed_projects and |
| 2571 | change.project.name not in frozen_job.allowed_projects): |
| 2572 | raise Exception("Project %s is not allowed to run job %s" % |
| 2573 | (change.project.name, frozen_job.name)) |
James E. Blair | 8eb564a | 2017-08-10 09:21:41 -0700 | [diff] [blame] | 2574 | if ((not pipeline.post_review) and frozen_job.post_review): |
| 2575 | raise Exception("Pre-review pipeline %s does not allow " |
| 2576 | "post-review job %s" % ( |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 2577 | pipeline.name, frozen_job.name)) |
James E. Blair | 0029267 | 2017-10-26 15:29:39 -0700 | [diff] [blame] | 2578 | if not frozen_job.run: |
| 2579 | raise Exception("Job %s does not specify a run playbook" % ( |
| 2580 | frozen_job.name,)) |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 2581 | job_graph.addJob(frozen_job) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2582 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 2583 | def createJobGraph(self, item): |
Paul Belanger | 15e3e20 | 2016-10-14 16:27:34 -0400 | [diff] [blame] | 2584 | # NOTE(pabelanger): It is possible for a foreign project not to have a |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 2585 | # configured pipeline, if so return an empty JobGraph. |
James E. Blair | c945500 | 2017-09-06 09:22:19 -0700 | [diff] [blame] | 2586 | ret = JobGraph() |
| 2587 | ppc = self.getProjectPipelineConfig(item.change.project, |
| 2588 | item.pipeline) |
| 2589 | if ppc: |
| 2590 | self._createJobGraph(item, ppc.job_list, ret) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 2591 | return ret |
| 2592 | |
James E. Blair | c945500 | 2017-09-06 09:22:19 -0700 | [diff] [blame] | 2593 | def getProjectPipelineConfig(self, project, pipeline): |
| 2594 | project_config = self.project_configs.get( |
| 2595 | project.canonical_name, None) |
| 2596 | if not project_config: |
| 2597 | return None |
| 2598 | return project_config.pipelines.get(pipeline.name, None) |
James E. Blair | 0d3e83b | 2017-06-05 13:51:57 -0700 | [diff] [blame] | 2599 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2600 | |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2601 | class Semaphore(object): |
| 2602 | def __init__(self, name, max=1): |
| 2603 | self.name = name |
| 2604 | self.max = int(max) |
| 2605 | |
| 2606 | |
| 2607 | class SemaphoreHandler(object): |
| 2608 | log = logging.getLogger("zuul.SemaphoreHandler") |
| 2609 | |
| 2610 | def __init__(self): |
| 2611 | self.semaphores = {} |
| 2612 | |
| 2613 | def acquire(self, item, job): |
| 2614 | if not job.semaphore: |
| 2615 | return True |
| 2616 | |
| 2617 | semaphore_key = job.semaphore |
| 2618 | |
| 2619 | m = self.semaphores.get(semaphore_key) |
| 2620 | if not m: |
| 2621 | # The semaphore is not held, acquire it |
| 2622 | self._acquire(semaphore_key, item, job.name) |
| 2623 | return True |
| 2624 | if (item, job.name) in m: |
| 2625 | # This item already holds the semaphore |
| 2626 | return True |
| 2627 | |
| 2628 | # semaphore is there, check max |
| 2629 | if len(m) < self._max_count(item, job.semaphore): |
| 2630 | self._acquire(semaphore_key, item, job.name) |
| 2631 | return True |
| 2632 | |
| 2633 | return False |
| 2634 | |
| 2635 | def release(self, item, job): |
| 2636 | if not job.semaphore: |
| 2637 | return |
| 2638 | |
| 2639 | semaphore_key = job.semaphore |
| 2640 | |
| 2641 | m = self.semaphores.get(semaphore_key) |
| 2642 | if not m: |
| 2643 | # The semaphore is not held, nothing to do |
| 2644 | self.log.error("Semaphore can not be released for %s " |
| 2645 | "because the semaphore is not held" % |
| 2646 | item) |
| 2647 | return |
| 2648 | if (item, job.name) in m: |
| 2649 | # This item is a holder of the semaphore |
| 2650 | self._release(semaphore_key, item, job.name) |
| 2651 | return |
| 2652 | self.log.error("Semaphore can not be released for %s " |
| 2653 | "which does not hold it" % item) |
| 2654 | |
| 2655 | def _acquire(self, semaphore_key, item, job_name): |
| 2656 | self.log.debug("Semaphore acquire {semaphore}: job {job}, item {item}" |
| 2657 | .format(semaphore=semaphore_key, |
| 2658 | job=job_name, |
| 2659 | item=item)) |
| 2660 | if semaphore_key not in self.semaphores: |
| 2661 | self.semaphores[semaphore_key] = [] |
| 2662 | self.semaphores[semaphore_key].append((item, job_name)) |
| 2663 | |
| 2664 | def _release(self, semaphore_key, item, job_name): |
| 2665 | self.log.debug("Semaphore release {semaphore}: job {job}, item {item}" |
| 2666 | .format(semaphore=semaphore_key, |
| 2667 | job=job_name, |
| 2668 | item=item)) |
| 2669 | sem_item = (item, job_name) |
| 2670 | if sem_item in self.semaphores[semaphore_key]: |
| 2671 | self.semaphores[semaphore_key].remove(sem_item) |
| 2672 | |
| 2673 | # cleanup if there is no user of the semaphore anymore |
| 2674 | if len(self.semaphores[semaphore_key]) == 0: |
| 2675 | del self.semaphores[semaphore_key] |
| 2676 | |
| 2677 | @staticmethod |
| 2678 | def _max_count(item, semaphore_name): |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 2679 | if not item.layout: |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2680 | # This should not occur as the layout of the item must already be |
| 2681 | # built when acquiring or releasing a semaphore for a job. |
| 2682 | raise Exception("Item {} has no layout".format(item)) |
| 2683 | |
| 2684 | # find the right semaphore |
| 2685 | default_semaphore = Semaphore(semaphore_name, 1) |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 2686 | semaphores = item.layout.semaphores |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2687 | return semaphores.get(semaphore_name, default_semaphore).max |
| 2688 | |
| 2689 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2690 | class Tenant(object): |
| 2691 | def __init__(self, name): |
| 2692 | self.name = name |
Tristan Cacqueray | 82f864b | 2017-08-01 05:54:42 +0000 | [diff] [blame] | 2693 | self.max_nodes_per_job = 5 |
Tristan Cacqueray | c98bff7 | 2017-09-10 15:25:26 +0000 | [diff] [blame] | 2694 | self.max_job_timeout = 10800 |
Tobias Henkel | eca4620 | 2017-08-02 20:27:10 +0200 | [diff] [blame] | 2695 | self.exclude_unprotected_branches = False |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 2696 | self.default_base_job = None |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2697 | self.layout = None |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 2698 | # The unparsed configuration from the main zuul config for |
| 2699 | # this tenant. |
| 2700 | self.unparsed_config = None |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 2701 | # The list of projects from which we will read full |
James E. Blair | 8a395f9 | 2017-03-30 11:15:33 -0700 | [diff] [blame] | 2702 | # configuration. |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 2703 | self.config_projects = [] |
| 2704 | # The unparsed config from those projects. |
| 2705 | self.config_projects_config = None |
| 2706 | # The list of projects from which we will read untrusted |
| 2707 | # in-repo configuration. |
| 2708 | self.untrusted_projects = [] |
| 2709 | # The unparsed config from those projects. |
| 2710 | self.untrusted_projects_config = None |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2711 | self.semaphore_handler = SemaphoreHandler() |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2712 | # Metadata about projects for this tenant |
| 2713 | # canonical project name -> TenantProjectConfig |
| 2714 | self.project_configs = {} |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 2715 | |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 2716 | # A mapping of project names to projects. project_name -> |
| 2717 | # VALUE where VALUE is a further dictionary of |
| 2718 | # canonical_hostname -> Project. |
| 2719 | self.projects = {} |
| 2720 | self.canonical_hostnames = set() |
| 2721 | |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2722 | def _addProject(self, tpc): |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 2723 | """Add a project to the project index |
| 2724 | |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2725 | :arg TenantProjectConfig tpc: The TenantProjectConfig (with |
| 2726 | associated project) to add. |
| 2727 | |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 2728 | """ |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2729 | project = tpc.project |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 2730 | self.canonical_hostnames.add(project.canonical_hostname) |
| 2731 | hostname_dict = self.projects.setdefault(project.name, {}) |
| 2732 | if project.canonical_hostname in hostname_dict: |
| 2733 | raise Exception("Project %s is already in project index" % |
| 2734 | (project,)) |
| 2735 | hostname_dict[project.canonical_hostname] = project |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2736 | self.project_configs[project.canonical_name] = tpc |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 2737 | |
| 2738 | def getProject(self, name): |
| 2739 | """Return a project given its name. |
| 2740 | |
| 2741 | :arg str name: The name of the project. It may be fully |
| 2742 | qualified (E.g., "git.example.com/subpath/project") or may |
| 2743 | contain only the project name name may be supplied (E.g., |
| 2744 | "subpath/project"). |
| 2745 | |
| 2746 | :returns: A tuple (trusted, project) or (None, None) if the |
| 2747 | project is not found or ambiguous. The "trusted" boolean |
| 2748 | indicates whether or not the project is trusted by this |
| 2749 | tenant. |
| 2750 | :rtype: (bool, Project) |
| 2751 | |
| 2752 | """ |
| 2753 | path = name.split('/', 1) |
| 2754 | if path[0] in self.canonical_hostnames: |
| 2755 | hostname = path[0] |
| 2756 | project_name = path[1] |
| 2757 | else: |
| 2758 | hostname = None |
| 2759 | project_name = name |
| 2760 | hostname_dict = self.projects.get(project_name) |
| 2761 | project = None |
| 2762 | if hostname_dict: |
| 2763 | if hostname: |
| 2764 | project = hostname_dict.get(hostname) |
| 2765 | else: |
Clint Byrum | 1d0c7d1 | 2017-05-10 19:40:53 -0700 | [diff] [blame] | 2766 | values = list(hostname_dict.values()) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 2767 | if len(values) == 1: |
| 2768 | project = values[0] |
| 2769 | else: |
| 2770 | raise Exception("Project name '%s' is ambiguous, " |
| 2771 | "please fully qualify the project " |
| 2772 | "with a hostname" % (name,)) |
| 2773 | if project is None: |
| 2774 | return (None, None) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 2775 | if project in self.config_projects: |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 2776 | return (True, project) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 2777 | if project in self.untrusted_projects: |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 2778 | return (False, project) |
| 2779 | # This should never happen: |
| 2780 | raise Exception("Project %s is neither trusted nor untrusted" % |
| 2781 | (project,)) |
| 2782 | |
James E. Blair | daaf326 | 2017-10-23 13:51:48 -0700 | [diff] [blame] | 2783 | def getProjectBranches(self, project): |
| 2784 | """Return a project's branches (filtered by this tenant config) |
| 2785 | |
| 2786 | :arg Project project: The project object. |
| 2787 | |
| 2788 | :returns: A list of branch names. |
| 2789 | :rtype: [str] |
| 2790 | |
| 2791 | """ |
| 2792 | tpc = self.project_configs[project.canonical_name] |
| 2793 | return tpc.branches |
| 2794 | |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2795 | def addConfigProject(self, tpc): |
| 2796 | self.config_projects.append(tpc.project) |
| 2797 | self._addProject(tpc) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 2798 | |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 2799 | def addUntrustedProject(self, tpc): |
| 2800 | self.untrusted_projects.append(tpc.project) |
| 2801 | self._addProject(tpc) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 2802 | |
Jamie Lennox | bf997c8 | 2017-05-10 09:58:40 +1000 | [diff] [blame] | 2803 | def getSafeAttributes(self): |
| 2804 | return Attributes(name=self.name) |
| 2805 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 2806 | |
| 2807 | class Abide(object): |
| 2808 | def __init__(self): |
| 2809 | self.tenants = OrderedDict() |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 2810 | |
| 2811 | |
| 2812 | class JobTimeData(object): |
| 2813 | format = 'B10H10H10B' |
| 2814 | version = 0 |
| 2815 | |
| 2816 | def __init__(self, path): |
| 2817 | self.path = path |
| 2818 | self.success_times = [0 for x in range(10)] |
| 2819 | self.failure_times = [0 for x in range(10)] |
| 2820 | self.results = [0 for x in range(10)] |
| 2821 | |
| 2822 | def load(self): |
| 2823 | if not os.path.exists(self.path): |
| 2824 | return |
Clint Byrum | a4471d1 | 2017-05-10 20:57:40 -0400 | [diff] [blame] | 2825 | with open(self.path, 'rb') as f: |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 2826 | data = struct.unpack(self.format, f.read()) |
| 2827 | version = data[0] |
| 2828 | if version != self.version: |
| 2829 | raise Exception("Unkown data version") |
| 2830 | self.success_times = list(data[1:11]) |
| 2831 | self.failure_times = list(data[11:21]) |
| 2832 | self.results = list(data[21:32]) |
| 2833 | |
| 2834 | def save(self): |
| 2835 | tmpfile = self.path + '.tmp' |
| 2836 | data = [self.version] |
| 2837 | data.extend(self.success_times) |
| 2838 | data.extend(self.failure_times) |
| 2839 | data.extend(self.results) |
| 2840 | data = struct.pack(self.format, *data) |
Clint Byrum | a4471d1 | 2017-05-10 20:57:40 -0400 | [diff] [blame] | 2841 | with open(tmpfile, 'wb') as f: |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 2842 | f.write(data) |
| 2843 | os.rename(tmpfile, self.path) |
| 2844 | |
| 2845 | def add(self, elapsed, result): |
| 2846 | elapsed = int(elapsed) |
| 2847 | if result == 'SUCCESS': |
| 2848 | self.success_times.append(elapsed) |
| 2849 | self.success_times.pop(0) |
| 2850 | result = 0 |
| 2851 | else: |
| 2852 | self.failure_times.append(elapsed) |
| 2853 | self.failure_times.pop(0) |
| 2854 | result = 1 |
| 2855 | self.results.append(result) |
| 2856 | self.results.pop(0) |
| 2857 | |
| 2858 | def getEstimatedTime(self): |
| 2859 | times = [x for x in self.success_times if x] |
| 2860 | if times: |
| 2861 | return float(sum(times)) / len(times) |
| 2862 | return 0.0 |
| 2863 | |
| 2864 | |
| 2865 | class TimeDataBase(object): |
| 2866 | def __init__(self, root): |
| 2867 | self.root = root |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 2868 | |
James E. Blair | ae0f23c | 2017-09-13 10:55:15 -0600 | [diff] [blame] | 2869 | def _getTD(self, build): |
| 2870 | if hasattr(build.build_set.item.change, 'branch'): |
| 2871 | branch = build.build_set.item.change.branch |
| 2872 | else: |
| 2873 | branch = '' |
| 2874 | |
| 2875 | dir_path = os.path.join( |
| 2876 | self.root, |
| 2877 | build.build_set.item.pipeline.layout.tenant.name, |
| 2878 | build.build_set.item.change.project.canonical_name, |
| 2879 | branch) |
| 2880 | if not os.path.exists(dir_path): |
| 2881 | os.makedirs(dir_path) |
| 2882 | path = os.path.join(dir_path, build.job.name) |
| 2883 | |
| 2884 | td = JobTimeData(path) |
| 2885 | td.load() |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 2886 | return td |
| 2887 | |
| 2888 | def getEstimatedTime(self, name): |
| 2889 | return self._getTD(name).getEstimatedTime() |
| 2890 | |
James E. Blair | ae0f23c | 2017-09-13 10:55:15 -0600 | [diff] [blame] | 2891 | def update(self, build, elapsed, result): |
| 2892 | td = self._getTD(build) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 2893 | td.add(elapsed, result) |
| 2894 | td.save() |