blob: 6482ba28916ce9d9e6f9961cd1d5f09c31825d44 [file] [log] [blame]
Mike Frysingerd6925b52012-07-16 16:11:00 -04001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Brian Harring3fec5a82012-03-01 05:57:03 -08002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Main builder code for Chromium OS.
6
7Used by Chromium OS buildbot configuration for all Chromium OS builds including
8full and pre-flight-queue builds.
9"""
10
Mike Frysinger383367e2014-09-16 15:06:17 -040011from __future__ import print_function
12
Aviv Keshet593014d2017-07-18 17:28:25 -070013import distutils.version # pylint: disable=import-error,no-name-in-module
Brian Harring3fec5a82012-03-01 05:57:03 -080014import glob
Aviv Keshet669eb5e2014-06-23 08:53:01 -070015import json
Mike Frysinger05c5faf2015-02-04 21:46:46 -050016import mock
Mike Frysingerb0b0caa2015-11-07 01:05:18 -050017import optparse # pylint: disable=deprecated-module
Brian Harring3fec5a82012-03-01 05:57:03 -080018import os
Aviv Keshetcf9c2722014-02-25 15:15:10 -080019import pickle
Brian Harring3fec5a82012-03-01 05:57:03 -080020import sys
21
Mike Frysingere4d68c22015-02-04 21:26:24 -050022from chromite.cbuildbot import builders
Don Garrett88b8d782014-05-13 17:30:55 -070023from chromite.cbuildbot import cbuildbot_run
Don Garrett88b8d782014-05-13 17:30:55 -070024from chromite.cbuildbot import remote_try
25from chromite.cbuildbot import repository
26from chromite.cbuildbot import tee
Aviv Keshet420de512015-05-18 14:28:48 -070027from chromite.cbuildbot import topology
Don Garrett88b8d782014-05-13 17:30:55 -070028from chromite.cbuildbot import trybot_patch_pool
Don Garrett88b8d782014-05-13 17:30:55 -070029from chromite.cbuildbot.stages import completion_stages
Aviv Keshet593014d2017-07-18 17:28:25 -070030from chromite.lib.const import waterfall
Ningning Xiaf342b952017-02-15 14:13:33 -080031from chromite.lib import builder_status_lib
Aviv Keshet2982af52014-08-13 16:07:57 -070032from chromite.lib import cidb
Brian Harringc92a7012012-02-29 10:11:34 -080033from chromite.lib import cgroups
Brian Harringa184efa2012-03-04 11:51:25 -080034from chromite.lib import cleanup
Brian Harringb6cf9142012-09-01 20:43:17 -070035from chromite.lib import commandline
Ningning Xia6a718052016-12-22 10:08:15 -080036from chromite.lib import config_lib
37from chromite.lib import constants
Brian Harring1b8c4c82012-05-29 23:03:04 -070038from chromite.lib import cros_build_lib
Ralph Nathan91874ca2015-03-19 13:29:41 -070039from chromite.lib import cros_logging as logging
Ningning Xia6a718052016-12-22 10:08:15 -080040from chromite.lib import failures_lib
David James97d95872012-11-16 15:09:56 -080041from chromite.lib import git
Stefan Zagerd49d9ff2014-08-15 21:33:37 -070042from chromite.lib import gob_util
Brian Harringaf019fb2012-05-10 15:06:13 -070043from chromite.lib import osutils
David James6450a0a2012-12-04 07:59:53 -080044from chromite.lib import parallel
Don Garrettb4318362014-10-03 15:49:36 -070045from chromite.lib import retry_stats
Brian Harring3fec5a82012-03-01 05:57:03 -080046from chromite.lib import sudo
David James3432acd2013-11-27 10:02:18 -080047from chromite.lib import timeout_util
Drew Davenportd7c22c12017-06-07 16:16:54 -060048from chromite.lib import tree_status
Paul Hobbsfcf10342015-12-29 15:52:31 -080049from chromite.lib import ts_mon_config
Brian Harring3fec5a82012-03-01 05:57:03 -080050
Ryan Cuiadd49122012-03-21 22:19:58 -070051
Brian Harring3fec5a82012-03-01 05:57:03 -080052_DEFAULT_LOG_DIR = 'cbuildbot_logs'
53_BUILDBOT_LOG_FILE = 'cbuildbot.log'
54_DEFAULT_EXT_BUILDROOT = 'trybot'
55_DEFAULT_INT_BUILDROOT = 'trybot-internal'
Brian Harring351ce442012-03-09 16:38:14 -080056_BUILDBOT_REQUIRED_BINARIES = ('pbzip2',)
Ryan Cui1c13a252012-10-16 15:00:16 -070057_API_VERSION_ATTR = 'api_version'
Brian Harring3fec5a82012-03-01 05:57:03 -080058
59
Don Garrett4af20982015-05-29 19:02:23 -070060def _PrintValidConfigs(site_config, display_all=False):
Brian Harring3fec5a82012-03-01 05:57:03 -080061 """Print a list of valid buildbot configs.
62
Mike Frysinger02e1e072013-11-10 22:11:34 -050063 Args:
Don Garrett4af20982015-05-29 19:02:23 -070064 site_config: config_lib.SiteConfig containing all config info.
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070065 display_all: Print all configs. Otherwise, prints only configs with
66 trybot_list=True.
Brian Harring3fec5a82012-03-01 05:57:03 -080067 """
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070068 def _GetSortKey(config_name):
Don Garrett4af20982015-05-29 19:02:23 -070069 config_dict = site_config[config_name]
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070070 return (not config_dict['trybot_list'], config_dict['description'],
71 config_name)
72
Brian Harring3fec5a82012-03-01 05:57:03 -080073 COLUMN_WIDTH = 45
Mike Frysinger13b08882015-05-28 04:36:10 -040074 if not display_all:
75 print('Note: This is the common list; for all configs, use --all.')
Mike Frysinger383367e2014-09-16 15:06:17 -040076 print('config'.ljust(COLUMN_WIDTH), 'description')
77 print('------'.ljust(COLUMN_WIDTH), '-----------')
Don Garrett4af20982015-05-29 19:02:23 -070078 config_names = site_config.keys()
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070079 config_names.sort(key=_GetSortKey)
Brian Harring3fec5a82012-03-01 05:57:03 -080080 for name in config_names:
Don Garrett4af20982015-05-29 19:02:23 -070081 if display_all or site_config[name]['trybot_list']:
82 desc = site_config[name].get('description')
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070083 desc = desc if desc else ''
Mike Frysinger383367e2014-09-16 15:06:17 -040084 print(name.ljust(COLUMN_WIDTH), desc)
Brian Harring3fec5a82012-03-01 05:57:03 -080085
86
Brian Harring3fec5a82012-03-01 05:57:03 -080087def _ConfirmBuildRoot(buildroot):
88 """Confirm with user the inferred buildroot, and mark it as confirmed."""
Ralph Nathan446aee92015-03-23 14:44:56 -070089 logging.warning('Using default directory %s as buildroot', buildroot)
Brian Harring521e7242012-11-01 16:57:42 -070090 if not cros_build_lib.BooleanPrompt(default=False):
91 print('Please specify a different buildroot via the --buildroot option.')
Brian Harring3fec5a82012-03-01 05:57:03 -080092 sys.exit(0)
93
94 if not os.path.exists(buildroot):
95 os.mkdir(buildroot)
96
97 repository.CreateTrybotMarker(buildroot)
98
99
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700100def _ConfirmRemoteBuildbotRun():
101 """Confirm user wants to run with --buildbot --remote."""
Ralph Nathan446aee92015-03-23 14:44:56 -0700102 logging.warning(
David Jameseecba232014-06-11 11:35:11 -0700103 'You are about to launch a PRODUCTION job! This is *NOT* a '
104 'trybot run! Are you sure?')
Brian Harring521e7242012-11-01 16:57:42 -0700105 if not cros_build_lib.BooleanPrompt(default=False):
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700106 print('Please specify --pass-through="--debug".')
107 sys.exit(0)
108
109
Ryan Cui5ba7e152012-05-10 14:36:52 -0700110def _DetermineDefaultBuildRoot(sourceroot, internal_build):
Brian Harring3fec5a82012-03-01 05:57:03 -0800111 """Default buildroot to be under the directory that contains current checkout.
112
Mike Frysinger02e1e072013-11-10 22:11:34 -0500113 Args:
Brian Harring3fec5a82012-03-01 05:57:03 -0800114 internal_build: Whether the build is an internal build
Ryan Cui5ba7e152012-05-10 14:36:52 -0700115 sourceroot: Use specified sourceroot.
Brian Harring3fec5a82012-03-01 05:57:03 -0800116 """
Ryan Cui5ba7e152012-05-10 14:36:52 -0700117 if not repository.IsARepoRoot(sourceroot):
Brian Harring1b8c4c82012-05-29 23:03:04 -0700118 cros_build_lib.Die(
119 'Could not find root of local checkout at %s. Please specify '
120 'using the --sourceroot option.' % sourceroot)
Brian Harring3fec5a82012-03-01 05:57:03 -0800121
122 # Place trybot buildroot under the directory containing current checkout.
Ryan Cui5ba7e152012-05-10 14:36:52 -0700123 top_level = os.path.dirname(os.path.realpath(sourceroot))
Brian Harring3fec5a82012-03-01 05:57:03 -0800124 if internal_build:
125 buildroot = os.path.join(top_level, _DEFAULT_INT_BUILDROOT)
126 else:
127 buildroot = os.path.join(top_level, _DEFAULT_EXT_BUILDROOT)
128
129 return buildroot
130
131
132def _BackupPreviousLog(log_file, backup_limit=25):
133 """Rename previous log.
134
135 Args:
136 log_file: The absolute path to the previous log.
Aviv Keshet9e4236b2013-12-13 13:07:50 -0800137 backup_limit: Maximum number of old logs to keep.
Brian Harring3fec5a82012-03-01 05:57:03 -0800138 """
139 if os.path.exists(log_file):
140 old_logs = sorted(glob.glob(log_file + '.*'),
141 key=distutils.version.LooseVersion)
142
143 if len(old_logs) >= backup_limit:
144 os.remove(old_logs[0])
145
146 last = 0
147 if old_logs:
148 last = int(old_logs.pop().rpartition('.')[2])
149
150 os.rename(log_file, log_file + '.' + str(last + 1))
151
Ryan Cui5616a512012-08-17 13:39:36 -0700152
Gaurav Shah298aa372014-01-31 09:27:24 -0800153def _IsDistributedBuilder(options, chrome_rev, build_config):
154 """Determines whether the builder should be a DistributedBuilder.
155
156 Args:
157 options: options passed on the commandline.
158 chrome_rev: Chrome revision to build.
159 build_config: Builder configuration dictionary.
160
161 Returns:
162 True if the builder should be a distributed_builder
163 """
Don Garrett0bc85672015-07-23 19:46:00 +0000164 if build_config['pre_cq']:
Gaurav Shah298aa372014-01-31 09:27:24 -0800165 return True
166 elif not options.buildbot:
167 return False
168 elif chrome_rev in (constants.CHROME_REV_TOT,
169 constants.CHROME_REV_LOCAL,
170 constants.CHROME_REV_SPEC):
171 # We don't do distributed logic to TOT Chrome PFQ's, nor local
172 # chrome roots (e.g. chrome try bots)
173 # TODO(davidjames): Update any builders that rely on this logic to use
174 # manifest_version=False instead.
175 return False
176 elif build_config['manifest_version']:
177 return True
178
179 return False
180
181
Don Garretta52a5b02015-06-02 14:52:57 -0700182def _RunBuildStagesWrapper(options, site_config, build_config):
Brian Harring3fec5a82012-03-01 05:57:03 -0800183 """Helper function that wraps RunBuildStages()."""
Ralph Nathan03047282015-03-23 11:09:32 -0700184 logging.info('cbuildbot was executed with args %s' %
185 cros_build_lib.CmdToStr(sys.argv))
Brian Harring3fec5a82012-03-01 05:57:03 -0800186
David Jamesa0a664e2013-02-13 09:52:01 -0800187 chrome_rev = build_config['chrome_rev']
188 if options.chrome_rev:
189 chrome_rev = options.chrome_rev
190 if chrome_rev == constants.CHROME_REV_TOT:
Stefan Zagerd49d9ff2014-08-15 21:33:37 -0700191 options.chrome_version = gob_util.GetTipOfTrunkRevision(
192 constants.CHROMIUM_GOB_URL)
David Jamesa0a664e2013-02-13 09:52:01 -0800193 options.chrome_rev = constants.CHROME_REV_SPEC
194
David James4a404a52013-02-19 13:07:59 -0800195 # If it's likely we'll need to build Chrome, fetch the source.
Mike Frysingerf4d9ab82013-03-05 19:38:05 -0500196 if build_config['sync_chrome'] is None:
David Jameseecba232014-06-11 11:35:11 -0700197 options.managed_chrome = (
198 chrome_rev != constants.CHROME_REV_LOCAL and
Mike Frysingerf4d9ab82013-03-05 19:38:05 -0500199 (not build_config['usepkg_build_packages'] or chrome_rev or
David James3cce4642013-05-10 17:50:23 -0700200 build_config['profile'] or options.rietveld_patches))
Mike Frysingerf4d9ab82013-03-05 19:38:05 -0500201 else:
202 options.managed_chrome = build_config['sync_chrome']
David James2333c182013-02-13 16:16:15 -0800203
204 if options.managed_chrome:
205 # Tell Chrome to fetch the source locally.
Matt Tennant628ffdd2013-11-27 14:44:39 -0800206 internal = constants.USE_CHROME_INTERNAL in build_config['useflags']
David James2333c182013-02-13 16:16:15 -0800207 chrome_src = 'chrome-src-internal' if internal else 'chrome-src'
YH Linb1ea83c2016-10-13 15:47:09 -0700208 target_name = 'target'
209 if options.branch:
210 # Tie the cache per branch
211 target_name = 'target-%s' % options.branch
212 options.chrome_root = os.path.join(options.cache_dir, 'distfiles',
213 target_name, chrome_src)
214 # Create directory if in need
215 osutils.SafeMakedirsNonRoot(options.chrome_root)
David James9e27e662013-02-14 13:42:43 -0800216 elif options.rietveld_patches:
David James4a404a52013-02-19 13:07:59 -0800217 cros_build_lib.Die('This builder does not support Rietveld patches.')
David James2333c182013-02-13 16:16:15 -0800218
Aviv Keshet669eb5e2014-06-23 08:53:01 -0700219 metadata_dump_dict = {}
220 if options.metadata_dump:
221 with open(options.metadata_dump, 'r') as metadata_file:
222 metadata_dump_dict = json.loads(metadata_file.read())
223
Matt Tennant95a42ad2013-12-27 15:38:36 -0800224 # We are done munging options values, so freeze options object now to avoid
225 # further abuse of it.
226 # TODO(mtennant): one by one identify each options value override and see if
227 # it can be handled another way. Try to push this freeze closer and closer
228 # to the start of the script (e.g. in or after _PostParseCheck).
229 options.Freeze()
230
Matt Tennant0940c382014-01-21 20:43:55 -0800231 with parallel.Manager() as manager:
Don Garretta52a5b02015-06-02 14:52:57 -0700232 builder_run = cbuildbot_run.BuilderRun(
233 options, site_config, build_config, manager)
Aviv Keshet669eb5e2014-06-23 08:53:01 -0700234 if metadata_dump_dict:
235 builder_run.attrs.metadata.UpdateWithDict(metadata_dump_dict)
Mike Frysingere4d68c22015-02-04 21:26:24 -0500236
Mike Frysinger05c5faf2015-02-04 21:46:46 -0500237 if builder_run.config.builder_class_name is None:
Don Garrett56e6ed32015-06-23 16:52:20 -0700238 # TODO: This should get relocated to chromeos_config.
Mike Frysingere4d68c22015-02-04 21:26:24 -0500239 if _IsDistributedBuilder(options, chrome_rev, build_config):
Mike Frysinger05c5faf2015-02-04 21:46:46 -0500240 builder_cls_name = 'simple_builders.DistributedBuilder'
Mike Frysingere4d68c22015-02-04 21:26:24 -0500241 else:
Mike Frysinger05c5faf2015-02-04 21:46:46 -0500242 builder_cls_name = 'simple_builders.SimpleBuilder'
243 builder_cls = builders.GetBuilderClass(builder_cls_name)
244 builder = builder_cls(builder_run)
Matt Tennant0940c382014-01-21 20:43:55 -0800245 else:
Mike Frysinger05c5faf2015-02-04 21:46:46 -0500246 builder = builders.Builder(builder_run)
Mike Frysingere4d68c22015-02-04 21:26:24 -0500247
Matt Tennant0940c382014-01-21 20:43:55 -0800248 if not builder.Run():
249 sys.exit(1)
Brian Harring3fec5a82012-03-01 05:57:03 -0800250
251
252# Parser related functions
Ryan Cui5ba7e152012-05-10 14:36:52 -0700253def _CheckLocalPatches(sourceroot, local_patches):
Brian Harring3fec5a82012-03-01 05:57:03 -0800254 """Do an early quick check of the passed-in patches.
255
256 If the branch of a project is not specified we append the current branch the
257 project is on.
Ryan Cui5ba7e152012-05-10 14:36:52 -0700258
David Jamese3b06062013-11-09 18:52:02 -0800259 TODO(davidjames): The project:branch format isn't unique, so this means that
260 we can't differentiate what directory the user intended to apply patches to.
261 We should references by directory instead.
262
Ryan Cui5ba7e152012-05-10 14:36:52 -0700263 Args:
264 sourceroot: The checkout where patches are coming from.
Aviv Keshet9e4236b2013-12-13 13:07:50 -0800265 local_patches: List of patches to check in project:branch format.
David Jamese3b06062013-11-09 18:52:02 -0800266
267 Returns:
268 A list of patches that have been verified, in project:branch format.
Brian Harring3fec5a82012-03-01 05:57:03 -0800269 """
Ryan Cuicedd8a52012-03-22 02:28:35 -0700270 verified_patches = []
David James97d95872012-11-16 15:09:56 -0800271 manifest = git.ManifestCheckout.Cached(sourceroot)
Ryan Cuicedd8a52012-03-22 02:28:35 -0700272 for patch in local_patches:
David Jamese3b06062013-11-09 18:52:02 -0800273 project, _, branch = patch.partition(':')
274
David Jamese6301e02016-11-03 17:14:03 -0700275 checkouts = manifest.FindCheckouts(project)
David Jamese3b06062013-11-09 18:52:02 -0800276 if not checkouts:
277 cros_build_lib.Die('Project %s does not exist.' % (project,))
278 if len(checkouts) > 1:
Brian Harring1b8c4c82012-05-29 23:03:04 -0700279 cros_build_lib.Die(
David Jamese3b06062013-11-09 18:52:02 -0800280 'We do not yet support local patching for projects that are checked '
281 'out to multiple directories. Try uploading your patch to gerrit '
282 'and referencing it via the -g option instead.'
283 )
Brian Harring3fec5a82012-03-01 05:57:03 -0800284
David Jamese3b06062013-11-09 18:52:02 -0800285 ok = False
286 for checkout in checkouts:
287 project_dir = checkout.GetPath(absolute=True)
Brian Harring3fec5a82012-03-01 05:57:03 -0800288
David Jamese3b06062013-11-09 18:52:02 -0800289 # If no branch was specified, we use the project's current branch.
Brian Harring3fec5a82012-03-01 05:57:03 -0800290 if not branch:
David Jamese3b06062013-11-09 18:52:02 -0800291 local_branch = git.GetCurrentBranch(project_dir)
292 else:
293 local_branch = branch
Brian Harring3fec5a82012-03-01 05:57:03 -0800294
David Jamesf1a07612014-04-28 17:48:52 -0700295 if local_branch and git.DoesCommitExistInRepo(project_dir, local_branch):
David Jamese3b06062013-11-09 18:52:02 -0800296 verified_patches.append('%s:%s' % (project, local_branch))
297 ok = True
298
299 if not ok:
300 if branch:
301 cros_build_lib.Die('Project %s does not have branch %s'
David Jameseecba232014-06-11 11:35:11 -0700302 % (project, branch))
David Jamese3b06062013-11-09 18:52:02 -0800303 else:
304 cros_build_lib.Die('Project %s is not on a branch!' % (project,))
Brian Harring3fec5a82012-03-01 05:57:03 -0800305
Ryan Cuicedd8a52012-03-22 02:28:35 -0700306 return verified_patches
Brian Harring3fec5a82012-03-01 05:57:03 -0800307
308
Brian Harring3fec5a82012-03-01 05:57:03 -0800309def _CheckChromeVersionOption(_option, _opt_str, value, parser):
310 """Upgrade other options based on chrome_version being passed."""
311 value = value.strip()
312
313 if parser.values.chrome_rev is None and value:
314 parser.values.chrome_rev = constants.CHROME_REV_SPEC
315
316 parser.values.chrome_version = value
317
318
319def _CheckChromeRootOption(_option, _opt_str, value, parser):
320 """Validate and convert chrome_root to full-path form."""
Brian Harring3fec5a82012-03-01 05:57:03 -0800321 if parser.values.chrome_rev is None:
322 parser.values.chrome_rev = constants.CHROME_REV_LOCAL
323
Ryan Cui5ba7e152012-05-10 14:36:52 -0700324 parser.values.chrome_root = value
Brian Harring3fec5a82012-03-01 05:57:03 -0800325
326
David Jamesac8c2a72013-02-13 18:44:33 -0800327def FindCacheDir(_parser, _options):
Brian Harringae0a5322012-09-15 01:46:51 -0700328 return None
329
330
Ryan Cui5ba7e152012-05-10 14:36:52 -0700331class CustomGroup(optparse.OptionGroup):
Aviv Keshet9e4236b2013-12-13 13:07:50 -0800332 """Custom option group which supports arguments passed-through to trybot."""
David Jameseecba232014-06-11 11:35:11 -0700333
Ryan Cui5ba7e152012-05-10 14:36:52 -0700334 def add_remote_option(self, *args, **kwargs):
335 """For arguments that are passed-through to remote trybot."""
336 return optparse.OptionGroup.add_option(self, *args,
337 remote_pass_through=True,
338 **kwargs)
339
340
Ryan Cui1c13a252012-10-16 15:00:16 -0700341class CustomOption(commandline.FilteringOption):
342 """Subclass FilteringOption class to implement pass-through and api."""
Ryan Cui5ba7e152012-05-10 14:36:52 -0700343
Ryan Cui5ba7e152012-05-10 14:36:52 -0700344 def __init__(self, *args, **kwargs):
345 # The remote_pass_through argument specifies whether we should directly
346 # pass the argument (with its value) onto the remote trybot.
347 self.pass_through = kwargs.pop('remote_pass_through', False)
Ryan Cui1c13a252012-10-16 15:00:16 -0700348 self.api_version = int(kwargs.pop('api', '0'))
349 commandline.FilteringOption.__init__(self, *args, **kwargs)
Ryan Cui5ba7e152012-05-10 14:36:52 -0700350
Ryan Cui5ba7e152012-05-10 14:36:52 -0700351
Ryan Cui1c13a252012-10-16 15:00:16 -0700352class CustomParser(commandline.FilteringParser):
Aviv Keshet9e4236b2013-12-13 13:07:50 -0800353 """Custom option parser which supports arguments passed-trhough to trybot"""
Matt Tennante8179042013-10-01 15:47:32 -0700354
Brian Harringb6cf9142012-09-01 20:43:17 -0700355 DEFAULT_OPTION_CLASS = CustomOption
356
357 def add_remote_option(self, *args, **kwargs):
358 """For arguments that are passed-through to remote trybot."""
Ryan Cui1c13a252012-10-16 15:00:16 -0700359 return self.add_option(*args, remote_pass_through=True, **kwargs)
Brian Harringb6cf9142012-09-01 20:43:17 -0700360
361
Don Garrett86881cb2017-02-15 15:41:55 -0800362def CreateParser():
363 """Expose _CreateParser publicly."""
364 # Name _CreateParser is needed for commandline library.
365 return _CreateParser()
366
367
Brian Harring3fec5a82012-03-01 05:57:03 -0800368def _CreateParser():
Ryan Cui16d9e1f2012-05-11 10:50:18 -0700369 """Generate and return the parser with all the options."""
Brian Harring3fec5a82012-03-01 05:57:03 -0800370 # Parse options
David Jameseecba232014-06-11 11:35:11 -0700371 usage = 'usage: %prog [options] buildbot_config [buildbot_config ...]'
Brian Harringae0a5322012-09-15 01:46:51 -0700372 parser = CustomParser(usage=usage, caching=FindCacheDir)
Brian Harring3fec5a82012-03-01 05:57:03 -0800373
374 # Main options
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400375 parser.add_option('-l', '--list', action='store_true', dest='list',
376 default=False,
377 help='List the suggested trybot configs to use (see --all)')
Brian Harring3fec5a82012-03-01 05:57:03 -0800378 parser.add_option('-a', '--all', action='store_true', dest='print_all',
379 default=False,
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400380 help='List all of the buildbot configs available w/--list')
381
Hidehiko Abe863d7882017-03-09 22:27:28 +0900382 parser.add_option('--local', action='store_true', default=False,
383 help='Specifies that this tryjob should be run locally. '
384 'Implies --debug.')
385 parser.add_option('--remote', action='store_true', default=False,
Ryan Cui88b901c2013-06-21 11:35:30 -0700386 help='Specifies that this tryjob should be run remotely.')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400387
Ryan Cuie1e4e662012-05-21 16:39:46 -0700388 parser.add_remote_option('-b', '--branch',
Hidehiko Abe863d7882017-03-09 22:27:28 +0900389 help='The manifest branch to test. The branch to '
390 'check the buildroot out to.')
391 parser.add_option('-r', '--buildroot', type='path', dest='buildroot',
392 help='Root directory where source is checked out to, and '
393 'where the build occurs. For external build configs, '
394 "defaults to 'trybot' directory at top level of your "
395 'repo-managed checkout.')
396 parser.add_option('--bootstrap-dir', type='path',
Prathmesh Prabhu867c1172015-06-02 17:57:59 -0700397 help='Bootstrapping cbuildbot may involve checking out '
398 'multiple copies of chromite. All these checkouts '
399 'will be contained in the directory specified here. '
400 'Default:%s' % osutils.GetGlobalTempDir())
Hidehiko Abe863d7882017-03-09 22:27:28 +0900401 parser.add_remote_option('--android_rev', type='choice',
David Riley486b2612016-02-22 19:59:26 -0800402 choices=constants.VALID_ANDROID_REVISIONS,
403 help=('Revision of Android to use, of type [%s]'
404 % '|'.join(constants.VALID_ANDROID_REVISIONS)))
Hidehiko Abe863d7882017-03-09 22:27:28 +0900405 parser.add_remote_option('--chrome_rev', type='choice',
David Riley486b2612016-02-22 19:59:26 -0800406 choices=constants.VALID_CHROME_REVISIONS,
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700407 help=('Revision of Chrome to use, of type [%s]'
408 % '|'.join(constants.VALID_CHROME_REVISIONS)))
Hidehiko Abe863d7882017-03-09 22:27:28 +0900409 parser.add_remote_option('--profile',
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700410 help='Name of profile to sub-specify board variant.')
Don Garrettb85658c2015-06-30 19:07:22 -0700411 parser.add_option('-c', '--config_repo',
Don Garrett58e85e02017-06-21 15:24:29 -0700412 help='Deprecated option. Do not use!')
Hidehiko Abe95d7cf62017-03-07 23:34:30 +0900413 # TODO(crbug.com/279618): Running GOMA is under development. Following
414 # flags are added for development purpose due to repository dependency,
415 # but not officially supported yet.
416 parser.add_option('--goma_dir', type='path',
417 api=constants.REEXEC_API_GOMA,
418 help='Specify a directory containing goma. When this is '
419 'set, GOMA is used to build Chrome.')
420 parser.add_option('--goma_client_json', type='path',
421 api=constants.REEXEC_API_GOMA,
422 help='Specify a service-account-goma-client.json path. '
423 'The file is needed on bots to run GOMA.')
Brian Harring3fec5a82012-03-01 05:57:03 -0800424
Ryan Cuif4f84be2012-07-09 18:50:41 -0700425 #
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400426 # Patch selection options.
427 #
428
429 group = CustomGroup(
430 parser,
431 'Patch Options')
432
Mike Frysingerdad205d2017-08-11 16:00:14 -0400433 group.add_remote_option('-g', '--gerrit-patches', action='split_extend',
Hidehiko Abe863d7882017-03-09 22:27:28 +0900434 type='string', default=[],
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400435 metavar="'Id1 *int_Id2...IdN'",
Hidehiko Abe863d7882017-03-09 22:27:28 +0900436 help='Space-separated list of short-form Gerrit '
437 "Change-Id's or change numbers to patch. "
438 "Please prepend '*' to internal Change-Id's")
Mike Frysingerdad205d2017-08-11 16:00:14 -0400439 group.add_remote_option('-G', '--rietveld-patches', action='split_extend',
Hidehiko Abe863d7882017-03-09 22:27:28 +0900440 type='string', default=[],
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400441 metavar="'id1[:subdir1]...idN[:subdirN]'",
Hidehiko Abe863d7882017-03-09 22:27:28 +0900442 help='Space-separated list of short-form Rietveld '
443 'issue numbers to patch. If no subdir is '
444 'specified, the src directory is used.')
Mike Frysingerdad205d2017-08-11 16:00:14 -0400445 group.add_option('-p', '--local-patches', action='split_extend', default=[],
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400446 metavar="'<project1>[:<branch1>]...<projectN>[:<branchN>]'",
Hidehiko Abe863d7882017-03-09 22:27:28 +0900447 help='Space-separated list of project branches with '
448 'patches to apply. Projects are specified by name. '
449 'If no branch is specified the current branch of the '
450 'project will be used.')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400451
Mike Frysinger68893242017-08-11 14:16:39 -0400452 parser.add_argument_group(group)
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400453
454 #
455 # Remote trybot options.
456 #
457
458 group = CustomGroup(
459 parser,
460 'Remote Trybot Options (--remote)')
461
Don Garrett6b0cf682017-03-29 18:21:04 -0700462 # TODO(dgarrett): Remove after a reasonable delay.
463 group.add_option('--use-buildbucket', action='store_true',
464 dest='deprecated_use_buildbucket',
465 help='Deprecated option. Ignored.')
466
467 group.add_option('--do-not-use-buildbucket', action='store_false',
468 dest='use_buildbucket', default=True,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900469 help='Use buildbucket instead of git to request'
470 'the tryjob(s).')
Paul Hobbs5cd050d2015-06-30 16:52:28 -0700471
Hidehiko Abe863d7882017-03-09 22:27:28 +0900472 group.add_remote_option('--hwtest', action='store_true', default=False,
David Jameseecba232014-06-11 11:35:11 -0700473 help='Run the HWTest stage (tests on real hardware)')
Hidehiko Abe863d7882017-03-09 22:27:28 +0900474 group.add_option('--remote-description',
475 help='Attach an optional description to a --remote run '
476 'to make it easier to identify the results when it '
477 'finishes')
Mike Frysingerdad205d2017-08-11 16:00:14 -0400478 group.add_option('--slaves', action='split_extend', default=[],
Hidehiko Abe863d7882017-03-09 22:27:28 +0900479 help='Specify specific remote tryslaves to run on (e.g. '
480 'build149-m2); if the bot is busy, it will be queued')
Mike Frysingerdad205d2017-08-11 16:00:14 -0400481 group.add_remote_option('--channel', action='split_extend', dest='channels',
Don Garrett4bb21682014-03-03 16:16:23 -0800482 default=[],
Hidehiko Abe863d7882017-03-09 22:27:28 +0900483 help='Specify a channel for a payloads trybot. Can '
484 'be specified multiple times. No valid for '
485 'non-payloads configs.')
486 group.add_option('--test-tryjob', action='store_true', default=False,
487 help='Submit a tryjob to the test repository. Will not '
488 'show up on the production trybot waterfall.')
Dominik Behradc592c2017-01-31 16:06:26 -0800489 group.add_option('--committer-email', type='string',
490 help='Override default git committer email.')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400491
Mike Frysinger68893242017-08-11 14:16:39 -0400492 parser.add_argument_group(group)
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400493
494 #
Ryan Cui88b901c2013-06-21 11:35:30 -0700495 # Branch creation options.
496 #
497
498 group = CustomGroup(
499 parser,
500 'Branch Creation Options (used with branch-util)')
501
502 group.add_remote_option('--branch-name',
503 help='The branch to create or delete.')
Hidehiko Abe863d7882017-03-09 22:27:28 +0900504 group.add_remote_option('--delete-branch', action='store_true', default=False,
Ryan Cui88b901c2013-06-21 11:35:30 -0700505 help='Delete the branch specified in --branch-name.')
Hidehiko Abe863d7882017-03-09 22:27:28 +0900506 group.add_remote_option('--rename-to',
Ryan Cui88b901c2013-06-21 11:35:30 -0700507 help='Rename a branch to the specified name.')
Hidehiko Abe863d7882017-03-09 22:27:28 +0900508 group.add_remote_option('--force-create', action='store_true', default=False,
Ryan Cui88b901c2013-06-21 11:35:30 -0700509 help='Overwrites an existing branch.')
Hidehiko Abe863d7882017-03-09 22:27:28 +0900510 group.add_remote_option('--skip-remote-push', action='store_true',
511 default=False,
Prathmesh Prabhue5f4d472015-05-07 16:52:10 -0700512 help='Do not actually push to remote git repos. '
513 'Used for end-to-end testing branching.')
Ryan Cui88b901c2013-06-21 11:35:30 -0700514
Mike Frysinger68893242017-08-11 14:16:39 -0400515 parser.add_argument_group(group)
Ryan Cui88b901c2013-06-21 11:35:30 -0700516
517 #
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400518 # Advanced options.
Ryan Cuif4f84be2012-07-09 18:50:41 -0700519 #
520
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700521 group = CustomGroup(
Brian Harring3fec5a82012-03-01 05:57:03 -0800522 parser,
523 'Advanced Options',
524 'Caution: use these options at your own risk.')
525
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400526 group.add_remote_option('--bootstrap-args', action='append', default=[],
Hidehiko Abe863d7882017-03-09 22:27:28 +0900527 help='Args passed directly to the bootstrap re-exec '
528 'to skip verification by the bootstrap code')
529 group.add_remote_option('--buildbot', action='store_true', dest='buildbot',
Bernie Thompson63f30062016-12-21 15:24:25 -0800530 default=False,
531 help='This is running on a buildbot. '
532 'This can be used to make a build operate '
533 'like an official builder, e.g. generate '
534 'new version numbers and archive official '
535 'artifacts and such. This should only be '
536 'used if you are confident in what you are '
537 'doing, as it will make automated commits.')
Don Garrett07d46262017-04-13 12:06:44 -0700538 parser.add_remote_option('--repo-cache', type='path', dest='_repo_cache',
539 help='Present for backwards compatibility, ignored.')
Prathmesh Prabhu51d774f2015-07-17 15:11:49 -0700540 group.add_remote_option('--no-buildbot-tags', action='store_false',
541 dest='enable_buildbot_tags', default=True,
542 help='Suppress buildbot specific tags from log '
543 'output. This is used to hide recursive '
544 'cbuilbot runs on the waterfall.')
Hidehiko Abe863d7882017-03-09 22:27:28 +0900545 group.add_remote_option('--buildnumber', type='int', default=0,
546 help='build number')
547 group.add_option('--chrome_root', action='callback', type='path',
548 callback=_CheckChromeRootOption,
549 help='Local checkout of Chrome to use.')
550 group.add_remote_option('--chrome_version', action='callback', type='string',
551 dest='chrome_version',
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700552 callback=_CheckChromeVersionOption,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900553 help='Used with SPEC logic to force a particular '
554 'git revision of chrome rather than the '
555 'latest.')
556 group.add_remote_option('--clobber', action='store_true', default=False,
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700557 help='Clears an old checkout before syncing')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400558 group.add_remote_option('--latest-toolchain', action='store_true',
559 default=False,
560 help='Use the latest toolchain.')
Ryan Cui5ba7e152012-05-10 14:36:52 -0700561 parser.add_option('--log_dir', dest='log_dir', type='path',
Hidehiko Abe863d7882017-03-09 22:27:28 +0900562 help='Directory where logs are stored.')
563 group.add_remote_option('--maxarchives', type='int',
564 dest='max_archive_builds', default=3,
David Jameseecba232014-06-11 11:35:11 -0700565 help='Change the local saved build count limit.')
Ryan Cuibbd3d4b2012-08-17 12:20:37 -0700566 parser.add_remote_option('--manifest-repo-url',
Hidehiko Abe863d7882017-03-09 22:27:28 +0900567 help='Overrides the default manifest repo url.')
David James565bc9a2013-04-08 14:54:45 -0700568 group.add_remote_option('--compilecheck', action='store_true', default=False,
569 help='Only verify compilation and unit tests.')
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700570 group.add_remote_option('--noarchive', action='store_false', dest='archive',
571 default=True, help="Don't run archive stage.")
Ryan Cuif7f24692012-05-18 16:35:33 -0700572 group.add_remote_option('--nobootstrap', action='store_false',
573 dest='bootstrap', default=True,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900574 help="Don't checkout and run from a standalone "
575 'chromite repo.')
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700576 group.add_remote_option('--nobuild', action='store_false', dest='build',
577 default=True,
578 help="Don't actually build (for cbuildbot dev)")
579 group.add_remote_option('--noclean', action='store_false', dest='clean',
580 default=True, help="Don't clean the buildroot")
Ryan Cuif7f24692012-05-18 16:35:33 -0700581 group.add_remote_option('--nocgroups', action='store_false', dest='cgroups',
582 default=True,
583 help='Disable cbuildbots usage of cgroups.')
Ryan Cui3ea98e02013-08-07 16:01:48 -0700584 group.add_remote_option('--nochromesdk', action='store_false',
585 dest='chrome_sdk', default=True,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900586 help="Don't run the ChromeSDK stage which builds "
587 'Chrome outside of the chroot.')
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700588 group.add_remote_option('--noprebuilts', action='store_false',
589 dest='prebuilts', default=True,
590 help="Don't upload prebuilts.")
Ryan Cui88b901c2013-06-21 11:35:30 -0700591 group.add_remote_option('--nopatch', action='store_false',
592 dest='postsync_patch', default=True,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900593 help="Don't run PatchChanges stage. This does not "
594 'disable patching in of chromite patches '
595 'during BootstrapStage.')
Don Garrett82c0ae82014-02-03 18:25:11 -0800596 group.add_remote_option('--nopaygen', action='store_false',
597 dest='paygen', default=True,
598 help="Don't generate payloads.")
Ryan Cui88b901c2013-06-21 11:35:30 -0700599 group.add_remote_option('--noreexec', action='store_false',
600 dest='postsync_reexec', default=True,
601 help="Don't reexec into the buildroot after syncing.")
Hidehiko Abe863d7882017-03-09 22:27:28 +0900602 group.add_remote_option('--nosdk', action='store_true', default=False,
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400603 help='Re-create the SDK from scratch.')
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700604 group.add_remote_option('--nosync', action='store_false', dest='sync',
605 default=True, help="Don't sync before building.")
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700606 group.add_remote_option('--notests', action='store_false', dest='tests',
607 default=True,
xixuan7774ba82017-06-22 16:04:00 -0700608 help='Override values from buildconfig, run no '
609 'tests, and build no autotest and artifacts.')
610 group.add_remote_option('--novmtests', action='store_false', dest='vmtests',
611 default=True,
612 help='Override values from buildconfig, run no '
613 'vmtests.')
Nam T. Nguyenc93f1342014-07-11 14:40:54 -0700614 group.add_remote_option('--noimagetests', action='store_false',
615 dest='image_test', default=True,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900616 help='Override values from buildconfig and run no '
617 'image tests.')
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700618 group.add_remote_option('--nouprev', action='store_false', dest='uprev',
619 default=True,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900620 help='Override values from buildconfig and never '
621 'uprev.')
622 group.add_option('--reference-repo',
623 help='Reuse git data stored in an existing repo '
624 'checkout. This can drastically reduce the network '
625 'time spent setting up the trybot checkout. By '
626 "default, if this option isn't given but cbuildbot "
627 'is invoked from a repo checkout, cbuildbot will '
628 'use the repo root.')
Ryan Cuicedd8a52012-03-22 02:28:35 -0700629 group.add_option('--resume', action='store_true', default=False,
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700630 help='Skip stages already successfully completed.')
Hidehiko Abe863d7882017-03-09 22:27:28 +0900631 group.add_remote_option('--timeout', type='int', default=0,
632 help='Specify the maximum amount of time this job '
633 'can run for, at which point the build will be '
634 'aborted. If set to zero, then there is no '
635 'timeout.')
636 group.add_remote_option('--version', dest='force_version',
637 help='Used with manifest logic. Forces use of this '
638 'version rather than create or get latest. '
639 'Examples: 4815.0.0-rc1, 4815.1.2')
640 group.add_remote_option('--git-cache-dir', type='path',
Don Garrettbb79be92016-09-27 11:14:07 -0700641 api=constants.REEXEC_API_GIT_CACHE_DIR,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900642 help='Specify the cache directory to store the '
643 'project caches populated by the git-cache '
644 'tool. Bootstrap the projects based on the git '
645 'cache files instead of fetching them directly '
646 'from the GoB servers.')
Ningning Xia9ab36022017-07-28 16:49:25 -0700647 group.add_remote_option('--sanity-check-build', action='store_true',
648 default=False, dest='sanity_check_build',
649 api=constants.REEXEC_API_SANITY_CHECK_BUILD,
650 help='Run the build as a sanity check build.')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400651
Mike Frysinger68893242017-08-11 14:16:39 -0400652 parser.add_argument_group(group)
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400653
654 #
655 # Internal options.
656 #
657
658 group = CustomGroup(
659 parser,
Mike Frysinger34db8692013-11-11 14:54:08 -0500660 'Internal Chromium OS Build Team Options',
661 'Caution: these are for meant for the Chromium OS build team only')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400662
663 group.add_remote_option('--archive-base', type='gs_path',
Hidehiko Abe863d7882017-03-09 22:27:28 +0900664 help='Base GS URL (gs://<bucket_name>/<path>) to '
665 'upload archive artifacts to')
David Jameseecba232014-06-11 11:35:11 -0700666 group.add_remote_option(
Hidehiko Abe863d7882017-03-09 22:27:28 +0900667 '--cq-gerrit-query', dest='cq_gerrit_override',
668 help='If given, this gerrit query will be used to find what patches to '
669 "test, rather than the normal 'CommitQueue>=1 AND Verified=1 AND "
670 "CodeReview=2' query it defaults to. Use with care- note "
671 'additionally this setting only has an effect if the buildbot '
672 "target is a cq target, and we're in buildbot mode.")
673 group.add_option('--pass-through', action='append', type='string',
674 dest='pass_through_args', default=[])
675 group.add_option('--reexec-api-version', action='store_true',
676 dest='output_api_version', default=False,
677 help='Used for handling forwards/backwards compatibility '
678 'with --resume and --bootstrap')
679 group.add_option('--remote-trybot', action='store_true', default=False,
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400680 help='Indicates this is running on a remote trybot machine')
Hidehiko Abe863d7882017-03-09 22:27:28 +0900681 group.add_option('--buildbucket-id',
682 help='The unique ID in buildbucket of current build '
683 'generated by buildbucket.')
Mike Frysingerdad205d2017-08-11 16:00:14 -0400684 group.add_remote_option('--remote-patches', action='split_extend', default=[],
Hidehiko Abe863d7882017-03-09 22:27:28 +0900685 help='Patches uploaded by the trybot client when '
686 'run using the -p option')
Brian Harringf611e6e2012-07-17 18:47:44 -0700687 # Note the default here needs to be hardcoded to 3; that is the last version
688 # that lacked this functionality.
Hidehiko Abe863d7882017-03-09 22:27:28 +0900689 group.add_option('--remote-version', type='int', default=3,
690 help='Used for compatibility checks w/tryjobs running in '
691 'older chromite instances')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400692 group.add_option('--sourceroot', type='path', default=constants.SOURCE_ROOT)
Paul Hobbs3cbdf332017-07-05 22:55:32 -0700693 group.add_option('--ts-mon-task-num', type='int', default=0,
Don Garrett5cd946b2017-07-20 13:42:20 -0700694 api=constants.REEXEC_API_TSMON_TASK_NUM,
Paul Hobbs3cbdf332017-07-05 22:55:32 -0700695 help='The task number of this process. Defaults to 0. '
696 'This argument is useful for running multiple copies '
697 'of cbuildbot without their metrics colliding.')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400698 group.add_remote_option('--test-bootstrap', action='store_true',
699 default=False,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900700 help='Causes cbuildbot to bootstrap itself twice, '
701 'in the sequence A->B->C: A(unpatched) patches '
702 'and bootstraps B; B patches and bootstraps C')
703 group.add_remote_option('--validation_pool',
704 help='Path to a pickled validation pool. Intended '
705 'for use only with the commit queue.')
706 group.add_remote_option('--metadata_dump',
707 help='Path to a json dumped metadata file. This '
708 'will be used as the initial metadata.')
709 group.add_remote_option('--master-build-id', type='int',
Aviv Keshetacf4cfb2014-07-30 12:31:22 -0700710 api=constants.REEXEC_API_MASTER_BUILD_ID,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900711 help='cidb build id of the master build to this '
712 'slave build.')
713 group.add_remote_option('--mock-tree-status',
714 help='Override the tree status value that would be '
715 'returned from the the actual tree. Example '
716 'values: open, closed, throttled. When used '
717 'in conjunction with --debug, the tree status '
718 'will not be ignored as it usually is in a '
719 '--debug run.')
720 group.add_remote_option('--mock-slave-status',
721 metavar='MOCK_SLAVE_STATUS_PICKLE_FILE',
722 help='Override the result of the _FetchSlaveStatuses '
723 'method of MasterSlaveSyncCompletionStage, by '
724 'specifying a file with a pickle of the result '
725 'to be returned.')
Mike Frysinger81af6ef2013-04-03 02:24:09 -0400726
Mike Frysinger68893242017-08-11 14:16:39 -0400727 parser.add_argument_group(group)
Ryan Cuif4f84be2012-07-09 18:50:41 -0700728
729 #
Brian Harring3fec5a82012-03-01 05:57:03 -0800730 # Debug options
Ryan Cuif4f84be2012-07-09 18:50:41 -0700731 #
Brian Harringfec89fe2012-09-23 07:30:54 -0700732 # Temporary hack; in place till --dry-run replaces --debug.
733 # pylint: disable=W0212
Brian Harring009db502012-10-10 02:21:37 -0700734 group = parser.debug_group
Brian Harringfec89fe2012-09-23 07:30:54 -0700735 debug = [x for x in group.option_list if x._long_opts == ['--debug']][0]
David Jameseecba232014-06-11 11:35:11 -0700736 debug.help += ' Currently functions as --dry-run in addition.'
Brian Harringfec89fe2012-09-23 07:30:54 -0700737 debug.pass_through = True
Brian Harring3fec5a82012-03-01 05:57:03 -0800738 group.add_option('--notee', action='store_false', dest='tee', default=True,
Hidehiko Abe863d7882017-03-09 22:27:28 +0900739 help='Disable logging and internal tee process. Primarily '
740 'used for debugging cbuildbot itself.')
Brian Harring3fec5a82012-03-01 05:57:03 -0800741 return parser
742
743
Ryan Cui85867972012-02-23 18:21:49 -0800744def _FinishParsing(options, args):
745 """Perform some parsing tasks that need to take place after optparse.
746
747 This function needs to be easily testable! Keep it free of
748 environment-dependent code. Put more detailed usage validation in
749 _PostParseCheck().
Brian Harring3fec5a82012-03-01 05:57:03 -0800750
751 Args:
Matt Tennant759e2352013-09-27 15:14:44 -0700752 options: The options object returned by optparse
753 args: The args object returned by optparse
Brian Harring3fec5a82012-03-01 05:57:03 -0800754 """
Ryan Cui41023d92012-11-13 19:59:50 -0800755 # Populate options.pass_through_args.
756 accepted, _ = commandline.FilteringParser.FilterArgs(
757 options.parsed_args, lambda x: x.opt_inst.pass_through)
758 options.pass_through_args.extend(accepted)
Brian Harring07039b52012-05-13 17:56:47 -0700759
Brian Harring3fec5a82012-03-01 05:57:03 -0800760 if options.chrome_root:
761 if options.chrome_rev != constants.CHROME_REV_LOCAL:
Brian Harring1b8c4c82012-05-29 23:03:04 -0700762 cros_build_lib.Die('Chrome rev must be %s if chrome_root is set.' %
763 constants.CHROME_REV_LOCAL)
David Jamesa0a664e2013-02-13 09:52:01 -0800764 elif options.chrome_rev == constants.CHROME_REV_LOCAL:
765 cros_build_lib.Die('Chrome root must be set if chrome_rev is %s.' %
766 constants.CHROME_REV_LOCAL)
Brian Harring3fec5a82012-03-01 05:57:03 -0800767
768 if options.chrome_version:
769 if options.chrome_rev != constants.CHROME_REV_SPEC:
Brian Harring1b8c4c82012-05-29 23:03:04 -0700770 cros_build_lib.Die('Chrome rev must be %s if chrome_version is set.' %
771 constants.CHROME_REV_SPEC)
David Jamesa0a664e2013-02-13 09:52:01 -0800772 elif options.chrome_rev == constants.CHROME_REV_SPEC:
773 cros_build_lib.Die(
774 'Chrome rev must not be %s if chrome_version is not set.'
775 % constants.CHROME_REV_SPEC)
Brian Harring3fec5a82012-03-01 05:57:03 -0800776
David James9e27e662013-02-14 13:42:43 -0800777 patches = bool(options.gerrit_patches or options.local_patches or
778 options.rietveld_patches)
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700779 if options.remote:
780 if options.local:
Brian Harring1b8c4c82012-05-29 23:03:04 -0700781 cros_build_lib.Die('Cannot specify both --remote and --local')
Ryan Cui54da0702012-04-19 18:38:08 -0700782
Don Garrett5af1d262014-05-16 15:49:37 -0700783 # options.channels is a convenient way to detect payloads builds.
Prathmesh Prabhu0bc7f122015-07-25 17:09:26 -0700784 if (not options.list and not options.buildbot and not options.channels and
785 not patches):
Gaurav Shahed0399e2014-02-03 13:36:22 -0800786 prompt = ('No patches were provided; are you sure you want to just '
787 'run a remote build of %s?' % (
788 options.branch if options.branch else 'ToT'))
789 if not cros_build_lib.BooleanPrompt(prompt=prompt, default=False):
Brian Harring7cc4b932012-11-01 16:58:55 -0700790 cros_build_lib.Die('Must provide patches when running with --remote.')
Brian Harring3fec5a82012-03-01 05:57:03 -0800791
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700792 # --debug needs to be explicitly passed through for remote invocations.
793 release_mode_with_patches = (options.buildbot and patches and
794 '--debug' not in options.pass_through_args)
795 else:
796 if len(args) > 1:
Brian Harring1b8c4c82012-05-29 23:03:04 -0700797 cros_build_lib.Die('Multiple configs not supported if not running with '
Brian Harringf1aad832012-07-18 10:46:39 -0700798 '--remote. Got %r', args)
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700799
Ryan Cui79319ab2012-05-21 12:59:18 -0700800 if options.slaves:
Brian Harring1b8c4c82012-05-29 23:03:04 -0700801 cros_build_lib.Die('Cannot use --slaves if not running with --remote.')
Ryan Cui79319ab2012-05-21 12:59:18 -0700802
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700803 release_mode_with_patches = (options.buildbot and patches and
804 not options.debug)
805
David James5734ea32012-08-15 20:23:49 -0700806 # When running in release mode, make sure we are running with checked-in code.
807 # We want checked-in cbuildbot/scripts to prevent errors, and we want to build
808 # a release image with checked-in code for CrOS packages.
809 if release_mode_with_patches:
810 cros_build_lib.Die(
811 'Cannot provide patches when running with --buildbot!')
812
Ryan Cuiba41ad32012-03-08 17:15:29 -0800813 if options.buildbot and options.remote_trybot:
Brian Harring1b8c4c82012-05-29 23:03:04 -0700814 cros_build_lib.Die(
815 '--buildbot and --remote-trybot cannot be used together.')
Ryan Cuiba41ad32012-03-08 17:15:29 -0800816
Ryan Cui85867972012-02-23 18:21:49 -0800817 # Record whether --debug was set explicitly vs. it was inferred.
818 options.debug_forced = False
819 if options.debug:
820 options.debug_forced = True
Ryan Cui88b901c2013-06-21 11:35:30 -0700821 if not options.debug:
Ryan Cui16ca5812012-03-08 20:34:27 -0800822 # We don't set debug by default for
823 # 1. --buildbot invocations.
824 # 2. --remote invocations, because it needs to push changes to the tryjob
825 # repo.
826 options.debug = not options.buildbot and not options.remote
Brian Harring3fec5a82012-03-01 05:57:03 -0800827
Ryan Cui1c13a252012-10-16 15:00:16 -0700828 # Record the configs targeted.
829 options.build_targets = args[:]
830
Ryan Cui88b901c2013-06-21 11:35:30 -0700831 if constants.BRANCH_UTIL_CONFIG in options.build_targets:
Ryan Cui7bd8db62013-08-08 16:29:51 -0700832 if options.remote:
833 cros_build_lib.Die(
Matt Tennanta130ea32013-12-19 09:38:39 -0800834 'Running %s as a remote tryjob is not yet supported.',
835 constants.BRANCH_UTIL_CONFIG)
Ryan Cui88b901c2013-06-21 11:35:30 -0700836 if len(options.build_targets) > 1:
837 cros_build_lib.Die(
Matt Tennanta130ea32013-12-19 09:38:39 -0800838 'Cannot run %s with any other configs.',
839 constants.BRANCH_UTIL_CONFIG)
Ryan Cui88b901c2013-06-21 11:35:30 -0700840 if not options.branch_name:
841 cros_build_lib.Die(
Matt Tennanta130ea32013-12-19 09:38:39 -0800842 'Must specify --branch-name with the %s config.',
843 constants.BRANCH_UTIL_CONFIG)
844 if options.branch and options.branch != options.branch_name:
Ryan Cui88b901c2013-06-21 11:35:30 -0700845 cros_build_lib.Die(
Matt Tennanta130ea32013-12-19 09:38:39 -0800846 'If --branch is specified with the %s config, it must'
847 ' have the same value as --branch-name.',
848 constants.BRANCH_UTIL_CONFIG)
849
850 exclusive_opts = {'--version': options.force_version,
851 '--delete-branch': options.delete_branch,
David Jameseecba232014-06-11 11:35:11 -0700852 '--rename-to': options.rename_to}
Matt Tennanta130ea32013-12-19 09:38:39 -0800853 if 1 != sum(1 for x in exclusive_opts.values() if x):
854 cros_build_lib.Die('When using the %s config, you must'
855 ' specifiy one and only one of the following'
856 ' options: %s.', constants.BRANCH_UTIL_CONFIG,
857 ', '.join(exclusive_opts.keys()))
858
859 # When deleting or renaming a branch, the --branch and --nobootstrap
860 # options are implied.
861 if options.delete_branch or options.rename_to:
862 if not options.branch:
Ralph Nathan03047282015-03-23 11:09:32 -0700863 logging.info('Automatically enabling sync to branch %s for this %s '
864 'flow.', options.branch_name,
865 constants.BRANCH_UTIL_CONFIG)
Matt Tennanta130ea32013-12-19 09:38:39 -0800866 options.branch = options.branch_name
867 if options.bootstrap:
Ralph Nathan03047282015-03-23 11:09:32 -0700868 logging.info('Automatically disabling bootstrap step for this %s flow.',
869 constants.BRANCH_UTIL_CONFIG)
Matt Tennanta130ea32013-12-19 09:38:39 -0800870 options.bootstrap = False
871
Ryan Cui88b901c2013-06-21 11:35:30 -0700872 elif any([options.delete_branch, options.rename_to, options.branch_name]):
873 cros_build_lib.Die(
874 'Cannot specify --delete-branch, --rename-to or --branch-name when not '
875 'running the %s config', constants.BRANCH_UTIL_CONFIG)
876
Brian Harring3fec5a82012-03-01 05:57:03 -0800877
Brian Harring1d7ba942012-04-24 06:37:18 -0700878# pylint: disable=W0613
Don Garrett4af20982015-05-29 19:02:23 -0700879def _PostParseCheck(parser, options, args, site_config):
Ryan Cui85867972012-02-23 18:21:49 -0800880 """Perform some usage validation after we've parsed the arguments
Brian Harring3fec5a82012-03-01 05:57:03 -0800881
Ryan Cui85867972012-02-23 18:21:49 -0800882 Args:
Aviv Keshet9e4236b2013-12-13 13:07:50 -0800883 parser: Option parser that was used to parse arguments.
884 options: The options returned by optparse.
885 args: The args returned by optparse.
Don Garrett4af20982015-05-29 19:02:23 -0700886 site_config: config_lib.SiteConfig containing all config info.
Ryan Cui85867972012-02-23 18:21:49 -0800887 """
Don Garrett0a873e02015-06-30 17:55:10 -0700888 if not args:
889 parser.error('Invalid usage: no configuration targets provided.'
890 'Use -h to see usage. Use -l to list supported configs.')
891
Ryan Cuie1e4e662012-05-21 16:39:46 -0700892 if not options.branch:
David James97d95872012-11-16 15:09:56 -0800893 options.branch = git.GetChromiteTrackingBranch()
Ryan Cuie1e4e662012-05-21 16:39:46 -0700894
Brian Harringae0a5322012-09-15 01:46:51 -0700895 if not repository.IsARepoRoot(options.sourceroot):
896 if options.local_patches:
897 raise Exception('Could not find repo checkout at %s!'
898 % options.sourceroot)
899
David Jamesac8c2a72013-02-13 18:44:33 -0800900 # Because the default cache dir depends on other options, FindCacheDir
901 # always returns None, and we setup the default here.
Brian Harringae0a5322012-09-15 01:46:51 -0700902 if options.cache_dir is None:
903 # Note, options.sourceroot is set regardless of the path
904 # actually existing.
David Jamesac8c2a72013-02-13 18:44:33 -0800905 if options.buildroot is not None:
Brian Harringae0a5322012-09-15 01:46:51 -0700906 options.cache_dir = os.path.join(options.buildroot, '.cache')
David Jamesac8c2a72013-02-13 18:44:33 -0800907 elif os.path.exists(options.sourceroot):
908 options.cache_dir = os.path.join(options.sourceroot, '.cache')
Brian Harringae0a5322012-09-15 01:46:51 -0700909 else:
910 options.cache_dir = parser.FindCacheDir(parser, options)
911 options.cache_dir = os.path.abspath(options.cache_dir)
Brian Harring8c1d7b12012-10-04 17:36:32 -0700912 parser.ConfigureCacheDir(options.cache_dir)
Brian Harringae0a5322012-09-15 01:46:51 -0700913
Yu-Ju Hong2c066762013-10-28 14:05:08 -0700914 osutils.SafeMakedirsNonRoot(options.cache_dir)
Ryan Cui5ba7e152012-05-10 14:36:52 -0700915
Brian Harring609dc4e2012-05-07 02:17:44 -0700916 if options.local_patches:
Brian Harring1d7ba942012-04-24 06:37:18 -0700917 options.local_patches = _CheckLocalPatches(
Brian Harring609dc4e2012-05-07 02:17:44 -0700918 options.sourceroot, options.local_patches)
Brian Harring1d7ba942012-04-24 06:37:18 -0700919
920 default = os.environ.get('CBUILDBOT_DEFAULT_MODE')
921 if (default and not any([options.local, options.buildbot,
922 options.remote, options.remote_trybot])):
Ralph Nathan03047282015-03-23 11:09:32 -0700923 logging.info('CBUILDBOT_DEFAULT_MODE=%s env var detected, using it.'
924 % default)
Brian Harring1d7ba942012-04-24 06:37:18 -0700925 default = default.lower()
926 if default == 'local':
927 options.local = True
928 elif default == 'remote':
929 options.remote = True
930 elif default == 'buildbot':
931 options.buildbot = True
932 else:
Brian Harring1b8c4c82012-05-29 23:03:04 -0700933 cros_build_lib.Die("CBUILDBOT_DEFAULT_MODE value %s isn't supported. "
934 % default)
Ryan Cui85867972012-02-23 18:21:49 -0800935
Matt Tennant763497d2014-01-17 16:45:54 -0800936 # Ensure that all args are legitimate config targets.
Chris Sosa55cdc942014-04-16 13:08:37 -0700937 invalid_targets = []
Matt Tennant763497d2014-01-17 16:45:54 -0800938 for arg in args:
Don Garrett4af20982015-05-29 19:02:23 -0700939 if arg not in site_config:
Chris Sosa55cdc942014-04-16 13:08:37 -0700940 invalid_targets.append(arg)
Ralph Nathan59900422015-03-24 10:41:17 -0700941 logging.error('No such configuraton target: "%s".', arg)
Don Garrett4bb21682014-03-03 16:16:23 -0800942 continue
943
Don Garrett4af20982015-05-29 19:02:23 -0700944 build_config = site_config[arg]
945
Don Garrett5af1d262014-05-16 15:49:37 -0700946 is_payloads_build = build_config.build_type == constants.PAYLOADS_TYPE
947
948 if options.channels and not is_payloads_build:
Don Garrett4bb21682014-03-03 16:16:23 -0800949 cros_build_lib.Die('--channel must only be used with a payload config,'
950 ' not target (%s).' % arg)
Matt Tennant763497d2014-01-17 16:45:54 -0800951
Don Garrett5af1d262014-05-16 15:49:37 -0700952 if not options.channels and is_payloads_build:
953 cros_build_lib.Die('payload configs (%s) require --channel to do anything'
954 ' useful.' % arg)
955
Matt Tennant2c192032014-01-16 13:49:28 -0800956 # The --version option is not compatible with an external target unless the
957 # --buildbot option is specified. More correctly, only "paladin versions"
958 # will work with external targets, and those are only used with --buildbot.
959 # If --buildbot is specified, then user should know what they are doing and
960 # only specify a version that will work. See crbug.com/311648.
Don Garrett4bb21682014-03-03 16:16:23 -0800961 if (options.force_version and
962 not (options.buildbot or build_config.internal)):
963 cros_build_lib.Die('Cannot specify --version without --buildbot for an'
964 ' external target (%s).' % arg)
Matt Tennant2c192032014-01-16 13:49:28 -0800965
Chris Sosa55cdc942014-04-16 13:08:37 -0700966 if invalid_targets:
967 cros_build_lib.Die('One or more invalid configuration targets specified. '
968 'You can check the available configs by running '
969 '`cbuildbot --list --all`')
Matt Tennant763497d2014-01-17 16:45:54 -0800970
Ryan Cui85867972012-02-23 18:21:49 -0800971
Don Garrett597ddff2017-02-17 18:29:37 -0800972def ParseCommandLine(parser, argv):
Ryan Cui85867972012-02-23 18:21:49 -0800973 """Completely parse the commandline arguments"""
Brian Harring3fec5a82012-03-01 05:57:03 -0800974 (options, args) = parser.parse_args(argv)
Brian Harring37e559b2012-05-22 20:47:32 -0700975
Matt Tennant763497d2014-01-17 16:45:54 -0800976 # Strip out null arguments.
977 # TODO(rcui): Remove when buildbot is fixed
978 args = [arg for arg in args if arg]
979
Don Garrett6b0cf682017-03-29 18:21:04 -0700980 if options.deprecated_use_buildbucket:
981 logging.warning('--use-buildbucket is deprecated, and ignored.')
982
Brian Harring37e559b2012-05-22 20:47:32 -0700983 if options.output_api_version:
Mike Frysinger383367e2014-09-16 15:06:17 -0400984 print(constants.REEXEC_API_VERSION)
Brian Harring37e559b2012-05-22 20:47:32 -0700985 sys.exit(0)
986
Ryan Cui85867972012-02-23 18:21:49 -0800987 _FinishParsing(options, args)
988 return options, args
989
990
Aviv Keshet420de512015-05-18 14:28:48 -0700991_ENVIRONMENT_PROD = 'prod'
992_ENVIRONMENT_DEBUG = 'debug'
993_ENVIRONMENT_STANDALONE = 'standalone'
994
995
996def _GetRunEnvironment(options, build_config):
997 """Determine whether this is a prod/debug/standalone run."""
998 # TODO(akeshet): This is a temporary workaround to make sure that the cidb
999 # is not used on waterfalls that the db schema does not support (in particular
1000 # the chromeos.chrome waterfall).
1001 # See crbug.com/406940
Aviv Keshet593014d2017-07-18 17:28:25 -07001002 wfall = os.environ.get('BUILDBOT_MASTERNAME', '')
1003 if not wfall in waterfall.CIDB_KNOWN_WATERFALLS:
Aviv Keshet420de512015-05-18 14:28:48 -07001004 return _ENVIRONMENT_STANDALONE
1005
1006 # TODO(akeshet): Clean up this code once we have better defined flags to
1007 # specify on-or-off waterfall and on-or-off production runs of cbuildbot.
1008 # See crbug.com/331417
1009
1010 # --buildbot runs should use the production services, unless the --debug flag
1011 # is also present.
1012 if options.buildbot:
1013 if options.debug:
1014 return _ENVIRONMENT_DEBUG
1015 else:
1016 return _ENVIRONMENT_PROD
1017
1018 # --remote-trybot runs should use the debug services, with the exception of
1019 # pre-cq builds, which should use the production services.
1020 if options.remote_trybot:
1021 if build_config['pre_cq']:
1022 return _ENVIRONMENT_PROD
1023 else:
1024 return _ENVIRONMENT_DEBUG
1025
1026 # If neither --buildbot nor --remote-trybot flag was used, don't use external
1027 # services.
1028 return _ENVIRONMENT_STANDALONE
1029
1030
Gabe Blackde694a32015-02-19 15:11:11 -08001031def _SetupConnections(options, build_config):
Aviv Keshet55491242017-07-13 17:04:07 -07001032 """Set up CIDB connections using the appropriate Setup call.
Aviv Keshet2982af52014-08-13 16:07:57 -07001033
1034 Args:
1035 options: Command line options structure.
Aviv Keshet64133022014-08-25 15:50:52 -07001036 build_config: Config object for this build.
Aviv Keshet2982af52014-08-13 16:07:57 -07001037 """
Aviv Keshet420de512015-05-18 14:28:48 -07001038 # Outline:
1039 # 1) Based on options and build_config, decide whether we are a production
1040 # run, debug run, or standalone run.
1041 # 2) Set up cidb instance accordingly.
1042 # 3) Update topology info from cidb, so that any other service set up can use
1043 # topology.
1044 # 4) Set up any other services.
1045 run_type = _GetRunEnvironment(options, build_config)
1046
1047 if run_type == _ENVIRONMENT_PROD:
1048 cidb.CIDBConnectionFactory.SetupProdCidb()
Paul Hobbs3cbdf332017-07-05 22:55:32 -07001049 context = ts_mon_config.SetupTsMonGlobalState(
1050 'cbuildbot', indirect=True, task_num=options.ts_mon_task_num)
Aviv Keshet420de512015-05-18 14:28:48 -07001051 elif run_type == _ENVIRONMENT_DEBUG:
1052 cidb.CIDBConnectionFactory.SetupDebugCidb()
Paul Hobbsd5a0f812016-07-26 16:10:31 -07001053 context = ts_mon_config.TrivialContextManager()
Aviv Keshet420de512015-05-18 14:28:48 -07001054 else:
Aviv Keshet62d1a0e2014-08-22 21:16:13 -07001055 cidb.CIDBConnectionFactory.SetupNoCidb()
Paul Hobbsd5a0f812016-07-26 16:10:31 -07001056 context = ts_mon_config.TrivialContextManager()
Aviv Keshet62d1a0e2014-08-22 21:16:13 -07001057
Aviv Keshet420de512015-05-18 14:28:48 -07001058 db = cidb.CIDBConnectionFactory.GetCIDBConnectionForBuilder()
1059 topology.FetchTopologyFromCIDB(db)
Aviv Keshet64133022014-08-25 15:50:52 -07001060
Paul Hobbsd5a0f812016-07-26 16:10:31 -07001061 return context
1062
Aviv Keshet2982af52014-08-13 16:07:57 -07001063
Matt Tennant759e2352013-09-27 15:14:44 -07001064# TODO(build): This function is too damn long.
Ryan Cui85867972012-02-23 18:21:49 -08001065def main(argv):
David James59a0a2b2013-03-22 14:04:44 -07001066 # Turn on strict sudo checks.
1067 cros_build_lib.STRICT_SUDO = True
1068
Ryan Cui85867972012-02-23 18:21:49 -08001069 # Set umask to 022 so files created by buildbot are readable.
Mike Frysinger60ec1012013-10-21 00:11:10 -04001070 os.umask(0o22)
Ryan Cui85867972012-02-23 18:21:49 -08001071
Ryan Cui85867972012-02-23 18:21:49 -08001072 parser = _CreateParser()
Don Garrett597ddff2017-02-17 18:29:37 -08001073 options, args = ParseCommandLine(parser, argv)
Don Garrett0a873e02015-06-30 17:55:10 -07001074
Don Garrettde81cc72015-07-07 13:23:28 -07001075 if options.config_repo:
Don Garrett58e85e02017-06-21 15:24:29 -07001076 cros_build_lib.Die('Deprecated usage. Ping crbug.com/735696 you need it.')
Don Garrettde81cc72015-07-07 13:23:28 -07001077
Don Garrettb85658c2015-06-30 19:07:22 -07001078 # Fetch our site_config now, because we need it to do anything else.
Don Garrettde81cc72015-07-07 13:23:28 -07001079 site_config = config_lib.GetConfig()
Don Garrettb85658c2015-06-30 19:07:22 -07001080
Don Garrett0a873e02015-06-30 17:55:10 -07001081 if options.list:
1082 _PrintValidConfigs(site_config, options.print_all)
1083 sys.exit(0)
Brian Harring3fec5a82012-03-01 05:57:03 -08001084
Don Garrett4af20982015-05-29 19:02:23 -07001085 _PostParseCheck(parser, options, args, site_config)
Brian Harring3fec5a82012-03-01 05:57:03 -08001086
Mike Frysinger8fd67dc2012-12-03 23:51:18 -05001087 cros_build_lib.AssertOutsideChroot()
Zdenek Behan98ec2fb2012-08-31 17:12:18 +02001088
Prathmesh Prabhu51d774f2015-07-17 15:11:49 -07001089 if options.enable_buildbot_tags:
1090 logging.EnableBuildbotMarkers()
Brian Harring3fec5a82012-03-01 05:57:03 -08001091 if options.remote:
Ralph Nathan23a12212015-03-25 10:27:54 -07001092 logging.getLogger().setLevel(logging.WARNING)
Ryan Cui16ca5812012-03-08 20:34:27 -08001093
Brian Harring3fec5a82012-03-01 05:57:03 -08001094 # Verify configs are valid.
Dan Shi0bdb7132013-07-30 16:22:12 -07001095 # If hwtest flag is enabled, post a warning that HWTest step may fail if the
1096 # specified board is not a released platform or it is a generic overlay.
Brian Harring3fec5a82012-03-01 05:57:03 -08001097 for bot in args:
Don Garrett4af20982015-05-29 19:02:23 -07001098 build_config = site_config[bot]
Aviv Kesheta96a2a92013-02-05 17:21:51 -08001099 if options.hwtest:
Ralph Nathan446aee92015-03-23 14:44:56 -07001100 logging.warning(
Dan Shi0bdb7132013-07-30 16:22:12 -07001101 'If %s is not a released platform or it is a generic overlay, '
1102 'the HWTest step will most likely not run; please ask the lab '
1103 'team for help if this is unexpected.' % build_config['boards'])
Brian Harring3fec5a82012-03-01 05:57:03 -08001104
1105 # Verify gerrit patches are valid.
Mike Frysinger383367e2014-09-16 15:06:17 -04001106 print('Verifying patches...')
Mike Frysingerb80d0192015-02-04 22:08:59 -05001107 patch_pool = trybot_patch_pool.TrybotPatchPool.FromOptions(
1108 gerrit_patches=options.gerrit_patches,
1109 local_patches=options.local_patches,
1110 sourceroot=options.sourceroot,
1111 remote_patches=options.remote_patches)
Ryan Cui16d9e1f2012-05-11 10:50:18 -07001112
Ryan Cuieaa9efd2012-04-25 17:56:45 -07001113 # --debug need to be explicitly passed through for remote invocations.
1114 if options.buildbot and '--debug' not in options.pass_through_args:
1115 _ConfirmRemoteBuildbotRun()
1116
Mike Frysinger383367e2014-09-16 15:06:17 -04001117 print('Submitting tryjob...')
Paul Hobbsd5a0f812016-07-26 16:10:31 -07001118 with _SetupConnections(options, build_config):
Don Garrett887ca932017-06-22 15:32:31 -07001119 description = options.remote_description
1120 if description is None:
1121 description = remote_try.DefaultDescription(
1122 options.branch,
1123 options.gerrit_patches+options.local_patches)
1124
1125 tryjob = remote_try.RemoteTryJob(args, patch_pool.local_patches,
1126 options.pass_through_args,
1127 options.cache_dir,
1128 description,
1129 options.committer_email,
1130 options.use_buildbucket,
1131 options.slaves)
Paul Hobbsd5a0f812016-07-26 16:10:31 -07001132 tryjob.Submit(testjob=options.test_tryjob, dryrun=False)
Mike Frysinger383367e2014-09-16 15:06:17 -04001133 print('Tryjob submitted!')
1134 print(('Go to %s to view the status of your job.'
1135 % tryjob.GetTrybotWaterfallLink()))
Brian Harring3fec5a82012-03-01 05:57:03 -08001136 sys.exit(0)
Matt Tennant759e2352013-09-27 15:14:44 -07001137
Ryan Cui54da0702012-04-19 18:38:08 -07001138 elif (not options.buildbot and not options.remote_trybot
1139 and not options.resume and not options.local):
Mike Frysinger5672a2a2014-05-31 22:29:57 -04001140 cros_build_lib.Die('Please use --remote or --local to run trybots')
Brian Harring3fec5a82012-03-01 05:57:03 -08001141
Ningning Xiac691e432016-08-11 14:52:59 -07001142 elif options.buildbot and not options.debug:
1143 if not cros_build_lib.HostIsCIBuilder():
1144 # Cannot run --buildbot if both --debug and --remote aren't specified.
1145 cros_build_lib.Die('This host isn\'t a continuous-integration builder.')
1146
Matt Tennant759e2352013-09-27 15:14:44 -07001147 # Only one config arg is allowed in this mode, which was confirmed earlier.
Ryan Cui8be16062012-04-24 12:05:26 -07001148 bot_id = args[-1]
Don Garrett4af20982015-05-29 19:02:23 -07001149 build_config = site_config[bot_id]
Brian Harring3fec5a82012-03-01 05:57:03 -08001150
Don Garrettbbd7b552014-05-16 13:15:21 -07001151 # TODO: Re-enable this block when reference_repo support handles this
1152 # properly. (see chromium:330775)
1153 # if options.reference_repo is None:
1154 # repo_path = os.path.join(options.sourceroot, '.repo')
1155 # # If we're being run from a repo checkout, reuse the repo's git pool to
1156 # # cut down on sync time.
1157 # if os.path.exists(repo_path):
1158 # options.reference_repo = options.sourceroot
1159
1160 if options.reference_repo:
David Jamesdac7a912013-11-18 11:14:44 -08001161 if not os.path.exists(options.reference_repo):
1162 parser.error('Reference path %s does not exist'
1163 % (options.reference_repo,))
1164 elif not os.path.exists(os.path.join(options.reference_repo, '.repo')):
1165 parser.error('Reference path %s does not look to be the base of a '
1166 'repo checkout; no .repo exists in the root.'
1167 % (options.reference_repo,))
1168
Brian Harringf11bf682012-05-14 15:53:43 -07001169 if (options.buildbot or options.remote_trybot) and not options.resume:
Brian Harring470f6112012-03-02 11:47:10 -08001170 if not options.cgroups:
Ryan Cuid4a24212012-04-04 18:08:12 -07001171 parser.error('Options --buildbot/--remote-trybot and --nocgroups cannot '
1172 'be used together. Cgroup support is required for '
1173 'buildbot/remote-trybot mode.')
Mike Frysingera78a56e2012-11-20 06:02:30 -05001174 if not cgroups.Cgroup.IsSupported():
Ryan Cuid4a24212012-04-04 18:08:12 -07001175 parser.error('Option --buildbot/--remote-trybot was given, but this '
1176 'system does not support cgroups. Failing.')
Brian Harring3fec5a82012-03-01 05:57:03 -08001177
David Jamesaad5cc72012-10-26 15:03:13 -07001178 missing = osutils.FindMissingBinaries(_BUILDBOT_REQUIRED_BINARIES)
Brian Harring351ce442012-03-09 16:38:14 -08001179 if missing:
David Jameseecba232014-06-11 11:35:11 -07001180 parser.error('Option --buildbot/--remote-trybot requires the following '
Ryan Cuid4a24212012-04-04 18:08:12 -07001181 "binaries which couldn't be found in $PATH: %s"
Brian Harring351ce442012-03-09 16:38:14 -08001182 % (', '.join(missing)))
1183
David Jamesdac7a912013-11-18 11:14:44 -08001184 if options.reference_repo:
1185 options.reference_repo = os.path.abspath(options.reference_repo)
1186
Brian Harring3fec5a82012-03-01 05:57:03 -08001187 if not options.buildroot:
1188 if options.buildbot:
Gaurav Shah59abcb52014-12-09 15:27:11 -08001189 parser.error('Please specify a buildroot with the --buildroot option.')
Matt Tennantd55b1f42012-04-13 14:15:01 -07001190
Ryan Cui5ba7e152012-05-10 14:36:52 -07001191 options.buildroot = _DetermineDefaultBuildRoot(options.sourceroot,
1192 build_config['internal'])
Brian Harring470f6112012-03-02 11:47:10 -08001193 # We use a marker file in the buildroot to indicate the user has
1194 # consented to using this directory.
1195 if not os.path.exists(repository.GetTrybotMarkerPath(options.buildroot)):
1196 _ConfirmBuildRoot(options.buildroot)
Brian Harring3fec5a82012-03-01 05:57:03 -08001197
1198 # Sanity check of buildroot- specifically that it's not pointing into the
1199 # midst of an existing repo since git-repo doesn't support nesting.
Brian Harring3fec5a82012-03-01 05:57:03 -08001200 if (not repository.IsARepoRoot(options.buildroot) and
David James13a69c92013-05-09 10:37:42 -07001201 git.FindRepoDir(options.buildroot)):
Brian Harring3fec5a82012-03-01 05:57:03 -08001202 parser.error('Configured buildroot %s points into a repository checkout, '
1203 'rather than the root of it. This is not supported.'
1204 % options.buildroot)
1205
Chris Sosab5ea3b42012-10-25 15:25:20 -07001206 if not options.log_dir:
1207 options.log_dir = os.path.join(options.buildroot, _DEFAULT_LOG_DIR)
1208
Brian Harringd166aaf2012-05-14 18:31:53 -07001209 log_file = None
1210 if options.tee:
Chris Sosab5ea3b42012-10-25 15:25:20 -07001211 log_file = os.path.join(options.log_dir, _BUILDBOT_LOG_FILE)
1212 osutils.SafeMakedirs(options.log_dir)
Brian Harringd166aaf2012-05-14 18:31:53 -07001213 _BackupPreviousLog(log_file)
1214
Brian Harring1b8c4c82012-05-29 23:03:04 -07001215 with cros_build_lib.ContextManagerStack() as stack:
Ningning Xia96876d52016-03-31 10:26:39 -07001216 options.preserve_paths = set()
David Jamescebc7272013-07-17 16:45:05 -07001217 if log_file is not None:
1218 # We don't want the critical section to try to clean up the tee process,
1219 # so we run Tee (forked off) outside of it. This prevents a deadlock
1220 # because the Tee process only exits when its pipe is closed, and the
1221 # critical section accidentally holds on to that file handle.
1222 stack.Add(tee.Tee, log_file)
1223 options.preserve_paths.add(_DEFAULT_LOG_DIR)
1224
Brian Harringc2d09d92012-05-13 22:03:15 -07001225 critical_section = stack.Add(cleanup.EnforcedCleanupSection)
1226 stack.Add(sudo.SudoKeepAlive)
Brian Harringd166aaf2012-05-14 18:31:53 -07001227
Brian Harringc2d09d92012-05-13 22:03:15 -07001228 if not options.resume:
Brian Harring2bf55e12012-05-13 21:31:55 -07001229 # If we're in resume mode, use our parents tempdir rather than
1230 # nesting another layer.
David James4bc13702013-03-26 08:08:04 -07001231 stack.Add(osutils.TempDir, prefix='cbuildbot-tmp', set_global=True)
David Jameseecba232014-06-11 11:35:11 -07001232 logging.debug('Cbuildbot tempdir is %r.', os.environ.get('TMP'))
Brian Harringd166aaf2012-05-14 18:31:53 -07001233
Brian Harringc2d09d92012-05-13 22:03:15 -07001234 if options.cgroups:
1235 stack.Add(cgroups.SimpleContainChildren, 'cbuildbot')
Brian Harringa184efa2012-03-04 11:51:25 -08001236
Brian Harringc2d09d92012-05-13 22:03:15 -07001237 # Mark everything between EnforcedCleanupSection and here as having to
1238 # be rolled back via the contextmanager cleanup handlers. This
1239 # ensures that sudo bits cannot outlive cbuildbot, that anything
1240 # cgroups would kill gets killed, etc.
David Jamesfb3aac92013-10-16 13:26:52 -07001241 stack.Add(critical_section.ForkWatchdog)
Brian Harringd166aaf2012-05-14 18:31:53 -07001242
Brian Harringc2d09d92012-05-13 22:03:15 -07001243 if not options.buildbot:
Don Garrett6747eba2015-06-25 16:00:16 -07001244 build_config = config_lib.OverrideConfigForTrybot(
Don Garrett4bb21682014-03-03 16:16:23 -08001245 build_config, options)
Brian Harringc2d09d92012-05-13 22:03:15 -07001246
Aviv Kesheta0159be2013-12-12 13:56:28 -08001247 if options.mock_tree_status is not None:
Prathmesh Prabhud51d7502014-12-21 01:42:55 -08001248 stack.Add(mock.patch.object, tree_status, '_GetStatus',
Aviv Kesheta0159be2013-12-12 13:56:28 -08001249 return_value=options.mock_tree_status)
1250
Aviv Keshetcf9c2722014-02-25 15:15:10 -08001251 if options.mock_slave_status is not None:
1252 with open(options.mock_slave_status, 'r') as f:
Aviv Keshet4e750022014-03-07 16:50:34 -08001253 mock_statuses = pickle.load(f)
1254 for key, value in mock_statuses.iteritems():
Ningning Xiaf342b952017-02-15 14:13:33 -08001255 mock_statuses[key] = builder_status_lib.BuilderStatus(**value)
Yu-Ju Hongd0fda382014-05-09 15:28:24 -07001256 stack.Add(mock.patch.object,
1257 completion_stages.MasterSlaveSyncCompletionStage,
1258 '_FetchSlaveStatuses',
1259 return_value=mock_statuses)
Aviv Keshetcf9c2722014-02-25 15:15:10 -08001260
Paul Hobbsd5a0f812016-07-26 16:10:31 -07001261 stack.Add(_SetupConnections, options, build_config)
Don Garrettb4318362014-10-03 15:49:36 -07001262 retry_stats.SetupStats()
Aviv Keshet2982af52014-08-13 16:07:57 -07001263
Aviv Keshet446f07f2016-03-08 11:32:31 -08001264 timeout_display_message = None
Prathmesh Prabhu80e05df2014-12-11 15:20:33 -08001265 # For master-slave builds: Update slave's timeout using master's published
1266 # deadline.
1267 if options.buildbot and options.master_build_id is not None:
1268 slave_timeout = None
1269 if cidb.CIDBConnectionFactory.IsCIDBSetup():
1270 cidb_handle = cidb.CIDBConnectionFactory.GetCIDBConnectionForBuilder()
1271 if cidb_handle:
1272 slave_timeout = cidb_handle.GetTimeToDeadline(options.master_build_id)
1273
1274 if slave_timeout is not None:
Prathmesh Prabhue49f2aa2017-04-25 12:02:18 -07001275 # We artificially set a minimum slave_timeout because '0' is handled
1276 # specially, and because we don't want to timeout while trying to set
1277 # things up.
1278 slave_timeout = max(slave_timeout, 20)
Prathmesh Prabhu80e05df2014-12-11 15:20:33 -08001279 if options.timeout == 0 or slave_timeout < options.timeout:
1280 logging.info('Updating slave build timeout to %d seconds enforced '
Ralph Nathan03047282015-03-23 11:09:32 -07001281 'by the master', slave_timeout)
Prathmesh Prabhu80e05df2014-12-11 15:20:33 -08001282 options.timeout = slave_timeout
Aviv Keshet84017582016-05-18 16:59:59 -07001283 timeout_display_message = (
1284 'This build has reached the timeout deadline set by the master. '
1285 'Either this stage or a previous one took too long (see stage '
1286 'timing historical summary in ReportStage) or the build failed '
1287 'to start on time.')
Prathmesh Prabhu80e05df2014-12-11 15:20:33 -08001288 else:
1289 logging.warning('Could not get master deadline for master-slave build. '
1290 'Can not set slave timeout.')
1291
1292 if options.timeout > 0:
Aviv Keshet446f07f2016-03-08 11:32:31 -08001293 stack.Add(timeout_util.FatalTimeout, options.timeout,
1294 timeout_display_message)
Ningning Xiaad483542016-05-24 12:27:21 -07001295 try:
1296 _RunBuildStagesWrapper(options, site_config, build_config)
1297 except failures_lib.ExitEarlyException as ex:
1298 # This build finished successfully. Do not re-raise ExitEarlyException.
1299 logging.info('One stage exited early: %s', ex)