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 |
| 11 | script will use 'master'. |
| 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 |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 22 | import time |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 23 | |
| 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 |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 28 | from chromite.lib import config_lib |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 29 | from chromite.lib import constants |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 30 | from chromite.lib import cros_build_lib |
| 31 | from chromite.lib import cros_logging as logging |
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 |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 35 | from chromite.lib import ts_mon_config |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 36 | from chromite.scripts import cbuildbot |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [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. |
Mike Nichols | cb29fbd | 2018-07-24 11:09:33 -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 |
| 59 | BUILDER_STATE_FILENAME = '.cbuildbot_build_state.json' |
| 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): |
| 63 | """A Decorator that adds buildbot stage tags around a method. |
| 64 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 65 | It uses the method name as the stage name, and assumes failure on a true |
| 66 | return value, or an exception. |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 67 | """ |
| 68 | @functools.wraps(functor) |
| 69 | def wrapped_functor(*args, **kwargs): |
| 70 | try: |
| 71 | logging.PrintBuildbotStepName(functor.__name__) |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 72 | result = functor(*args, **kwargs) |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 73 | except Exception: |
| 74 | logging.PrintBuildbotStepFailure() |
| 75 | raise |
| 76 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 77 | if result: |
| 78 | logging.PrintBuildbotStepFailure() |
| 79 | return result |
| 80 | |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 81 | return wrapped_functor |
| 82 | |
| 83 | |
Don Garrett | b5fc08b | 2017-11-20 21:51:16 +0000 | [diff] [blame] | 84 | def field(fields, **kwargs): |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 85 | """Helper for inserting more fields into a metrics fields dictionary. |
| 86 | |
| 87 | Args: |
| 88 | fields: Dictionary of metrics fields. |
| 89 | kwargs: Each argument is a key/value pair to insert into dict. |
| 90 | |
| 91 | Returns: |
| 92 | Copy of original dictionary with kwargs set as fields. |
| 93 | """ |
| 94 | f = fields.copy() |
| 95 | f.update(kwargs) |
| 96 | return f |
| 97 | |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 98 | |
| 99 | def PrependPath(prepend): |
| 100 | """Generate path with new directory at the beginning. |
| 101 | |
| 102 | Args: |
| 103 | prepend: Directory to add at the beginning of the path. |
| 104 | |
| 105 | Returns: |
| 106 | Extended path as a string. |
| 107 | """ |
| 108 | return os.pathsep.join([prepend, os.environ.get('PATH', os.defpath)]) |
| 109 | |
Aviv Keshet | c38eebe | 2018-09-27 23:19:58 +0000 | [diff] [blame] | 110 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 111 | def PreParseArguments(argv): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 112 | """Extract the branch name from cbuildbot command line arguments. |
| 113 | |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 114 | Args: |
| 115 | argv: The command line arguments to parse. |
| 116 | |
| 117 | Returns: |
| 118 | Branch as a string ('master' if nothing is specified). |
| 119 | """ |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 120 | parser = cbuildbot.CreateParser() |
Mike Frysinger | 80bba8a | 2017-08-18 15:28:36 -0400 | [diff] [blame] | 121 | options = cbuildbot.ParseCommandLine(parser, argv) |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 122 | options.Freeze() |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 123 | |
| 124 | # This option isn't required for cbuildbot, but is for us. |
| 125 | if not options.buildroot: |
| 126 | cros_build_lib.Die('--buildroot is a required option.') |
| 127 | |
| 128 | return options |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 129 | |
Aviv Keshet | c38eebe | 2018-09-27 23:19:58 +0000 | [diff] [blame] | 130 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 131 | def GetCurrentBuildState(options, branch): |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 132 | """Extract information about the current build state from command-line args. |
| 133 | |
| 134 | Args: |
| 135 | options: A parsed options object from a cbuildbot parser. |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 136 | branch: The name of the branch this builder was called with. |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 137 | |
| 138 | Returns: |
| 139 | A BuildSummary object describing the current build. |
| 140 | """ |
| 141 | build_state = build_summary.BuildSummary( |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 142 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 143 | buildroot_layout=BUILDROOT_BUILDROOT_LAYOUT, |
| 144 | branch=branch) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 145 | if options.buildnumber: |
| 146 | build_state.build_number = options.buildnumber |
| 147 | if options.buildbucket_id: |
| 148 | build_state.buildbucket_id = options.buildbucket_id |
| 149 | if options.master_build_id: |
| 150 | build_state.master_build_id = options.master_build_id |
| 151 | return build_state |
| 152 | |
| 153 | |
| 154 | def GetLastBuildState(root): |
| 155 | """Fetch the state of the last build run from |root|. |
| 156 | |
| 157 | If the saved state file can't be read or doesn't contain valid JSON, a default |
| 158 | state will be returned. |
| 159 | |
| 160 | Args: |
| 161 | root: Root of the working directory tree as a string. |
| 162 | |
| 163 | Returns: |
| 164 | A BuildSummary object representing the previous build. |
| 165 | """ |
| 166 | state_file = os.path.join(root, BUILDER_STATE_FILENAME) |
| 167 | |
| 168 | state = build_summary.BuildSummary() |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 169 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 170 | try: |
| 171 | state_raw = osutils.ReadFile(state_file) |
| 172 | state.from_json(state_raw) |
| 173 | except IOError as e: |
| 174 | logging.warning('Unable to read %s: %s', state_file, e) |
| 175 | return state |
| 176 | except ValueError as e: |
| 177 | logging.warning('Saved state file %s is not valid JSON: %s', state_file, e) |
| 178 | return state |
| 179 | |
| 180 | if not state.is_valid(): |
| 181 | logging.warning('Previous build state is not valid. Ignoring.') |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 182 | state = build_summary.BuildSummary() |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 183 | |
| 184 | return state |
| 185 | |
| 186 | |
| 187 | def SetLastBuildState(root, new_state): |
| 188 | """Save the state of the last build under |root|. |
| 189 | |
| 190 | Args: |
| 191 | root: Root of the working directory tree as a string. |
| 192 | new_state: BuildSummary object containing the state to be saved. |
| 193 | """ |
| 194 | state_file = os.path.join(root, BUILDER_STATE_FILENAME) |
| 195 | osutils.WriteFile(state_file, new_state.to_json()) |
| 196 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 197 | # Remove old state file. Its contents have been migrated into the new file. |
| 198 | old_state_file = os.path.join(root, '.cbuildbot_launch_state') |
| 199 | osutils.SafeUnlink(old_state_file) |
| 200 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 201 | |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 202 | def _MaybeCleanDistfiles(repo, distfiles_ts): |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 203 | """Cleans the distfiles directory if too old. |
| 204 | |
| 205 | Args: |
| 206 | repo: repository.RepoRepository instance. |
| 207 | distfiles_ts: A timestamp str for the last time distfiles was cleaned. May |
| 208 | be None. |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 209 | |
| 210 | Returns: |
| 211 | The new distfiles_ts to persist in state. |
| 212 | """ |
Don Garrett | e3f1a47 | 2018-06-19 13:03:21 -0700 | [diff] [blame] | 213 | # distfiles_ts can be None for a fresh environment, which means clean. |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 214 | if distfiles_ts is None: |
Don Garrett | e3f1a47 | 2018-06-19 13:03:21 -0700 | [diff] [blame] | 215 | return time.time() |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 216 | |
| 217 | distfiles_age = (time.time() - distfiles_ts) / 3600.0 |
| 218 | if distfiles_age < _DISTFILES_CACHE_EXPIRY_HOURS: |
| 219 | return distfiles_ts |
| 220 | |
| 221 | logging.info('Remove old distfiles cache (cache expiry %d hours)', |
| 222 | _DISTFILES_CACHE_EXPIRY_HOURS) |
| 223 | osutils.RmDir(os.path.join(repo.directory, '.cache', 'distfiles'), |
| 224 | ignore_missing=True, sudo=True) |
| 225 | metrics.Counter(METRIC_DISTFILES_CLEANUP).increment( |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 226 | field({}, reason='cache_expired')) |
Don Garrett | e3f1a47 | 2018-06-19 13:03:21 -0700 | [diff] [blame] | 227 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 228 | # Cleaned cache, so reset distfiles_ts |
Don Garrett | e3f1a47 | 2018-06-19 13:03:21 -0700 | [diff] [blame] | 229 | return time.time() |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 230 | |
| 231 | |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 232 | @StageDecorator |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 233 | def CleanBuildRoot(root, repo, build_state): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 234 | """Some kinds of branch transitions break builds. |
| 235 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 236 | This method ensures that cbuildbot's buildroot is a clean checkout on the |
| 237 | given branch when it starts. If necessary (a branch transition) it will wipe |
| 238 | assorted state that cannot be safely reused from the previous build. |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 239 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 240 | Args: |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 241 | root: Root directory owned by cbuildbot_launch. |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 242 | repo: repository.RepoRepository instance. |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 243 | build_state: BuildSummary object containing the current build state that |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 244 | will be saved into the cleaned root. The distfiles_ts property will |
| 245 | be updated if the distfiles cache is cleaned. |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 246 | """ |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 247 | previous_state = GetLastBuildState(root) |
| 248 | build_state.distfiles_ts = _MaybeCleanDistfiles( |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 249 | repo, previous_state.distfiles_ts) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 250 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 251 | if previous_state.buildroot_layout != BUILDROOT_BUILDROOT_LAYOUT: |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 252 | logging.PrintBuildbotStepText('Unknown layout: Wiping buildroot.') |
Don Garrett | b5fc08b | 2017-11-20 21:51:16 +0000 | [diff] [blame] | 253 | metrics.Counter(METRIC_CLOBBER).increment( |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 254 | field({}, reason='layout_change')) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 255 | chroot_dir = os.path.join(root, constants.DEFAULT_CHROOT_DIR) |
Don Garrett | b5fc08b | 2017-11-20 21:51:16 +0000 | [diff] [blame] | 256 | if os.path.exists(chroot_dir) or os.path.exists(chroot_dir + '.img'): |
Don Garrett | 3665011 | 2018-06-28 15:54:34 -0700 | [diff] [blame] | 257 | cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=True) |
Don Garrett | b5fc08b | 2017-11-20 21:51:16 +0000 | [diff] [blame] | 258 | osutils.RmDir(root, ignore_missing=True, sudo=True) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 259 | else: |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 260 | if previous_state.branch != repo.branch: |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 261 | logging.PrintBuildbotStepText('Branch change: Cleaning buildroot.') |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 262 | logging.info('Unmatched branch: %s -> %s', previous_state.branch, |
| 263 | repo.branch) |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 264 | metrics.Counter(METRIC_BRANCH_CLEANUP).increment( |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 265 | field({}, old_branch=previous_state.branch)) |
Don Garrett | 3996360 | 2017-02-27 14:41:58 -0800 | [diff] [blame] | 266 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 267 | logging.info('Remove Chroot.') |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 268 | chroot_dir = os.path.join(repo.directory, constants.DEFAULT_CHROOT_DIR) |
Benjamin Gordon | 59ba2f8 | 2017-08-28 15:31:06 -0600 | [diff] [blame] | 269 | if os.path.exists(chroot_dir) or os.path.exists(chroot_dir + '.img'): |
Don Garrett | 3665011 | 2018-06-28 15:54:34 -0700 | [diff] [blame] | 270 | cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 271 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 272 | logging.info('Remove Chrome checkout.') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 273 | osutils.RmDir(os.path.join(repo.directory, '.cache', 'distfiles'), |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 274 | ignore_missing=True, sudo=True) |
| 275 | |
Don Garrett | 1241c4e | 2018-08-06 17:33:41 -0700 | [diff] [blame] | 276 | try: |
| 277 | # If there is any failure doing the cleanup, wipe everything. |
| 278 | # The previous run might have been killed in the middle leaving stale git |
| 279 | # locks. Clean those up, first. |
| 280 | repo.PreLoad() |
| 281 | repo.CleanStaleLocks() |
| 282 | repo.BuildRootGitCleanup(prune_all=True) |
| 283 | except Exception: |
| 284 | logging.info('Checkout cleanup failed, wiping buildroot:', exc_info=True) |
| 285 | metrics.Counter(METRIC_CLOBBER).increment( |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 286 | field({}, reason='repo_cleanup_failure')) |
Don Garrett | 1241c4e | 2018-08-06 17:33:41 -0700 | [diff] [blame] | 287 | repository.ClearBuildRoot(repo.directory) |
Don Garrett | 3996360 | 2017-02-27 14:41:58 -0800 | [diff] [blame] | 288 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 289 | # Ensure buildroot exists. Save the state we are prepped for. |
| 290 | osutils.SafeMakedirs(repo.directory) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 291 | SetLastBuildState(root, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 292 | |
| 293 | |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 294 | @StageDecorator |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 295 | def InitialCheckout(repo): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 296 | """Preliminary ChromeOS checkout. |
| 297 | |
| 298 | Perform a complete checkout of ChromeOS on the specified branch. This does NOT |
| 299 | match what the build needs, but ensures the buildroot both has a 'hot' |
| 300 | checkout, and is close enough that the branched cbuildbot can successfully get |
| 301 | the right checkout. |
| 302 | |
| 303 | This checks out full ChromeOS, even if a ChromiumOS build is going to be |
| 304 | performed. This is because we have no knowledge of the build config to be |
| 305 | used. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 306 | |
| 307 | Args: |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 308 | repo: repository.RepoRepository instance. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 309 | """ |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 310 | logging.PrintBuildbotStepText('Branch: %s' % repo.branch) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 311 | logging.info('Bootstrap script starting initial sync on branch: %s', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 312 | repo.branch) |
Don Garrett | 5516acb | 2018-11-15 16:02:59 -0800 | [diff] [blame^] | 313 | repo.PreLoad('/preload/chromeos') |
Don Garrett | 7649691 | 2017-05-11 16:59:11 -0700 | [diff] [blame] | 314 | repo.Sync(detach=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 315 | |
| 316 | |
Lann Martin | 8c4c680 | 2018-05-23 11:09:46 -0600 | [diff] [blame] | 317 | def ShouldFixBotoCerts(options): |
| 318 | """Decide if FixBotoCerts should be applied for this branch.""" |
| 319 | try: |
| 320 | # Only apply to factory and firmware branches. |
| 321 | branch = options.branch or '' |
| 322 | prefix = branch.split('-')[0] |
| 323 | if prefix not in ('factory', 'firmware'): |
| 324 | return False |
| 325 | |
| 326 | # Only apply to "old" branches. |
| 327 | if branch.endswith('.B'): |
| 328 | version = branch[:-2].split('-')[-1] |
| 329 | major = int(version.split('.')[0]) |
| 330 | return major <= 9667 # This is the newest known to be failing. |
| 331 | |
| 332 | return False |
Mike Frysinger | 9f47026 | 2018-08-03 15:09:58 -0400 | [diff] [blame] | 333 | except Exception as e: |
Lann Martin | 8c4c680 | 2018-05-23 11:09:46 -0600 | [diff] [blame] | 334 | logging.warning(' failed: %s', e) |
| 335 | # Conservatively continue without the fix. |
| 336 | return False |
| 337 | |
| 338 | |
Don Garrett | 066e6f5 | 2017-09-28 19:14:01 -0700 | [diff] [blame] | 339 | @StageDecorator |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 340 | def Cbuildbot(buildroot, depot_tools_path, argv): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 341 | """Start cbuildbot in specified directory with all arguments. |
| 342 | |
| 343 | Args: |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 344 | buildroot: Directory to be passed to cbuildbot with --buildroot. |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 345 | depot_tools_path: Directory for depot_tools to be used by cbuildbot. |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 346 | argv: Command line options passed to cbuildbot_launch. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 347 | |
| 348 | Returns: |
| 349 | Return code of cbuildbot as an integer. |
| 350 | """ |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 351 | logging.info('Bootstrap cbuildbot in: %s', buildroot) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 352 | |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 353 | # Fixup buildroot parameter. |
| 354 | argv = argv[:] |
| 355 | for i in xrange(len(argv)): |
| 356 | if argv[i] in ('-r', '--buildroot'): |
| 357 | argv[i+1] = buildroot |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 358 | |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 359 | # This filters out command line arguments not supported by older versions |
| 360 | # of cbuildbot. |
| 361 | parser = cbuildbot.CreateParser() |
Mike Frysinger | 80bba8a | 2017-08-18 15:28:36 -0400 | [diff] [blame] | 362 | options = cbuildbot.ParseCommandLine(parser, argv) |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 363 | cbuildbot_path = os.path.join(buildroot, 'chromite', 'bin', 'cbuildbot') |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 364 | cmd = sync_stages.BootstrapStage.FilterArgsForTargetCbuildbot( |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 365 | buildroot, cbuildbot_path, options) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 366 | |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 367 | # We want cbuildbot to use branched depot_tools scripts from our manifest, |
| 368 | # so that depot_tools is branched to match cbuildbot. |
| 369 | logging.info('Adding depot_tools into PATH: %s', depot_tools_path) |
| 370 | extra_env = {'PATH': PrependPath(depot_tools_path)} |
| 371 | |
Lann Martin | ebae73d | 2018-05-21 17:12:00 -0600 | [diff] [blame] | 372 | # TODO(crbug.com/845304): Remove once underlying boto issues are resolved. |
Lann Martin | 8c4c680 | 2018-05-23 11:09:46 -0600 | [diff] [blame] | 373 | fix_boto = ShouldFixBotoCerts(options) |
Lann Martin | ebae73d | 2018-05-21 17:12:00 -0600 | [diff] [blame] | 374 | |
| 375 | with boto_compat.FixBotoCerts(activate=fix_boto): |
| 376 | result = cros_build_lib.RunCommand( |
| 377 | cmd, extra_env=extra_env, error_code_ok=True, cwd=buildroot) |
| 378 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 379 | return result.returncode |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 380 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 381 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 382 | @StageDecorator |
| 383 | def CleanupChroot(buildroot): |
| 384 | """Unmount/clean up an image-based chroot without deleting the backing image. |
| 385 | |
| 386 | Args: |
| 387 | buildroot: Directory containing the chroot to be cleaned up. |
| 388 | """ |
| 389 | chroot_dir = os.path.join(buildroot, constants.DEFAULT_CHROOT_DIR) |
| 390 | logging.info('Cleaning up chroot at %s', chroot_dir) |
| 391 | if os.path.exists(chroot_dir) or os.path.exists(chroot_dir + '.img'): |
Don Garrett | 3665011 | 2018-06-28 15:54:34 -0700 | [diff] [blame] | 392 | cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=False) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 393 | |
| 394 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 395 | def ConfigureGlobalEnvironment(): |
| 396 | """Setup process wide environmental changes.""" |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 397 | # Set umask to 022 so files created by buildbot are readable. |
| 398 | os.umask(0o22) |
| 399 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 400 | # These variables can interfere with LANG / locale behavior. |
| 401 | unwanted_local_vars = [ |
| 402 | 'LC_ALL', 'LC_CTYPE', 'LC_COLLATE', 'LC_TIME', 'LC_NUMERIC', |
| 403 | 'LC_MONETARY', 'LC_MESSAGES', 'LC_PAPER', 'LC_NAME', 'LC_ADDRESS', |
| 404 | 'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION', |
| 405 | ] |
| 406 | for v in unwanted_local_vars: |
| 407 | os.environ.pop(v, None) |
| 408 | |
| 409 | # This variable is required for repo sync's to work in all cases. |
| 410 | os.environ['LANG'] = 'en_US.UTF-8' |
| 411 | |
Aviv Keshet | c38eebe | 2018-09-27 23:19:58 +0000 | [diff] [blame] | 412 | |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 413 | def _main(options, argv): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 414 | """main method of script. |
| 415 | |
| 416 | Args: |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 417 | options: preparsed options object for the build. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 418 | argv: All command line arguments to pass as list of strings. |
| 419 | |
| 420 | Returns: |
| 421 | Return code of cbuildbot as an integer. |
| 422 | """ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 423 | branchname = options.branch or 'master' |
| 424 | root = options.buildroot |
| 425 | buildroot = os.path.join(root, 'repository') |
Don Garrett | b497f55 | 2018-07-09 16:01:13 -0700 | [diff] [blame] | 426 | workspace = os.path.join(root, 'workspace') |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 427 | depot_tools_path = os.path.join(buildroot, constants.DEPOT_TOOLS_SUBPATH) |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 428 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 429 | # Does the entire build pass or fail. |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 430 | with metrics.Presence(METRIC_ACTIVE), \ |
| 431 | metrics.SuccessCounter(METRIC_COMPLETED) as s_fields: |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 432 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 433 | # Preliminary set, mostly command line parsing. |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 434 | with metrics.SuccessCounter(METRIC_INVOKED): |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 435 | if options.enable_buildbot_tags: |
| 436 | logging.EnableBuildbotMarkers() |
| 437 | ConfigureGlobalEnvironment() |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 438 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 439 | # Prepare the buildroot with source for the build. |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 440 | with metrics.SuccessCounter(METRIC_PREP): |
Alex Klein | 2ab29cc | 2018-07-19 12:01:00 -0600 | [diff] [blame] | 441 | manifest_url = config_lib.GetSiteParams().MANIFEST_INT_URL |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 442 | repo = repository.RepoRepository(manifest_url, buildroot, |
| 443 | branch=branchname, |
Don Garrett | 3387250 | 2018-08-03 22:30:40 +0000 | [diff] [blame] | 444 | git_cache_dir=options.git_cache_dir) |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 445 | previous_build_state = GetLastBuildState(root) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 446 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 447 | # Clean up the buildroot to a safe state. |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 448 | with metrics.SecondsTimer(METRIC_CLEAN): |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 449 | build_state = GetCurrentBuildState(options, branchname) |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 450 | CleanBuildRoot(root, repo, build_state) |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 451 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 452 | # Get a checkout close enough to the branch that cbuildbot can handle it. |
| 453 | if options.sync: |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 454 | with metrics.SecondsTimer(METRIC_INITIAL): |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 455 | InitialCheckout(repo) |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 456 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 457 | # Run cbuildbot inside the full ChromeOS checkout, on the specified branch. |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 458 | with metrics.SecondsTimer(METRIC_CBUILDBOT), \ |
| 459 | metrics.SecondsInstanceTimer(METRIC_CBUILDBOT_INSTANCE): |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 460 | if previous_build_state.is_valid(): |
| 461 | argv.append('--previous-build-state') |
| 462 | argv.append(base64.b64encode(previous_build_state.to_json())) |
Don Garrett | b497f55 | 2018-07-09 16:01:13 -0700 | [diff] [blame] | 463 | argv.extend(['--workspace', workspace]) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 464 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 465 | result = Cbuildbot(buildroot, depot_tools_path, argv) |
| 466 | s_fields['success'] = (result == 0) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 467 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 468 | build_state.status = ( |
| 469 | constants.BUILDER_STATUS_PASSED |
| 470 | if result == 0 else constants.BUILDER_STATUS_FAILED) |
| 471 | SetLastBuildState(root, build_state) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 472 | |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 473 | with metrics.SecondsTimer(METRIC_CHROOT_CLEANUP): |
Don Garrett | 91a8cfc | 2018-06-22 15:39:36 -0700 | [diff] [blame] | 474 | CleanupChroot(buildroot) |
| 475 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 476 | return result |
| 477 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 478 | |
| 479 | def main(argv): |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 480 | options = PreParseArguments(argv) |
| 481 | metric_fields = { |
| 482 | 'branch_name': options.branch or 'master', |
| 483 | 'build_config': options.build_config_name, |
| 484 | 'tryjob': options.remote_trybot, |
| 485 | } |
| 486 | |
Ben Pastene | c8e4e79 | 2018-05-14 20:20:25 +0000 | [diff] [blame] | 487 | # Enable Monarch metrics gathering. |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 488 | with ts_mon_config.SetupTsMonGlobalState('cbuildbot_launch', |
| 489 | common_metric_fields=metric_fields, |
| 490 | indirect=True): |
| 491 | return _main(options, argv) |