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