blob: 4393f9bc859207e15eaf20f6165c259ccb446775 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Don Garrettc4114cc2016-11-01 20:04:06 -07002# 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
8This 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
10arguments are not parsed, only passed along. If a branch is not specified, this
11script will use 'master'.
12
13Among other things, this allows us to invoke build configs that exist on a given
14branch, but not on TOT.
15"""
16
17from __future__ import print_function
18
Don Garrett125d4dc2017-04-25 16:26:03 -070019import functools
Don Garrettc4114cc2016-11-01 20:04:06 -070020import os
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -070021import time
Don Garrettc4114cc2016-11-01 20:04:06 -070022
23from chromite.cbuildbot import repository
Don Garrett597ddff2017-02-17 18:29:37 -080024from chromite.cbuildbot.stages import sync_stages
Don Garrett86881cb2017-02-15 15:41:55 -080025from chromite.lib import config_lib
Don Garretta50bf492017-09-28 18:33:02 -070026from chromite.lib import constants
Don Garrettc4114cc2016-11-01 20:04:06 -070027from chromite.lib import cros_build_lib
28from chromite.lib import cros_logging as logging
Don Garrettacbb2392017-05-11 18:27:41 -070029from chromite.lib import metrics
Don Garrettc4114cc2016-11-01 20:04:06 -070030from chromite.lib import osutils
Don Garrettacbb2392017-05-11 18:27:41 -070031from chromite.lib import ts_mon_config
Don Garrett86881cb2017-02-15 15:41:55 -080032from chromite.scripts import cbuildbot
Don Garrettc4114cc2016-11-01 20:04:06 -070033
Don Garrett60967922017-04-12 18:51:44 -070034# 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 Garrettbf90cdf2017-05-19 15:54:02 -070036BUILDROOT_BUILDROOT_LAYOUT = 2
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -070037_DISTFILES_CACHE_EXPIRY_HOURS = 8 * 24
Don Garrett60967922017-04-12 18:51:44 -070038
Don Garrettacbb2392017-05-11 18:27:41 -070039# Metrics reported to Monarch.
Don Garrett45e77412017-06-14 16:57:55 -070040METRIC_ACTIVE = 'chromeos/chromite/cbuildbot_launch/active'
Don Garrettacbb2392017-05-11 18:27:41 -070041METRIC_INVOKED = 'chromeos/chromite/cbuildbot_launch/invoked'
42METRIC_COMPLETED = 'chromeos/chromite/cbuildbot_launch/completed'
43METRIC_PREP = 'chromeos/chromite/cbuildbot_launch/prep_completed'
44METRIC_CLEAN = 'chromeos/chromite/cbuildbot_launch/clean_buildroot_durations'
45METRIC_INITIAL = 'chromeos/chromite/cbuildbot_launch/initial_checkout_durations'
46METRIC_CBUILDBOT = 'chromeos/chromite/cbuildbot_launch/cbuildbot_durations'
47METRIC_CLOBBER = 'chromeos/chromite/cbuildbot_launch/clobber'
48METRIC_BRANCH_CLEANUP = 'chromeos/chromite/cbuildbot_launch/branch_cleanup'
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -070049METRIC_DISTFILES_CLEANUP = (
50 'chromeos/chromite/cbuildbot_launch/distfiles_cleanup')
Don Garrett066e6f52017-09-28 19:14:01 -070051METRIC_DEPOT_TOOLS = 'chromeos/chromite/cbuildbot_launch/depot_tools_prep'
Don Garrettacbb2392017-05-11 18:27:41 -070052
Don Garrett60967922017-04-12 18:51:44 -070053
Don Garrett125d4dc2017-04-25 16:26:03 -070054def StageDecorator(functor):
55 """A Decorator that adds buildbot stage tags around a method.
56
Don Garrettacbb2392017-05-11 18:27:41 -070057 It uses the method name as the stage name, and assumes failure on a true
58 return value, or an exception.
Don Garrett125d4dc2017-04-25 16:26:03 -070059 """
60 @functools.wraps(functor)
61 def wrapped_functor(*args, **kwargs):
62 try:
63 logging.PrintBuildbotStepName(functor.__name__)
Don Garrettacbb2392017-05-11 18:27:41 -070064 result = functor(*args, **kwargs)
Don Garrett125d4dc2017-04-25 16:26:03 -070065 except Exception:
66 logging.PrintBuildbotStepFailure()
67 raise
68
Don Garrettacbb2392017-05-11 18:27:41 -070069 if result:
70 logging.PrintBuildbotStepFailure()
71 return result
72
Don Garrett125d4dc2017-04-25 16:26:03 -070073 return wrapped_functor
74
75
Don Garrettb5fc08b2017-11-20 21:51:16 +000076def field(fields, **kwargs):
Don Garrettacbb2392017-05-11 18:27:41 -070077 """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 Garretta50bf492017-09-28 18:33:02 -070090
91def 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 Garrett86881cb2017-02-15 15:41:55 -0800103def PreParseArguments(argv):
Don Garrettc4114cc2016-11-01 20:04:06 -0700104 """Extract the branch name from cbuildbot command line arguments.
105
Don Garrettc4114cc2016-11-01 20:04:06 -0700106 Args:
107 argv: The command line arguments to parse.
108
109 Returns:
110 Branch as a string ('master' if nothing is specified).
111 """
Don Garrett86881cb2017-02-15 15:41:55 -0800112 parser = cbuildbot.CreateParser()
Mike Frysinger80bba8a2017-08-18 15:28:36 -0400113 options = cbuildbot.ParseCommandLine(parser, argv)
Don Garrettd1d90dd2017-06-13 17:35:52 -0700114 options.Freeze()
Don Garrett86881cb2017-02-15 15:41:55 -0800115
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 Garrettc4114cc2016-11-01 20:04:06 -0700121
122
Don Garrettbf90cdf2017-05-19 15:54:02 -0700123def 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 Prabhuc41a0f52018-04-03 13:26:52 -0700134 Last distfiles clearance time in POSIX time (None for unknown).
Don Garrettbf90cdf2017-05-19 15:54:02 -0700135 """
136 state_file = os.path.join(root, '.cbuildbot_launch_state')
Don Garrett60967922017-04-12 18:51:44 -0700137
138 try:
139 state = osutils.ReadFile(state_file)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700140 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 Garrett60967922017-04-12 18:51:44 -0700148 # If we are unable to either read or parse the state file, we get here.
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700149 return 0, '', None
Don Garrett60967922017-04-12 18:51:44 -0700150
151
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700152def SetState(branchname, root, distfiles_ts=None):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700153 """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 Prabhuc41a0f52018-04-03 13:26:52 -0700158 distfiles_ts: float unix timestamp to include as the distfiles timestamp. If
159 None, current time will be used.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700160 """
Don Garrett60967922017-04-12 18:51:44 -0700161 assert branchname
Don Garrettbf90cdf2017-05-19 15:54:02 -0700162 state_file = os.path.join(root, '.cbuildbot_launch_state')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700163 if distfiles_ts is None:
164 distfiles_ts = time.time()
165 new_state = '%d %s %f' % (
166 BUILDROOT_BUILDROOT_LAYOUT, branchname, distfiles_ts)
Don Garrett60967922017-04-12 18:51:44 -0700167 osutils.WriteFile(state_file, new_state)
168
169
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700170def _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 Garrett125d4dc2017-04-25 16:26:03 -0700200@StageDecorator
Don Garrettbf90cdf2017-05-19 15:54:02 -0700201def CleanBuildRoot(root, repo, metrics_fields):
Don Garrett7ade05a2017-02-17 13:31:47 -0800202 """Some kinds of branch transitions break builds.
203
Don Garrettbf90cdf2017-05-19 15:54:02 -0700204 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 Garrett7ade05a2017-02-17 13:31:47 -0800207
Don Garrett7ade05a2017-02-17 13:31:47 -0800208 Args:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700209 root: Root directory owned by cbuildbot_launch.
Don Garrettf324bc32017-05-23 14:00:53 -0700210 repo: repository.RepoRepository instance.
Don Garrettacbb2392017-05-11 18:27:41 -0700211 metrics_fields: Dictionary of fields to include in metrics.
Don Garrett7ade05a2017-02-17 13:31:47 -0800212 """
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700213 old_buildroot_layout, old_branch, distfiles_ts = GetState(root)
214 distfiles_ts = _MaybeCleanDistfiles(repo, distfiles_ts, metrics_fields)
Don Garrette17e1d92017-04-12 15:28:19 -0700215
Don Garrett60967922017-04-12 18:51:44 -0700216 if old_buildroot_layout != BUILDROOT_BUILDROOT_LAYOUT:
Don Garrett125d4dc2017-04-25 16:26:03 -0700217 logging.PrintBuildbotStepText('Unknown layout: Wiping buildroot.')
Don Garrettb5fc08b2017-11-20 21:51:16 +0000218 metrics.Counter(METRIC_CLOBBER).increment(
219 field(metrics_fields, reason='layout_change'))
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700220 chroot_dir = os.path.join(root, constants.DEFAULT_CHROOT_DIR)
Don Garrettb5fc08b2017-11-20 21:51:16 +0000221 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 Garrettf324bc32017-05-23 14:00:53 -0700224 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 Garrettacbb2392017-05-11 18:27:41 -0700228 metrics.Counter(METRIC_BRANCH_CLEANUP).increment(
Don Garrettb5fc08b2017-11-20 21:51:16 +0000229 field(metrics_fields, old_branch=old_branch))
Don Garrett39963602017-02-27 14:41:58 -0800230
Don Garrettf324bc32017-05-23 14:00:53 -0700231 logging.info('Remove Chroot.')
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700232 chroot_dir = os.path.join(repo.directory, constants.DEFAULT_CHROOT_DIR)
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600233 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 Garrett7ade05a2017-02-17 13:31:47 -0800236
Don Garrettf324bc32017-05-23 14:00:53 -0700237 logging.info('Remove Chrome checkout.')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700238 osutils.RmDir(os.path.join(repo.directory, '.cache', 'distfiles'),
Don Garrettf324bc32017-05-23 14:00:53 -0700239 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 Garrettacbb2392017-05-11 18:27:41 -0700246 metrics.Counter(METRIC_CLOBBER).increment(
Don Garrettb5fc08b2017-11-20 21:51:16 +0000247 field(metrics_fields, reason='repo_cleanup_failure'))
Don Garrettbf90cdf2017-05-19 15:54:02 -0700248 repository.ClearBuildRoot(repo.directory)
Don Garrett39963602017-02-27 14:41:58 -0800249
Don Garrettbf90cdf2017-05-19 15:54:02 -0700250 # Ensure buildroot exists. Save the state we are prepped for.
251 osutils.SafeMakedirs(repo.directory)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700252 SetState(repo.branch, root, distfiles_ts)
Don Garrett7ade05a2017-02-17 13:31:47 -0800253
254
Don Garrett125d4dc2017-04-25 16:26:03 -0700255@StageDecorator
Don Garrettf324bc32017-05-23 14:00:53 -0700256def InitialCheckout(repo):
Don Garrett86881cb2017-02-15 15:41:55 -0800257 """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 Garrettc4114cc2016-11-01 20:04:06 -0700267
268 Args:
Don Garrettf324bc32017-05-23 14:00:53 -0700269 repo: repository.RepoRepository instance.
Don Garrettc4114cc2016-11-01 20:04:06 -0700270 """
Don Garrettf324bc32017-05-23 14:00:53 -0700271 logging.PrintBuildbotStepText('Branch: %s' % repo.branch)
Don Garrett7ade05a2017-02-17 13:31:47 -0800272 logging.info('Bootstrap script starting initial sync on branch: %s',
Don Garrettf324bc32017-05-23 14:00:53 -0700273 repo.branch)
Don Garrett76496912017-05-11 16:59:11 -0700274 repo.Sync(detach=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700275
276
Don Garrett125d4dc2017-04-25 16:26:03 -0700277@StageDecorator
Don Garrett066e6f52017-09-28 19:14:01 -0700278def 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 Garretta50bf492017-09-28 18:33:02 -0700301def RunCbuildbot(buildroot, depot_tools_path, argv):
Don Garrettc4114cc2016-11-01 20:04:06 -0700302 """Start cbuildbot in specified directory with all arguments.
303
304 Args:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700305 buildroot: Directory to be passed to cbuildbot with --buildroot.
Don Garretta50bf492017-09-28 18:33:02 -0700306 depot_tools_path: Directory for depot_tools to be used by cbuildbot.
Don Garrettd1d90dd2017-06-13 17:35:52 -0700307 argv: Command line options passed to cbuildbot_launch.
Don Garrettc4114cc2016-11-01 20:04:06 -0700308
309 Returns:
310 Return code of cbuildbot as an integer.
311 """
Don Garrettbf90cdf2017-05-19 15:54:02 -0700312 logging.info('Bootstrap cbuildbot in: %s', buildroot)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700313
Don Garrettd1d90dd2017-06-13 17:35:52 -0700314 # 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 Garrett597ddff2017-02-17 18:29:37 -0800319
Don Garrettd1d90dd2017-06-13 17:35:52 -0700320 # This filters out command line arguments not supported by older versions
321 # of cbuildbot.
322 parser = cbuildbot.CreateParser()
Mike Frysinger80bba8a2017-08-18 15:28:36 -0400323 options = cbuildbot.ParseCommandLine(parser, argv)
Don Garrettd1d90dd2017-06-13 17:35:52 -0700324 cbuildbot_path = os.path.join(buildroot, 'chromite', 'bin', 'cbuildbot')
Don Garrett597ddff2017-02-17 18:29:37 -0800325 cmd = sync_stages.BootstrapStage.FilterArgsForTargetCbuildbot(
Don Garrettbf90cdf2017-05-19 15:54:02 -0700326 buildroot, cbuildbot_path, options)
Don Garrett597ddff2017-02-17 18:29:37 -0800327
Don Garretta50bf492017-09-28 18:33:02 -0700328 # 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 Garrettacbb2392017-05-11 18:27:41 -0700335 return result.returncode
Don Garrettc4114cc2016-11-01 20:04:06 -0700336
Don Garrett60967922017-04-12 18:51:44 -0700337
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700338@StageDecorator
339def 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 Garrettf15d65b2017-04-12 12:39:55 -0700351def ConfigureGlobalEnvironment():
352 """Setup process wide environmental changes."""
Don Garrettf15d65b2017-04-12 12:39:55 -0700353 # Set umask to 022 so files created by buildbot are readable.
354 os.umask(0o22)
355
Don Garrett86fec482017-05-17 18:13:33 -0700356 # 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 Garrettc4114cc2016-11-01 20:04:06 -0700368
Don Garrettacbb2392017-05-11 18:27:41 -0700369def _main(argv):
Don Garrettc4114cc2016-11-01 20:04:06 -0700370 """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 Garrettd1d90dd2017-06-13 17:35:52 -0700378 options = PreParseArguments(argv)
379
380 branchname = options.branch or 'master'
381 root = options.buildroot
382 buildroot = os.path.join(root, 'repository')
Don Garretta50bf492017-09-28 18:33:02 -0700383 depot_tools_path = os.path.join(buildroot, constants.DEPOT_TOOLS_SUBPATH)
Don Garrettd1d90dd2017-06-13 17:35:52 -0700384
385 metrics_fields = {
386 'branch_name': branchname,
Don Garrettf0761152017-10-19 19:38:27 -0700387 'build_config': options.build_config_name,
Don Garrettd1d90dd2017-06-13 17:35:52 -0700388 'tryjob': options.remote_trybot,
389 }
Don Garrettf15d65b2017-04-12 12:39:55 -0700390
Don Garrettacbb2392017-05-11 18:27:41 -0700391 # Does the entire build pass or fail.
Don Garrett45e77412017-06-14 16:57:55 -0700392 with metrics.Presence(METRIC_ACTIVE, metrics_fields), \
393 metrics.SuccessCounter(METRIC_COMPLETED, metrics_fields) as s_fields:
Don Garrettc4114cc2016-11-01 20:04:06 -0700394
Don Garrettacbb2392017-05-11 18:27:41 -0700395 # Preliminary set, mostly command line parsing.
Don Garrettd1d90dd2017-06-13 17:35:52 -0700396 with metrics.SuccessCounter(METRIC_INVOKED, metrics_fields):
Don Garrettfbbccec2017-09-20 14:04:48 -0700397 if options.enable_buildbot_tags:
398 logging.EnableBuildbotMarkers()
Don Garrettacbb2392017-05-11 18:27:41 -0700399 ConfigureGlobalEnvironment()
Don Garrett86881cb2017-02-15 15:41:55 -0800400
Don Garrettacbb2392017-05-11 18:27:41 -0700401 # 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 Garrettd1d90dd2017-06-13 17:35:52 -0700407 git_cache_dir=options.git_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -0800408
Don Garrettacbb2392017-05-11 18:27:41 -0700409 # Clean up the buildroot to a safe state.
410 with metrics.SecondsTimer(METRIC_CLEAN, fields=metrics_fields):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700411 CleanBuildRoot(root, repo, metrics_fields)
Don Garrettacbb2392017-05-11 18:27:41 -0700412
Don Garretta50bf492017-09-28 18:33:02 -0700413 # Get a checkout close enough to the branch that cbuildbot can handle it.
Mike Frysingerdb117c82017-12-15 18:10:33 -0500414 if options.sync:
415 with metrics.SecondsTimer(METRIC_INITIAL, fields=metrics_fields):
416 InitialCheckout(repo)
Don Garrettacbb2392017-05-11 18:27:41 -0700417
Don Garrett066e6f52017-09-28 19:14:01 -0700418 # 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 Garrettacbb2392017-05-11 18:27:41 -0700422 # Run cbuildbot inside the full ChromeOS checkout, on the specified branch.
423 with metrics.SecondsTimer(METRIC_CBUILDBOT, fields=metrics_fields):
Don Garretta50bf492017-09-28 18:33:02 -0700424 result = RunCbuildbot(buildroot, depot_tools_path, argv)
Don Garrettd1d90dd2017-06-13 17:35:52 -0700425 s_fields['success'] = (result == 0)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700426 CleanupChroot(buildroot)
Don Garrettacbb2392017-05-11 18:27:41 -0700427 return result
428
429
430def main(argv):
431 # Enable Monarch metrics gathering.
432 with ts_mon_config.SetupTsMonGlobalState('cbuildbot_launch', indirect=True):
433 return _main(argv)