James E. Blair | dbfe1cd | 2015-02-07 11:41:19 -0800 | [diff] [blame] | 1 | # Copyright 2012-2015 Hewlett-Packard Development Company, L.P. |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 2 | # Copyright 2013 OpenStack Foundation |
Antoine Musso | 80edd5a | 2013-02-13 15:37:53 +0100 | [diff] [blame] | 3 | # Copyright 2013 Antoine "hashar" Musso |
| 4 | # Copyright 2013 Wikimedia Foundation Inc. |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 18 | import json |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 19 | import logging |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 20 | import os |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 21 | import pickle |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 22 | import queue |
James E. Blair | 8b2a147 | 2017-02-19 15:33:55 -0800 | [diff] [blame] | 23 | import socket |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 24 | import sys |
Zhongyue Luo | 1c860d7 | 2012-07-19 11:03:56 +0800 | [diff] [blame] | 25 | import threading |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 26 | import time |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 27 | |
Joshua Hesketh | 0aa7e8b | 2016-07-14 00:12:25 +1000 | [diff] [blame] | 28 | from zuul import configloader |
Morgan Fainberg | 9c4700a | 2016-05-30 14:25:19 -0700 | [diff] [blame] | 29 | from zuul import model |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 30 | from zuul import exceptions |
Sergey Lukjanov | 5ba961b | 2013-12-27 01:21:04 +0400 | [diff] [blame] | 31 | from zuul import version as zuul_version |
Tristan Cacqueray | 91601d7 | 2017-06-15 06:00:12 +0000 | [diff] [blame] | 32 | from zuul.lib.config import get_default |
James E. Blair | ded241e | 2017-10-10 13:22:40 -0700 | [diff] [blame] | 33 | from zuul.lib.statsd import get_statsd |
James E. Blair | 419a867 | 2017-10-18 14:48:25 -0700 | [diff] [blame^] | 34 | import zuul.lib.queue |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 35 | |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 36 | |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 37 | class ManagementEvent(object): |
| 38 | """An event that should be processed within the main queue run loop""" |
| 39 | def __init__(self): |
| 40 | self._wait_event = threading.Event() |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 41 | self._exc_info = None |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 42 | |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 43 | def exception(self, exc_info): |
| 44 | self._exc_info = exc_info |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 45 | self._wait_event.set() |
| 46 | |
| 47 | def done(self): |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 48 | self._wait_event.set() |
| 49 | |
| 50 | def wait(self, timeout=None): |
| 51 | self._wait_event.wait(timeout) |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 52 | if self._exc_info: |
Thomas Bechtold | 7f68ec4 | 2017-06-30 14:24:52 +0200 | [diff] [blame] | 53 | # sys.exc_info returns (type, value, traceback) |
| 54 | type_, exception_instance, traceback = self._exc_info |
| 55 | raise exception_instance.with_traceback(traceback) |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 56 | return self._wait_event.is_set() |
| 57 | |
| 58 | |
| 59 | class ReconfigureEvent(ManagementEvent): |
| 60 | """Reconfigure the scheduler. The layout will be (re-)loaded from |
| 61 | the path specified in the configuration. |
| 62 | |
| 63 | :arg ConfigParser config: the new configuration |
| 64 | """ |
| 65 | def __init__(self, config): |
| 66 | super(ReconfigureEvent, self).__init__() |
| 67 | self.config = config |
| 68 | |
| 69 | |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 70 | class TenantReconfigureEvent(ManagementEvent): |
| 71 | """Reconfigure the given tenant. The layout will be (re-)loaded from |
| 72 | the path specified in the configuration. |
| 73 | |
| 74 | :arg Tenant tenant: the tenant to reconfigure |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 75 | :arg Project project: if supplied, clear the cached configuration |
| 76 | from this project first |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 77 | """ |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 78 | def __init__(self, tenant, project): |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 79 | super(TenantReconfigureEvent, self).__init__() |
James E. Blair | 419a867 | 2017-10-18 14:48:25 -0700 | [diff] [blame^] | 80 | self.tenant_name = tenant.name |
| 81 | self.projects = set([project]) |
| 82 | |
| 83 | def __ne__(self, other): |
| 84 | return not self.__eq__(other) |
| 85 | |
| 86 | def __eq__(self, other): |
| 87 | if not isinstance(other, TenantReconfigureEvent): |
| 88 | return False |
| 89 | # We don't check projects because they will get combined when |
| 90 | # merged. |
| 91 | return (self.tenant_name == other.tenant_name) |
| 92 | |
| 93 | def merge(self, other): |
| 94 | if self.tenant_name != other.tenant_name: |
| 95 | raise Exception("Can not merge events from different tenants") |
| 96 | self.projects |= other.projects |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 97 | |
| 98 | |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 99 | class PromoteEvent(ManagementEvent): |
| 100 | """Promote one or more changes to the head of the queue. |
| 101 | |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 102 | :arg str tenant_name: the name of the tenant |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 103 | :arg str pipeline_name: the name of the pipeline |
| 104 | :arg list change_ids: a list of strings of change ids in the form |
| 105 | 1234,1 |
| 106 | """ |
| 107 | |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 108 | def __init__(self, tenant_name, pipeline_name, change_ids): |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 109 | super(PromoteEvent, self).__init__() |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 110 | self.tenant_name = tenant_name |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 111 | self.pipeline_name = pipeline_name |
| 112 | self.change_ids = change_ids |
| 113 | |
| 114 | |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 115 | class EnqueueEvent(ManagementEvent): |
| 116 | """Enqueue a change into a pipeline |
| 117 | |
| 118 | :arg TriggerEvent trigger_event: a TriggerEvent describing the |
| 119 | trigger, pipeline, and change to enqueue |
| 120 | """ |
| 121 | |
| 122 | def __init__(self, trigger_event): |
| 123 | super(EnqueueEvent, self).__init__() |
| 124 | self.trigger_event = trigger_event |
| 125 | |
| 126 | |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 127 | class ResultEvent(object): |
| 128 | """An event that needs to modify the pipeline state due to a |
| 129 | result from an external system.""" |
| 130 | |
| 131 | pass |
| 132 | |
| 133 | |
| 134 | class BuildStartedEvent(ResultEvent): |
| 135 | """A build has started. |
| 136 | |
| 137 | :arg Build build: The build which has started. |
| 138 | """ |
| 139 | |
| 140 | def __init__(self, build): |
| 141 | self.build = build |
| 142 | |
| 143 | |
| 144 | class BuildCompletedEvent(ResultEvent): |
| 145 | """A build has completed |
| 146 | |
| 147 | :arg Build build: The build which has completed. |
| 148 | """ |
| 149 | |
| 150 | def __init__(self, build): |
| 151 | self.build = build |
| 152 | |
| 153 | |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 154 | class MergeCompletedEvent(ResultEvent): |
| 155 | """A remote merge operation has completed |
| 156 | |
| 157 | :arg BuildSet build_set: The build_set which is ready. |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 158 | :arg bool merged: Whether the merge succeeded (changes with refs). |
| 159 | :arg bool updated: Whether the repo was updated (changes without refs). |
| 160 | :arg str commit: The SHA of the merged commit (changes with refs). |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 161 | :arg dict repo_state: The starting repo state before the merge. |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 162 | """ |
| 163 | |
Tobias Henkel | 34ee088 | 2017-07-31 22:26:12 +0200 | [diff] [blame] | 164 | def __init__(self, build_set, merged, updated, commit, |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 165 | files, repo_state): |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 166 | self.build_set = build_set |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 167 | self.merged = merged |
| 168 | self.updated = updated |
| 169 | self.commit = commit |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 170 | self.files = files |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 171 | self.repo_state = repo_state |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 172 | |
| 173 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 174 | class NodesProvisionedEvent(ResultEvent): |
| 175 | """Nodes have been provisioned for a build_set |
| 176 | |
| 177 | :arg BuildSet build_set: The build_set which has nodes. |
| 178 | :arg list of Node objects nodes: The provisioned nodes |
| 179 | """ |
| 180 | |
| 181 | def __init__(self, request): |
| 182 | self.request = request |
David Shrewsbury | 94e9588 | 2017-10-04 15:26:04 -0400 | [diff] [blame] | 183 | self.request_id = request.id |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 184 | |
| 185 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 186 | def toList(item): |
| 187 | if not item: |
| 188 | return [] |
| 189 | if isinstance(item, list): |
| 190 | return item |
| 191 | return [item] |
| 192 | |
| 193 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 194 | class Scheduler(threading.Thread): |
James E. Blair | e4de4f4 | 2017-01-19 10:35:24 -0800 | [diff] [blame] | 195 | """The engine of Zuul. |
| 196 | |
| 197 | The Scheduler is reponsible for recieving events and dispatching |
| 198 | them to appropriate components (including pipeline managers, |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 199 | mergers and executors). |
James E. Blair | e4de4f4 | 2017-01-19 10:35:24 -0800 | [diff] [blame] | 200 | |
| 201 | It runs a single threaded main loop which processes events |
| 202 | received one at a time and takes action as appropriate. Other |
| 203 | parts of Zuul may run in their own thread, but synchronization is |
| 204 | performed within the scheduler to reduce or eliminate the need for |
| 205 | locking in most circumstances. |
| 206 | |
| 207 | The main daemon will have one instance of the Scheduler class |
| 208 | running which will persist for the life of the process. The |
| 209 | Scheduler instance is supplied to other Zuul components so that |
| 210 | they can submit events or otherwise communicate with other |
| 211 | components. |
| 212 | |
| 213 | """ |
| 214 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 215 | log = logging.getLogger("zuul.Scheduler") |
| 216 | |
James E. Blair | e4d229c | 2016-05-25 15:25:41 -0700 | [diff] [blame] | 217 | def __init__(self, config, testonly=False): |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 218 | threading.Thread.__init__(self) |
James E. Blair | 8a6f0c2 | 2013-07-01 12:31:34 -0400 | [diff] [blame] | 219 | self.daemon = True |
James E. Blair | 8b2a147 | 2017-02-19 15:33:55 -0800 | [diff] [blame] | 220 | self.hostname = socket.gethostname() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 221 | self.wake_event = threading.Event() |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 222 | self.layout_lock = threading.Lock() |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 223 | self.run_handler_lock = threading.Lock() |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 224 | self._pause = False |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 225 | self._exit = False |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 226 | self._stopped = False |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 227 | self.executor = None |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 228 | self.merger = None |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 229 | self.connections = None |
James E. Blair | ded241e | 2017-10-10 13:22:40 -0700 | [diff] [blame] | 230 | self.statsd = get_statsd(config) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 231 | # TODO(jeblair): fix this |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 232 | # Despite triggers being part of the pipeline, there is one trigger set |
| 233 | # per scheduler. The pipeline handles the trigger filters but since |
| 234 | # the events are handled by the scheduler itself it needs to handle |
| 235 | # the loading of the triggers. |
| 236 | # self.triggers['connection_name'] = triggerObject |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 237 | self.triggers = dict() |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 238 | self.config = config |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 239 | |
Monty Taylor | b934c1a | 2017-06-16 19:31:47 -0500 | [diff] [blame] | 240 | self.trigger_event_queue = queue.Queue() |
| 241 | self.result_event_queue = queue.Queue() |
James E. Blair | 419a867 | 2017-10-18 14:48:25 -0700 | [diff] [blame^] | 242 | self.management_event_queue = zuul.lib.queue.MergedQueue() |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 243 | self.abide = model.Abide() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 244 | |
James E. Blair | e4d229c | 2016-05-25 15:25:41 -0700 | [diff] [blame] | 245 | if not testonly: |
| 246 | time_dir = self._get_time_database_dir() |
| 247 | self.time_database = model.TimeDataBase(time_dir) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 248 | |
Jeremy Stanley | 98b38de | 2015-06-04 21:20:43 +0000 | [diff] [blame] | 249 | self.zuul_version = zuul_version.version_info.release_string() |
Sergey Lukjanov | 5d0438d | 2013-12-24 03:36:39 +0400 | [diff] [blame] | 250 | self.last_reconfigured = None |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 251 | self.tenant_last_reconfigured = {} |
David Shrewsbury | ffab07a | 2017-07-24 12:45:07 -0400 | [diff] [blame] | 252 | self.autohold_requests = {} |
Sergey Lukjanov | 5ba961b | 2013-12-27 01:21:04 +0400 | [diff] [blame] | 253 | |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 254 | def stop(self): |
| 255 | self._stopped = True |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 256 | self.stopConnections() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 257 | self.wake_event.set() |
| 258 | |
Jan Hruban | 7083edd | 2015-08-21 14:00:54 +0200 | [diff] [blame] | 259 | def registerConnections(self, connections, webapp, load=True): |
Joshua Hesketh | 9a25675 | 2016-04-04 13:38:51 +1000 | [diff] [blame] | 260 | # load: whether or not to trigger the onLoad for the connection. This |
| 261 | # is useful for not doing a full load during layout validation. |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 262 | self.connections = connections |
Jan Hruban | 7083edd | 2015-08-21 14:00:54 +0200 | [diff] [blame] | 263 | self.connections.registerWebapp(webapp) |
Joshua Hesketh | 0aa7e8b | 2016-07-14 00:12:25 +1000 | [diff] [blame] | 264 | self.connections.registerScheduler(self, load) |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 265 | |
| 266 | def stopConnections(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 267 | self.connections.stop() |
James E. Blair | 14abdf4 | 2015-12-09 16:11:53 -0800 | [diff] [blame] | 268 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 269 | def setExecutor(self, executor): |
| 270 | self.executor = executor |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 271 | |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 272 | def setMerger(self, merger): |
| 273 | self.merger = merger |
| 274 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 275 | def setNodepool(self, nodepool): |
| 276 | self.nodepool = nodepool |
| 277 | |
James E. Blair | dce6cea | 2016-12-20 16:45:32 -0800 | [diff] [blame] | 278 | def setZooKeeper(self, zk): |
| 279 | self.zk = zk |
| 280 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 281 | def addEvent(self, event): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 282 | self.trigger_event_queue.put(event) |
| 283 | self.wake_event.set() |
| 284 | |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 285 | def onBuildStarted(self, build): |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 286 | build.start_time = time.time() |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 287 | event = BuildStartedEvent(build) |
| 288 | self.result_event_queue.put(event) |
James E. Blair | 11700c3 | 2012-07-05 17:50:05 -0700 | [diff] [blame] | 289 | self.wake_event.set() |
| 290 | |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 291 | def onBuildCompleted(self, build, result, result_data): |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 292 | build.end_time = time.time() |
James E. Blair | 196f61a | 2017-06-30 15:42:29 -0700 | [diff] [blame] | 293 | build.result_data = result_data |
James E. Blair | f035866 | 2015-07-20 15:19:12 -0700 | [diff] [blame] | 294 | # Note, as soon as the result is set, other threads may act |
| 295 | # upon this, even though the event hasn't been fully |
| 296 | # processed. Ensure that any other data from the event (eg, |
| 297 | # timing) is recorded before setting the result. |
| 298 | build.result = result |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 299 | try: |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 300 | if self.statsd and build.pipeline: |
James E. Blair | 80ac158 | 2017-10-09 07:02:40 -0700 | [diff] [blame] | 301 | tenant = build.pipeline.layout.tenant |
| 302 | jobname = build.job.name.replace('.', '_').replace('/', '_') |
| 303 | hostname = (build.build_set.item.change.project. |
| 304 | canonical_hostname.replace('.', '_')) |
| 305 | projectname = (build.build_set.item.change.project.name. |
| 306 | replace('.', '_').replace('/', '_')) |
| 307 | branchname = (build.build_set.item.change.branch. |
| 308 | replace('.', '_').replace('/', '_')) |
| 309 | basekey = 'zuul.tenant.%s' % tenant.name |
| 310 | pipekey = '%s.pipeline.%s' % (basekey, build.pipeline.name) |
| 311 | # zuul.tenant.<tenant>.pipeline.<pipeline>.all_jobs |
| 312 | key = '%s.all_jobs' % pipekey |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 313 | self.statsd.incr(key) |
James E. Blair | 80ac158 | 2017-10-09 07:02:40 -0700 | [diff] [blame] | 314 | jobkey = '%s.project.%s.%s.%s.job.%s' % ( |
| 315 | pipekey, hostname, projectname, branchname, jobname) |
| 316 | # zuul.tenant.<tenant>.pipeline.<pipeline>.project. |
| 317 | # <host>.<project>.<branch>.job.<job>.<result> |
| 318 | key = '%s.%s' % (jobkey, build.result) |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 319 | if build.result in ['SUCCESS', 'FAILURE'] and build.start_time: |
| 320 | dt = int((build.end_time - build.start_time) * 1000) |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 321 | self.statsd.timing(key, dt) |
| 322 | self.statsd.incr(key) |
James E. Blair | 80ac158 | 2017-10-09 07:02:40 -0700 | [diff] [blame] | 323 | # zuul.tenant.<tenant>.pipeline.<pipeline>.project. |
| 324 | # <host>.<project>.<branch>.job.<job>.wait_time |
| 325 | key = '%s.wait_time' % jobkey |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 326 | dt = int((build.start_time - build.execute_time) * 1000) |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 327 | self.statsd.timing(key, dt) |
James E. Blair | 80ac158 | 2017-10-09 07:02:40 -0700 | [diff] [blame] | 328 | except Exception: |
James E. Blair | 23ec1ba | 2013-01-04 18:06:10 -0800 | [diff] [blame] | 329 | self.log.exception("Exception reporting runtime stats") |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 330 | event = BuildCompletedEvent(build) |
| 331 | self.result_event_queue.put(event) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 332 | self.wake_event.set() |
| 333 | |
Tobias Henkel | 34ee088 | 2017-07-31 22:26:12 +0200 | [diff] [blame] | 334 | def onMergeCompleted(self, build_set, merged, updated, |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 335 | commit, files, repo_state): |
Tobias Henkel | 34ee088 | 2017-07-31 22:26:12 +0200 | [diff] [blame] | 336 | event = MergeCompletedEvent(build_set, merged, |
James E. Blair | 1960d68 | 2017-04-28 15:44:14 -0700 | [diff] [blame] | 337 | updated, commit, files, repo_state) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 338 | self.result_event_queue.put(event) |
| 339 | self.wake_event.set() |
| 340 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 341 | def onNodesProvisioned(self, req): |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 342 | event = NodesProvisionedEvent(req) |
| 343 | self.result_event_queue.put(event) |
| 344 | self.wake_event.set() |
| 345 | |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 346 | def reconfigureTenant(self, tenant, project): |
| 347 | self.log.debug("Submitting tenant reconfiguration event for " |
| 348 | "%s due to project %s", tenant.name, project) |
| 349 | event = TenantReconfigureEvent(tenant, project) |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 350 | self.management_event_queue.put(event) |
| 351 | self.wake_event.set() |
| 352 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 353 | def reconfigure(self, config): |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 354 | self.log.debug("Submitting reconfiguration event") |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 355 | event = ReconfigureEvent(config) |
| 356 | self.management_event_queue.put(event) |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 357 | self.wake_event.set() |
| 358 | self.log.debug("Waiting for reconfiguration") |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 359 | event.wait() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 360 | self.log.debug("Reconfiguration complete") |
Sergey Lukjanov | 5d0438d | 2013-12-24 03:36:39 +0400 | [diff] [blame] | 361 | self.last_reconfigured = int(time.time()) |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 362 | # TODOv3(jeblair): reconfigure time should be per-tenant |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 363 | |
David Shrewsbury | 36b2adf | 2017-07-31 15:40:13 -0400 | [diff] [blame] | 364 | def autohold(self, tenant_name, project_name, job_name, reason, count): |
David Shrewsbury | ffab07a | 2017-07-24 12:45:07 -0400 | [diff] [blame] | 365 | key = (tenant_name, project_name, job_name) |
| 366 | if count == 0 and key in self.autohold_requests: |
| 367 | self.log.debug("Removing autohold for %s", key) |
| 368 | del self.autohold_requests[key] |
| 369 | else: |
| 370 | self.log.debug("Autohold requested for %s", key) |
David Shrewsbury | 36b2adf | 2017-07-31 15:40:13 -0400 | [diff] [blame] | 371 | self.autohold_requests[key] = (count, reason) |
David Shrewsbury | ffab07a | 2017-07-24 12:45:07 -0400 | [diff] [blame] | 372 | |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 373 | def promote(self, tenant_name, pipeline_name, change_ids): |
| 374 | event = PromoteEvent(tenant_name, pipeline_name, change_ids) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 375 | self.management_event_queue.put(event) |
| 376 | self.wake_event.set() |
| 377 | self.log.debug("Waiting for promotion") |
| 378 | event.wait() |
| 379 | self.log.debug("Promotion complete") |
| 380 | |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 381 | def enqueue(self, trigger_event): |
| 382 | event = EnqueueEvent(trigger_event) |
| 383 | self.management_event_queue.put(event) |
| 384 | self.wake_event.set() |
| 385 | self.log.debug("Waiting for enqueue") |
| 386 | event.wait() |
| 387 | self.log.debug("Enqueue complete") |
| 388 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 389 | def exit(self): |
| 390 | self.log.debug("Prepare to exit") |
| 391 | self._pause = True |
| 392 | self._exit = True |
| 393 | self.wake_event.set() |
| 394 | self.log.debug("Waiting for exit") |
| 395 | |
| 396 | def _get_queue_pickle_file(self): |
James E. Blair | d1de946 | 2017-06-23 20:53:09 +0100 | [diff] [blame] | 397 | state_dir = get_default(self.config, 'scheduler', 'state_dir', |
Tristan Cacqueray | 91601d7 | 2017-06-15 06:00:12 +0000 | [diff] [blame] | 398 | '/var/lib/zuul', expand_user=True) |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 399 | return os.path.join(state_dir, 'queue.pickle') |
| 400 | |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 401 | def _get_time_database_dir(self): |
James E. Blair | d1de946 | 2017-06-23 20:53:09 +0100 | [diff] [blame] | 402 | state_dir = get_default(self.config, 'scheduler', 'state_dir', |
Tristan Cacqueray | 91601d7 | 2017-06-15 06:00:12 +0000 | [diff] [blame] | 403 | '/var/lib/zuul', expand_user=True) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 404 | d = os.path.join(state_dir, 'times') |
| 405 | if not os.path.exists(d): |
| 406 | os.mkdir(d) |
| 407 | return d |
| 408 | |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 409 | def _get_project_key_dir(self): |
James E. Blair | d1de946 | 2017-06-23 20:53:09 +0100 | [diff] [blame] | 410 | state_dir = get_default(self.config, 'scheduler', 'state_dir', |
Tristan Cacqueray | 91601d7 | 2017-06-15 06:00:12 +0000 | [diff] [blame] | 411 | '/var/lib/zuul', expand_user=True) |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 412 | key_dir = os.path.join(state_dir, 'keys') |
| 413 | if not os.path.exists(key_dir): |
| 414 | os.mkdir(key_dir, 0o700) |
| 415 | st = os.stat(key_dir) |
| 416 | mode = st.st_mode & 0o777 |
| 417 | if mode != 0o700: |
| 418 | raise Exception("Project key directory %s must be mode 0700; " |
| 419 | "current mode is %o" % (key_dir, mode)) |
| 420 | return key_dir |
| 421 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 422 | def _save_queue(self): |
| 423 | pickle_file = self._get_queue_pickle_file() |
| 424 | events = [] |
| 425 | while not self.trigger_event_queue.empty(): |
| 426 | events.append(self.trigger_event_queue.get()) |
| 427 | self.log.debug("Queue length is %s" % len(events)) |
| 428 | if events: |
| 429 | self.log.debug("Saving queue") |
| 430 | pickle.dump(events, open(pickle_file, 'wb')) |
| 431 | |
| 432 | def _load_queue(self): |
| 433 | pickle_file = self._get_queue_pickle_file() |
| 434 | if os.path.exists(pickle_file): |
| 435 | self.log.debug("Loading queue") |
| 436 | events = pickle.load(open(pickle_file, 'rb')) |
| 437 | self.log.debug("Queue length is %s" % len(events)) |
| 438 | for event in events: |
| 439 | self.trigger_event_queue.put(event) |
| 440 | else: |
| 441 | self.log.debug("No queue file found") |
| 442 | |
| 443 | def _delete_queue(self): |
| 444 | pickle_file = self._get_queue_pickle_file() |
| 445 | if os.path.exists(pickle_file): |
| 446 | self.log.debug("Deleting saved queue") |
| 447 | os.unlink(pickle_file) |
| 448 | |
| 449 | def resume(self): |
| 450 | try: |
| 451 | self._load_queue() |
| 452 | except: |
| 453 | self.log.exception("Unable to load queue") |
| 454 | try: |
| 455 | self._delete_queue() |
| 456 | except: |
| 457 | self.log.exception("Unable to delete saved queue") |
| 458 | self.log.debug("Resuming queue processing") |
| 459 | self.wake_event.set() |
| 460 | |
| 461 | def _doPauseEvent(self): |
| 462 | if self._exit: |
| 463 | self.log.debug("Exiting") |
| 464 | self._save_queue() |
| 465 | os._exit(0) |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 466 | |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 467 | def _doReconfigureEvent(self, event): |
| 468 | # This is called in the scheduler loop after another thread submits |
| 469 | # a request |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 470 | self.layout_lock.acquire() |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 471 | self.config = event.config |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 472 | try: |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 473 | self.log.debug("Full reconfiguration beginning") |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 474 | loader = configloader.ConfigLoader() |
| 475 | abide = loader.loadConfig( |
James E. Blair | 3984036 | 2017-06-23 20:34:02 +0100 | [diff] [blame] | 476 | self.config.get('scheduler', 'tenant_config'), |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 477 | self._get_project_key_dir(), |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 478 | self, self.merger, self.connections) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 479 | for tenant in abide.tenants.values(): |
| 480 | self._reconfigureTenant(tenant) |
| 481 | self.abide = abide |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 482 | finally: |
| 483 | self.layout_lock.release() |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 484 | self.log.debug("Full reconfiguration complete") |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 485 | |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 486 | def _doTenantReconfigureEvent(self, event): |
| 487 | # This is called in the scheduler loop after another thread submits |
| 488 | # a request |
| 489 | self.layout_lock.acquire() |
| 490 | try: |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 491 | self.log.debug("Tenant reconfiguration beginning") |
| 492 | # If a change landed to a project, clear out the cached |
| 493 | # config before reconfiguring. |
James E. Blair | 419a867 | 2017-10-18 14:48:25 -0700 | [diff] [blame^] | 494 | for project in event.projects: |
| 495 | project.unparsed_config = None |
| 496 | old_tenant = self.abide.tenants[event.tenant_name] |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 497 | loader = configloader.ConfigLoader() |
| 498 | abide = loader.reloadTenant( |
James E. Blair | 3984036 | 2017-06-23 20:34:02 +0100 | [diff] [blame] | 499 | self.config.get('scheduler', 'tenant_config'), |
Ricardo Carrillo Cruz | 22994f9 | 2016-12-02 11:41:58 +0000 | [diff] [blame] | 500 | self._get_project_key_dir(), |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 501 | self, self.merger, self.connections, |
James E. Blair | 419a867 | 2017-10-18 14:48:25 -0700 | [diff] [blame^] | 502 | self.abide, old_tenant) |
| 503 | tenant = abide.tenants[event.tenant_name] |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 504 | self._reconfigureTenant(tenant) |
| 505 | self.abide = abide |
| 506 | finally: |
| 507 | self.layout_lock.release() |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 508 | self.log.debug("Tenant reconfiguration complete") |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 509 | |
James E. Blair | aa30de4 | 2017-04-25 10:56:59 -0700 | [diff] [blame] | 510 | def _reenqueueGetProject(self, tenant, item): |
| 511 | project = item.change.project |
James E. Blair | 6053de4 | 2017-04-05 11:27:11 -0700 | [diff] [blame] | 512 | # Attempt to get the same project as the one passed in. If |
| 513 | # the project is now found on a different connection, return |
| 514 | # the new version of the project. If it is no longer |
| 515 | # available (due to a connection being removed), return None. |
James E. Blair | aa30de4 | 2017-04-25 10:56:59 -0700 | [diff] [blame] | 516 | (trusted, new_project) = tenant.getProject(project.canonical_name) |
James E. Blair | 6053de4 | 2017-04-05 11:27:11 -0700 | [diff] [blame] | 517 | if new_project: |
| 518 | return new_project |
James E. Blair | aa30de4 | 2017-04-25 10:56:59 -0700 | [diff] [blame] | 519 | # If this is a non-live item we may be looking at a |
| 520 | # "foreign" project, ie, one which is not defined in the |
| 521 | # config but is constructed ad-hoc to satisfy a |
| 522 | # cross-repo-dependency. Find the corresponding live item |
| 523 | # and use its source. |
| 524 | child = item |
| 525 | while child and not child.live: |
| 526 | # This assumes that the queue does not branch behind this |
| 527 | # item, which is currently true for non-live items; if |
| 528 | # that changes, this traversal will need to be more |
| 529 | # complex. |
| 530 | if child.items_behind: |
| 531 | child = child.items_behind[0] |
| 532 | else: |
| 533 | child = None |
| 534 | if child is item: |
| 535 | return None |
| 536 | if child and child.live: |
| 537 | (child_trusted, child_project) = tenant.getProject( |
| 538 | child.change.project.canonical_name) |
| 539 | if child_project: |
| 540 | source = child_project.source |
| 541 | new_project = source.getProject(project.name) |
| 542 | return new_project |
| 543 | return None |
James E. Blair | 6053de4 | 2017-04-05 11:27:11 -0700 | [diff] [blame] | 544 | |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 545 | def _reenqueueTenant(self, old_tenant, tenant): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 546 | for name, new_pipeline in tenant.layout.pipelines.items(): |
| 547 | old_pipeline = old_tenant.layout.pipelines.get(name) |
| 548 | if not old_pipeline: |
| 549 | self.log.warning("No old pipeline matching %s found " |
| 550 | "when reconfiguring" % name) |
| 551 | continue |
| 552 | self.log.debug("Re-enqueueing changes for pipeline %s" % name) |
| 553 | items_to_remove = [] |
| 554 | builds_to_cancel = [] |
| 555 | last_head = None |
| 556 | for shared_queue in old_pipeline.queues: |
| 557 | for item in shared_queue.queue: |
| 558 | if not item.item_ahead: |
| 559 | last_head = item |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 560 | item.pipeline = None |
| 561 | item.queue = None |
James E. Blair | 6053de4 | 2017-04-05 11:27:11 -0700 | [diff] [blame] | 562 | item.change.project = self._reenqueueGetProject( |
James E. Blair | aa30de4 | 2017-04-25 10:56:59 -0700 | [diff] [blame] | 563 | tenant, item) |
| 564 | item.item_ahead = None |
| 565 | item.items_behind = [] |
James E. Blair | 027ba99 | 2017-09-20 13:48:32 -0700 | [diff] [blame] | 566 | reenqueued = False |
| 567 | if item.change.project: |
| 568 | try: |
| 569 | reenqueued = new_pipeline.manager.reEnqueueItem( |
| 570 | item, last_head) |
| 571 | except Exception: |
| 572 | self.log.exception( |
| 573 | "Exception while re-enqueing item %s", |
| 574 | item) |
| 575 | if reenqueued: |
James E. Blair | 3b5ff3b | 2016-07-21 10:08:24 -0700 | [diff] [blame] | 576 | for build in item.current_build_set.getBuilds(): |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 577 | new_job = item.getJob(build.job.name) |
| 578 | if new_job: |
| 579 | build.job = new_job |
James E. Blair | 3b5ff3b | 2016-07-21 10:08:24 -0700 | [diff] [blame] | 580 | else: |
| 581 | item.removeBuild(build) |
| 582 | builds_to_cancel.append(build) |
| 583 | else: |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 584 | items_to_remove.append(item) |
| 585 | for item in items_to_remove: |
James E. Blair | b5a8f0b | 2017-07-07 17:01:18 -0700 | [diff] [blame] | 586 | self.log.warning( |
| 587 | "Removing item %s during reconfiguration" % (item,)) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 588 | for build in item.current_build_set.getBuilds(): |
| 589 | builds_to_cancel.append(build) |
| 590 | for build in builds_to_cancel: |
| 591 | self.log.warning( |
| 592 | "Canceling build %s during reconfiguration" % (build,)) |
| 593 | try: |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 594 | self.executor.cancel(build) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 595 | except Exception: |
| 596 | self.log.exception( |
| 597 | "Exception while canceling build %s " |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 598 | "for change %s" % (build, build.build_set.item.change)) |
Tobias Henkel | fb91a49 | 2017-02-15 07:29:43 +0100 | [diff] [blame] | 599 | finally: |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 600 | tenant.semaphore_handler.release( |
| 601 | build.build_set.item, build.job) |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 602 | |
| 603 | def _reconfigureTenant(self, tenant): |
| 604 | # This is called from _doReconfigureEvent while holding the |
| 605 | # layout lock |
| 606 | old_tenant = self.abide.tenants.get(tenant.name) |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 607 | |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 608 | if old_tenant: |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 609 | # Copy over semaphore handler so we don't loose the currently |
| 610 | # held semaphores. |
| 611 | tenant.semaphore_handler = old_tenant.semaphore_handler |
| 612 | |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 613 | self._reenqueueTenant(old_tenant, tenant) |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 614 | |
James E. Blair | b0a95ab | 2017-10-18 09:39:18 -0700 | [diff] [blame] | 615 | # TODOv3(jeblair): update for tenants |
| 616 | # self.maintainConnectionCache() |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 617 | self.connections.reconfigureDrivers(tenant) |
Tobias Henkel | 9a0e194 | 2017-03-20 16:16:02 +0100 | [diff] [blame] | 618 | |
James E. Blair | e511d2f | 2016-12-08 15:22:26 -0800 | [diff] [blame] | 619 | # TODOv3(jeblair): remove postconfig calls? |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 620 | for pipeline in tenant.layout.pipelines.values(): |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 621 | for trigger in pipeline.triggers: |
| 622 | trigger.postConfig(pipeline) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 623 | for reporter in pipeline.actions: |
| 624 | reporter.postConfig() |
Jesse Keating | 71a47ff | 2017-06-06 11:36:43 -0700 | [diff] [blame] | 625 | self.tenant_last_reconfigured[tenant.name] = int(time.time()) |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 626 | if self.statsd: |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 627 | try: |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 628 | for pipeline in tenant.layout.pipelines.values(): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 629 | items = len(pipeline.getAllItems()) |
| 630 | # stats.gauges.zuul.pipeline.NAME.current_changes |
| 631 | key = 'zuul.pipeline.%s' % pipeline.name |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 632 | self.statsd.gauge(key + '.current_changes', items) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 633 | except Exception: |
| 634 | self.log.exception("Exception reporting initial " |
| 635 | "pipeline stats:") |
| 636 | |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 637 | def _doPromoteEvent(self, event): |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 638 | tenant = self.abide.tenants.get(event.tenant_name) |
| 639 | pipeline = tenant.layout.pipelines[event.pipeline_name] |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 640 | change_ids = [c.split(',') for c in event.change_ids] |
| 641 | items_to_enqueue = [] |
| 642 | change_queue = None |
| 643 | for shared_queue in pipeline.queues: |
| 644 | if change_queue: |
| 645 | break |
| 646 | for item in shared_queue.queue: |
| 647 | if (item.change.number == change_ids[0][0] and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 648 | item.change.patchset == change_ids[0][1]): |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 649 | change_queue = shared_queue |
| 650 | break |
| 651 | if not change_queue: |
| 652 | raise Exception("Unable to find shared change queue for %s" % |
| 653 | event.change_ids[0]) |
| 654 | for number, patchset in change_ids: |
| 655 | found = False |
| 656 | for item in change_queue.queue: |
| 657 | if (item.change.number == number and |
Joshua Hesketh | 29d99b7 | 2014-08-19 16:27:42 +1000 | [diff] [blame] | 658 | item.change.patchset == patchset): |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 659 | found = True |
| 660 | items_to_enqueue.append(item) |
| 661 | break |
| 662 | if not found: |
| 663 | raise Exception("Unable to find %s,%s in queue %s" % |
| 664 | (number, patchset, change_queue)) |
| 665 | for item in change_queue.queue[:]: |
| 666 | if item not in items_to_enqueue: |
| 667 | items_to_enqueue.append(item) |
| 668 | pipeline.manager.cancelJobs(item) |
| 669 | pipeline.manager.dequeueItem(item) |
| 670 | for item in items_to_enqueue: |
Sean Dague | f39b9ca | 2014-01-10 21:34:35 -0500 | [diff] [blame] | 671 | pipeline.manager.addChange( |
| 672 | item.change, |
| 673 | enqueue_time=item.enqueue_time, |
James E. Blair | f9ab884 | 2014-07-10 13:12:07 -0700 | [diff] [blame] | 674 | quiet=True, |
| 675 | ignore_requirements=True) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 676 | |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 677 | def _doEnqueueEvent(self, event): |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 678 | tenant = self.abide.tenants.get(event.tenant_name) |
James E. Blair | 0ffa010 | 2017-03-30 13:11:33 -0700 | [diff] [blame] | 679 | (trusted, project) = tenant.getProject(event.project_name) |
Paul Belanger | baca313 | 2016-11-04 12:49:54 -0400 | [diff] [blame] | 680 | pipeline = tenant.layout.pipelines[event.forced_pipeline] |
James E. Blair | 6053de4 | 2017-04-05 11:27:11 -0700 | [diff] [blame] | 681 | change = project.source.getChange(event, project) |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 682 | self.log.debug("Event %s for change %s was directly assigned " |
| 683 | "to pipeline %s" % (event, change, self)) |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 684 | pipeline.manager.addChange(change, ignore_requirements=True) |
| 685 | |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 686 | def _areAllBuildsComplete(self): |
| 687 | self.log.debug("Checking if all builds are complete") |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 688 | if self.merger.areMergesOutstanding(): |
| 689 | self.log.debug("Waiting on merger") |
| 690 | return False |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 691 | waiting = False |
Paul Belanger | debd7a7 | 2016-11-11 19:56:15 -0500 | [diff] [blame] | 692 | for tenant in self.abide.tenants.values(): |
| 693 | for pipeline in tenant.layout.pipelines.values(): |
| 694 | for item in pipeline.getAllItems(): |
| 695 | for build in item.current_build_set.getBuilds(): |
| 696 | if build.result is None: |
| 697 | self.log.debug("%s waiting on %s" % |
| 698 | (pipeline.manager, build)) |
| 699 | waiting = True |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 700 | if not waiting: |
| 701 | self.log.debug("All builds are complete") |
| 702 | return True |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 703 | return False |
| 704 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 705 | def run(self): |
James E. Blair | 552b54f | 2016-07-22 13:55:32 -0700 | [diff] [blame] | 706 | if self.statsd: |
James E. Blair | 71e9412 | 2012-12-24 17:53:08 -0800 | [diff] [blame] | 707 | self.log.debug("Statsd enabled") |
| 708 | else: |
| 709 | self.log.debug("Statsd disabled because python statsd " |
| 710 | "package not found") |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 711 | while True: |
| 712 | self.log.debug("Run handler sleeping") |
| 713 | self.wake_event.wait() |
| 714 | self.wake_event.clear() |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 715 | if self._stopped: |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 716 | self.log.debug("Run handler stopping") |
James E. Blair | b0fcae4 | 2012-07-17 11:12:10 -0700 | [diff] [blame] | 717 | return |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 718 | self.log.debug("Run handler awake") |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 719 | self.run_handler_lock.acquire() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 720 | try: |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 721 | while not self.management_event_queue.empty(): |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 722 | self.process_management_queue() |
James E. Blair | cdccd97 | 2013-07-01 12:10:22 -0700 | [diff] [blame] | 723 | |
James E. Blair | 263fba9 | 2013-02-27 13:07:19 -0800 | [diff] [blame] | 724 | # Give result events priority -- they let us stop builds, |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 725 | # whereas trigger events cause us to execute builds. |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 726 | while not self.result_event_queue.empty(): |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 727 | self.process_result_queue() |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 728 | |
| 729 | if not self._pause: |
| 730 | while not self.trigger_event_queue.empty(): |
James E. Blair | 263fba9 | 2013-02-27 13:07:19 -0800 | [diff] [blame] | 731 | self.process_event_queue() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 732 | |
James E. Blair | 5d5bc2b | 2012-07-06 10:24:01 -0700 | [diff] [blame] | 733 | if self._pause and self._areAllBuildsComplete(): |
| 734 | self._doPauseEvent() |
James E. Blair | e9d45c3 | 2012-05-31 09:56:45 -0700 | [diff] [blame] | 735 | |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 736 | for tenant in self.abide.tenants.values(): |
| 737 | for pipeline in tenant.layout.pipelines.values(): |
| 738 | while pipeline.manager.processQueue(): |
| 739 | pass |
James E. Blair | 0e933c5 | 2013-07-11 10:18:52 -0700 | [diff] [blame] | 740 | |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 741 | except Exception: |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 742 | self.log.exception("Exception in run handler:") |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 743 | # There may still be more events to process |
| 744 | self.wake_event.set() |
| 745 | finally: |
| 746 | self.run_handler_lock.release() |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 747 | |
James E. Blair | b0a95ab | 2017-10-18 09:39:18 -0700 | [diff] [blame] | 748 | def maintainConnectionCache(self): |
| 749 | # TODOv3(jeblair): update for tenants |
| 750 | relevant = set() |
| 751 | for tenant in self.abide.tenants.values(): |
| 752 | for pipeline in tenant.layout.pipelines.values(): |
| 753 | self.log.debug("Gather relevant cache items for: %s" % |
| 754 | pipeline) |
| 755 | |
| 756 | for item in pipeline.getAllItems(): |
| 757 | relevant.add(item.change) |
| 758 | relevant.update(item.change.getRelatedChanges()) |
| 759 | for connection in self.connections.values(): |
| 760 | connection.maintainCache(relevant) |
| 761 | self.log.debug( |
| 762 | "End maintain connection cache for: %s" % connection) |
| 763 | self.log.debug("Connection cache size: %s" % len(relevant)) |
| 764 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 765 | def process_event_queue(self): |
| 766 | self.log.debug("Fetching trigger event") |
| 767 | event = self.trigger_event_queue.get() |
| 768 | self.log.debug("Processing trigger event %s" % event) |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 769 | try: |
James E. Blair | aa30de4 | 2017-04-25 10:56:59 -0700 | [diff] [blame] | 770 | full_project_name = ('/'.join([event.project_hostname, |
| 771 | event.project_name])) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 772 | for tenant in self.abide.tenants.values(): |
James E. Blair | aa30de4 | 2017-04-25 10:56:59 -0700 | [diff] [blame] | 773 | (trusted, project) = tenant.getProject(full_project_name) |
| 774 | if project is None: |
| 775 | continue |
| 776 | try: |
| 777 | change = project.source.getChange(event) |
| 778 | except exceptions.ChangeNotFound as e: |
| 779 | self.log.debug("Unable to get change %s from " |
| 780 | "source %s", |
| 781 | e.change, project.source) |
| 782 | continue |
James E. Blair | 72facdc | 2017-08-17 10:29:12 -0700 | [diff] [blame] | 783 | if ((event.branch_updated and |
| 784 | hasattr(change, 'files') and |
| 785 | change.updatesConfig()) or |
| 786 | event.branch_created or |
| 787 | event.branch_deleted): |
| 788 | # The change that just landed updates the config |
| 789 | # or a branch was just created or deleted. Clear |
| 790 | # out cached data for this project and perform a |
| 791 | # reconfiguration. |
James E. Blair | a615c36 | 2017-10-02 17:34:42 -0700 | [diff] [blame] | 792 | self.reconfigureTenant(tenant, change.project) |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 793 | for pipeline in tenant.layout.pipelines.values(): |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 794 | if event.isPatchsetCreated(): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 795 | pipeline.manager.removeOldVersionsOfChange(change) |
Jan Hruban | 324ca5b | 2015-11-05 19:28:54 +0100 | [diff] [blame] | 796 | elif event.isChangeAbandoned(): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 797 | pipeline.manager.removeAbandonedChange(change) |
| 798 | if pipeline.manager.eventMatches(event, change): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 799 | pipeline.manager.addChange(change) |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 800 | finally: |
James E. Blair | ff79197 | 2013-01-09 11:45:43 -0800 | [diff] [blame] | 801 | self.trigger_event_queue.task_done() |
James E. Blair | 1e8dd89 | 2012-05-30 09:15:05 -0700 | [diff] [blame] | 802 | |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 803 | def process_management_queue(self): |
| 804 | self.log.debug("Fetching management event") |
| 805 | event = self.management_event_queue.get() |
| 806 | self.log.debug("Processing management event %s" % event) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 807 | try: |
| 808 | if isinstance(event, ReconfigureEvent): |
| 809 | self._doReconfigureEvent(event) |
James E. Blair | 21603e6 | 2017-02-20 16:23:05 -0500 | [diff] [blame] | 810 | elif isinstance(event, TenantReconfigureEvent): |
James E. Blair | 646322f | 2017-01-27 15:50:34 -0800 | [diff] [blame] | 811 | self._doTenantReconfigureEvent(event) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 812 | elif isinstance(event, PromoteEvent): |
| 813 | self._doPromoteEvent(event) |
James E. Blair | d27a96d | 2014-07-10 13:25:13 -0700 | [diff] [blame] | 814 | elif isinstance(event, EnqueueEvent): |
| 815 | self._doEnqueueEvent(event.trigger_event) |
James E. Blair | 36658cf | 2013-12-06 17:53:48 -0800 | [diff] [blame] | 816 | else: |
| 817 | self.log.error("Unable to handle event %s" % event) |
| 818 | event.done() |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 819 | except Exception: |
James E. Blair | 59424ea | 2017-07-11 09:52:58 -0700 | [diff] [blame] | 820 | self.log.exception("Exception in management event:") |
Morgan Fainberg | 1b9bd78 | 2016-05-30 14:03:30 -0700 | [diff] [blame] | 821 | event.exception(sys.exc_info()) |
James E. Blair | 468c851 | 2013-12-06 13:27:19 -0800 | [diff] [blame] | 822 | self.management_event_queue.task_done() |
| 823 | |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 824 | def process_result_queue(self): |
| 825 | self.log.debug("Fetching result event") |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 826 | event = self.result_event_queue.get() |
| 827 | self.log.debug("Processing result event %s" % event) |
| 828 | try: |
| 829 | if isinstance(event, BuildStartedEvent): |
| 830 | self._doBuildStartedEvent(event) |
| 831 | elif isinstance(event, BuildCompletedEvent): |
| 832 | self._doBuildCompletedEvent(event) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 833 | elif isinstance(event, MergeCompletedEvent): |
| 834 | self._doMergeCompletedEvent(event) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 835 | elif isinstance(event, NodesProvisionedEvent): |
| 836 | self._doNodesProvisionedEvent(event) |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 837 | else: |
| 838 | self.log.error("Unable to handle event %s" % event) |
| 839 | finally: |
| 840 | self.result_event_queue.task_done() |
| 841 | |
| 842 | def _doBuildStartedEvent(self, event): |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 843 | build = event.build |
| 844 | if build.build_set is not build.build_set.item.current_build_set: |
| 845 | self.log.warning("Build %s is not in the current build set" % |
| 846 | (build,)) |
| 847 | return |
| 848 | pipeline = build.build_set.item.pipeline |
| 849 | if not pipeline: |
| 850 | self.log.warning("Build %s is not associated with a pipeline" % |
| 851 | (build,)) |
| 852 | return |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 853 | try: |
| 854 | build.estimated_time = float(self.time_database.getEstimatedTime( |
James E. Blair | ae0f23c | 2017-09-13 10:55:15 -0600 | [diff] [blame] | 855 | build)) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 856 | except Exception: |
| 857 | self.log.exception("Exception estimating build time:") |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 858 | pipeline.manager.onBuildStarted(event.build) |
James E. Blair | a84f0e4 | 2014-02-06 07:09:22 -0800 | [diff] [blame] | 859 | |
| 860 | def _doBuildCompletedEvent(self, event): |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 861 | build = event.build |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 862 | |
| 863 | # Regardless of any other conditions which might cause us not |
| 864 | # to pass this on to the pipeline manager, make sure we return |
| 865 | # the nodes to nodepool. |
| 866 | try: |
| 867 | nodeset = build.build_set.getJobNodeSet(build.job.name) |
David Shrewsbury | ffab07a | 2017-07-24 12:45:07 -0400 | [diff] [blame] | 868 | autohold_key = (build.pipeline.layout.tenant.name, |
| 869 | build.build_set.item.change.project.canonical_name, |
| 870 | build.job.name) |
| 871 | |
| 872 | try: |
| 873 | self.nodepool.holdNodeSet(nodeset, autohold_key) |
| 874 | except Exception: |
David Shrewsbury | f21bb28 | 2017-10-13 11:10:00 -0400 | [diff] [blame] | 875 | self.log.exception("Unable to process autohold for %s:", |
David Shrewsbury | ffab07a | 2017-07-24 12:45:07 -0400 | [diff] [blame] | 876 | autohold_key) |
David Shrewsbury | f21bb28 | 2017-10-13 11:10:00 -0400 | [diff] [blame] | 877 | if autohold_key in self.autohold_requests: |
| 878 | self.log.debug("Removing autohold %s due to exception", |
| 879 | autohold_key) |
| 880 | del self.autohold_requests[autohold_key] |
David Shrewsbury | ffab07a | 2017-07-24 12:45:07 -0400 | [diff] [blame] | 881 | |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 882 | self.nodepool.returnNodeSet(nodeset) |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 883 | except Exception: |
| 884 | self.log.exception("Unable to return nodeset %s" % (nodeset,)) |
| 885 | |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 886 | if build.build_set is not build.build_set.item.current_build_set: |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 887 | self.log.debug("Build %s is not in the current build set" % |
| 888 | (build,)) |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 889 | return |
| 890 | pipeline = build.build_set.item.pipeline |
| 891 | if not pipeline: |
| 892 | self.log.warning("Build %s is not associated with a pipeline" % |
| 893 | (build,)) |
| 894 | return |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 895 | if build.end_time and build.start_time and build.result: |
| 896 | duration = build.end_time - build.start_time |
Paul Belanger | 87e4ab0 | 2016-06-08 14:17:20 -0400 | [diff] [blame] | 897 | try: |
James E. Blair | ae0f23c | 2017-09-13 10:55:15 -0600 | [diff] [blame] | 898 | self.time_database.update(build, duration, build.result) |
Paul Belanger | 87e4ab0 | 2016-06-08 14:17:20 -0400 | [diff] [blame] | 899 | except Exception: |
| 900 | self.log.exception("Exception recording build time:") |
James E. Blair | 4076e2b | 2014-01-28 12:42:20 -0800 | [diff] [blame] | 901 | pipeline.manager.onBuildCompleted(event.build) |
| 902 | |
| 903 | def _doMergeCompletedEvent(self, event): |
| 904 | build_set = event.build_set |
| 905 | if build_set is not build_set.item.current_build_set: |
| 906 | self.log.warning("Build set %s is not current" % (build_set,)) |
| 907 | return |
| 908 | pipeline = build_set.item.pipeline |
| 909 | if not pipeline: |
| 910 | self.log.warning("Build set %s is not associated with a pipeline" % |
| 911 | (build_set,)) |
| 912 | return |
| 913 | pipeline.manager.onMergeCompleted(event) |
James E. Blair | ee74361 | 2012-05-29 14:49:32 -0700 | [diff] [blame] | 914 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 915 | def _doNodesProvisionedEvent(self, event): |
| 916 | request = event.request |
David Shrewsbury | 94e9588 | 2017-10-04 15:26:04 -0400 | [diff] [blame] | 917 | request_id = event.request_id |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 918 | build_set = request.build_set |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 919 | |
David Shrewsbury | 94e9588 | 2017-10-04 15:26:04 -0400 | [diff] [blame] | 920 | self.nodepool.acceptNodes(request, request_id) |
James E. Blair | cbbce0d | 2017-05-19 07:28:29 -0700 | [diff] [blame] | 921 | if request.canceled: |
| 922 | return |
James E. Blair | a38c28e | 2017-01-04 10:33:20 -0800 | [diff] [blame] | 923 | |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 924 | if build_set is not build_set.item.current_build_set: |
| 925 | self.log.warning("Build set %s is not current" % (build_set,)) |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 926 | if request.fulfilled: |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 927 | self.nodepool.returnNodeSet(request.nodeset) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 928 | return |
| 929 | pipeline = build_set.item.pipeline |
| 930 | if not pipeline: |
| 931 | self.log.warning("Build set %s is not associated with a pipeline" % |
| 932 | (build_set,)) |
James E. Blair | 6ab79e0 | 2017-01-06 10:10:17 -0800 | [diff] [blame] | 933 | if request.fulfilled: |
James E. Blair | 1511bc3 | 2017-01-18 09:25:31 -0800 | [diff] [blame] | 934 | self.nodepool.returnNodeSet(request.nodeset) |
James E. Blair | 8d69239 | 2016-04-08 17:47:58 -0700 | [diff] [blame] | 935 | return |
| 936 | pipeline.manager.onNodesProvisioned(event) |
| 937 | |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 938 | def formatStatusJSON(self, tenant_name): |
James E. Blair | 59fdbac | 2015-12-07 17:08:06 -0800 | [diff] [blame] | 939 | # TODOv3(jeblair): use tenants |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 940 | data = {} |
Sergey Lukjanov | 5ba961b | 2013-12-27 01:21:04 +0400 | [diff] [blame] | 941 | |
| 942 | data['zuul_version'] = self.zuul_version |
Tobias Henkel | b4407fc | 2017-07-07 13:52:56 +0200 | [diff] [blame] | 943 | websocket_url = get_default(self.config, 'web', 'websocket_url', None) |
Sergey Lukjanov | 5ba961b | 2013-12-27 01:21:04 +0400 | [diff] [blame] | 944 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 945 | if self._pause: |
| 946 | ret = '<p><b>Queue only mode:</b> preparing to ' |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 947 | if self._exit: |
| 948 | ret += 'exit' |
| 949 | ret += ', queue length: %s' % self.trigger_event_queue.qsize() |
| 950 | ret += '</p>' |
| 951 | data['message'] = ret |
| 952 | |
James E. Blair | fb682cc | 2013-02-26 15:23:27 -0800 | [diff] [blame] | 953 | data['trigger_event_queue'] = {} |
| 954 | data['trigger_event_queue']['length'] = \ |
| 955 | self.trigger_event_queue.qsize() |
| 956 | data['result_event_queue'] = {} |
| 957 | data['result_event_queue']['length'] = \ |
| 958 | self.result_event_queue.qsize() |
James E. Blair | 3692b61 | 2017-10-18 13:59:41 -0700 | [diff] [blame] | 959 | data['management_event_queue'] = {} |
| 960 | data['management_event_queue']['length'] = \ |
| 961 | self.management_event_queue.qsize() |
James E. Blair | fb682cc | 2013-02-26 15:23:27 -0800 | [diff] [blame] | 962 | |
Sergey Lukjanov | 5d0438d | 2013-12-24 03:36:39 +0400 | [diff] [blame] | 963 | if self.last_reconfigured: |
| 964 | data['last_reconfigured'] = self.last_reconfigured * 1000 |
| 965 | |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 966 | pipelines = [] |
| 967 | data['pipelines'] = pipelines |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 968 | tenant = self.abide.tenants.get(tenant_name) |
| 969 | for pipeline in tenant.layout.pipelines.values(): |
Tobias Henkel | b4407fc | 2017-07-07 13:52:56 +0200 | [diff] [blame] | 970 | pipelines.append(pipeline.formatStatusJSON(websocket_url)) |
James E. Blair | 8dbd56a | 2012-12-22 10:55:10 -0800 | [diff] [blame] | 971 | return json.dumps(data) |