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