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