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