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