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