blob: 50df291f9ccce5fb1c3737b2beb89f4887b4ed8a [file] [log] [blame]
Don Garrettc4114cc2016-11-01 20:04:06 -07001# 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
7This 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
9arguments are not parsed, only passed along. If a branch is not specified, this
10script will use 'master'.
11
12Among other things, this allows us to invoke build configs that exist on a given
13branch, but not on TOT.
14"""
15
16from __future__ import print_function
17
18import os
19
20from chromite.cbuildbot import repository
Don Garrett86881cb2017-02-15 15:41:55 -080021from chromite.lib import config_lib
Don Garrettc4114cc2016-11-01 20:04:06 -070022from chromite.lib import cros_build_lib
23from chromite.lib import cros_logging as logging
Don Garrettc4114cc2016-11-01 20:04:06 -070024from chromite.lib import osutils
Don Garrett86881cb2017-02-15 15:41:55 -080025from chromite.scripts import cbuildbot
Don Garrettc4114cc2016-11-01 20:04:06 -070026
Don Garrett86881cb2017-02-15 15:41:55 -080027def PreParseArguments(argv):
Don Garrettc4114cc2016-11-01 20:04:06 -070028 """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 Garrett86881cb2017-02-15 15:41:55 -080039 parser = cbuildbot.CreateParser()
Don Garrettc4114cc2016-11-01 20:04:06 -070040
41 # Extract the branch argument, if present, ignore the rest.
Don Garrett86881cb2017-02-15 15:41:55 -080042 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 Garrettc4114cc2016-11-01 20:04:06 -070049
50
Don Garrett86881cb2017-02-15 15:41:55 -080051def 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 Garrettc4114cc2016-11-01 20:04:06 -070062
63 Args:
Don Garrett86881cb2017-02-15 15:41:55 -080064 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 Garrettc4114cc2016-11-01 20:04:06 -070067 """
Don Garrett86881cb2017-02-15 15:41:55 -080068 site_config = config_lib.GetConfig()
69 manifest_url = site_config.params['MANIFEST_INT_URL']
Don Garrettc4114cc2016-11-01 20:04:06 -070070
Don Garrett86881cb2017-02-15 15:41:55 -080071 osutils.SafeMakedirs(buildroot)
72 repo = repository.RepoRepository(manifest_url, buildroot,
73 branch=branchname,
74 git_cache_dir=git_cache_dir)
75 repo.Sync()
Don Garrettc4114cc2016-11-01 20:04:06 -070076
77
Don Garrett86881cb2017-02-15 15:41:55 -080078def RunCbuildbot(buildroot, argv):
Don Garrettc4114cc2016-11-01 20:04:06 -070079 """Start cbuildbot in specified directory with all arguments.
80
81 Args:
Don Garrett86881cb2017-02-15 15:41:55 -080082 buildroot: Root of ChromeOS checkout to run cbuildbot in.
Don Garrettc4114cc2016-11-01 20:04:06 -070083 argv: All command line arguments to pass as list of strings.
84
85 Returns:
86 Return code of cbuildbot as an integer.
87 """
Don Garrett86881cb2017-02-15 15:41:55 -080088 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 Garrettc4114cc2016-11-01 20:04:06 -070091 error_code_ok=True,
Don Garrett86881cb2017-02-15 15:41:55 -080092 cwd=buildroot)
Don Garrettc4114cc2016-11-01 20:04:06 -070093
94 logging.debug('cbuildbot result is: %s', result.returncode)
95 return result.returncode
96
97
98def 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 Garrett86881cb2017-02-15 15:41:55 -0800108 options = PreParseArguments(argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700109
Don Garrett86881cb2017-02-15 15:41:55 -0800110 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)