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