Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 1 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Bootstrap for cbuildbot. |
| 6 | |
| 7 | This script is intended to checkout chromite on the branch specified by -b or |
| 8 | --branch (as normally accepted by cbuildbot), and then invoke cbuildbot. Most |
| 9 | arguments are not parsed, only passed along. If a branch is not specified, this |
| 10 | script will use 'master'. |
| 11 | |
| 12 | Among other things, this allows us to invoke build configs that exist on a given |
| 13 | branch, but not on TOT. |
| 14 | """ |
| 15 | |
| 16 | from __future__ import print_function |
| 17 | |
| 18 | import os |
| 19 | |
| 20 | from chromite.cbuildbot import repository |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 21 | from chromite.lib import config_lib |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 22 | from chromite.lib import cros_build_lib |
| 23 | from chromite.lib import cros_logging as logging |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 24 | from chromite.lib import osutils |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 25 | from chromite.scripts import cbuildbot |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 26 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 27 | def PreParseArguments(argv): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 28 | """Extract the branch name from cbuildbot command line arguments. |
| 29 | |
| 30 | Ignores all arguments, other than the branch name. |
| 31 | |
| 32 | Args: |
| 33 | argv: The command line arguments to parse. |
| 34 | |
| 35 | Returns: |
| 36 | Branch as a string ('master' if nothing is specified). |
| 37 | """ |
| 38 | # Must match cbuildbot._CreateParser(). |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 39 | parser = cbuildbot.CreateParser() |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 40 | |
| 41 | # Extract the branch argument, if present, ignore the rest. |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 42 | options, _ = parser.parse_args(argv) |
| 43 | |
| 44 | # This option isn't required for cbuildbot, but is for us. |
| 45 | if not options.buildroot: |
| 46 | cros_build_lib.Die('--buildroot is a required option.') |
| 47 | |
| 48 | return options |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 49 | |
| 50 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 51 | def InitialCheckout(branchname, buildroot, git_cache_dir): |
| 52 | """Preliminary ChromeOS checkout. |
| 53 | |
| 54 | Perform a complete checkout of ChromeOS on the specified branch. This does NOT |
| 55 | match what the build needs, but ensures the buildroot both has a 'hot' |
| 56 | checkout, and is close enough that the branched cbuildbot can successfully get |
| 57 | the right checkout. |
| 58 | |
| 59 | This checks out full ChromeOS, even if a ChromiumOS build is going to be |
| 60 | performed. This is because we have no knowledge of the build config to be |
| 61 | used. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 62 | |
| 63 | Args: |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 64 | branchname: Name of branch to checkout. None for no branch. |
| 65 | buildroot: Directory to checkout into. |
| 66 | git_cache_dir: Directory to use for git cache. None to not use it. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 67 | """ |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 68 | site_config = config_lib.GetConfig() |
| 69 | manifest_url = site_config.params['MANIFEST_INT_URL'] |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 70 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 71 | osutils.SafeMakedirs(buildroot) |
| 72 | repo = repository.RepoRepository(manifest_url, buildroot, |
| 73 | branch=branchname, |
| 74 | git_cache_dir=git_cache_dir) |
| 75 | repo.Sync() |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 76 | |
| 77 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 78 | def RunCbuildbot(buildroot, argv): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 79 | """Start cbuildbot in specified directory with all arguments. |
| 80 | |
| 81 | Args: |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 82 | buildroot: Root of ChromeOS checkout to run cbuildbot in. |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 83 | argv: All command line arguments to pass as list of strings. |
| 84 | |
| 85 | Returns: |
| 86 | Return code of cbuildbot as an integer. |
| 87 | """ |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 88 | logging.info('Bootstrap cbuildbot in: %s', buildroot) |
| 89 | cbuildbot_cmd = os.path.join(buildroot, 'chromite', 'bin', 'cbuildbot') |
| 90 | result = cros_build_lib.RunCommand([cbuildbot_cmd] + argv, |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 91 | error_code_ok=True, |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 92 | cwd=buildroot) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 93 | |
| 94 | logging.debug('cbuildbot result is: %s', result.returncode) |
| 95 | return result.returncode |
| 96 | |
| 97 | |
| 98 | def main(argv): |
| 99 | """main method of script. |
| 100 | |
| 101 | Args: |
| 102 | argv: All command line arguments to pass as list of strings. |
| 103 | |
| 104 | Returns: |
| 105 | Return code of cbuildbot as an integer. |
| 106 | """ |
| 107 | # Specified branch, or 'master' |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 108 | options = PreParseArguments(argv) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 109 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame^] | 110 | branchname = options.branch |
| 111 | buildroot = options.buildroot |
| 112 | git_cache_dir = options.git_cache_dir |
| 113 | |
| 114 | # Get a checkout close enough the branched cbuildbot can handle it. |
| 115 | InitialCheckout(branchname, buildroot, git_cache_dir) |
| 116 | |
| 117 | # Run cbuildbot inside the full ChromeOS checkout, on the specified branch. |
| 118 | RunCbuildbot(buildroot, argv) |