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