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