Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2016 The ChromiumOS Authors |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Bootstrap for cbuildbot. |
| 6 | |
| 7 | This script is intended to checkout chromite on the branch specified by -b or |
| 8 | --branch (as normally accepted by cbuildbot), and then invoke cbuildbot. Most |
| 9 | arguments are not parsed, only passed along. If a branch is not specified, this |
Julio Hurtado | 9265c7e | 2021-04-19 23:04:44 +0000 | [diff] [blame] | 10 | script will use 'main'. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 11 | |
| 12 | Among other things, this allows us to invoke build configs that exist on a given |
| 13 | branch, but not on TOT. |
| 14 | """ |
| 15 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 16 | import base64 |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 17 | import functools |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 18 | import logging |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 19 | import os |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 20 | import time |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 21 | |
Chris McDonald | b55b703 | 2021-06-17 16:41:32 -0600 | [diff] [blame] | 22 | from chromite.cbuildbot import cbuildbot_alerts |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 23 | from chromite.cbuildbot import repository |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 24 | from chromite.cbuildbot.stages import sync_stages |
Lann Martin | ebae73d | 2018-05-21 17:12:00 -0600 | [diff] [blame] | 25 | from chromite.lib import boto_compat |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 26 | from chromite.lib import build_summary |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 27 | from chromite.lib import config_lib |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 28 | from chromite.lib import constants |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 29 | from chromite.lib import cros_build_lib |
Benjamin Gordon | 7464523 | 2018-05-04 17:40:42 -0600 | [diff] [blame] | 30 | from chromite.lib import cros_sdk_lib |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 31 | from chromite.lib import metrics |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 32 | from chromite.lib import osutils |
Mike Frysinger | f014625 | 2019-09-02 13:31:05 -0400 | [diff] [blame] | 33 | from chromite.lib import timeout_util |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 34 | from chromite.lib import ts_mon_config |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 35 | from chromite.scripts import cbuildbot |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 36 | |
Mike Frysinger | 3c831a7 | 2020-02-19 02:47:04 -0500 | [diff] [blame] | 37 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 38 | # This number should be incremented when we change the layout of the buildroot |
| 39 | # in a non-backwards compatible way. This wipes all buildroots. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 40 | BUILDROOT_BUILDROOT_LAYOUT = 2 |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 41 | _DISTFILES_CACHE_EXPIRY_HOURS = 8 * 24 |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 42 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 43 | # Metrics reported to Monarch. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | METRIC_PREFIX = "chromeos/chromite/cbuildbot_launch/" |
| 45 | METRIC_ACTIVE = METRIC_PREFIX + "active" |
| 46 | METRIC_INVOKED = METRIC_PREFIX + "invoked" |
| 47 | METRIC_COMPLETED = METRIC_PREFIX + "completed" |
| 48 | METRIC_PREP = METRIC_PREFIX + "prep_completed" |
| 49 | METRIC_CLEAN = METRIC_PREFIX + "clean_buildroot_durations" |
| 50 | METRIC_INITIAL = METRIC_PREFIX + "initial_checkout_durations" |
| 51 | METRIC_CBUILDBOT = METRIC_PREFIX + "cbuildbot_durations" |
| 52 | METRIC_CBUILDBOT_INSTANCE = METRIC_PREFIX + "cbuildbot_instance_durations" |
| 53 | METRIC_CLOBBER = METRIC_PREFIX + "clobber" |
| 54 | METRIC_BRANCH_CLEANUP = METRIC_PREFIX + "branch_cleanup" |
| 55 | METRIC_DISTFILES_CLEANUP = METRIC_PREFIX + "distfiles_cleanup" |
| 56 | METRIC_CHROOT_CLEANUP = METRIC_PREFIX + "chroot_cleanup" |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 57 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 58 | # Builder state |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | BUILDER_STATE_FILENAME = ".cbuildbot_build_state.json" |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 60 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 61 | |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 62 | def StageDecorator(functor): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 63 | """A Decorator that adds buildbot stage tags around a method. |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | It uses the method name as the stage name, and assumes failure on a true |
| 66 | return value, or an exception. |
| 67 | """ |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | @functools.wraps(functor) |
| 70 | def wrapped_functor(*args, **kwargs): |
| 71 | try: |
| 72 | cbuildbot_alerts.PrintBuildbotStepName(functor.__name__) |
| 73 | result = functor(*args, **kwargs) |
| 74 | except Exception: |
| 75 | cbuildbot_alerts.PrintBuildbotStepFailure() |
| 76 | raise |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 77 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | if result: |
| 79 | cbuildbot_alerts.PrintBuildbotStepFailure() |
| 80 | return result |
| 81 | |
| 82 | return wrapped_functor |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 83 | |
| 84 | |
Don Garrett | b5fc08b | 2017-11-20 21:51:16 +0000 | [diff] [blame] | 85 | def field(fields, **kwargs): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | """Helper for inserting more fields into a metrics fields dictionary. |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 87 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | Args: |
Alex Klein | 361062b | 2023-04-05 09:45:28 -0600 | [diff] [blame] | 89 | fields: Dictionary of metrics fields. |
| 90 | **kwargs: Each argument is a key/value pair to insert into dict. |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 91 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | Returns: |
Alex Klein | 361062b | 2023-04-05 09:45:28 -0600 | [diff] [blame] | 93 | Copy of original dictionary with kwargs set as fields. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | """ |
| 95 | f = fields.copy() |
| 96 | f.update(kwargs) |
| 97 | return f |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 98 | |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 99 | |
| 100 | def PrependPath(prepend): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | """Generate path with new directory at the beginning. |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 104 | prepend: Directory to add at the beginning of the path. |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 105 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | Returns: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 107 | Extended path as a string. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | """ |
| 109 | return os.pathsep.join([prepend, os.environ.get("PATH", os.defpath)]) |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 110 | |
Aviv Keshet | c38eebe | 2018-09-27 23:19:58 +0000 | [diff] [blame] | 111 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 112 | def PreParseArguments(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | """Extract the branch name from cbuildbot command line arguments. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 114 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 115 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 116 | argv: The command line arguments to parse. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | Returns: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 119 | Branch as a string ('main' if nothing is specified). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | """ |
| 121 | parser = cbuildbot.CreateParser() |
| 122 | options = cbuildbot.ParseCommandLine(parser, argv) |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 123 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | if not options.cache_dir: |
| 125 | options.cache_dir = os.path.join( |
| 126 | options.buildroot, "repository", ".cache" |
| 127 | ) |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | options.Freeze() |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | # This option isn't required for cbuildbot, but is for us. |
| 132 | if not options.buildroot: |
| 133 | cros_build_lib.Die("--buildroot is a required option.") |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 134 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 135 | return options |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 136 | |
Aviv Keshet | c38eebe | 2018-09-27 23:19:58 +0000 | [diff] [blame] | 137 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 138 | def GetCurrentBuildState(options, branch): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | """Extract information about the current build state from command-line args. |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 142 | options: A parsed options object from a cbuildbot parser. |
| 143 | branch: The name of the branch this builder was called with. |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | Returns: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 146 | A BuildSummary object describing the current build. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | """ |
| 148 | build_state = build_summary.BuildSummary( |
| 149 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 150 | buildroot_layout=BUILDROOT_BUILDROOT_LAYOUT, |
| 151 | branch=branch, |
| 152 | ) |
| 153 | if options.buildnumber: |
| 154 | build_state.build_number = options.buildnumber |
| 155 | if options.buildbucket_id: |
| 156 | build_state.buildbucket_id = options.buildbucket_id |
| 157 | if options.master_build_id: |
| 158 | build_state.master_build_id = options.master_build_id |
| 159 | return build_state |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 160 | |
| 161 | |
| 162 | def GetLastBuildState(root): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | """Fetch the state of the last build run from |root|. |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 164 | |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 165 | If the saved state file can't be read or doesn't contain valid JSON, a |
| 166 | default state will be returned. |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 167 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 168 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 169 | root: Root of the working directory tree as a string. |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 170 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | Returns: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 172 | A BuildSummary object representing the previous build. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | """ |
| 174 | state_file = os.path.join(root, BUILDER_STATE_FILENAME) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 175 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 176 | state = build_summary.BuildSummary() |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | try: |
| 179 | state_raw = osutils.ReadFile(state_file) |
| 180 | state.from_json(state_raw) |
| 181 | except IOError as e: |
| 182 | logging.info( |
| 183 | "Unable to read %s: %s. Expected for first task on bot.", |
| 184 | state_file, |
| 185 | e, |
| 186 | ) |
| 187 | return state |
| 188 | except ValueError as e: |
| 189 | logging.warning( |
| 190 | "Saved state file %s is not valid JSON: %s", state_file, e |
| 191 | ) |
| 192 | return state |
| 193 | |
| 194 | if not state.is_valid(): |
| 195 | logging.warning("Previous build state is not valid. Ignoring.") |
| 196 | state = build_summary.BuildSummary() |
| 197 | |
| 198 | return state |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 199 | |
| 200 | |
| 201 | def SetLastBuildState(root, new_state): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 202 | """Save the state of the last build under |root|. |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 203 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 205 | root: Root of the working directory tree as a string. |
| 206 | new_state: BuildSummary object containing the state to be saved. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 207 | """ |
| 208 | state_file = os.path.join(root, BUILDER_STATE_FILENAME) |
| 209 | osutils.WriteFile(state_file, new_state.to_json()) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 210 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 211 | # Remove old state file. Its contents have been migrated into the new file. |
| 212 | old_state_file = os.path.join(root, ".cbuildbot_launch_state") |
| 213 | osutils.SafeUnlink(old_state_file) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 214 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 215 | |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 216 | def _MaybeCleanDistfiles(cache_dir, distfiles_ts): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 217 | """Cleans the distfiles directory if too old. |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 218 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 219 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 220 | cache_dir: Directory of the cache, as a string. |
| 221 | distfiles_ts: A timestamp str for the last time distfiles was cleaned. |
| 222 | May be None. |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 223 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | Returns: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 225 | The new distfiles_ts to persist in state. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 226 | """ |
| 227 | # distfiles_ts can be None for a fresh environment, which means clean. |
| 228 | if distfiles_ts is None: |
| 229 | return time.time() |
| 230 | |
| 231 | distfiles_age = (time.time() - distfiles_ts) / 3600.0 |
| 232 | if distfiles_age < _DISTFILES_CACHE_EXPIRY_HOURS: |
| 233 | return distfiles_ts |
| 234 | |
| 235 | logging.info( |
| 236 | "Remove old distfiles cache (cache expiry %d hours)", |
| 237 | _DISTFILES_CACHE_EXPIRY_HOURS, |
| 238 | ) |
| 239 | osutils.RmDir( |
| 240 | os.path.join(cache_dir, "distfiles"), ignore_missing=True, sudo=True |
| 241 | ) |
| 242 | metrics.Counter(METRIC_DISTFILES_CLEANUP).increment( |
| 243 | fields=field({}, reason="cache_expired") |
| 244 | ) |
| 245 | |
| 246 | # Cleaned cache, so reset distfiles_ts |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 247 | return time.time() |
| 248 | |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 249 | |
| 250 | def SanitizeCacheDir(cache_dir): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 251 | """Make certain the .cache directory is valid. |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 252 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 253 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 254 | cache_dir: Directory of the cache, as a string. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | """ |
| 256 | logging.info("Cleaning up cache dir at %s", cache_dir) |
| 257 | # Verify that .cache is writable by the current user. |
| 258 | try: |
| 259 | osutils.Touch( |
| 260 | os.path.join(cache_dir, ".cbuildbot_launch"), makedirs=True |
| 261 | ) |
| 262 | except IOError: |
| 263 | logging.info("Bad Permissions for cache dir, wiping: %s", cache_dir) |
| 264 | osutils.RmDir(cache_dir, sudo=True) |
| 265 | osutils.Touch( |
| 266 | os.path.join(cache_dir, ".cbuildbot_launch"), makedirs=True |
| 267 | ) |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 268 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 269 | osutils.RmDir( |
| 270 | os.path.join(cache_dir, "paygen_cache"), ignore_missing=True, sudo=True |
| 271 | ) |
| 272 | logging.info("Finished cleaning cache_dir.") |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 273 | |
| 274 | |
| 275 | @StageDecorator |
Mike Nichols | fe1a5e6 | 2021-09-03 13:11:17 -0400 | [diff] [blame] | 276 | def CleanBuildRoot(root, repo, cache_dir, build_state, source_cache=False): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 277 | """Some kinds of branch transitions break builds. |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 278 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 279 | This method ensures that cbuildbot's buildroot is a clean checkout on the |
| 280 | given branch when it starts. If necessary (a branch transition) it will wipe |
| 281 | assorted state that cannot be safely reused from the previous build. |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 282 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 283 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 284 | root: Root directory owned by cbuildbot_launch. |
| 285 | repo: repository.RepoRepository instance. |
| 286 | cache_dir: Cache directory. |
| 287 | build_state: BuildSummary object containing the current build state that |
| 288 | will be saved into the cleaned root. The distfiles_ts property will |
| 289 | be updated if the distfiles cache is cleaned. |
| 290 | source_cache: Bool whether to use source cache mounts. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 291 | """ |
| 292 | previous_state = GetLastBuildState(root) |
| 293 | SetLastBuildState(root, build_state) |
| 294 | SanitizeCacheDir(cache_dir) |
| 295 | build_state.distfiles_ts = _MaybeCleanDistfiles( |
| 296 | cache_dir, previous_state.distfiles_ts |
| 297 | ) |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 298 | |
Mike Nichols | fe1a5e6 | 2021-09-03 13:11:17 -0400 | [diff] [blame] | 299 | if not source_cache: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 300 | if previous_state.buildroot_layout != BUILDROOT_BUILDROOT_LAYOUT: |
| 301 | cbuildbot_alerts.PrintBuildbotStepText( |
| 302 | "Unknown layout: Wiping buildroot." |
| 303 | ) |
| 304 | metrics.Counter(METRIC_CLOBBER).increment( |
| 305 | fields=field({}, reason="layout_change") |
| 306 | ) |
| 307 | chroot_dir = os.path.join(root, constants.DEFAULT_CHROOT_DIR) |
| 308 | if os.path.exists(chroot_dir) or os.path.exists( |
| 309 | chroot_dir + ".img" |
| 310 | ): |
| 311 | cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=True) |
| 312 | osutils.RmDir(root, ignore_missing=True, sudo=True) |
| 313 | osutils.RmDir(cache_dir, ignore_missing=True, sudo=True) |
| 314 | else: |
| 315 | if previous_state.branch != repo.branch: |
| 316 | cbuildbot_alerts.PrintBuildbotStepText( |
| 317 | "Branch change: Cleaning buildroot." |
| 318 | ) |
| 319 | logging.info( |
| 320 | "Unmatched branch: %s -> %s", |
| 321 | previous_state.branch, |
| 322 | repo.branch, |
| 323 | ) |
| 324 | metrics.Counter(METRIC_BRANCH_CLEANUP).increment( |
| 325 | fields=field({}, old_branch=previous_state.branch) |
| 326 | ) |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | logging.info("Remove Chroot.") |
| 329 | chroot_dir = os.path.join( |
| 330 | repo.directory, constants.DEFAULT_CHROOT_DIR |
| 331 | ) |
| 332 | if os.path.exists(chroot_dir) or os.path.exists( |
| 333 | chroot_dir + ".img" |
| 334 | ): |
| 335 | cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=True) |
| 336 | |
| 337 | logging.info("Remove Chrome checkout.") |
| 338 | osutils.RmDir( |
| 339 | os.path.join(repo.directory, ".cache", "distfiles"), |
| 340 | ignore_missing=True, |
| 341 | sudo=True, |
| 342 | ) |
| 343 | |
| 344 | try: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 345 | # If there is any failure doing the cleanup, wipe everything. The |
| 346 | # previous run might have been killed in the middle leaving stale git |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 347 | # locks. Clean those up, first. |
| 348 | if not source_cache: |
| 349 | repo.PreLoad() |
| 350 | |
| 351 | # If the previous build didn't exit normally, run an expensive step to |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 352 | # clean up abandoned git locks. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 353 | if previous_state.status not in ( |
| 354 | constants.BUILDER_STATUS_FAILED, |
| 355 | constants.BUILDER_STATUS_PASSED, |
| 356 | ): |
| 357 | repo.CleanStaleLocks() |
| 358 | |
| 359 | if not source_cache: |
| 360 | repo.BuildRootGitCleanup(prune_all=True) |
| 361 | except Exception: |
| 362 | logging.info( |
| 363 | "Checkout cleanup failed, wiping buildroot:", exc_info=True |
| 364 | ) |
| 365 | metrics.Counter(METRIC_CLOBBER).increment( |
| 366 | fields=field({}, reason="repo_cleanup_failure") |
| 367 | ) |
| 368 | repository.ClearBuildRoot(repo.directory) |
| 369 | |
| 370 | if not source_cache: |
| 371 | # Ensure buildroot exists. Save the state we are prepped for. |
| 372 | osutils.SafeMakedirs(repo.directory) |
| 373 | SetLastBuildState(root, build_state) |
Mike Nichols | d0acc7f | 2021-05-21 17:18:24 +0000 | [diff] [blame] | 374 | |
| 375 | |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 376 | @StageDecorator |
Mike Nichols | 9fb4883 | 2021-01-29 14:47:15 -0700 | [diff] [blame] | 377 | def InitialCheckout(repo, options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 378 | """Preliminary ChromeOS checkout. |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 379 | |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 380 | Perform a complete checkout of ChromeOS on the specified branch. This does |
| 381 | NOT match what the build needs, but ensures the buildroot both has a 'hot' |
| 382 | checkout, and is close enough that the branched cbuildbot can successfully |
| 383 | get the right checkout. |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 384 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 385 | This checks out full ChromeOS, even if a ChromiumOS build is going to be |
| 386 | performed. This is because we have no knowledge of the build config to be |
| 387 | used. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 388 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 389 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 390 | repo: repository.RepoRepository instance. |
| 391 | options: A parsed options object from a cbuildbot parser. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 392 | """ |
| 393 | cbuildbot_alerts.PrintBuildbotStepText("Branch: %s" % repo.branch) |
| 394 | if not options.source_cache: |
| 395 | logging.info( |
| 396 | "Bootstrap script starting initial sync on branch: %s", repo.branch |
| 397 | ) |
| 398 | repo.PreLoad("/preload/chromeos") |
| 399 | repo.Sync( |
| 400 | jobs=32, detach=True, downgrade_repo=_ShouldDowngradeRepo(options) |
| 401 | ) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 402 | |
| 403 | |
Lann Martin | 8c4c680 | 2018-05-23 11:09:46 -0600 | [diff] [blame] | 404 | def ShouldFixBotoCerts(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 405 | """Decide if FixBotoCerts should be applied for this branch.""" |
| 406 | try: |
| 407 | # Only apply to factory and firmware branches. |
| 408 | branch = options.branch or "" |
| 409 | prefix = branch.split("-")[0] |
| 410 | if prefix not in ("factory", "firmware"): |
| 411 | return False |
Lann Martin | 8c4c680 | 2018-05-23 11:09:46 -0600 | [diff] [blame] | 412 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 413 | # Only apply to "old" branches. |
| 414 | if branch.endswith(".B"): |
| 415 | version = branch[:-2].split("-")[-1] |
| 416 | major = int(version.split(".")[0]) |
| 417 | return major <= 9667 # This is the newest known to be failing. |
Lann Martin | 8c4c680 | 2018-05-23 11:09:46 -0600 | [diff] [blame] | 418 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 419 | return False |
| 420 | except Exception as e: |
| 421 | logging.warning(" failed: %s", e) |
| 422 | # Conservatively continue without the fix. |
| 423 | return False |
Lann Martin | 8c4c680 | 2018-05-23 11:09:46 -0600 | [diff] [blame] | 424 | |
| 425 | |
Mike Nichols | 9fb4883 | 2021-01-29 14:47:15 -0700 | [diff] [blame] | 426 | def _ShouldDowngradeRepo(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 427 | """Determine which repo version to set for the branch. |
Mike Nichols | 9fb4883 | 2021-01-29 14:47:15 -0700 | [diff] [blame] | 428 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 429 | Repo version is set at cache creation time, in the nightly builder, |
| 430 | which means we are typically at the latest version. Older branches |
| 431 | are incompatible with newer version of ToT, therefore we downgrade |
| 432 | repo to a known working version. |
Mike Nichols | 9fb4883 | 2021-01-29 14:47:15 -0700 | [diff] [blame] | 433 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 434 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 435 | options: A parsed options object from a cbuildbot parser. |
Mike Nichols | 9fb4883 | 2021-01-29 14:47:15 -0700 | [diff] [blame] | 436 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 437 | Returns: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 438 | bool of whether to downgrade repo version based on branch. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 439 | """ |
| 440 | try: |
| 441 | branch = options.branch or "" |
| 442 | # Only apply to "old" branches. |
| 443 | if branch.endswith(".B"): |
| 444 | branch_num = branch[:-2].split("-")[1][1:3] |
| 445 | return branch_num <= 79 # This is the newest known to be failing. |
Mike Nichols | 9fb4883 | 2021-01-29 14:47:15 -0700 | [diff] [blame] | 446 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 447 | return False |
| 448 | except Exception as e: |
| 449 | logging.warning(" failed: %s", e) |
| 450 | # Conservatively continue without the fix. |
| 451 | return False |
Mike Nichols | 9fb4883 | 2021-01-29 14:47:15 -0700 | [diff] [blame] | 452 | |
| 453 | |
Don Garrett | 066e6f5 | 2017-09-28 19:14:01 -0700 | [diff] [blame] | 454 | @StageDecorator |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 455 | def Cbuildbot(buildroot, depot_tools_path, argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 456 | """Start cbuildbot in specified directory with all arguments. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 457 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 458 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 459 | buildroot: Directory to be passed to cbuildbot with --buildroot. |
| 460 | depot_tools_path: Directory for depot_tools to be used by cbuildbot. |
| 461 | argv: Command line options passed to cbuildbot_launch. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 462 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 463 | Returns: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 464 | Return code of cbuildbot as an integer. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 465 | """ |
| 466 | logging.info("Bootstrap cbuildbot in: %s", buildroot) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | # Fixup buildroot parameter. |
| 469 | argv = argv[:] |
| 470 | for i, arg in enumerate(argv): |
| 471 | if arg in ("-r", "--buildroot"): |
| 472 | argv[i + 1] = buildroot |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 473 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 474 | # Source_cache flag is only used to indicate a transition to cache disks |
| 475 | # and doesn't need to be passed back to Cbuildbot. |
| 476 | if "--source_cache" in argv: |
| 477 | argv.remove("--source_cache") |
Mike Nichols | e333e40 | 2021-10-04 15:57:03 -0400 | [diff] [blame] | 478 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 479 | logging.info("Cbuildbot Args: %s", argv) |
Mike Nichols | 0b5555e | 2021-10-05 17:03:23 -0400 | [diff] [blame] | 480 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 481 | # This filters out command line arguments not supported by older versions |
| 482 | # of cbuildbot. |
| 483 | parser = cbuildbot.CreateParser() |
| 484 | options = cbuildbot.ParseCommandLine(parser, argv) |
| 485 | cbuildbot_path = os.path.join(buildroot, "chromite", "bin", "cbuildbot") |
| 486 | cmd = sync_stages.BootstrapStage.FilterArgsForTargetCbuildbot( |
| 487 | buildroot, cbuildbot_path, options |
| 488 | ) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 489 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 490 | # We want cbuildbot to use branched depot_tools scripts from our manifest, |
| 491 | # so that depot_tools is branched to match cbuildbot. |
| 492 | logging.info("Adding depot_tools into PATH: %s", depot_tools_path) |
| 493 | extra_env = {"PATH": PrependPath(depot_tools_path)} |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 494 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 495 | # TODO(crbug.com/845304): Remove once underlying boto issues are resolved. |
| 496 | fix_boto = ShouldFixBotoCerts(options) |
Lann Martin | ebae73d | 2018-05-21 17:12:00 -0600 | [diff] [blame] | 497 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 498 | with boto_compat.FixBotoCerts(activate=fix_boto): |
| 499 | result = cros_build_lib.run( |
| 500 | cmd, extra_env=extra_env, check=False, cwd=buildroot |
| 501 | ) |
Lann Martin | ebae73d | 2018-05-21 17:12:00 -0600 | [diff] [blame] | 502 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 503 | return result.returncode |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 504 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 505 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 506 | @StageDecorator |
| 507 | def CleanupChroot(buildroot): |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 508 | """Unmount/cleanup an image-based chroot without deleting the backing image. |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 509 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 510 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 511 | buildroot: Directory containing the chroot to be cleaned up. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 512 | """ |
| 513 | chroot_dir = os.path.join(buildroot, constants.DEFAULT_CHROOT_DIR) |
| 514 | logging.info("Cleaning up chroot at %s", chroot_dir) |
| 515 | if os.path.exists(chroot_dir) or os.path.exists(chroot_dir + ".img"): |
| 516 | try: |
| 517 | cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=False) |
| 518 | except timeout_util.TimeoutError: |
| 519 | logging.exception("Cleaning up chroot timed out") |
| 520 | # Dump debug info to help https://crbug.com/1000034. |
| 521 | cros_build_lib.run(["mount"], check=True) |
| 522 | cros_build_lib.run(["uname", "-a"], check=True) |
| 523 | cros_build_lib.sudo_run(["losetup", "-a"], check=True) |
| 524 | cros_build_lib.run(["dmesg"], check=True) |
| 525 | logging.warning( |
| 526 | "Assuming the bot is going to reboot, so ignoring this " |
| 527 | "failure; see https://crbug.com/1000034" |
| 528 | ) |
Mike Frysinger | f014625 | 2019-09-02 13:31:05 -0400 | [diff] [blame] | 529 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 530 | # NB: We ignore errors at this point because this stage runs last. If the |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 531 | # chroot failed to unmount, we're going to reboot the system once we're |
| 532 | # done, and that will implicitly take care of cleaning things up. If the |
| 533 | # bots stop rebooting after every run, we'll need to make this fatal all the |
| 534 | # time. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 535 | # |
| 536 | # TODO(crbug.com/1000034): This should be fatal all the time. |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 537 | |
| 538 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 539 | def ConfigureGlobalEnvironment(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 540 | """Setup process wide environmental changes.""" |
| 541 | # Set umask to 022 so files created by buildbot are readable. |
| 542 | os.umask(0o22) |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 543 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 544 | # These variables can interfere with LANG / locale behavior. |
| 545 | unwanted_local_vars = [ |
| 546 | "LC_ALL", |
| 547 | "LC_CTYPE", |
| 548 | "LC_COLLATE", |
| 549 | "LC_TIME", |
| 550 | "LC_NUMERIC", |
| 551 | "LC_MONETARY", |
| 552 | "LC_MESSAGES", |
| 553 | "LC_PAPER", |
| 554 | "LC_NAME", |
| 555 | "LC_ADDRESS", |
| 556 | "LC_TELEPHONE", |
| 557 | "LC_MEASUREMENT", |
| 558 | "LC_IDENTIFICATION", |
| 559 | ] |
| 560 | for v in unwanted_local_vars: |
| 561 | os.environ.pop(v, None) |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 562 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 563 | # This variable is required for repo sync's to work in all cases. |
| 564 | os.environ["LANG"] = "en_US.UTF-8" |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 565 | |
Aviv Keshet | c38eebe | 2018-09-27 23:19:58 +0000 | [diff] [blame] | 566 | |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 567 | def _main(options, argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 568 | """main method of script. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 569 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 570 | Args: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 571 | options: preparsed options object for the build. |
| 572 | argv: All command line arguments to pass as list of strings. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 573 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 574 | Returns: |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 575 | Return code of cbuildbot as an integer. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 576 | """ |
| 577 | branchname = options.branch or "main" |
| 578 | root = options.buildroot |
| 579 | buildroot = os.path.join(root, "repository") |
| 580 | workspace = os.path.join(root, "workspace") |
| 581 | if options.source_cache: |
| 582 | buildroot = options.buildroot |
| 583 | if options.workspace: |
| 584 | workspace = options.workspace |
| 585 | depot_tools_path = os.path.join(buildroot, constants.DEPOT_TOOLS_SUBPATH) |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 586 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 587 | # Does the entire build pass or fail. |
| 588 | with metrics.Presence(METRIC_ACTIVE), metrics.SuccessCounter( |
| 589 | METRIC_COMPLETED |
| 590 | ) as s_fields: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 591 | # Preliminary set, mostly command line parsing. |
| 592 | with metrics.SuccessCounter(METRIC_INVOKED): |
| 593 | if options.enable_buildbot_tags: |
| 594 | cbuildbot_alerts.EnableBuildbotMarkers() |
| 595 | ConfigureGlobalEnvironment() |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 596 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 597 | # Prepare the buildroot with source for the build. |
| 598 | with metrics.SuccessCounter(METRIC_PREP): |
| 599 | manifest_url = config_lib.GetSiteParams().MANIFEST_INT_URL |
| 600 | repo = repository.RepoRepository( |
| 601 | manifest_url, |
| 602 | buildroot, |
| 603 | branch=branchname, |
| 604 | git_cache_dir=options.git_cache_dir, |
| 605 | ) |
| 606 | previous_build_state = GetLastBuildState(root) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 607 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 608 | # Clean up the buildroot to a safe state. |
| 609 | with metrics.SecondsTimer(METRIC_CLEAN): |
| 610 | build_state = GetCurrentBuildState(options, branchname) |
| 611 | CleanBuildRoot( |
| 612 | root, |
| 613 | repo, |
| 614 | options.cache_dir, |
| 615 | build_state, |
| 616 | options.source_cache, |
| 617 | ) |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 618 | |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 619 | # Get a checkout close enough to the branch that cbuildbot can |
| 620 | # handle it. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 621 | if options.sync: |
| 622 | with metrics.SecondsTimer(METRIC_INITIAL): |
| 623 | InitialCheckout(repo, options) |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 624 | |
Alex Klein | 9e7b29e | 2023-04-11 16:10:31 -0600 | [diff] [blame^] | 625 | # Run cbuildbot inside the full ChromeOS checkout, on the specified |
| 626 | # branch. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 627 | with metrics.SecondsTimer( |
| 628 | METRIC_CBUILDBOT |
| 629 | ), metrics.SecondsInstanceTimer(METRIC_CBUILDBOT_INSTANCE): |
| 630 | if previous_build_state.is_valid(): |
| 631 | argv.append("--previous-build-state") |
| 632 | argv.append( |
| 633 | base64.b64encode( |
| 634 | previous_build_state.to_json().encode("utf-8") |
| 635 | ).decode("utf-8") |
| 636 | ) |
| 637 | argv.extend(["--workspace", workspace]) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 638 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 639 | if not options.cache_dir_specified: |
| 640 | argv.extend(["--cache-dir", options.cache_dir]) |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 641 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 642 | result = Cbuildbot(buildroot, depot_tools_path, argv) |
| 643 | s_fields["success"] = result == 0 |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 644 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 645 | build_state.status = ( |
| 646 | constants.BUILDER_STATUS_PASSED |
| 647 | if result == 0 |
| 648 | else constants.BUILDER_STATUS_FAILED |
| 649 | ) |
| 650 | SetLastBuildState(root, build_state) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 651 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 652 | with metrics.SecondsTimer(METRIC_CHROOT_CLEANUP): |
| 653 | CleanupChroot(buildroot) |
Don Garrett | 91a8cfc | 2018-06-22 15:39:36 -0700 | [diff] [blame] | 654 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 655 | return result |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 656 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 657 | |
| 658 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 659 | options = PreParseArguments(argv) |
| 660 | metric_fields = { |
| 661 | "branch_name": options.branch or "main", |
| 662 | "build_config": options.build_config_name, |
| 663 | "tryjob": options.remote_trybot, |
| 664 | } |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 665 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 666 | # Enable Monarch metrics gathering. |
| 667 | with ts_mon_config.SetupTsMonGlobalState( |
| 668 | "cbuildbot_launch", common_metric_fields=metric_fields, indirect=True |
| 669 | ): |
| 670 | return _main(options, argv) |