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