Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 5 | import logging, time |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 6 | |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 7 | import base_event, board_enumerator, build_event, deduping_scheduler |
| 8 | import forgiving_config_parser, manifest_versions, task, timed_event |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class Driver(object): |
| 12 | """Implements the main loop of the suite_scheduler. |
| 13 | |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 14 | @var EVENT_CLASSES: list of the event classes Driver supports. |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 15 | @var _LOOP_INTERVAL_SECONDS: seconds to wait between loop iterations. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 16 | |
| 17 | @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE. |
Chris Masone | 3fba86f | 2012-04-03 10:06:56 -0700 | [diff] [blame] | 18 | @var _enumerator: a BoardEnumerator, used to list plaforms known to |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 19 | the AFE |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 20 | @var _events: dict of BaseEvents to be handled each time through main loop. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 21 | """ |
| 22 | |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 23 | EVENT_CLASSES = [timed_event.Nightly, timed_event.Weekly, |
| 24 | build_event.NewBuild] |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 25 | _LOOP_INTERVAL_SECONDS = 5 * 60 |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 26 | |
| 27 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 28 | def __init__(self, scheduler, enumerator): |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 29 | """Constructor |
| 30 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 31 | @param scheduler: an instance of deduping_scheduler.DedupingScheduler. |
| 32 | @param enumerator: an instance of board_enumerator.BoardEnumerator. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 33 | """ |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 34 | self._scheduler = scheduler |
| 35 | self._enumerator = enumerator |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 36 | |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 37 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 38 | def RereadAndReprocessConfig(self, config, mv): |
| 39 | """Re-read config, re-populate self._events and recreate task lists. |
| 40 | |
| 41 | @param config: an instance of ForgivingConfigParser. |
| 42 | @param mv: an instance of ManifestVersions. |
| 43 | """ |
| 44 | config.reread() |
| 45 | new_events = self._CreateEventsWithTasks(config, mv) |
| 46 | for keyword, event in self._events.iteritems(): |
| 47 | event.Merge(new_events[keyword]) |
| 48 | |
| 49 | |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 50 | def SetUpEventsAndTasks(self, config, mv): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 51 | """Populate self._events and create task lists from config. |
| 52 | |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 53 | @param config: an instance of ForgivingConfigParser. |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 54 | @param mv: an instance of ManifestVersions. |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 55 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 56 | self._events = self._CreateEventsWithTasks(config, mv) |
| 57 | |
| 58 | |
| 59 | def _CreateEventsWithTasks(self, config, mv): |
| 60 | """Create task lists from config, and assign to newly-minted events. |
| 61 | |
| 62 | Calling multiple times should start afresh each time. |
| 63 | |
| 64 | @param config: an instance of ForgivingConfigParser. |
| 65 | @param mv: an instance of ManifestVersions. |
| 66 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 67 | events = {} |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 68 | for klass in self.EVENT_CLASSES: |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 69 | events[klass.KEYWORD] = klass.CreateFromConfig(config, mv) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 70 | |
| 71 | tasks = self.TasksFromConfig(config) |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 72 | for keyword, task_list in tasks.iteritems(): |
| 73 | if keyword in events: |
| 74 | events[keyword].tasks = task_list |
| 75 | else: |
| 76 | logging.warn('%s, is an unknown keyword.', keyword) |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 77 | return events |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 78 | |
| 79 | |
| 80 | def TasksFromConfig(self, config): |
| 81 | """Generate a dict of {event_keyword: [tasks]} mappings from |config|. |
| 82 | |
| 83 | For each section in |config| that encodes a Task, instantiate a Task |
| 84 | object. Determine the event that Task is supposed to run_on and |
| 85 | append the object to a list associated with the appropriate event |
| 86 | keyword. Return a dictionary of these keyword: list of task mappings. |
| 87 | |
| 88 | @param config: a ForgivingConfigParser containing tasks to be parsed. |
| 89 | @return dict of {event_keyword: [tasks]} mappings. |
| 90 | @raise MalformedConfigEntry on a task parsing error. |
| 91 | """ |
| 92 | tasks = {} |
| 93 | for section in config.sections(): |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 94 | if not base_event.HonoredSection(section): |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 95 | try: |
| 96 | keyword, new_task = task.Task.CreateFromConfigSection( |
| 97 | config, section) |
| 98 | except task.MalformedConfigEntry as e: |
| 99 | logging.warn('%s is malformed: %s', section, e) |
| 100 | continue |
| 101 | tasks.setdefault(keyword, []).append(new_task) |
| 102 | return tasks |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 103 | |
| 104 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 105 | def RunForever(self, config, mv): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 106 | """Main loop of the scheduler. Runs til the process is killed. |
| 107 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 108 | @param config: an instance of ForgivingConfigParser. |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 109 | @param mv: an instance of manifest_versions.ManifestVersions. |
| 110 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 111 | for event in self._events.itervalues(): |
Chris Masone | 73a7838 | 2012-04-20 13:25:51 -0700 | [diff] [blame] | 112 | event.Prepare() |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 113 | while True: |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 114 | try: |
| 115 | self.HandleEventsOnce(mv) |
Scott Zawalski | c15c6b4 | 2012-07-09 13:16:05 -0400 | [diff] [blame] | 116 | except board_enumerator.EnumeratorException as e: |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 117 | logging.warn('Failed to enumerate boards: %r', e) |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 118 | mv.Update() |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 119 | time.sleep(self._LOOP_INTERVAL_SECONDS) |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 120 | self.RereadAndReprocessConfig(config, mv) |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 121 | |
| 122 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 123 | def HandleEventsOnce(self, mv): |
| 124 | """One turn through the loop. Separated out for unit testing. |
| 125 | |
| 126 | @param mv: an instance of manifest_versions.ManifestVersions. |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 127 | @raise EnumeratorException if we can't enumerate any supported boards. |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 128 | """ |
Chris Masone | 92874d3 | 2012-04-03 10:13:04 -0700 | [diff] [blame] | 129 | boards = self._enumerator.Enumerate() |
Chris Masone | a3a3817 | 2012-05-14 15:19:56 -0700 | [diff] [blame] | 130 | logging.info('Boards currently in the lab: %r', boards) |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 131 | for e in self._events.itervalues(): |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 132 | if e.ShouldHandle(): |
Chris Masone | a3a3817 | 2012-05-14 15:19:56 -0700 | [diff] [blame] | 133 | logging.info('Handling %s event', e.keyword) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 134 | for board in boards: |
Chris Masone | d17d185 | 2012-04-20 11:52:49 -0700 | [diff] [blame] | 135 | branch_builds = e.GetBranchBuildsForBoard(board) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 136 | e.Handle(self._scheduler, branch_builds, board) |
Chris Masone | bbde386 | 2012-05-07 14:29:51 -0700 | [diff] [blame] | 137 | e.UpdateCriteria() |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 138 | |
| 139 | |
| 140 | def ForceEventsOnceForBuild(self, keywords, build_name): |
| 141 | """Force events with provided keywords to happen, with given build. |
| 142 | |
| 143 | @param keywords: iterable of event keywords to force |
| 144 | @param build_name: instead of looking up builds to test, test this one. |
| 145 | """ |
| 146 | board, type, milestone, manifest = base_event.ParseBuildName(build_name) |
Chris Masone | cbe4277 | 2012-04-30 22:18:23 -0700 | [diff] [blame] | 147 | branch_builds = {task.PickBranchName(type, milestone): [build_name]} |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 148 | logging.info('Testing build %s-%s on %s' % (milestone, manifest, board)) |
| 149 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 150 | for e in self._events.itervalues(): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 151 | if e.keyword in keywords: |
| 152 | e.Handle(self._scheduler, branch_builds, board, force=True) |