David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Chris Sosa | c13bba5 | 2011-05-24 15:14:09 -0700 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Brian Harring | af019fb | 2012-05-10 15:06:13 -0700 | [diff] [blame] | 6 | """This script is used to upload host prebuilts as well as board BINHOSTS. |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 7 | |
| 8 | If the URL starts with 'gs://', we upload using gsutil to Google Storage. |
| 9 | Otherwise, rsync is used. |
| 10 | |
| 11 | After a build is successfully uploaded a file is updated with the proper |
| 12 | BINHOST version as well as the target board. This file is defined in GIT_FILE |
| 13 | |
| 14 | |
| 15 | To read more about prebuilts/binhost binary packages please refer to: |
| 16 | http://sites/chromeos/for-team-members/engineering/releng/prebuilt-binaries-for-streamlining-the-build-process |
| 17 | |
| 18 | |
| 19 | Example of uploading prebuilt amd64 host files to Google Storage: |
| 20 | ./prebuilt.py -p /b/cbuild/build -s -u gs://chromeos-prebuilt |
| 21 | |
| 22 | Example of uploading x86-dogfood binhosts to Google Storage: |
| 23 | ./prebuilt.py -b x86-dogfood -p /b/cbuild/build/ -u gs://chromeos-prebuilt -g |
| 24 | |
| 25 | Example of uploading prebuilt amd64 host files using rsync: |
| 26 | ./prebuilt.py -p /b/cbuild/build -s -u codf30.jail:/tmp |
| 27 | """ |
| 28 | |
Chris Sosa | 1dc9613 | 2012-05-11 15:40:50 -0700 | [diff] [blame] | 29 | import datetime |
| 30 | import multiprocessing |
| 31 | import optparse |
| 32 | import os |
| 33 | import sys |
| 34 | import tempfile |
| 35 | |
| 36 | if __name__ == '__main__': |
| 37 | import constants |
| 38 | sys.path.insert(0, constants.SOURCE_ROOT) |
| 39 | |
| 40 | from chromite.lib import cros_build_lib |
Brian Harring | af019fb | 2012-05-10 15:06:13 -0700 | [diff] [blame] | 41 | from chromite.lib import osutils |
Chris Sosa | 1dc9613 | 2012-05-11 15:40:50 -0700 | [diff] [blame] | 42 | from chromite.lib.binpkg import (GrabLocalPackageIndex, GrabRemotePackageIndex) |
| 43 | |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 44 | _RETRIES = 3 |
| 45 | _GSUTIL_BIN = '/b/build/third_party/gsutil/gsutil' |
| 46 | _HOST_PACKAGES_PATH = 'chroot/var/lib/portage/pkgs' |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 47 | _CATEGORIES_PATH = 'chroot/etc/portage/categories' |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 48 | _PYM_PATH = 'chroot/usr/lib/portage/pym' |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 49 | _HOST_ARCH = 'amd64' |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 50 | _BOARD_PATH = 'chroot/build/%(board)s' |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 51 | _REL_BOARD_PATH = 'board/%(target)s/%(version)s' |
| 52 | _REL_HOST_PATH = 'host/%(host_arch)s/%(target)s/%(version)s' |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 53 | # Private overlays to look at for builds to filter |
| 54 | # relative to build path |
| 55 | _PRIVATE_OVERLAY_DIR = 'src/private-overlays' |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 56 | _GOOGLESTORAGE_ACL_FILE = 'googlestorage_acl.xml' |
David James | ce61929 | 2011-11-08 11:42:36 -0800 | [diff] [blame] | 57 | _BINHOST_BASE_URL = 'https://commondatastorage.googleapis.com/chromeos-prebuilt' |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 58 | _PREBUILT_BASE_DIR = 'src/third_party/chromiumos-overlay/chromeos/config/' |
| 59 | # Created in the event of new host targets becoming available |
| 60 | _PREBUILT_MAKE_CONF = {'amd64': os.path.join(_PREBUILT_BASE_DIR, |
| 61 | 'make.conf.amd64-host')} |
| 62 | _BINHOST_CONF_DIR = 'src/third_party/chromiumos-overlay/chromeos/binhost' |
| 63 | |
| 64 | |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 65 | class UploadFailed(Exception): |
| 66 | """Raised when one of the files uploaded failed.""" |
| 67 | pass |
| 68 | |
| 69 | class UnknownBoardFormat(Exception): |
| 70 | """Raised when a function finds an unknown board format.""" |
| 71 | pass |
| 72 | |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 73 | |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 74 | class BuildTarget(object): |
| 75 | """A board/variant/profile tuple.""" |
| 76 | |
| 77 | def __init__(self, board_variant, profile=None): |
| 78 | self.board_variant = board_variant |
| 79 | self.board, _, self.variant = board_variant.partition('_') |
| 80 | self.profile = profile |
| 81 | |
| 82 | def __str__(self): |
| 83 | if self.profile: |
| 84 | return '%s_%s' % (self.board_variant, self.profile) |
| 85 | else: |
| 86 | return self.board_variant |
| 87 | |
| 88 | def __eq__(self, other): |
| 89 | return str(other) == str(self) |
| 90 | |
| 91 | def __hash__(self): |
| 92 | return hash(str(self)) |
| 93 | |
| 94 | |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 95 | def UpdateLocalFile(filename, value, key='PORTAGE_BINHOST'): |
| 96 | """Update the key in file with the value passed. |
| 97 | File format: |
| 98 | key="value" |
| 99 | Note quotes are added automatically |
| 100 | |
| 101 | Args: |
| 102 | filename: Name of file to modify. |
| 103 | value: Value to write with the key. |
| 104 | key: The variable key to update. (Default: PORTAGE_BINHOST) |
| 105 | """ |
| 106 | if os.path.exists(filename): |
| 107 | file_fh = open(filename) |
| 108 | else: |
| 109 | file_fh = open(filename, 'w+') |
| 110 | file_lines = [] |
| 111 | found = False |
| 112 | keyval_str = '%(key)s=%(value)s' |
| 113 | for line in file_fh: |
| 114 | # Strip newlines from end of line. We already add newlines below. |
| 115 | line = line.rstrip("\n") |
| 116 | |
| 117 | if len(line.split('=')) != 2: |
| 118 | # Skip any line that doesn't fit key=val. |
| 119 | file_lines.append(line) |
| 120 | continue |
| 121 | |
| 122 | file_var, file_val = line.split('=') |
| 123 | if file_var == key: |
| 124 | found = True |
David James | 20b2b6f | 2011-11-18 15:11:58 -0800 | [diff] [blame] | 125 | print 'Updating %s=%s to %s="%s"' % (file_var, file_val, key, value) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 126 | value = '"%s"' % value |
| 127 | file_lines.append(keyval_str % {'key': key, 'value': value}) |
| 128 | else: |
| 129 | file_lines.append(keyval_str % {'key': file_var, 'value': file_val}) |
| 130 | |
| 131 | if not found: |
Brian Harring | 2a01430 | 2012-05-12 00:53:33 -0700 | [diff] [blame] | 132 | value = '"%s"' % value |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 133 | file_lines.append(keyval_str % {'key': key, 'value': value}) |
| 134 | |
| 135 | file_fh.close() |
| 136 | # write out new file |
Brian Harring | af019fb | 2012-05-10 15:06:13 -0700 | [diff] [blame] | 137 | osutils.WriteFile(filename, '\n'.join(file_lines) + '\n') |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 138 | |
| 139 | |
David James | 27fa7d1 | 2011-06-29 17:24:14 -0700 | [diff] [blame] | 140 | def RevGitFile(filename, value, retries=5, key='PORTAGE_BINHOST', dryrun=False): |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 141 | """Update and push the git file. |
| 142 | |
| 143 | Args: |
| 144 | filename: file to modify that is in a git repo already |
| 145 | value: string representing the version of the prebuilt that has been |
| 146 | uploaded. |
| 147 | retries: The number of times to retry before giving up, default: 5 |
| 148 | key: The variable key to update in the git file. |
| 149 | (Default: PORTAGE_BINHOST) |
| 150 | """ |
| 151 | prebuilt_branch = 'prebuilt_branch' |
David James | 1b6e67a | 2011-05-19 21:32:38 -0700 | [diff] [blame] | 152 | cwd = os.path.abspath(os.path.dirname(filename)) |
Brian Harring | 609dc4e | 2012-05-07 02:17:44 -0700 | [diff] [blame] | 153 | commit = cros_build_lib.RunGitCommand( |
| 154 | cwd, ['rev-parse', 'HEAD']).output.rstrip() |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 155 | description = 'Update %s="%s" in %s' % (key, value, filename) |
| 156 | print description |
David James | 6600946 | 2012-03-25 10:08:38 -0700 | [diff] [blame] | 157 | |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 158 | try: |
David James | 6600946 | 2012-03-25 10:08:38 -0700 | [diff] [blame] | 159 | cros_build_lib.CreatePushBranch(prebuilt_branch, cwd) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 160 | UpdateLocalFile(filename, value, key) |
Brian Harring | 609dc4e | 2012-05-07 02:17:44 -0700 | [diff] [blame] | 161 | cros_build_lib.RunGitCommand(cwd, ['add', filename]) |
| 162 | cros_build_lib.RunGitCommand(cwd, ['commit', '-m', description]) |
| 163 | cros_build_lib.GitPushWithRetry(prebuilt_branch, cwd, dryrun=dryrun, |
David James | 6600946 | 2012-03-25 10:08:38 -0700 | [diff] [blame] | 164 | retries=retries) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 165 | finally: |
David James | 1b6e67a | 2011-05-19 21:32:38 -0700 | [diff] [blame] | 166 | cros_build_lib.RunCommand(['git', 'checkout', commit], cwd=cwd) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 167 | |
| 168 | |
| 169 | def GetVersion(): |
| 170 | """Get the version to put in LATEST and update the git version with.""" |
| 171 | return datetime.datetime.now().strftime('%d.%m.%y.%H%M%S') |
| 172 | |
| 173 | |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 174 | def _GsUpload(args): |
| 175 | """Upload to GS bucket. |
| 176 | |
| 177 | Args: |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 178 | args: a tuple of three arguments that contains local_file, remote_file, and |
| 179 | the acl used for uploading the file. |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 180 | |
| 181 | Returns: |
| 182 | Return the arg tuple of two if the upload failed |
| 183 | """ |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 184 | (local_file, remote_file, acl) = args |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 185 | CANNED_ACLS = ['public-read', 'private', 'bucket-owner-read', |
| 186 | 'authenticated-read', 'bucket-owner-full-control', |
| 187 | 'public-read-write'] |
| 188 | acl_cmd = None |
| 189 | if acl in CANNED_ACLS: |
Peter Mayo | 193f68f | 2011-04-19 19:08:21 -0400 | [diff] [blame] | 190 | cmd = [_GSUTIL_BIN, 'cp', '-a', acl, local_file, remote_file] |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 191 | else: |
| 192 | # For private uploads we assume that the overlay board is set up properly |
| 193 | # and a googlestore_acl.xml is present, if not this script errors |
Peter Mayo | 193f68f | 2011-04-19 19:08:21 -0400 | [diff] [blame] | 194 | cmd = [_GSUTIL_BIN, 'cp', '-a', 'private', local_file, remote_file] |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 195 | if not os.path.exists(acl): |
| 196 | print >> sys.stderr, ('You are specifying either a file that does not ' |
| 197 | 'exist or an unknown canned acl: %s. Aborting ' |
| 198 | 'upload') % acl |
| 199 | # emulate the failing of an upload since we are not uploading the file |
| 200 | return (local_file, remote_file) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 201 | |
Peter Mayo | 193f68f | 2011-04-19 19:08:21 -0400 | [diff] [blame] | 202 | acl_cmd = [_GSUTIL_BIN, 'setacl', acl, remote_file] |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 203 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 204 | if not cros_build_lib.RunCommandWithRetries( |
| 205 | _RETRIES, cmd, print_cmd=False, error_code_ok=True).returncode == 0: |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 206 | return (local_file, remote_file) |
| 207 | |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 208 | if acl_cmd: |
| 209 | # Apply the passed in ACL xml file to the uploaded object. |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 210 | cros_build_lib.RunCommandWithRetries(_RETRIES, acl_cmd, print_cmd=False, |
| 211 | error_code_ok=True) |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 212 | |
| 213 | |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 214 | def RemoteUpload(acl, files, pool=10): |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 215 | """Upload to google storage. |
| 216 | |
| 217 | Create a pool of process and call _GsUpload with the proper arguments. |
| 218 | |
| 219 | Args: |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 220 | acl: The canned acl used for uploading. acl can be one of: "public-read", |
| 221 | "public-read-write", "authenticated-read", "bucket-owner-read", |
| 222 | "bucket-owner-full-control", or "private". |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 223 | files: dictionary with keys to local files and values to remote path. |
| 224 | pool: integer of maximum proesses to have at the same time. |
| 225 | |
| 226 | Returns: |
| 227 | Return a set of tuple arguments of the failed uploads |
| 228 | """ |
| 229 | # TODO(scottz) port this to use _RunManyParallel when it is available in |
| 230 | # cros_build_lib |
| 231 | pool = multiprocessing.Pool(processes=pool) |
| 232 | workers = [] |
| 233 | for local_file, remote_path in files.iteritems(): |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 234 | workers.append((local_file, remote_path, acl)) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 235 | |
| 236 | result = pool.map_async(_GsUpload, workers, chunksize=1) |
| 237 | while True: |
| 238 | try: |
Chris Sosa | 471532a | 2011-02-01 15:10:06 -0800 | [diff] [blame] | 239 | return set(result.get(60 * 60)) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 240 | except multiprocessing.TimeoutError: |
| 241 | pass |
| 242 | |
| 243 | |
| 244 | def GenerateUploadDict(base_local_path, base_remote_path, pkgs): |
| 245 | """Build a dictionary of local remote file key pairs to upload. |
| 246 | |
| 247 | Args: |
| 248 | base_local_path: The base path to the files on the local hard drive. |
| 249 | remote_path: The base path to the remote paths. |
| 250 | pkgs: The packages to upload. |
| 251 | |
| 252 | Returns: |
| 253 | Returns a dictionary of local_path/remote_path pairs |
| 254 | """ |
| 255 | upload_files = {} |
| 256 | for pkg in pkgs: |
| 257 | suffix = pkg['CPV'] + '.tbz2' |
| 258 | local_path = os.path.join(base_local_path, suffix) |
| 259 | assert os.path.exists(local_path) |
| 260 | remote_path = '%s/%s' % (base_remote_path.rstrip('/'), suffix) |
| 261 | upload_files[local_path] = remote_path |
| 262 | |
| 263 | return upload_files |
| 264 | |
| 265 | def GetBoardPathFromCrosOverlayList(build_path, target): |
| 266 | """Use the cros_overlay_list to determine the path to the board overlay |
| 267 | Args: |
| 268 | build_path: The path to the root of the build directory |
| 269 | target: The target that we are looking for, could consist of board and |
| 270 | board_variant, we handle that properly |
| 271 | Returns: |
| 272 | The last line from cros_overlay_list as a string |
| 273 | """ |
Chris Sosa | 471532a | 2011-02-01 15:10:06 -0800 | [diff] [blame] | 274 | script_dir = os.path.join(build_path, 'src/platform/dev/host') |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 275 | cmd = ['./cros_overlay_list', '--board', target.board] |
| 276 | if target.variant: |
| 277 | cmd += ['--variant', target.variant] |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 278 | |
| 279 | cmd_output = cros_build_lib.RunCommand(cmd, redirect_stdout=True, |
| 280 | cwd=script_dir) |
| 281 | # We only care about the last entry |
| 282 | return cmd_output.output.splitlines().pop() |
| 283 | |
| 284 | |
| 285 | def DeterminePrebuiltConfFile(build_path, target): |
| 286 | """Determine the prebuilt.conf file that needs to be updated for prebuilts. |
| 287 | |
| 288 | Args: |
| 289 | build_path: The path to the root of the build directory |
| 290 | target: String representation of the board. This includes host and board |
| 291 | targets |
| 292 | |
| 293 | Returns |
| 294 | A string path to a prebuilt.conf file to be updated. |
| 295 | """ |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 296 | if _HOST_ARCH == target: |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 297 | # We are host. |
| 298 | # Without more examples of hosts this is a kludge for now. |
| 299 | # TODO(Scottz): as new host targets come online expand this to |
| 300 | # work more like boards. |
Chris Sosa | 471532a | 2011-02-01 15:10:06 -0800 | [diff] [blame] | 301 | make_path = _PREBUILT_MAKE_CONF[target] |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 302 | else: |
| 303 | # We are a board |
| 304 | board = GetBoardPathFromCrosOverlayList(build_path, target) |
| 305 | make_path = os.path.join(board, 'prebuilt.conf') |
| 306 | |
| 307 | return make_path |
| 308 | |
| 309 | |
| 310 | def UpdateBinhostConfFile(path, key, value): |
| 311 | """Update binhost config file file with key=value. |
| 312 | |
| 313 | Args: |
| 314 | path: Filename to update. |
| 315 | key: Key to update. |
| 316 | value: New value for key. |
| 317 | """ |
| 318 | cwd = os.path.dirname(os.path.abspath(path)) |
| 319 | filename = os.path.basename(path) |
Brian Harring | af019fb | 2012-05-10 15:06:13 -0700 | [diff] [blame] | 320 | osutils.SafeMakedirs(cwd) |
Brian Harring | 22edb44 | 2012-05-11 23:55:18 -0700 | [diff] [blame] | 321 | osutils.WriteFile(path, '', mode='a') |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 322 | UpdateLocalFile(path, value, key) |
Chris Sosa | c13bba5 | 2011-05-24 15:14:09 -0700 | [diff] [blame] | 323 | cros_build_lib.RunCommand(['git', 'add', filename], cwd=cwd) |
David James | 20b2b6f | 2011-11-18 15:11:58 -0800 | [diff] [blame] | 324 | description = 'Update %s="%s" in %s' % (key, value, filename) |
Peter Mayo | 193f68f | 2011-04-19 19:08:21 -0400 | [diff] [blame] | 325 | cros_build_lib.RunCommand(['git', 'commit', '-m', description], cwd=cwd) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 326 | |
| 327 | |
David James | ce093af | 2011-02-23 15:21:58 -0800 | [diff] [blame] | 328 | def _GrabAllRemotePackageIndexes(binhost_urls): |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 329 | """Grab all of the packages files associated with a list of binhost_urls. |
| 330 | |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 331 | Args: |
| 332 | binhost_urls: The URLs for the directories containing the Packages files we |
| 333 | want to grab. |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 334 | |
| 335 | Returns: |
| 336 | A list of PackageIndex objects. |
| 337 | """ |
| 338 | pkg_indexes = [] |
| 339 | for url in binhost_urls: |
| 340 | pkg_index = GrabRemotePackageIndex(url) |
| 341 | if pkg_index: |
| 342 | pkg_indexes.append(pkg_index) |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 343 | return pkg_indexes |
| 344 | |
| 345 | |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 346 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 347 | class PrebuiltUploader(object): |
| 348 | """Synchronize host and board prebuilts.""" |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 349 | |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 350 | def __init__(self, upload_location, acl, binhost_base_url, |
David James | 32b0b2f | 2011-07-13 20:56:50 -0700 | [diff] [blame] | 351 | pkg_indexes, build_path, packages, skip_upload, |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 352 | binhost_conf_dir, debug, target, slave_targets): |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 353 | """Constructor for prebuilt uploader object. |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 354 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 355 | This object can upload host or prebuilt files to Google Storage. |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 356 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 357 | Args: |
| 358 | upload_location: The upload location. |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 359 | acl: The canned acl used for uploading to Google Storage. acl can be one |
| 360 | of: "public-read", "public-read-write", "authenticated-read", |
| 361 | "bucket-owner-read", "bucket-owner-full-control", or "private". If |
| 362 | we are not uploading to Google Storage, this parameter is unused. |
| 363 | binhost_base_url: The URL used for downloading the prebuilts. |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 364 | pkg_indexes: Old uploaded prebuilts to compare against. Instead of |
| 365 | uploading duplicate files, we just link to the old files. |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 366 | build_path: The path to the directory containing the chroot. |
| 367 | packages: Packages to upload. |
David James | 32b0b2f | 2011-07-13 20:56:50 -0700 | [diff] [blame] | 368 | skip_upload: Don't actually upload the tarballs. |
| 369 | binhost_conf_dir: Directory where to store binhost.conf files. |
| 370 | debug: Don't push or upload prebuilts. |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 371 | target: BuildTarget managed by this builder. |
| 372 | slave_targets: List of BuildTargets managed by slave builders. |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 373 | """ |
| 374 | self._upload_location = upload_location |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 375 | self._acl = acl |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 376 | self._binhost_base_url = binhost_base_url |
| 377 | self._pkg_indexes = pkg_indexes |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 378 | self._build_path = build_path |
| 379 | self._packages = set(packages) |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 380 | self._skip_upload = skip_upload |
David James | 32b0b2f | 2011-07-13 20:56:50 -0700 | [diff] [blame] | 381 | self._binhost_conf_dir = binhost_conf_dir |
David James | 27fa7d1 | 2011-06-29 17:24:14 -0700 | [diff] [blame] | 382 | self._debug = debug |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 383 | self._target = target |
| 384 | self._slave_targets = slave_targets |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 385 | |
| 386 | def _ShouldFilterPackage(self, pkg): |
| 387 | if not self._packages: |
| 388 | return False |
| 389 | pym_path = os.path.abspath(os.path.join(self._build_path, _PYM_PATH)) |
David James | 710b7dc | 2012-02-07 16:49:59 -0800 | [diff] [blame] | 390 | sys.path.insert(0, pym_path) |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 391 | import portage.versions |
| 392 | cat, pkgname = portage.versions.catpkgsplit(pkg['CPV'])[0:2] |
| 393 | cp = '%s/%s' % (cat, pkgname) |
| 394 | return pkgname not in self._packages and cp not in self._packages |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 395 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 396 | def _UploadPrebuilt(self, package_path, url_suffix): |
| 397 | """Upload host or board prebuilt files to Google Storage space. |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 398 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 399 | Args: |
| 400 | package_path: The path to the packages dir. |
David James | ce093af | 2011-02-23 15:21:58 -0800 | [diff] [blame] | 401 | url_suffix: The remote subdirectory where we should upload the packages. |
David James | a3bba14 | 2011-05-26 21:24:20 -0700 | [diff] [blame] | 402 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 403 | """ |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 404 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 405 | # Process Packages file, removing duplicates and filtered packages. |
| 406 | pkg_index = GrabLocalPackageIndex(package_path) |
| 407 | pkg_index.SetUploadLocation(self._binhost_base_url, url_suffix) |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 408 | pkg_index.RemoveFilteredPackages(self._ShouldFilterPackage) |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 409 | uploads = pkg_index.ResolveDuplicateUploads(self._pkg_indexes) |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 410 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 411 | # Write Packages file. |
| 412 | tmp_packages_file = pkg_index.WriteToNamedTemporaryFile() |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 413 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 414 | remote_location = '%s/%s' % (self._upload_location.rstrip('/'), url_suffix) |
| 415 | if remote_location.startswith('gs://'): |
| 416 | # Build list of files to upload. |
| 417 | upload_files = GenerateUploadDict(package_path, remote_location, uploads) |
| 418 | remote_file = '%s/Packages' % remote_location.rstrip('/') |
| 419 | upload_files[tmp_packages_file.name] = remote_file |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 420 | |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 421 | failed_uploads = RemoteUpload(self._acl, upload_files) |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 422 | if len(failed_uploads) > 1 or (None not in failed_uploads): |
David James | f4db112 | 2011-03-17 16:18:05 -0700 | [diff] [blame] | 423 | error_msg = ['%s -> %s\n' % args for args in failed_uploads if args] |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 424 | raise UploadFailed('Error uploading:\n%s' % error_msg) |
| 425 | else: |
Peter Mayo | 193f68f | 2011-04-19 19:08:21 -0400 | [diff] [blame] | 426 | pkgs = [p['CPV'] + '.tbz2' for p in uploads] |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 427 | ssh_server, remote_path = remote_location.split(':', 1) |
Peter Mayo | 193f68f | 2011-04-19 19:08:21 -0400 | [diff] [blame] | 428 | remote_path = remote_path.rstrip('/') |
| 429 | pkg_index = tmp_packages_file.name |
| 430 | remote_location = remote_location.rstrip('/') |
| 431 | remote_packages = '%s/Packages' % remote_location |
| 432 | cmds = [['ssh', ssh_server, 'mkdir', '-p', remote_path], |
| 433 | ['rsync', '-av', '--chmod=a+r', pkg_index, remote_packages]] |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 434 | if pkgs: |
Peter Mayo | 193f68f | 2011-04-19 19:08:21 -0400 | [diff] [blame] | 435 | cmds.append(['rsync', '-Rav'] + pkgs + [remote_location + '/']) |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 436 | for cmd in cmds: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 437 | try: |
| 438 | cros_build_lib.RunCommandWithRetries(_RETRIES, cmd, cwd=package_path) |
| 439 | except cros_build_lib.RunCommandError: |
| 440 | raise UploadFailed('Could not run %s' % cmd) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 441 | |
Zdenek Behan | 5ad96c0 | 2011-06-23 01:04:06 +0200 | [diff] [blame] | 442 | def _UploadBoardTarball(self, board_path, url_suffix, version): |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 443 | """Upload a tarball of the board at the specified path to Google Storage. |
| 444 | |
| 445 | Args: |
| 446 | board_path: The path to the board dir. |
| 447 | url_suffix: The remote subdirectory where we should upload the packages. |
Zdenek Behan | 5ad96c0 | 2011-06-23 01:04:06 +0200 | [diff] [blame] | 448 | version: The version of the board. |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 449 | """ |
| 450 | remote_location = '%s/%s' % (self._upload_location.rstrip('/'), url_suffix) |
| 451 | assert remote_location.startswith('gs://') |
| 452 | cwd, boardname = os.path.split(board_path.rstrip(os.path.sep)) |
| 453 | tmpdir = tempfile.mkdtemp() |
| 454 | try: |
| 455 | tarfile = os.path.join(tmpdir, '%s.tbz2' % boardname) |
Brian Harring | d223a24 | 2012-02-03 20:12:10 -0800 | [diff] [blame] | 456 | cmd = ['tar', '-I', 'pbzip2', '-cf', tarfile] |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 457 | excluded_paths = ('usr/lib/debug', 'usr/local/autotest', 'packages', |
| 458 | 'tmp') |
| 459 | for path in excluded_paths: |
Zdenek Behan | e3ed346 | 2011-06-16 00:36:08 +0200 | [diff] [blame] | 460 | cmd.append('--exclude=%s/*' % path) |
| 461 | cmd.append('.') |
Brian Harring | d223a24 | 2012-02-03 20:12:10 -0800 | [diff] [blame] | 462 | cros_build_lib.SudoRunCommand(cmd, cwd=os.path.join(cwd, boardname)) |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 463 | remote_tarfile = '%s/%s.tbz2' % (remote_location.rstrip('/'), boardname) |
Zdenek Behan | 5ad96c0 | 2011-06-23 01:04:06 +0200 | [diff] [blame] | 464 | # FIXME(zbehan): Temporary hack to upload amd64-host chroots to a |
| 465 | # different gs bucket. The right way is to do the upload in a separate |
| 466 | # pass of this script. |
| 467 | if boardname == 'amd64-host': |
Zdenek Behan | af3c900 | 2011-06-24 10:07:40 +0200 | [diff] [blame] | 468 | # FIXME(zbehan): Why does version contain the prefix "chroot-"? |
| 469 | remote_tarfile = \ |
| 470 | 'gs://chromiumos-sdk/cros-sdk-%s.tbz2' % version.strip('chroot-') |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 471 | if _GsUpload((tarfile, remote_tarfile, self._acl)): |
| 472 | sys.exit(1) |
| 473 | finally: |
Brian Harring | d223a24 | 2012-02-03 20:12:10 -0800 | [diff] [blame] | 474 | cros_build_lib.SudoRunCommand(['rm', '-rf', tmpdir], cwd=cwd) |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 475 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 476 | def _GetTargets(self): |
| 477 | """Retuns the list of targets to use.""" |
| 478 | targets = self._slave_targets[:] |
| 479 | if self._target: |
| 480 | targets.append(self._target) |
| 481 | |
| 482 | return targets |
| 483 | |
| 484 | def SyncHostPrebuilts(self, version, key, git_sync, sync_binhost_conf): |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 485 | """Synchronize host prebuilt files. |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 486 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 487 | This function will sync both the standard host packages, plus the host |
| 488 | packages associated with all targets that have been "setup" with the |
| 489 | current host's chroot. For instance, if this host has been used to build |
| 490 | x86-generic, it will sync the host packages associated with |
| 491 | 'i686-pc-linux-gnu'. If this host has also been used to build arm-generic, |
| 492 | it will also sync the host packages associated with |
| 493 | 'armv7a-cros-linux-gnueabi'. |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 494 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 495 | Args: |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 496 | version: A unique string, intended to be included in the upload path, |
| 497 | which identifies the version number of the uploaded prebuilts. |
| 498 | key: The variable key to update in the git file. |
| 499 | git_sync: If set, update make.conf of target to reference the latest |
| 500 | prebuilt packages generated here. |
| 501 | sync_binhost_conf: If set, update binhost config file in |
| 502 | chromiumos-overlay for the host. |
| 503 | """ |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 504 | # Slave boards are listed before the master board so that the master board |
| 505 | # takes priority (i.e. x86-generic preflight host prebuilts takes priority |
| 506 | # over preflight host prebuilts from other builders.) |
| 507 | binhost_urls = [] |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 508 | for target in self._GetTargets(): |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 509 | url_suffix = _REL_HOST_PATH % {'version': version, |
| 510 | 'host_arch': _HOST_ARCH, |
| 511 | 'target': target} |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 512 | packages_url_suffix = '%s/packages' % url_suffix.rstrip('/') |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 513 | |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 514 | if self._target == target and not self._skip_upload and not self._debug: |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 515 | # Upload prebuilts. |
| 516 | package_path = os.path.join(self._build_path, _HOST_PACKAGES_PATH) |
| 517 | self._UploadPrebuilt(package_path, packages_url_suffix) |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 518 | |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 519 | # Record URL where prebuilts were uploaded. |
| 520 | binhost_urls.append('%s/%s/' % (self._binhost_base_url.rstrip('/'), |
| 521 | packages_url_suffix.rstrip('/'))) |
| 522 | |
David James | 20b2b6f | 2011-11-18 15:11:58 -0800 | [diff] [blame] | 523 | binhost = ' '.join(binhost_urls) |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 524 | if git_sync: |
| 525 | git_file = os.path.join(self._build_path, |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 526 | _PREBUILT_MAKE_CONF[_HOST_ARCH]) |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 527 | RevGitFile(git_file, binhost, key=key, dryrun=self._debug) |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 528 | if sync_binhost_conf: |
David James | 32b0b2f | 2011-07-13 20:56:50 -0700 | [diff] [blame] | 529 | binhost_conf = os.path.join(self._build_path, self._binhost_conf_dir, |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 530 | 'host', '%s-%s.conf' % (_HOST_ARCH, key)) |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 531 | UpdateBinhostConfFile(binhost_conf, key, binhost) |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 532 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 533 | def SyncBoardPrebuilts(self, version, key, git_sync, sync_binhost_conf, |
| 534 | upload_board_tarball): |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 535 | """Synchronize board prebuilt files. |
| 536 | |
| 537 | Args: |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 538 | version: A unique string, intended to be included in the upload path, |
| 539 | which identifies the version number of the uploaded prebuilts. |
| 540 | key: The variable key to update in the git file. |
| 541 | git_sync: If set, update make.conf of target to reference the latest |
| 542 | prebuilt packages generated here. |
| 543 | sync_binhost_conf: If set, update binhost config file in |
| 544 | chromiumos-overlay for the current board. |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 545 | upload_board_tarball: Include a tarball of the board in our upload. |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 546 | """ |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 547 | for target in self._GetTargets(): |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 548 | board_path = os.path.join(self._build_path, |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 549 | _BOARD_PATH % {'board': target.board_variant}) |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 550 | package_path = os.path.join(board_path, 'packages') |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 551 | url_suffix = _REL_BOARD_PATH % {'target': target, 'version': version} |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 552 | packages_url_suffix = '%s/packages' % url_suffix.rstrip('/') |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 553 | |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 554 | if self._target == target and not self._skip_upload and not self._debug: |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 555 | # Upload board tarballs in the background. |
| 556 | if upload_board_tarball: |
| 557 | tar_process = multiprocessing.Process(target=self._UploadBoardTarball, |
| 558 | args=(board_path, url_suffix, |
| 559 | version)) |
| 560 | tar_process.start() |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 561 | |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 562 | # Upload prebuilts. |
| 563 | self._UploadPrebuilt(package_path, packages_url_suffix) |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 564 | |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 565 | # Make sure we finished uploading the board tarballs. |
| 566 | if upload_board_tarball: |
| 567 | tar_process.join() |
| 568 | assert tar_process.exitcode == 0 |
| 569 | # TODO(zbehan): This should be done cleaner. |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 570 | if target.board == 'amd64-host': |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 571 | sdk_conf = os.path.join(self._build_path, self._binhost_conf_dir, |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 572 | 'host/sdk_version.conf') |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 573 | RevGitFile(sdk_conf, version.strip('chroot-'), |
| 574 | key='SDK_LATEST_VERSION', dryrun=self._debug) |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 575 | |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 576 | # Record URL where prebuilts were uploaded. |
| 577 | url_value = '%s/%s/' % (self._binhost_base_url.rstrip('/'), |
| 578 | packages_url_suffix.rstrip('/')) |
| 579 | |
| 580 | if git_sync: |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 581 | git_file = DeterminePrebuiltConfFile(self._build_path, target) |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 582 | RevGitFile(git_file, url_value, key=key, dryrun=self._debug) |
| 583 | if sync_binhost_conf: |
| 584 | binhost_conf = os.path.join(self._build_path, self._binhost_conf_dir, |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 585 | 'target', '%s-%s.conf' % (target, key)) |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 586 | UpdateBinhostConfFile(binhost_conf, key, url_value) |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 587 | |
| 588 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 589 | def Usage(parser, msg): |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 590 | """Display usage message and parser help then exit with 1.""" |
| 591 | print >> sys.stderr, msg |
| 592 | parser.print_help() |
| 593 | sys.exit(1) |
| 594 | |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 595 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 596 | def _AddSlaveBoard(_option, _opt_str, value, parser): |
| 597 | """Callback that adds a slave board to the list of slave targets.""" |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 598 | parser.values.slave_targets.append(BuildTarget(value)) |
| 599 | |
| 600 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 601 | def _AddSlaveProfile(_option, _opt_str, value, parser): |
| 602 | """Callback that adds a slave profile to the list of slave targets.""" |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 603 | if not parser.values.slave_targets: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 604 | Usage(parser, 'Must specify --slave-board before --slave-profile') |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 605 | if parser.values.slave_targets[-1].profile is not None: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 606 | Usage(parser, 'Cannot specify --slave-profile twice for same board') |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 607 | parser.values.slave_targets[-1].profile = value |
| 608 | |
| 609 | |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 610 | def ParseOptions(): |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 611 | """Returns options given by the user and the target specified. |
| 612 | |
| 613 | Returns a tuple containing a parsed options object and BuildTarget. |
| 614 | target instance is None if no board is specified. |
| 615 | """ |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 616 | parser = optparse.OptionParser() |
| 617 | parser.add_option('-H', '--binhost-base-url', dest='binhost_base_url', |
| 618 | default=_BINHOST_BASE_URL, |
| 619 | help='Base URL to use for binhost in make.conf updates') |
| 620 | parser.add_option('', '--previous-binhost-url', action='append', |
| 621 | default=[], dest='previous_binhost_url', |
| 622 | help='Previous binhost URL') |
| 623 | parser.add_option('-b', '--board', dest='board', default=None, |
| 624 | help='Board type that was built on this machine') |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 625 | parser.add_option('', '--profile', dest='profile', default=None, |
| 626 | help='Profile that was built on this machine') |
| 627 | parser.add_option('', '--slave-board', default=[], action='callback', |
| 628 | dest='slave_targets', type='string', |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 629 | callback=_AddSlaveBoard, |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 630 | help='Board type that was built on a slave machine. To ' |
| 631 | 'add a profile to this board, use --slave-profile.') |
| 632 | parser.add_option('', '--slave-profile', action='callback', type='string', |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 633 | callback=_AddSlaveProfile, |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 634 | help='Board profile that was built on a slave machine. ' |
| 635 | 'Applies to previous slave board.') |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 636 | parser.add_option('-p', '--build-path', dest='build_path', |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 637 | help='Path to the directory containing the chroot') |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 638 | parser.add_option('', '--packages', action='append', |
| 639 | default=[], dest='packages', |
| 640 | help='Only include the specified packages. ' |
| 641 | '(Default is to include all packages.)') |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 642 | parser.add_option('-s', '--sync-host', dest='sync_host', |
| 643 | default=False, action='store_true', |
| 644 | help='Sync host prebuilts') |
| 645 | parser.add_option('-g', '--git-sync', dest='git_sync', |
| 646 | default=False, action='store_true', |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 647 | help='Enable git version sync (This commits to a repo.) ' |
| 648 | 'This is used by full builders to commit directly ' |
| 649 | 'to board overlays.') |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 650 | parser.add_option('-u', '--upload', dest='upload', |
| 651 | default=None, |
| 652 | help='Upload location') |
| 653 | parser.add_option('-V', '--prepend-version', dest='prepend_version', |
| 654 | default=None, |
| 655 | help='Add an identifier to the front of the version') |
| 656 | parser.add_option('-f', '--filters', dest='filters', action='store_true', |
| 657 | default=False, |
| 658 | help='Turn on filtering of private ebuild packages') |
| 659 | parser.add_option('-k', '--key', dest='key', |
| 660 | default='PORTAGE_BINHOST', |
| 661 | help='Key to update in make.conf / binhost.conf') |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 662 | parser.add_option('', '--set-version', dest='set_version', |
| 663 | default=None, |
| 664 | help='Specify the version string') |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 665 | parser.add_option('', '--sync-binhost-conf', dest='sync_binhost_conf', |
| 666 | default=False, action='store_true', |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 667 | help='Update binhost.conf in chromiumos-overlay or ' |
| 668 | 'chromeos-overlay. Commit the changes, but don\'t ' |
| 669 | 'push them. This is used for preflight binhosts.') |
David James | 32b0b2f | 2011-07-13 20:56:50 -0700 | [diff] [blame] | 670 | parser.add_option('', '--binhost-conf-dir', dest='binhost_conf_dir', |
| 671 | default=_BINHOST_CONF_DIR, |
| 672 | help='Directory to commit binhost config with ' |
| 673 | '--sync-binhost-conf.') |
David James | fd0b085 | 2011-02-23 11:15:36 -0800 | [diff] [blame] | 674 | parser.add_option('-P', '--private', dest='private', action='store_true', |
| 675 | default=False, help='Mark gs:// uploads as private.') |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 676 | parser.add_option('', '--skip-upload', dest='skip_upload', |
| 677 | action='store_true', default=False, |
| 678 | help='Skip upload step.') |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 679 | parser.add_option('', '--upload-board-tarball', dest='upload_board_tarball', |
| 680 | action='store_true', default=False, |
| 681 | help='Upload board tarball to Google Storage.') |
David James | 27fa7d1 | 2011-06-29 17:24:14 -0700 | [diff] [blame] | 682 | parser.add_option('', '--debug', dest='debug', |
| 683 | action='store_true', default=False, |
| 684 | help='Don\'t push or upload prebuilts.') |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 685 | |
| 686 | options, args = parser.parse_args() |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 687 | if not options.build_path: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 688 | Usage(parser, 'Error: you need provide a chroot path') |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 689 | if not options.upload and not options.skip_upload: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 690 | Usage(parser, 'Error: you need to provide an upload location using -u') |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 691 | if not options.set_version and options.skip_upload: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 692 | Usage(parser, 'Error: If you are using --skip-upload, you must specify a ' |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 693 | 'version number using --set-version.') |
David James | 9417f27 | 2011-05-26 13:24:47 -0700 | [diff] [blame] | 694 | if args: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 695 | Usage(parser, 'Error: invalid arguments passed to prebuilt.py: %r' % args) |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 696 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 697 | target = None |
| 698 | if options.board: |
| 699 | target = BuildTarget(options.board, options.profile) |
| 700 | |
| 701 | if target in options.slave_targets: |
| 702 | Usage(parser, 'Error: --board/--profile must not also be a slave target.') |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 703 | |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 704 | if len(set(options.slave_targets)) != len(options.slave_targets): |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 705 | Usage(parser, 'Error: --slave-boards must not have duplicates.') |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 706 | |
David James | 4058b0d | 2011-12-08 21:24:50 -0800 | [diff] [blame] | 707 | if options.slave_targets and options.git_sync: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 708 | Usage(parser, 'Error: --slave-boards is not compatible with --git-sync') |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 709 | |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 710 | if (options.upload_board_tarball and options.skip_upload and |
| 711 | options.board == 'amd64-host'): |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 712 | Usage(parser, 'Error: --skip-upload is not compatible with ' |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 713 | '--upload-board-tarball and --board=amd64-host') |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 714 | |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 715 | if (options.upload_board_tarball and not options.skip_upload and |
| 716 | not options.upload.startswith('gs://')): |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 717 | Usage(parser, 'Error: --upload-board-tarball only works with gs:// URLs.\n' |
David James | 8fa34ea | 2011-04-15 13:00:20 -0700 | [diff] [blame] | 718 | '--upload must be a gs:// URL.') |
| 719 | |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 720 | if options.private: |
| 721 | if options.sync_host: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 722 | Usage(parser, 'Error: --private and --sync-host/-s cannot be specified ' |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 723 | 'together, we do not support private host prebuilts') |
| 724 | |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 725 | if not options.upload or not options.upload.startswith('gs://'): |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 726 | Usage(parser, 'Error: --private is only valid for gs:// URLs.\n' |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 727 | '--upload must be a gs:// URL.') |
| 728 | |
| 729 | if options.binhost_base_url != _BINHOST_BASE_URL: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 730 | Usage(parser, 'Error: when using --private the --binhost-base-url ' |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 731 | 'is automatically derived.') |
David James | 27fa7d1 | 2011-06-29 17:24:14 -0700 | [diff] [blame] | 732 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 733 | return options, target |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 734 | |
| 735 | def main(): |
David James | db40107 | 2011-06-10 12:17:16 -0700 | [diff] [blame] | 736 | # Set umask to a sane value so that files created as root are readable. |
| 737 | os.umask(022) |
| 738 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 739 | options, target = ParseOptions() |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 740 | |
David James | 05bcb2b | 2011-02-09 09:25:47 -0800 | [diff] [blame] | 741 | # Calculate a list of Packages index files to compare against. Whenever we |
| 742 | # upload a package, we check to make sure it's not already stored in one of |
| 743 | # the packages files we uploaded. This list of packages files might contain |
| 744 | # both board and host packages. |
David James | ce093af | 2011-02-23 15:21:58 -0800 | [diff] [blame] | 745 | pkg_indexes = _GrabAllRemotePackageIndexes(options.previous_binhost_url) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 746 | |
David James | 8ece7ee | 2011-06-29 16:02:30 -0700 | [diff] [blame] | 747 | if options.set_version: |
| 748 | version = options.set_version |
| 749 | else: |
| 750 | version = GetVersion() |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 751 | if options.prepend_version: |
| 752 | version = '%s-%s' % (options.prepend_version, version) |
| 753 | |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 754 | acl = 'public-read' |
| 755 | binhost_base_url = options.binhost_base_url |
| 756 | |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 757 | if target and options.private: |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 758 | binhost_base_url = options.upload |
| 759 | board_path = GetBoardPathFromCrosOverlayList(options.build_path, |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 760 | target) |
Scott Zawalski | ab1bed3 | 2011-03-16 15:24:24 -0700 | [diff] [blame] | 761 | acl = os.path.join(board_path, _GOOGLESTORAGE_ACL_FILE) |
| 762 | |
| 763 | uploader = PrebuiltUploader(options.upload, acl, binhost_base_url, |
David James | 615e5b5 | 2011-06-03 11:10:15 -0700 | [diff] [blame] | 764 | pkg_indexes, options.build_path, |
David James | 27fa7d1 | 2011-06-29 17:24:14 -0700 | [diff] [blame] | 765 | options.packages, options.skip_upload, |
David James | e248864 | 2011-11-14 16:15:20 -0800 | [diff] [blame] | 766 | options.binhost_conf_dir, options.debug, |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 767 | target, options.slave_targets) |
David James | c0f158a | 2011-02-22 16:07:29 -0800 | [diff] [blame] | 768 | |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 769 | if options.sync_host: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 770 | uploader.SyncHostPrebuilts(version, options.key, options.git_sync, |
| 771 | options.sync_binhost_conf) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 772 | |
Chris Sosa | 62c8ff5 | 2012-06-04 15:03:12 -0700 | [diff] [blame] | 773 | if options.board or options.slave_targets: |
Chris Sosa | 6a5dceb | 2012-05-14 13:48:56 -0700 | [diff] [blame] | 774 | uploader.SyncBoardPrebuilts(version, options.key, options.git_sync, |
| 775 | options.sync_binhost_conf, |
| 776 | options.upload_board_tarball) |
David James | 8c84649 | 2011-01-25 17:07:29 -0800 | [diff] [blame] | 777 | |
| 778 | if __name__ == '__main__': |
| 779 | main() |