blob: 107f15fe4b172f40e6a837ab5c2a7e743436def1 [file] [log] [blame]
Brian Harring3fec5a82012-03-01 05:57:03 -08001#!/usr/bin/python
2
3# Copyright (c) 2011-2012 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Main builder code for Chromium OS.
8
9Used by Chromium OS buildbot configuration for all Chromium OS builds including
10full and pre-flight-queue builds.
11"""
12
13import distutils.version
14import glob
Chris Sosa4f6ffaf2012-05-01 17:05:44 -070015import logging
David James58e0c092012-03-04 20:31:12 -080016import multiprocessing
Brian Harring3fec5a82012-03-01 05:57:03 -080017import optparse
18import os
19import pprint
20import sys
Ryan Cui54da0702012-04-19 18:38:08 -070021import time
Brian Harring3fec5a82012-03-01 05:57:03 -080022
23from chromite.buildbot import builderstage as bs
24from chromite.buildbot import cbuildbot_background as background
25from chromite.buildbot import cbuildbot_config
26from chromite.buildbot import cbuildbot_stages as stages
27from chromite.buildbot import cbuildbot_results as results_lib
Brian Harring3fec5a82012-03-01 05:57:03 -080028from chromite.buildbot import constants
29from chromite.buildbot import gerrit_helper
30from chromite.buildbot import patch as cros_patch
31from chromite.buildbot import remote_try
32from chromite.buildbot import repository
33from chromite.buildbot import tee
34
Brian Harringc92a7012012-02-29 10:11:34 -080035from chromite.lib import cgroups
Brian Harringa184efa2012-03-04 11:51:25 -080036from chromite.lib import cleanup
Brian Harring3fec5a82012-03-01 05:57:03 -080037from chromite.lib import cros_build_lib as cros_lib
38from chromite.lib import sudo
39
Ryan Cuiadd49122012-03-21 22:19:58 -070040
Brian Harring3fec5a82012-03-01 05:57:03 -080041cros_lib.STRICT_SUDO = True
42
43_DEFAULT_LOG_DIR = 'cbuildbot_logs'
44_BUILDBOT_LOG_FILE = 'cbuildbot.log'
45_DEFAULT_EXT_BUILDROOT = 'trybot'
46_DEFAULT_INT_BUILDROOT = 'trybot-internal'
Matt Tennantf1e30972012-03-02 16:30:07 -080047_PATH_TO_CBUILDBOT = os.path.join(constants.CHROMITE_BIN_SUBDIR, 'cbuildbot')
Brian Harring3fec5a82012-03-01 05:57:03 -080048_DISTRIBUTED_TYPES = [constants.COMMIT_QUEUE_TYPE, constants.PFQ_TYPE,
49 constants.CANARY_TYPE, constants.CHROME_PFQ_TYPE,
50 constants.PALADIN_TYPE]
Brian Harring351ce442012-03-09 16:38:14 -080051_BUILDBOT_REQUIRED_BINARIES = ('pbzip2',)
Brian Harring3fec5a82012-03-01 05:57:03 -080052
53
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070054def _PrintValidConfigs(display_all=False):
Brian Harring3fec5a82012-03-01 05:57:03 -080055 """Print a list of valid buildbot configs.
56
57 Arguments:
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070058 display_all: Print all configs. Otherwise, prints only configs with
59 trybot_list=True.
Brian Harring3fec5a82012-03-01 05:57:03 -080060 """
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070061 def _GetSortKey(config_name):
62 config_dict = cbuildbot_config.config[config_name]
63 return (not config_dict['trybot_list'], config_dict['description'],
64 config_name)
65
Brian Harring3fec5a82012-03-01 05:57:03 -080066 COLUMN_WIDTH = 45
67 print 'config'.ljust(COLUMN_WIDTH), 'description'
68 print '------'.ljust(COLUMN_WIDTH), '-----------'
69 config_names = cbuildbot_config.config.keys()
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070070 config_names.sort(key=_GetSortKey)
Brian Harring3fec5a82012-03-01 05:57:03 -080071 for name in config_names:
Ryan Cui4f6cf7e2012-04-18 16:12:27 -070072 if display_all or cbuildbot_config.config[name]['trybot_list']:
73 desc = cbuildbot_config.config[name].get('description')
74 desc = desc if desc else ''
Brian Harring3fec5a82012-03-01 05:57:03 -080075 print name.ljust(COLUMN_WIDTH), desc
76
77
78def _GetConfig(config_name):
79 """Gets the configuration for the build"""
80 if not cbuildbot_config.config.has_key(config_name):
81 print 'Non-existent configuration %s specified.' % config_name
82 print 'Please specify one of:'
83 _PrintValidConfigs()
84 sys.exit(1)
85
86 result = cbuildbot_config.config[config_name]
87
88 return result
89
90
91def _GetChromiteTrackingBranch():
David James66009462012-03-25 10:08:38 -070092 """Returns the remote branch associated with chromite."""
Brian Harring3fec5a82012-03-01 05:57:03 -080093 cwd = os.path.dirname(os.path.realpath(__file__))
David James66009462012-03-25 10:08:38 -070094 branch = cros_lib.GetCurrentBranch(cwd)
95 if branch:
96 tracking_branch = cros_lib.GetTrackingBranch(branch, cwd)[1]
97 if tracking_branch.startswith('refs/heads/'):
98 return tracking_branch.replace('refs/heads/', '')
99 # If we are not on a branch, or if the tracking branch is a revision,
David James8b3c1bf2012-03-28 09:10:16 -0700100 # use the push branch. For repo repositories, this will be the manifest
101 # branch configured for this project. For other repositories, we'll just
102 # guess 'master', since there's no easy way to find out what branch
103 # we're on.
104 return cros_lib.GetPushBranch(cwd)[1]
Brian Harring3fec5a82012-03-01 05:57:03 -0800105
106
107def _CheckBuildRootBranch(buildroot, tracking_branch):
108 """Make sure buildroot branch is the same as Chromite branch."""
109 manifest_branch = cros_lib.GetManifestDefaultBranch(buildroot)
110 if manifest_branch != tracking_branch:
111 cros_lib.Die('Chromite is not on same branch as buildroot checkout\n' +
112 'Chromite is on branch %s.\n' % tracking_branch +
113 'Buildroot checked out to %s\n' % manifest_branch)
114
115
116def _PreProcessPatches(gerrit_patches, local_patches):
117 """Validate patches ASAP to catch user errors. Also generate patch info.
118
119 Args:
120 gerrit_patches: List of gerrit CL ID's passed in by user.
121 local_patches: List of local project branches to generate patches from.
122
123 Returns:
124 A tuple containing a list of cros_patch.GerritPatch and a list of
Matt Tennantd55b1f42012-04-13 14:15:01 -0700125 cros_patch.LocalGitRepoPatch objects.
Brian Harring3fec5a82012-03-01 05:57:03 -0800126 """
127 gerrit_patch_info = []
128 local_patch_info = []
129
130 try:
131 if gerrit_patches:
132 gerrit_patch_info = gerrit_helper.GetGerritPatchInfo(gerrit_patches)
133 for patch in gerrit_patch_info:
134 if patch.IsAlreadyMerged():
135 cros_lib.Warning('Patch %s has already been merged.' % str(patch))
136 except gerrit_helper.GerritException as e:
137 cros_lib.Die(str(e))
138
139 try:
140 if local_patches:
141 local_patch_info = cros_patch.PrepareLocalPatches(
142 local_patches,
143 _GetChromiteTrackingBranch())
144
145 except cros_patch.PatchException as e:
146 cros_lib.Die(str(e))
147
148 return gerrit_patch_info, local_patch_info
149
150
151def _IsIncrementalBuild(buildroot, clobber):
152 """Returns True if we are reusing an existing buildroot."""
153 repo_dir = os.path.join(buildroot, '.repo')
154 return not clobber and os.path.isdir(repo_dir)
155
156
157class Builder(object):
158 """Parent class for all builder types.
159
160 This class functions as a parent class for various build types. It's intended
161 use is builder_instance.Run().
162
163 Vars:
Brian Harring3fec5a82012-03-01 05:57:03 -0800164 build_config: The configuration dictionary from cbuildbot_config.
165 options: The options provided from optparse in main().
166 completed_stages_file: Where we store resume state.
167 archive_url: Where our artifacts for this builder will be archived.
168 tracking_branch: The tracking branch for this build.
169 release_tag: The associated "chrome os version" of this build.
170 gerrit_patches: Gerrit patches to be included in build.
171 local_patches: Local patches to be included in build.
172 """
173
David James944a48e2012-03-07 12:19:03 -0800174 def __init__(self, options, build_config):
Brian Harring3fec5a82012-03-01 05:57:03 -0800175 """Initializes instance variables. Must be called by all subclasses."""
Brian Harring3fec5a82012-03-01 05:57:03 -0800176 self.build_config = build_config
177 self.options = options
178
179 # TODO, Remove here and in config after bug chromium-os:14649 is fixed.
180 if self.build_config['chromeos_official']:
181 os.environ['CHROMEOS_OFFICIAL'] = '1'
182
183 self.completed_stages_file = os.path.join(options.buildroot,
184 '.completed_stages')
David James58e0c092012-03-04 20:31:12 -0800185 self.archive_stages = {}
Brian Harring3fec5a82012-03-01 05:57:03 -0800186 self.archive_urls = {}
187 self.release_tag = None
Brian Harringeb237932012-05-07 02:08:06 -0700188 self.target_manifest_branch = _GetChromiteTrackingBranch()
Brian Harring3fec5a82012-03-01 05:57:03 -0800189 self.gerrit_patches = None
190 self.local_patches = None
191
192 def Initialize(self):
193 """Runs through the initialization steps of an actual build."""
194 if self.options.resume and os.path.exists(self.completed_stages_file):
195 with open(self.completed_stages_file, 'r') as load_file:
196 results_lib.Results.RestoreCompletedStages(load_file)
197
198 # We only want to do this if we need to patch changes.
199 if not results_lib.Results.GetPrevious().get(
200 self._GetStageInstance(stages.PatchChangesStage, None, None).name):
201 self.gerrit_patches, self.local_patches = _PreProcessPatches(
202 self.options.gerrit_patches, self.options.local_patches)
203
Brian Harringeb237932012-05-07 02:08:06 -0700204 bs.BuilderStage.SetManifestBranch(self.target_manifest_branch)
Brian Harring3fec5a82012-03-01 05:57:03 -0800205
206 # Check branch matching early.
207 if _IsIncrementalBuild(self.options.buildroot, self.options.clobber):
Brian Harringeb237932012-05-07 02:08:06 -0700208 _CheckBuildRootBranch(self.options.buildroot, self.target_manifest_branch)
Brian Harring3fec5a82012-03-01 05:57:03 -0800209
210 self._RunStage(stages.CleanUpStage)
211
212 def _GetStageInstance(self, stage, *args, **kwargs):
213 """Helper function to get an instance given the args.
214
David James944a48e2012-03-07 12:19:03 -0800215 Useful as almost all stages just take in options and build_config.
Brian Harring3fec5a82012-03-01 05:57:03 -0800216 """
David James944a48e2012-03-07 12:19:03 -0800217 config = kwargs.pop('config', self.build_config)
218 return stage(self.options, config, *args, **kwargs)
Brian Harring3fec5a82012-03-01 05:57:03 -0800219
220 def _SetReleaseTag(self):
221 """Sets the release tag from the manifest_manager.
222
223 Must be run after sync stage as syncing enables us to have a release tag.
224 """
225 # Extract version we have decided to build into self.release_tag.
226 manifest_manager = stages.ManifestVersionedSyncStage.manifest_manager
227 if manifest_manager:
228 self.release_tag = manifest_manager.current_version
229
230 def _RunStage(self, stage, *args, **kwargs):
231 """Wrapper to run a stage."""
232 stage_instance = self._GetStageInstance(stage, *args, **kwargs)
233 return stage_instance.Run()
234
235 def GetSyncInstance(self):
236 """Returns an instance of a SyncStage that should be run.
237
238 Subclasses must override this method.
239 """
240 raise NotImplementedError()
241
242 def RunStages(self):
243 """Subclasses must override this method. Runs the appropriate code."""
244 raise NotImplementedError()
245
246 def _WriteCheckpoint(self):
247 """Drops a completed stages file with current state."""
248 with open(self.completed_stages_file, 'w+') as save_file:
249 results_lib.Results.SaveCompletedStages(save_file)
250
251 def _ShouldReExecuteInBuildRoot(self):
252 """Returns True if this build should be re-executed in the buildroot."""
253 abs_buildroot = os.path.abspath(self.options.buildroot)
254 return not os.path.abspath(__file__).startswith(abs_buildroot)
255
256 def _ReExecuteInBuildroot(self, sync_instance):
257 """Reexecutes self in buildroot and returns True if build succeeds.
258
259 This allows the buildbot code to test itself when changes are patched for
260 buildbot-related code. This is a no-op if the buildroot == buildroot
261 of the running chromite checkout.
262
263 Args:
264 sync_instance: Instance of the sync stage that was run to sync.
265
266 Returns:
267 True if the Build succeeded.
268 """
269 # If we are resuming, use last checkpoint.
270 if not self.options.resume:
271 self._WriteCheckpoint()
272
273 # Re-write paths to use absolute paths.
274 # Suppress any timeout options given from the commandline in the
275 # invoked cbuildbot; our timeout will enforce it instead.
Ryan Cui721cab42012-05-11 11:32:30 -0700276 args_to_append = ['--resume', '--timeout', '0', '--buildroot',
Brian Harring3fec5a82012-03-01 05:57:03 -0800277 os.path.abspath(self.options.buildroot)]
278
279 if self.options.chrome_root:
280 args_to_append += ['--chrome_root',
281 os.path.abspath(self.options.chrome_root)]
282
283 if stages.ManifestVersionedSyncStage.manifest_manager:
284 ver = stages.ManifestVersionedSyncStage.manifest_manager.current_version
285 args_to_append += ['--version', ver]
286
287 if isinstance(sync_instance, stages.CommitQueueSyncStage):
288 vp_file = sync_instance.SaveValidationPool()
289 args_to_append += ['--validation_pool', vp_file]
290
291 # Re-run the command in the buildroot.
292 # Finally, be generous and give the invoked cbuildbot 30s to shutdown
293 # when something occurs. It should exit quicker, but the sigterm may
294 # hit while the system is particularly busy.
295 return_obj = cros_lib.RunCommand(
296 [_PATH_TO_CBUILDBOT] + sys.argv[1:] + args_to_append,
297 cwd=self.options.buildroot, error_code_ok=True, kill_timeout=30)
298 return return_obj.returncode == 0
299
300 def Run(self):
301 """Main runner for this builder class. Runs build and prints summary."""
302 print_report = True
David James3d4d3502012-04-09 15:12:06 -0700303 exception_thrown = False
Brian Harring3fec5a82012-03-01 05:57:03 -0800304 success = True
305 try:
306 self.Initialize()
307 sync_instance = self.GetSyncInstance()
308 sync_instance.Run()
309 self._SetReleaseTag()
310
Ryan Cuicedd8a52012-03-22 02:28:35 -0700311 if (self.gerrit_patches or self.local_patches
312 or self.options.remote_patches):
Brian Harring3fec5a82012-03-01 05:57:03 -0800313 self._RunStage(stages.PatchChangesStage,
314 self.gerrit_patches, self.local_patches)
315
316 if self._ShouldReExecuteInBuildRoot():
317 print_report = False
318 success = self._ReExecuteInBuildroot(sync_instance)
319 else:
320 self.RunStages()
David James3d4d3502012-04-09 15:12:06 -0700321 except Exception:
322 exception_thrown = True
323 raise
Brian Harring3fec5a82012-03-01 05:57:03 -0800324 finally:
325 if print_report:
326 self._WriteCheckpoint()
327 print '\n\n\n@@@BUILD_STEP Report@@@\n'
328 results_lib.Results.Report(sys.stdout, self.archive_urls,
329 self.release_tag)
330 success = results_lib.Results.BuildSucceededSoFar()
David James3d4d3502012-04-09 15:12:06 -0700331 if exception_thrown and success:
332 success = False
333 print >> sys.stderr, """
334@@@STEP_FAILURE@@@
335Exception thrown, but all stages marked successful. This is an internal error,
336because the stage that threw the exception should be marked as failing."""
Brian Harring3fec5a82012-03-01 05:57:03 -0800337
338 return success
339
340
341class SimpleBuilder(Builder):
342 """Builder that performs basic vetting operations."""
343
344 def GetSyncInstance(self):
345 """Sync to lkgm or TOT as necessary.
346
347 Returns: the instance of the sync stage that was run.
348 """
349 if self.options.lkgm or self.build_config['use_lkgm']:
350 sync_stage = self._GetStageInstance(stages.LKGMSyncStage)
351 else:
352 sync_stage = self._GetStageInstance(stages.SyncStage)
353
354 return sync_stage
355
David James58e0c092012-03-04 20:31:12 -0800356 def _RunBackgroundStagesForBoard(self, board):
357 """Run background board-specific stages for the specified board."""
David James58e0c092012-03-04 20:31:12 -0800358 archive_stage = self.archive_stages[board]
David James944a48e2012-03-07 12:19:03 -0800359 configs = self.build_config['board_specific_configs']
360 config = configs.get(board, self.build_config)
361 stage_list = [[stages.VMTestStage, board, archive_stage],
362 [stages.ChromeTestStage, board, archive_stage],
363 [stages.UnitTestStage, board],
Chris Sosa1dc96132012-05-11 15:40:50 -0700364 [stages.UploadPrebuiltsStage, board, archive_stage]]
Brian Harring3fec5a82012-03-01 05:57:03 -0800365
David James58e0c092012-03-04 20:31:12 -0800366 # We can not run hw tests without archiving the payloads.
367 if self.options.archive:
David James944a48e2012-03-07 12:19:03 -0800368 for suite in config['hw_tests']:
369 stage_list.append([stages.HWTestStage, board, archive_stage, suite])
Chris Sosab50dc932012-03-01 14:00:58 -0800370
David James944a48e2012-03-07 12:19:03 -0800371 steps = [self._GetStageInstance(*x, config=config).Run for x in stage_list]
372 background.RunParallelSteps(steps + [archive_stage.Run])
Brian Harring3fec5a82012-03-01 05:57:03 -0800373
374 def RunStages(self):
375 """Runs through build process."""
376 self._RunStage(stages.BuildBoardStage)
377
378 # TODO(sosa): Split these out into classes.
Brian Harring3fec5a82012-03-01 05:57:03 -0800379 if self.build_config['build_type'] == constants.CHROOT_BUILDER_TYPE:
380 self._RunStage(stages.SDKTestStage)
381 self._RunStage(stages.UploadPrebuiltsStage,
Chris Sosa1dc96132012-05-11 15:40:50 -0700382 constants.CHROOT_BUILDER_BOARD, None)
Brian Harring3fec5a82012-03-01 05:57:03 -0800383 elif self.build_config['build_type'] == constants.REFRESH_PACKAGES_TYPE:
384 self._RunStage(stages.RefreshPackageStatusStage)
385 else:
386 self._RunStage(stages.UprevStage)
Brian Harring3fec5a82012-03-01 05:57:03 -0800387
David James944a48e2012-03-07 12:19:03 -0800388 configs = self.build_config['board_specific_configs']
David James58e0c092012-03-04 20:31:12 -0800389 for board in self.build_config['boards']:
David James944a48e2012-03-07 12:19:03 -0800390 config = configs.get(board, self.build_config)
391 archive_stage = self._GetStageInstance(stages.ArchiveStage, board,
392 config=config)
David James58e0c092012-03-04 20:31:12 -0800393 self.archive_stages[board] = archive_stage
394
David James944a48e2012-03-07 12:19:03 -0800395 # Set up a process pool to run test/archive stages in the background.
396 # This process runs task(board) for each board added to the queue.
David James58e0c092012-03-04 20:31:12 -0800397 queue = multiprocessing.Queue()
398 task = self._RunBackgroundStagesForBoard
399 with background.BackgroundTaskRunner(queue, task):
David James944a48e2012-03-07 12:19:03 -0800400 for board in self.build_config['boards']:
David James58e0c092012-03-04 20:31:12 -0800401 # Run BuildTarget in the foreground.
David James944a48e2012-03-07 12:19:03 -0800402 archive_stage = self.archive_stages[board]
403 config = configs.get(board, self.build_config)
404 self._RunStage(stages.BuildTargetStage, board, archive_stage,
Chris Sosa1a87b3e2012-04-12 13:20:42 -0700405 self.release_tag, config=config)
David James58e0c092012-03-04 20:31:12 -0800406 self.archive_urls[board] = archive_stage.GetDownloadUrl()
407
David James944a48e2012-03-07 12:19:03 -0800408 # Kick off task(board) in the background.
David James58e0c092012-03-04 20:31:12 -0800409 queue.put([board])
410
Brian Harring3fec5a82012-03-01 05:57:03 -0800411
412class DistributedBuilder(SimpleBuilder):
413 """Build class that has special logic to handle distributed builds.
414
415 These builds sync using git/manifest logic in manifest_versions. In general
416 they use a non-distributed builder code for the bulk of the work.
417 """
David James944a48e2012-03-07 12:19:03 -0800418 def __init__(self, options, build_config):
Brian Harring3fec5a82012-03-01 05:57:03 -0800419 """Initializes a buildbot builder.
420
421 Extra variables:
422 completion_stage_class: Stage used to complete a build. Set in the Sync
423 stage.
424 """
David James944a48e2012-03-07 12:19:03 -0800425 super(DistributedBuilder, self).__init__(options, build_config)
Brian Harring3fec5a82012-03-01 05:57:03 -0800426 self.completion_stage_class = None
427
428 def GetSyncInstance(self):
429 """Syncs the tree using one of the distributed sync logic paths.
430
431 Returns: the instance of the sync stage that was run.
432 """
433 # Determine sync class to use. CQ overrides PFQ bits so should check it
434 # first.
435 if cbuildbot_config.IsCQType(self.build_config['build_type']):
436 sync_stage = self._GetStageInstance(stages.CommitQueueSyncStage)
437 self.completion_stage_class = stages.CommitQueueCompletionStage
438 elif cbuildbot_config.IsPFQType(self.build_config['build_type']):
439 sync_stage = self._GetStageInstance(stages.LKGMCandidateSyncStage)
440 self.completion_stage_class = stages.LKGMCandidateSyncCompletionStage
441 else:
442 sync_stage = self._GetStageInstance(stages.ManifestVersionedSyncStage)
443 self.completion_stage_class = stages.ManifestVersionedSyncCompletionStage
444
445 return sync_stage
446
447 def Publish(self, was_build_successful):
448 """Completes build by publishing any required information."""
449 completion_stage = self._GetStageInstance(self.completion_stage_class,
450 was_build_successful)
451 completion_stage.Run()
452 name = completion_stage.name
453 if not results_lib.Results.WasStageSuccessful(name):
454 should_publish_changes = False
455 else:
456 should_publish_changes = (self.build_config['master'] and
457 was_build_successful)
458
459 if should_publish_changes:
460 self._RunStage(stages.PublishUprevChangesStage)
461
462 def RunStages(self):
463 """Runs simple builder logic and publishes information to overlays."""
464 was_build_successful = False
465 try:
David Jamesf55709e2012-03-13 09:10:15 -0700466 super(DistributedBuilder, self).RunStages()
467 was_build_successful = results_lib.Results.BuildSucceededSoFar()
Brian Harring3fec5a82012-03-01 05:57:03 -0800468 except SystemExit as ex:
469 # If a stage calls sys.exit(0), it's exiting with success, so that means
470 # we should mark ourselves as successful.
471 if ex.code == 0:
472 was_build_successful = True
473 raise
474 finally:
475 self.Publish(was_build_successful)
476
Brian Harring3fec5a82012-03-01 05:57:03 -0800477
478def _ConfirmBuildRoot(buildroot):
479 """Confirm with user the inferred buildroot, and mark it as confirmed."""
480 warning = 'Using default directory %s as buildroot' % buildroot
481 response = cros_lib.YesNoPrompt(default=cros_lib.NO, warning=warning,
482 full=True)
483 if response == cros_lib.NO:
484 print('Please specify a buildroot with the --buildroot option.')
485 sys.exit(0)
486
487 if not os.path.exists(buildroot):
488 os.mkdir(buildroot)
489
490 repository.CreateTrybotMarker(buildroot)
491
492
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700493def _ConfirmRemoteBuildbotRun():
494 """Confirm user wants to run with --buildbot --remote."""
495 warning = ('You are about to launch a PRODUCTION job! This is *NOT* a '
496 'trybot run! Are you sure?')
497 response = cros_lib.YesNoPrompt(default=cros_lib.NO, warning=warning,
498 full=True)
499
500 if response == cros_lib.NO:
501 print('Please specify --pass-through="--debug".')
502 sys.exit(0)
503
504
Brian Harring3fec5a82012-03-01 05:57:03 -0800505def _DetermineDefaultBuildRoot(internal_build):
506 """Default buildroot to be under the directory that contains current checkout.
507
508 Arguments:
509 internal_build: Whether the build is an internal build
510 """
511 repo_dir = cros_lib.FindRepoDir()
512 if not repo_dir:
513 cros_lib.Die('Could not find root of local checkout. Please specify'
514 'using --buildroot option.')
515
516 # Place trybot buildroot under the directory containing current checkout.
517 top_level = os.path.dirname(os.path.realpath(os.path.dirname(repo_dir)))
518 if internal_build:
519 buildroot = os.path.join(top_level, _DEFAULT_INT_BUILDROOT)
520 else:
521 buildroot = os.path.join(top_level, _DEFAULT_EXT_BUILDROOT)
522
523 return buildroot
524
525
526def _BackupPreviousLog(log_file, backup_limit=25):
527 """Rename previous log.
528
529 Args:
530 log_file: The absolute path to the previous log.
531 """
532 if os.path.exists(log_file):
533 old_logs = sorted(glob.glob(log_file + '.*'),
534 key=distutils.version.LooseVersion)
535
536 if len(old_logs) >= backup_limit:
537 os.remove(old_logs[0])
538
539 last = 0
540 if old_logs:
541 last = int(old_logs.pop().rpartition('.')[2])
542
543 os.rename(log_file, log_file + '.' + str(last + 1))
544
545
David James944a48e2012-03-07 12:19:03 -0800546def _RunBuildStagesWrapper(options, build_config):
Brian Harring3fec5a82012-03-01 05:57:03 -0800547 """Helper function that wraps RunBuildStages()."""
548 def IsDistributedBuilder():
549 """Determines whether the build_config should be a DistributedBuilder."""
550 if not options.buildbot:
551 return False
552 elif build_config['build_type'] in _DISTRIBUTED_TYPES:
553 chrome_rev = build_config['chrome_rev']
554 if options.chrome_rev: chrome_rev = options.chrome_rev
555 # We don't do distributed logic to TOT Chrome PFQ's, nor local
556 # chrome roots (e.g. chrome try bots)
557 if chrome_rev not in [constants.CHROME_REV_TOT,
558 constants.CHROME_REV_LOCAL,
559 constants.CHROME_REV_SPEC]:
560 return True
561
562 return False
563
564 # Start tee-ing output to file.
565 log_file = None
566 if options.tee:
567 default_dir = os.path.join(options.buildroot, _DEFAULT_LOG_DIR)
568 dirname = options.log_dir or default_dir
569 log_file = os.path.join(dirname, _BUILDBOT_LOG_FILE)
570
571 cros_lib.SafeMakedirs(dirname)
572 _BackupPreviousLog(log_file)
573
574 try:
575 with cros_lib.AllowDisabling(options.tee, tee.Tee, log_file):
576 cros_lib.Info("cbuildbot executed with args %s"
577 % ' '.join(map(repr, sys.argv)))
578 if IsDistributedBuilder():
David James944a48e2012-03-07 12:19:03 -0800579 buildbot = DistributedBuilder(options, build_config)
Brian Harring3fec5a82012-03-01 05:57:03 -0800580 else:
David James944a48e2012-03-07 12:19:03 -0800581 buildbot = SimpleBuilder(options, build_config)
Brian Harring3fec5a82012-03-01 05:57:03 -0800582
583 if not buildbot.Run():
584 sys.exit(1)
585 finally:
586 if options.tee:
587 cros_lib.Info('Output should be saved to %s' % log_file)
588
589
590# Parser related functions
Ryan Cuicedd8a52012-03-22 02:28:35 -0700591def _CheckLocalPatches(local_patches):
Brian Harring3fec5a82012-03-01 05:57:03 -0800592 """Do an early quick check of the passed-in patches.
593
594 If the branch of a project is not specified we append the current branch the
595 project is on.
596 """
Ryan Cuicedd8a52012-03-22 02:28:35 -0700597 verified_patches = []
598 for patch in local_patches:
Brian Harring3fec5a82012-03-01 05:57:03 -0800599 components = patch.split(':')
600 if len(components) > 2:
601 msg = 'Specify local patches in project[:branch] format.'
602 raise optparse.OptionValueError(msg)
603
604 # validate project
605 project = components[0]
606 if not cros_lib.DoesProjectExist('.', project):
607 raise optparse.OptionValueError('Project %s does not exist.' % project)
608
609 project_dir = cros_lib.GetProjectDir('.', project)
610
611 # If no branch was specified, we use the project's current branch.
612 if len(components) == 1:
613 branch = cros_lib.GetCurrentBranch(project_dir)
614 if not branch:
615 raise optparse.OptionValueError('project %s is not on a branch!'
616 % project)
617 # Append branch information to patch
618 patch = '%s:%s' % (project, branch)
619 else:
620 branch = components[1]
621 if not cros_lib.DoesLocalBranchExist(project_dir, branch):
622 raise optparse.OptionValueError('Project %s does not have branch %s'
623 % (project, branch))
624
Ryan Cuicedd8a52012-03-22 02:28:35 -0700625 verified_patches.append(patch)
Brian Harring3fec5a82012-03-01 05:57:03 -0800626
Ryan Cuicedd8a52012-03-22 02:28:35 -0700627 return verified_patches
Brian Harring3fec5a82012-03-01 05:57:03 -0800628
629
630def _CheckBuildRootOption(_option, _opt_str, value, parser):
631 """Validate and convert buildroot to full-path form."""
632 value = value.strip()
633 if not value or value == '/':
634 raise optparse.OptionValueError('Invalid buildroot specified')
635
636 parser.values.buildroot = os.path.realpath(os.path.expanduser(value))
637
638
639def _CheckLogDirOption(_option, _opt_str, value, parser):
640 """Validate and convert buildroot to full-path form."""
641 parser.values.log_dir = os.path.abspath(os.path.expanduser(value))
642
643
644def _CheckChromeVersionOption(_option, _opt_str, value, parser):
645 """Upgrade other options based on chrome_version being passed."""
646 value = value.strip()
647
648 if parser.values.chrome_rev is None and value:
649 parser.values.chrome_rev = constants.CHROME_REV_SPEC
650
651 parser.values.chrome_version = value
652
653
654def _CheckChromeRootOption(_option, _opt_str, value, parser):
655 """Validate and convert chrome_root to full-path form."""
656 value = value.strip()
657 if not value or value == '/':
658 raise optparse.OptionValueError('Invalid chrome_root specified')
659
660 if parser.values.chrome_rev is None:
661 parser.values.chrome_rev = constants.CHROME_REV_LOCAL
662
663 parser.values.chrome_root = os.path.realpath(os.path.expanduser(value))
664
665
666def _CheckChromeRevOption(_option, _opt_str, value, parser):
667 """Validate the chrome_rev option."""
668 value = value.strip()
669 if value not in constants.VALID_CHROME_REVISIONS:
670 raise optparse.OptionValueError('Invalid chrome rev specified')
671
672 parser.values.chrome_rev = value
673
674
675def _CreateParser():
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700676 class CustomParser(optparse.OptionParser):
677 def add_remote_option(self, *args, **kwargs):
678 """For arguments that are passed-through to remote trybot."""
679 return optparse.OptionParser.add_option(self, *args,
680 remote_pass_through=True,
681 **kwargs)
682
683 class CustomGroup(optparse.OptionGroup):
684 def add_remote_option(self, *args, **kwargs):
685 """For arguments that are passed-through to remote trybot."""
686 return optparse.OptionGroup.add_option(self, *args,
687 remote_pass_through=True,
688 **kwargs)
689
690 class CustomOption(optparse.Option):
691 """Subclass Option class to implement pass-through."""
692 def __init__(self, *args, **kwargs):
693 # The remote_pass_through argument specifies whether we should directly
694 # pass the argument (with its value) onto the remote trybot.
695 self.pass_through = kwargs.pop('remote_pass_through', False)
696 optparse.Option.__init__(self, *args, **kwargs)
697
698 def take_action(self, action, dest, opt, value, values, parser):
699 optparse.Option.take_action(self, action, dest, opt, value, values,
700 parser)
701 if self.pass_through:
702 parser.values.pass_through_args.append(opt)
703 if self.nargs and self.nargs > 1:
704 # value is a tuple if nargs > 1
705 string_list = [str(val) for val in list(value)]
706 parser.values.pass_through_args.extend(string_list)
707 elif value:
708 parser.values.pass_through_args.append(str(value))
709
Brian Harring3fec5a82012-03-01 05:57:03 -0800710 """Generate and return the parser with all the options."""
711 # Parse options
712 usage = "usage: %prog [options] buildbot_config"
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700713 parser = CustomParser(usage=usage, option_class=CustomOption)
Brian Harring3fec5a82012-03-01 05:57:03 -0800714
715 # Main options
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700716 # The remote_pass_through parameter to add_option is implemented by the
717 # CustomOption class. See CustomOption for more information.
Brian Harring3fec5a82012-03-01 05:57:03 -0800718 parser.add_option('-a', '--all', action='store_true', dest='print_all',
719 default=False,
720 help=('List all of the buildbot configs available. Use '
721 'with the --list option'))
722 parser.add_option('-r', '--buildroot', action='callback', dest='buildroot',
723 type='string', callback=_CheckBuildRootOption,
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700724 help='Root directory where source is checked out to, and '
725 'where the build occurs. For external build configs, '
726 "defaults to 'trybot' directory at top level of your "
727 'repo-managed checkout.')
728 parser.add_remote_option('--chrome_rev', default=None, type='string',
729 action='callback', dest='chrome_rev',
730 callback=_CheckChromeRevOption,
731 help=('Revision of Chrome to use, of type [%s]'
732 % '|'.join(constants.VALID_CHROME_REVISIONS)))
733 parser.add_remote_option('-g', '--gerrit-patches', action='append',
734 default=[], type='string',
735 metavar="'Id1 *int_Id2...IdN'",
736 help=("Space-separated list of short-form Gerrit "
737 "Change-Id's or change numbers to patch. "
738 "Please prepend '*' to internal Change-Id's"))
Brian Harring3fec5a82012-03-01 05:57:03 -0800739 parser.add_option('-l', '--list', action='store_true', dest='list',
740 default=False,
741 help=('List the suggested trybot configs to use. Use '
742 '--all to list all of the available configs.'))
Ryan Cui54da0702012-04-19 18:38:08 -0700743 parser.add_option('--local', default=False, action='store_true',
744 help=('Specifies that this tryjob should be run locally.'))
Ryan Cuicedd8a52012-03-22 02:28:35 -0700745 parser.add_option('-p', '--local-patches', action='append', default=[],
Brian Harring3fec5a82012-03-01 05:57:03 -0800746 metavar="'<project1>[:<branch1>]...<projectN>[:<branchN>]'",
747 help=('Space-separated list of project branches with '
748 'patches to apply. Projects are specified by name. '
749 'If no branch is specified the current branch of the '
750 'project will be used.'))
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700751 parser.add_remote_option('--profile', default=None, type='string',
752 action='store', dest='profile',
753 help='Name of profile to sub-specify board variant.')
Brian Harring3fec5a82012-03-01 05:57:03 -0800754 parser.add_option('--remote', default=False, action='store_true',
Brian Harring3fec5a82012-03-01 05:57:03 -0800755 help=('Specifies that this tryjob should be run remotely.'))
756
757 # Advanced options
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700758 group = CustomGroup(
Brian Harring3fec5a82012-03-01 05:57:03 -0800759 parser,
760 'Advanced Options',
761 'Caution: use these options at your own risk.')
762
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700763 group.add_remote_option('--buildbot', dest='buildbot', action='store_true',
764 default=False, help='This is running on a buildbot')
765 group.add_remote_option('--buildnumber', help='build number', type='int',
766 default=0)
Brian Harring3fec5a82012-03-01 05:57:03 -0800767 group.add_option('--chrome_root', default=None, type='string',
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700768 action='callback', dest='chrome_root',
769 callback=_CheckChromeRootOption,
770 help='Local checkout of Chrome to use.')
771 group.add_remote_option('--chrome_version', default=None, type='string',
772 action='callback', dest='chrome_version',
773 callback=_CheckChromeVersionOption,
774 help='Used with SPEC logic to force a particular SVN '
775 'revision of chrome rather than the latest.')
776 group.add_remote_option('--clobber', action='store_true', dest='clobber',
777 default=False,
778 help='Clears an old checkout before syncing')
779 group.add_remote_option('--lkgm', action='store_true', dest='lkgm',
780 default=False,
781 help='Sync to last known good manifest blessed by '
782 'PFQ')
Brian Harring3fec5a82012-03-01 05:57:03 -0800783 parser.add_option('--log_dir', action='callback', dest='log_dir',
784 type='string', callback=_CheckLogDirOption,
785 help=('Directory where logs are stored.'))
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700786 group.add_remote_option('--maxarchives', dest='max_archive_builds',
787 default=3, type='int',
788 help="Change the local saved build count limit.")
789 group.add_remote_option('--noarchive', action='store_false', dest='archive',
790 default=True, help="Don't run archive stage.")
791 group.add_remote_option('--nobuild', action='store_false', dest='build',
792 default=True,
793 help="Don't actually build (for cbuildbot dev)")
794 group.add_remote_option('--noclean', action='store_false', dest='clean',
795 default=True, help="Don't clean the buildroot")
796 group.add_remote_option('--noprebuilts', action='store_false',
797 dest='prebuilts', default=True,
798 help="Don't upload prebuilts.")
799 group.add_remote_option('--nosync', action='store_false', dest='sync',
800 default=True, help="Don't sync before building.")
801 group.add_remote_option('--nocgroups', action='store_false', dest='cgroups',
802 default=True,
803 help='Disable cbuildbots usage of cgroups.')
804 group.add_remote_option('--notests', action='store_false', dest='tests',
805 default=True,
806 help='Override values from buildconfig and run no '
807 'tests.')
808 group.add_remote_option('--nouprev', action='store_false', dest='uprev',
809 default=True,
810 help='Override values from buildconfig and never '
811 'uprev.')
812 group.add_option('--pass-through', dest='pass_through_args', action='append',
813 type='string', default=[], help=optparse.SUPPRESS_HELP)
Brian Harring3fec5a82012-03-01 05:57:03 -0800814 group.add_option('--reference-repo', action='store', default=None,
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700815 dest='reference_repo',
816 help='Reuse git data stored in an existing repo '
817 'checkout. This can drastically reduce the network '
818 'time spent setting up the trybot checkout. By '
819 "default, if this option isn't given but cbuildbot "
820 'is invoked from a repo checkout, cbuildbot will '
821 'use the repo root.')
822 # Indicates this is running on a remote trybot machine.
Ryan Cuiba41ad32012-03-08 17:15:29 -0800823 group.add_option('--remote-trybot', dest='remote_trybot', action='store_true',
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700824 default=False, help=optparse.SUPPRESS_HELP)
Ryan Cuicedd8a52012-03-22 02:28:35 -0700825 # Patches uploaded by trybot client when run using the -p option.
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700826 group.add_remote_option('--remote-patches', action='append', default=[],
827 help=optparse.SUPPRESS_HELP)
Ryan Cuicedd8a52012-03-22 02:28:35 -0700828 group.add_option('--resume', action='store_true', default=False,
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700829 help='Skip stages already successfully completed.')
830 group.add_remote_option('--timeout', action='store', type='int', default=0,
831 help='Specify the maximum amount of time this job '
832 'can run for, at which point the build will be '
833 'aborted. If set to zero, then there is no '
834 'timeout.')
Ryan Cui39bdbbf2012-02-29 16:15:39 -0800835 group.add_option('--test-tryjob', action='store_true',
836 default=False,
837 help='Submit a tryjob to the test repository. Will not '
838 'show up on the production trybot waterfall.')
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700839 group.add_remote_option('--validation_pool', default=None,
840 help='Path to a pickled validation pool. Intended '
841 'for use only with the commit queue.')
842 group.add_remote_option('--version', dest='force_version', default=None,
843 help='Used with manifest logic. Forces use of this '
844 'version rather than create or get latest.')
Brian Harring3fec5a82012-03-01 05:57:03 -0800845
846 parser.add_option_group(group)
847
848 # Debug options
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700849 group = CustomGroup(parser, "Debug Options")
Brian Harring3fec5a82012-03-01 05:57:03 -0800850
Ryan Cui85867972012-02-23 18:21:49 -0800851 group.add_option('--debug', action='store_true', default=None,
Brian Harring3fec5a82012-03-01 05:57:03 -0800852 help='Override some options to run as a developer.')
853 group.add_option('--dump_config', action='store_true', dest='dump_config',
854 default=False,
855 help='Dump out build config options, and exit.')
856 group.add_option('--notee', action='store_false', dest='tee', default=True,
857 help="Disable logging and internal tee process. Primarily "
858 "used for debugging cbuildbot itself.")
859 parser.add_option_group(group)
860 return parser
861
862
Ryan Cui85867972012-02-23 18:21:49 -0800863def _FinishParsing(options, args):
864 """Perform some parsing tasks that need to take place after optparse.
865
866 This function needs to be easily testable! Keep it free of
867 environment-dependent code. Put more detailed usage validation in
868 _PostParseCheck().
Brian Harring3fec5a82012-03-01 05:57:03 -0800869
870 Args:
Ryan Cui85867972012-02-23 18:21:49 -0800871 options, args: The options/args object returned by optparse
Brian Harring3fec5a82012-03-01 05:57:03 -0800872 """
Brian Harring3fec5a82012-03-01 05:57:03 -0800873 if options.chrome_root:
874 if options.chrome_rev != constants.CHROME_REV_LOCAL:
875 cros_lib.Die('Chrome rev must be %s if chrome_root is set.' %
876 constants.CHROME_REV_LOCAL)
877 else:
878 if options.chrome_rev == constants.CHROME_REV_LOCAL:
879 cros_lib.Die('Chrome root must be set if chrome_rev is %s.' %
880 constants.CHROME_REV_LOCAL)
881
882 if options.chrome_version:
883 if options.chrome_rev != constants.CHROME_REV_SPEC:
884 cros_lib.Die('Chrome rev must be %s if chrome_version is set.' %
885 constants.CHROME_REV_SPEC)
886 else:
887 if options.chrome_rev == constants.CHROME_REV_SPEC:
888 cros_lib.Die('Chrome rev must not be %s if chrome_version is not set.' %
889 constants.CHROME_REV_SPEC)
890
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700891 patches = bool(options.gerrit_patches or options.local_patches)
892 if options.remote:
893 if options.local:
894 cros_lib.Die('Cannot specify both --remote and --local')
Ryan Cui54da0702012-04-19 18:38:08 -0700895
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700896 if not options.buildbot and not patches:
897 cros_lib.Die('Must provide patches when running with --remote.')
Brian Harring3fec5a82012-03-01 05:57:03 -0800898
Ryan Cuieaa9efd2012-04-25 17:56:45 -0700899 # --debug needs to be explicitly passed through for remote invocations.
900 release_mode_with_patches = (options.buildbot and patches and
901 '--debug' not in options.pass_through_args)
902 else:
903 if len(args) > 1:
904 cros_lib.Die('Multiple configs not supported if not running with '
905 '--remote.')
906
907 release_mode_with_patches = (options.buildbot and patches and
908 not options.debug)
909
910 # When running in release mode, make sure we are running with checked-in code.
911 # We want checked-in cbuildbot/scripts to prevent errors, and we want to build
912 # a release image with checked-in code for CrOS packages.
913 if release_mode_with_patches:
914 cros_lib.Die('Cannot provide patches when running with --buildbot!')
Brian Harring3fec5a82012-03-01 05:57:03 -0800915
Ryan Cuiba41ad32012-03-08 17:15:29 -0800916 if options.buildbot and options.remote_trybot:
917 cros_lib.Die('--buildbot and --remote-trybot cannot be used together.')
918
Ryan Cui85867972012-02-23 18:21:49 -0800919 # Record whether --debug was set explicitly vs. it was inferred.
920 options.debug_forced = False
921 if options.debug:
922 options.debug_forced = True
923 else:
Ryan Cui16ca5812012-03-08 20:34:27 -0800924 # We don't set debug by default for
925 # 1. --buildbot invocations.
926 # 2. --remote invocations, because it needs to push changes to the tryjob
927 # repo.
928 options.debug = not options.buildbot and not options.remote
Brian Harring3fec5a82012-03-01 05:57:03 -0800929
Brian Harring3fec5a82012-03-01 05:57:03 -0800930
Ryan Cuicedd8a52012-03-22 02:28:35 -0700931def _SplitAndFlatten(appended_items):
932 """Given a list of space-separated items, split into flattened list.
933
934 Given ['abc def', 'hij'] return ['abc', 'def', 'hij'].
935 Arguments:
936 appended_items: List of delimiter-separated items.
937
938 Returns: Flattened list.
939 """
940 new_list = []
941 for item in appended_items:
Mike Frysinger4bd23892012-03-26 15:08:52 -0400942 new_list.extend(item.split())
Ryan Cuicedd8a52012-03-22 02:28:35 -0700943 return new_list
944
945
Brian Harring1d7ba942012-04-24 06:37:18 -0700946# pylint: disable=W0613
Ryan Cui85867972012-02-23 18:21:49 -0800947def _PostParseCheck(options, args):
948 """Perform some usage validation after we've parsed the arguments
Brian Harring3fec5a82012-03-01 05:57:03 -0800949
Ryan Cui85867972012-02-23 18:21:49 -0800950 Args:
951 options/args: The options/args object returned by optparse
952 """
Brian Harring1d7ba942012-04-24 06:37:18 -0700953 if options.resume:
954 return
955
956 options.gerrit_patches = _SplitAndFlatten(options.gerrit_patches)
957 options.remote_patches = _SplitAndFlatten(options.remote_patches)
958 try:
959 # TODO(rcui): Split this into two stages, one that parses, another that
960 # validates. Parsing step will be called by _FinishParsing().
961 options.local_patches = _CheckLocalPatches(
962 _SplitAndFlatten(options.local_patches))
963 except optparse.OptionValueError as e:
964 cros_lib.Die(str(e))
965
966 default = os.environ.get('CBUILDBOT_DEFAULT_MODE')
967 if (default and not any([options.local, options.buildbot,
968 options.remote, options.remote_trybot])):
969 cros_lib.Info("CBUILDBOT_DEFAULT_MODE=%s env var detected, using it."
970 % default)
971 default = default.lower()
972 if default == 'local':
973 options.local = True
974 elif default == 'remote':
975 options.remote = True
976 elif default == 'buildbot':
977 options.buildbot = True
978 else:
979 cros_lib.Die("CBUILDBOT_DEFAULT_MODE value %s isn't supported. "
980 % default)
Ryan Cui85867972012-02-23 18:21:49 -0800981
982
983def _ParseCommandLine(parser, argv):
984 """Completely parse the commandline arguments"""
Brian Harring3fec5a82012-03-01 05:57:03 -0800985 (options, args) = parser.parse_args(argv)
Ryan Cui54da0702012-04-19 18:38:08 -0700986 if options.list:
987 _PrintValidConfigs(options.print_all)
988 sys.exit(0)
989
Ryan Cui8be16062012-04-24 12:05:26 -0700990 # Strip out null arguments.
991 # TODO(rcui): Remove when buildbot is fixed
992 args = [arg for arg in args if arg]
993 if not args:
994 parser.error('Invalid usage. Use -h to see usage. Use -l to list '
995 'supported configs.')
996
Ryan Cui85867972012-02-23 18:21:49 -0800997 _FinishParsing(options, args)
998 return options, args
999
1000
1001def main(argv):
1002 # Set umask to 022 so files created by buildbot are readable.
1003 os.umask(022)
1004
1005 if cros_lib.IsInsideChroot():
1006 cros_lib.Die('Please run cbuildbot from outside the chroot.')
1007
1008 parser = _CreateParser()
1009 (options, args) = _ParseCommandLine(parser, argv)
Brian Harring3fec5a82012-03-01 05:57:03 -08001010
Brian Harring3fec5a82012-03-01 05:57:03 -08001011 _PostParseCheck(options, args)
1012
1013 if options.remote:
Chris Sosa4f6ffaf2012-05-01 17:05:44 -07001014 cros_lib.logger.setLevel(logging.WARNING)
Ryan Cui16ca5812012-03-08 20:34:27 -08001015
Brian Harring3fec5a82012-03-01 05:57:03 -08001016 # Verify configs are valid.
1017 for bot in args:
1018 _GetConfig(bot)
1019
1020 # Verify gerrit patches are valid.
Ryan Cui16ca5812012-03-08 20:34:27 -08001021 print 'Verifying patches...'
Ryan Cuicedd8a52012-03-22 02:28:35 -07001022 _, local_patches = _PreProcessPatches(options.gerrit_patches,
1023 options.local_patches)
Ryan Cuieaa9efd2012-04-25 17:56:45 -07001024 # --debug need to be explicitly passed through for remote invocations.
1025 if options.buildbot and '--debug' not in options.pass_through_args:
1026 _ConfirmRemoteBuildbotRun()
1027
Ryan Cui16ca5812012-03-08 20:34:27 -08001028 print 'Submitting tryjob...'
Ryan Cuicedd8a52012-03-22 02:28:35 -07001029 tryjob = remote_try.RemoteTryJob(options, args, local_patches)
Ryan Cui39bdbbf2012-02-29 16:15:39 -08001030 tryjob.Submit(testjob=options.test_tryjob, dryrun=options.debug)
Ryan Cui16ca5812012-03-08 20:34:27 -08001031 print 'Tryjob submitted!'
1032 print ('Go to %s to view the status of your job.'
Ryan Cui4906e1c2012-04-03 20:09:34 -07001033 % tryjob.GetTrybotWaterfallLink())
Brian Harring3fec5a82012-03-01 05:57:03 -08001034 sys.exit(0)
Ryan Cui54da0702012-04-19 18:38:08 -07001035 elif (not options.buildbot and not options.remote_trybot
1036 and not options.resume and not options.local):
1037 cros_lib.Warning('Running in LOCAL TRYBOT mode! Use --remote to submit '
1038 'REMOTE tryjobs. Use --local to suppress this message.')
1039 cros_lib.Warning('Starting April 30th, --local will be required to run the '
1040 'local trybot.')
1041 time.sleep(5)
Brian Harring3fec5a82012-03-01 05:57:03 -08001042
Ryan Cui8be16062012-04-24 12:05:26 -07001043 # Only expecting one config
1044 bot_id = args[-1]
1045 build_config = _GetConfig(bot_id)
Brian Harring3fec5a82012-03-01 05:57:03 -08001046
1047 if options.reference_repo is None:
1048 repo_path = os.path.join(constants.SOURCE_ROOT, '.repo')
1049 # If we're being run from a repo checkout, reuse the repo's git pool to
1050 # cut down on sync time.
1051 if os.path.exists(repo_path):
1052 options.reference_repo = constants.SOURCE_ROOT
1053 elif options.reference_repo:
1054 if not os.path.exists(options.reference_repo):
1055 parser.error('Reference path %s does not exist'
1056 % (options.reference_repo,))
1057 elif not os.path.exists(os.path.join(options.reference_repo, '.repo')):
1058 parser.error('Reference path %s does not look to be the base of a '
1059 'repo checkout; no .repo exists in the root.'
1060 % (options.reference_repo,))
Ryan Cuid4a24212012-04-04 18:08:12 -07001061
1062 if options.buildbot or options.remote_trybot:
Brian Harring470f6112012-03-02 11:47:10 -08001063 if not options.cgroups:
Ryan Cuid4a24212012-04-04 18:08:12 -07001064 parser.error('Options --buildbot/--remote-trybot and --nocgroups cannot '
1065 'be used together. Cgroup support is required for '
1066 'buildbot/remote-trybot mode.')
Brian Harring470f6112012-03-02 11:47:10 -08001067 if not cgroups.Cgroup.CgroupsSupported():
Ryan Cuid4a24212012-04-04 18:08:12 -07001068 parser.error('Option --buildbot/--remote-trybot was given, but this '
1069 'system does not support cgroups. Failing.')
Brian Harring3fec5a82012-03-01 05:57:03 -08001070
Brian Harring351ce442012-03-09 16:38:14 -08001071 missing = []
1072 for program in _BUILDBOT_REQUIRED_BINARIES:
1073 ret = cros_lib.RunCommand('which %s' % program, shell=True,
1074 redirect_stderr=True, redirect_stdout=True,
1075 error_code_ok=True, print_cmd=False)
1076 if ret.returncode != 0:
1077 missing.append(program)
1078
1079 if missing:
Ryan Cuid4a24212012-04-04 18:08:12 -07001080 parser.error("Option --buildbot/--remote-trybot requires the following "
1081 "binaries which couldn't be found in $PATH: %s"
Brian Harring351ce442012-03-09 16:38:14 -08001082 % (', '.join(missing)))
1083
Brian Harring3fec5a82012-03-01 05:57:03 -08001084 if options.reference_repo:
1085 options.reference_repo = os.path.abspath(options.reference_repo)
1086
1087 if options.dump_config:
1088 # This works, but option ordering is bad...
1089 print 'Configuration %s:' % bot_id
1090 pretty_printer = pprint.PrettyPrinter(indent=2)
1091 pretty_printer.pprint(build_config)
1092 sys.exit(0)
1093
1094 if not options.buildroot:
1095 if options.buildbot:
1096 parser.error('Please specify a buildroot with the --buildroot option.')
Matt Tennantd55b1f42012-04-13 14:15:01 -07001097
Brian Harring470f6112012-03-02 11:47:10 -08001098 options.buildroot = _DetermineDefaultBuildRoot(build_config['internal'])
1099 # We use a marker file in the buildroot to indicate the user has
1100 # consented to using this directory.
1101 if not os.path.exists(repository.GetTrybotMarkerPath(options.buildroot)):
1102 _ConfirmBuildRoot(options.buildroot)
Brian Harring3fec5a82012-03-01 05:57:03 -08001103
1104 # Sanity check of buildroot- specifically that it's not pointing into the
1105 # midst of an existing repo since git-repo doesn't support nesting.
Brian Harring3fec5a82012-03-01 05:57:03 -08001106 if (not repository.IsARepoRoot(options.buildroot) and
David James6b80dc62012-02-29 15:34:40 -08001107 repository.InARepoRepository(options.buildroot)):
Brian Harring3fec5a82012-03-01 05:57:03 -08001108 parser.error('Configured buildroot %s points into a repository checkout, '
1109 'rather than the root of it. This is not supported.'
1110 % options.buildroot)
1111
Brian Harringa184efa2012-03-04 11:51:25 -08001112 with cleanup.EnforcedCleanupSection() as critical_section:
1113 with sudo.SudoKeepAlive():
1114 with cros_lib.AllowDisabling(options.cgroups,
Brian Harring4e6412d2012-03-09 20:54:02 -08001115 cgroups.SimpleContainChildren, 'cbuildbot'):
Brian Harringa184efa2012-03-04 11:51:25 -08001116 # Mark everything between EnforcedCleanupSection and here as having to
1117 # be rolled back via the contextmanager cleanup handlers. This ensures
1118 # that sudo bits cannot outlive cbuildbot, that anything cgroups
1119 # would kill gets killed, etc.
1120 critical_section.ForkWatchdog()
1121
1122 with cros_lib.AllowDisabling(options.timeout > 0,
1123 cros_lib.Timeout, options.timeout):
1124 if not options.buildbot:
1125 build_config = cbuildbot_config.OverrideConfigForTrybot(
Ryan Cui3d6b4742012-03-14 11:42:24 -07001126 build_config,
1127 options.remote_trybot)
Brian Harringa184efa2012-03-04 11:51:25 -08001128
1129 _RunBuildStagesWrapper(options, build_config)