blob: eef76f2c3c7afef4e23066eb1c723ce0c8699883 [file] [log] [blame]
Xixuan Wu40998892017-08-29 14:32:26 -07001# Copyright 2017 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
5"""Module for cron job to trigger events for suite scheduler."""
6
7import logging
8
Xixuan Wu40998892017-08-29 14:32:26 -07009import config_reader
10import constants
11import datastore_client
Xixuan Wu40998892017-08-29 14:32:26 -070012import file_getter
13import rest_client
Brigit Rossbacheb611ac2020-09-24 14:55:24 -060014import StringIO
Xixuan Wu51bb7102019-03-18 14:51:44 -070015import task_config_reader
Xixuan Wu40998892017-08-29 14:32:26 -070016
17
18class TriggerReceiver(object):
19 """The class for receiving event triggers."""
20
Brigit Rossbach50d086f2020-09-17 13:29:49 -060021 def __init__(self, fake=False):
Xixuan Wu40998892017-08-29 14:32:26 -070022 """Initialize a trigger receiver.
23
24 Its job is to fetch triggers from config files and trigger them.
25 """
Xixuan Wuc6819012019-05-23 11:34:59 -070026 self._build_client = rest_client.BuildBucketBigqueryClient(
27 rest_client.BaseRestClient(
28 constants.RestClient.BIGQUERY_CLIENT.scopes,
29 constants.RestClient.BIGQUERY_CLIENT.service_name,
30 constants.RestClient.BIGQUERY_CLIENT.service_version))
Xixuan Wu40998892017-08-29 14:32:26 -070031 self._last_exec_client = datastore_client.LastExecutionRecordStore()
32 self._android_client = rest_client.AndroidBuildRestClient(
33 rest_client.BaseRestClient(
34 constants.RestClient.ANDROID_BUILD_CLIENT.scopes,
35 constants.RestClient.ANDROID_BUILD_CLIENT.service_name,
36 constants.RestClient.ANDROID_BUILD_CLIENT.service_version))
Brigit Rossbach50d086f2020-09-17 13:29:49 -060037 if fake:
38 self._task_config = task_config_reader.TaskConfig(
39 config_reader.ConfigReader(
40 file_getter.TEST_SUITE_SCHEDULER_CONFIG_FILE))
41 fake_lab_config_reader = config_reader.ConfigReader(
Dhanya Ganesh23dbb202020-09-30 16:11:01 +000042 file_getter.TEST_LAB_CONFIG_FILE)
Brigit Rossbach50d086f2020-09-17 13:29:49 -060043 self._lab_config = config_reader.LabConfig(fake_lab_config_reader)
44 else:
45 file_getter.clone_config()
46 self._task_config = task_config_reader.TaskConfig(
Dhanya Ganesh643a49e2020-09-25 18:14:22 +000047 config_reader.ConfigReader(
48 file_getter.NEW_SUITE_SCHEDULER_CONFIG_FILE))
49 lab_config_reader = config_reader.ConfigReader(file_getter.NEW_LAB_CONFIG_FILE)
Brigit Rossbach50d086f2020-09-17 13:29:49 -060050 self._lab_config = config_reader.LabConfig(lab_config_reader)
Xixuan Wu40998892017-08-29 14:32:26 -070051
Xixuan Wu40998892017-08-29 14:32:26 -070052 # Initialize events
53 self.events = {}
Xixuan Wu51bb7102019-03-18 14:51:44 -070054 for keyword, klass in task_config_reader.EVENT_CLASSES.iteritems():
Xixuan Wu40998892017-08-29 14:32:26 -070055 logging.info('Initializing %s event', keyword)
56 new_event = klass(
57 self._task_config.get_event_setting(klass.section_name()),
58 self._last_exec_client.get_last_execute_time(klass.KEYWORD))
59
60 if new_event.should_handle:
61 new_event.set_task_list(
Xixuan Wu4ac56dd2017-10-12 11:59:30 -070062 self._task_config.get_tasks_by_keyword(klass.KEYWORD)['tasks'])
Xixuan Wu40998892017-08-29 14:32:26 -070063 logging.info('Got %d tasks for %s event',
64 len(new_event.task_list), klass.KEYWORD)
65
66 self.events[keyword] = new_event
67
68 self.event_results = {}
69
70 def cron(self):
71 """The cron job to scheduler suite jobs by config.
72
73 This cron job executes:
74 1. Filter out the tasks that shoud be run at this round.
75 2. Fetch launch_control_build for Android boards from API.
Xixuan Wu6ec23e32019-05-23 11:56:02 -070076 3. Fetch cros_builds for ChromeOS boards from Buildbucket bigquery.
Xixuan Wu40998892017-08-29 14:32:26 -070077 4. Schedule corresponding jobs with fetched builds.
78 5. Reset event when it's finished.
79 """
80 for keyword, event in self.events.iteritems():
81 logging.info('Handling %s event in cron job', keyword)
82
83 event.filter_tasks()
84 if event.task_list:
85 logging.info('Processing %d tasks.', len(event.task_list))
86 self._schedule_tasks(event)
87 else:
88 logging.info('No task list found')
89 event.finish()
90
91 def _schedule_tasks(self, event):
92 """Schedule tasks based on given event.
93
94 Args:
95 event: a kind of event, which contains its tasks that should be
96 scheduled.
97 """
Craig Bergstrom58263d32018-04-26 14:11:35 -060098 cros_builds_tuple = event.get_cros_builds(
Xixuan Wuc6819012019-05-23 11:34:59 -070099 self._lab_config, self._build_client)
Craig Bergstrom58263d32018-04-26 14:11:35 -0600100 logging.debug('Found CrOS builds: %r', cros_builds_tuple)
Garry Wangdce77572021-07-18 19:33:35 -0700101 daily_cros_builds_tuple = event.get_daily_cros_builds(
102 self._lab_config, self._build_client)
103 logging.debug('Found daily CrOS builds: %r', daily_cros_builds_tuple)
Xixuan Wu40998892017-08-29 14:32:26 -0700104 launch_control_builds = event.get_launch_control_builds(
105 self._lab_config, self._android_client)
106 logging.debug('Found launch_control_builds: %r', launch_control_builds)
Xinan Lin8b291982019-12-16 10:03:56 -0800107 firmware_builds = None
108 if [x for x in event.task_list if (x.firmware_rw_build_spec or
109 x.firmware_ro_build_spec)]:
110 firmware_builds = event.get_firmware_builds(self._build_client)
Xixuan Wu40998892017-08-29 14:32:26 -0700111
112 self.event_results[event.KEYWORD] = event.process_tasks(
113 launch_control_builds=launch_control_builds,
Craig Bergstrom58263d32018-04-26 14:11:35 -0600114 cros_builds_tuple=cros_builds_tuple,
Xinan Lin028f9582019-12-11 10:55:33 -0800115 firmware_builds=firmware_builds,
Garry Wangdce77572021-07-18 19:33:35 -0700116 configs=config_reader.Configs(lab_config=self._lab_config),
117 daily_cros_builds_tuple=daily_cros_builds_tuple)
Xixuan Wu40998892017-08-29 14:32:26 -0700118 logging.info('Finished processing all tasks')