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