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