Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 2 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This module uprevs Android for cbuildbot. |
| 7 | |
| 8 | After calling, it prints outs ANDROID_VERSION_ATOM=(version atom string). A |
| 9 | caller could then use this atom with emerge to build the newly uprevved version |
| 10 | of Android e.g. |
| 11 | |
Shuhei Takahashi | 6d02c19 | 2017-04-05 14:01:24 +0900 | [diff] [blame] | 12 | ./cros_mark_android_as_stable \ |
Shao-Chuan Lee | 9c39e0c | 2020-04-24 11:40:34 +0900 | [diff] [blame^] | 13 | --android_build_branch=git_pi-arc \ |
| 14 | --android_package=android-container-pi |
Shuhei Takahashi | 6d02c19 | 2017-04-05 14:01:24 +0900 | [diff] [blame] | 15 | |
Shao-Chuan Lee | 9c39e0c | 2020-04-24 11:40:34 +0900 | [diff] [blame^] | 16 | Returns chromeos-base/android-container-pi-6417892-r1 |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 17 | |
Shao-Chuan Lee | 9c39e0c | 2020-04-24 11:40:34 +0900 | [diff] [blame^] | 18 | emerge-eve =chromeos-base/android-container-pi-6417892-r1 |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 19 | """ |
| 20 | |
| 21 | from __future__ import print_function |
| 22 | |
Mike Frysinger | 00a0229 | 2020-04-19 06:28:03 -0400 | [diff] [blame] | 23 | import base64 |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 24 | import filecmp |
khmel@google.com | 778a1cd | 2018-04-13 11:11:58 -0700 | [diff] [blame] | 25 | import hashlib |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 26 | import glob |
| 27 | import os |
Hidehiko Abe | 12727dd | 2016-05-27 23:23:45 +0900 | [diff] [blame] | 28 | import re |
khmel@google.com | 778a1cd | 2018-04-13 11:11:58 -0700 | [diff] [blame] | 29 | import shutil |
| 30 | import tempfile |
khmel@google.com | 96c193e | 2018-05-10 14:00:38 -0700 | [diff] [blame] | 31 | import time |
khmel@google.com | 778a1cd | 2018-04-13 11:11:58 -0700 | [diff] [blame] | 32 | import subprocess |
Mike Frysinger | 00a0229 | 2020-04-19 06:28:03 -0400 | [diff] [blame] | 33 | import sys |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 34 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 35 | from chromite.lib import constants |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 36 | from chromite.lib import commandline |
| 37 | from chromite.lib import cros_build_lib |
| 38 | from chromite.lib import cros_logging as logging |
| 39 | from chromite.lib import git |
| 40 | from chromite.lib import gs |
| 41 | from chromite.lib import portage_util |
| 42 | from chromite.scripts import cros_mark_as_stable |
| 43 | |
| 44 | |
Mike Frysinger | 00a0229 | 2020-04-19 06:28:03 -0400 | [diff] [blame] | 45 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 46 | |
| 47 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 48 | # Dir where all the action happens. |
| 49 | _OVERLAY_DIR = '%(srcroot)s/private-overlays/project-cheets-private/' |
| 50 | |
Junichi Uekawa | 6d61ab0 | 2020-04-15 14:52:28 +0900 | [diff] [blame] | 51 | _GIT_COMMIT_MESSAGE = """Marking latest for %(android_package)s ebuild with \ |
| 52 | version %(android_version)s as stable. |
| 53 | |
| 54 | BUG=None |
| 55 | TEST=CQ |
| 56 | """ |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 57 | |
| 58 | # URLs that print lists of Android revisions between two build ids. |
| 59 | _ANDROID_VERSION_URL = ('http://android-build-uber.corp.google.com/repo.html?' |
| 60 | 'last_bid=%(old)s&bid=%(new)s&branch=%(branch)s') |
| 61 | |
| 62 | |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 63 | def IsBuildIdValid(bucket_url, build_branch, build_id, targets): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 64 | """Checks that a specific build_id is valid. |
| 65 | |
| 66 | Looks for that build_id for all builds. Confirms that the subpath can |
| 67 | be found and that the zip file is present in that subdirectory. |
| 68 | |
| 69 | Args: |
| 70 | bucket_url: URL of Android build gs bucket |
| 71 | build_branch: branch of Android builds |
| 72 | build_id: A string. The Android build id number to check. |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 73 | targets: Dict from build key to (targe build suffix, artifact file pattern) |
| 74 | pair. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 75 | |
| 76 | Returns: |
| 77 | Returns subpaths dictionary if build_id is valid. |
| 78 | None if the build_id is not valid. |
| 79 | """ |
| 80 | gs_context = gs.GSContext() |
| 81 | subpaths_dict = {} |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 82 | for build, (target, _) in targets.items(): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 83 | build_dir = '%s-%s' % (build_branch, target) |
| 84 | build_id_path = os.path.join(bucket_url, build_dir, build_id) |
| 85 | |
| 86 | # Find name of subpath. |
| 87 | try: |
| 88 | subpaths = gs_context.List(build_id_path) |
| 89 | except gs.GSNoSuchKey: |
| 90 | logging.warn( |
| 91 | 'Directory [%s] does not contain any subpath, ignoring it.', |
| 92 | build_id_path) |
| 93 | return None |
| 94 | if len(subpaths) > 1: |
| 95 | logging.warn( |
| 96 | 'Directory [%s] contains more than one subpath, ignoring it.', |
| 97 | build_id_path) |
| 98 | return None |
| 99 | |
| 100 | subpath_dir = subpaths[0].url.rstrip('/') |
| 101 | subpath_name = os.path.basename(subpath_dir) |
| 102 | |
| 103 | # Look for a zipfile ending in the build_id number. |
| 104 | try: |
Hidehiko Abe | 12727dd | 2016-05-27 23:23:45 +0900 | [diff] [blame] | 105 | gs_context.List(subpath_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 106 | except gs.GSNoSuchKey: |
| 107 | logging.warn( |
Hidehiko Abe | 12727dd | 2016-05-27 23:23:45 +0900 | [diff] [blame] | 108 | 'Did not find a file for build id [%s] in directory [%s].', |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 109 | build_id, subpath_dir) |
| 110 | return None |
| 111 | |
| 112 | # Record subpath for the build. |
| 113 | subpaths_dict[build] = subpath_name |
| 114 | |
| 115 | # If we got here, it means we found an appropriate build for all platforms. |
| 116 | return subpaths_dict |
| 117 | |
| 118 | |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 119 | def GetLatestBuild(bucket_url, build_branch, targets): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 120 | """Searches the gs bucket for the latest green build. |
| 121 | |
| 122 | Args: |
| 123 | bucket_url: URL of Android build gs bucket |
| 124 | build_branch: branch of Android builds |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 125 | targets: Dict from build key to (targe build suffix, artifact file pattern) |
| 126 | pair. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 127 | |
| 128 | Returns: |
| 129 | Tuple of (latest version string, subpaths dictionary) |
| 130 | If no latest build can be found, returns None, None |
| 131 | """ |
| 132 | gs_context = gs.GSContext() |
| 133 | common_build_ids = None |
| 134 | # Find builds for each target. |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 135 | for target, _ in targets.values(): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 136 | build_dir = '-'.join((build_branch, target)) |
| 137 | base_path = os.path.join(bucket_url, build_dir) |
| 138 | build_ids = [] |
| 139 | for gs_result in gs_context.List(base_path): |
| 140 | # Remove trailing slashes and get the base name, which is the build_id. |
| 141 | build_id = os.path.basename(gs_result.url.rstrip('/')) |
| 142 | if not build_id.isdigit(): |
| 143 | logging.warn('Directory [%s] does not look like a valid build_id.', |
| 144 | gs_result.url) |
| 145 | continue |
| 146 | build_ids.append(build_id) |
| 147 | |
| 148 | # Update current list of builds. |
| 149 | if common_build_ids is None: |
| 150 | # First run, populate it with the first platform. |
| 151 | common_build_ids = set(build_ids) |
| 152 | else: |
| 153 | # Already populated, find the ones that are common. |
| 154 | common_build_ids.intersection_update(build_ids) |
| 155 | |
| 156 | if common_build_ids is None: |
| 157 | logging.warn('Did not find a build_id common to all platforms.') |
| 158 | return None, None |
| 159 | |
| 160 | # Otherwise, find the most recent one that is valid. |
| 161 | for build_id in sorted(common_build_ids, key=int, reverse=True): |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 162 | subpaths = IsBuildIdValid(bucket_url, build_branch, build_id, targets) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 163 | if subpaths: |
| 164 | return build_id, subpaths |
| 165 | |
| 166 | # If not found, no build_id is valid. |
| 167 | logging.warn('Did not find a build_id valid on all platforms.') |
| 168 | return None, None |
| 169 | |
| 170 | |
| 171 | def FindAndroidCandidates(package_dir): |
| 172 | """Return a tuple of Android's unstable ebuild and stable ebuilds. |
| 173 | |
| 174 | Args: |
| 175 | package_dir: The path to where the package ebuild is stored. |
| 176 | |
| 177 | Returns: |
| 178 | Tuple [unstable_ebuild, stable_ebuilds]. |
| 179 | |
| 180 | Raises: |
| 181 | Exception: if no unstable ebuild exists for Android. |
| 182 | """ |
| 183 | stable_ebuilds = [] |
| 184 | unstable_ebuilds = [] |
| 185 | for path in glob.glob(os.path.join(package_dir, '*.ebuild')): |
| 186 | ebuild = portage_util.EBuild(path) |
| 187 | if ebuild.version == '9999': |
| 188 | unstable_ebuilds.append(ebuild) |
| 189 | else: |
| 190 | stable_ebuilds.append(ebuild) |
| 191 | |
| 192 | # Apply some sanity checks. |
| 193 | if not unstable_ebuilds: |
| 194 | raise Exception('Missing 9999 ebuild for %s' % package_dir) |
| 195 | if not stable_ebuilds: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 196 | logging.warning('Missing stable ebuild for %s', package_dir) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 197 | |
| 198 | return portage_util.BestEBuild(unstable_ebuilds), stable_ebuilds |
| 199 | |
| 200 | |
Nicolas Norvez | b08f54d | 2016-12-05 17:58:54 -0800 | [diff] [blame] | 201 | def _GetArcBasename(build, basename): |
| 202 | """Tweaks filenames between Android bucket and ARC++ bucket. |
| 203 | |
| 204 | Android builders create build artifacts with the same name for -user and |
| 205 | -userdebug builds, which breaks the android-container ebuild (b/33072485). |
| 206 | When copying the artifacts from the Android bucket to the ARC++ bucket some |
| 207 | artifacts will be renamed from the usual pattern |
| 208 | *cheets_${ARCH}-target_files-S{VERSION}.zip to |
| 209 | cheets_${BUILD_NAME}-target_files-S{VERSION}.zip which will typically look |
| 210 | like cheets_(${LABEL})*${ARCH}_userdebug-target_files-S{VERSION}.zip. |
| 211 | |
| 212 | Args: |
| 213 | build: the build being mirrored, e.g. 'X86', 'ARM', 'X86_USERDEBUG'. |
| 214 | basename: the basename of the artifact to copy. |
| 215 | |
| 216 | Returns: |
| 217 | The basename of the destination. |
| 218 | """ |
| 219 | if build not in constants.ARC_BUILDS_NEED_ARTIFACTS_RENAMED: |
| 220 | return basename |
Yūki Ishii | ef1ada9 | 2018-03-27 15:46:15 +0900 | [diff] [blame] | 221 | if basename in constants.ARC_ARTIFACTS_RENAME_NOT_NEEDED: |
| 222 | return basename |
Nicolas Norvez | b08f54d | 2016-12-05 17:58:54 -0800 | [diff] [blame] | 223 | to_discard, sep, to_keep = basename.partition('-') |
| 224 | if not sep: |
| 225 | logging.error(('Build %s: Could not find separator "-" in artifact' |
| 226 | ' basename %s'), build, basename) |
| 227 | return basename |
Bernie Thompson | 63ed561 | 2017-08-16 12:27:34 -0700 | [diff] [blame] | 228 | if 'cheets_' in to_discard: |
| 229 | return 'cheets_%s-%s' % (build.lower(), to_keep) |
| 230 | elif 'bertha_' in to_discard: |
| 231 | return 'bertha_%s-%s' % (build.lower(), to_keep) |
| 232 | logging.error('Build %s: Unexpected artifact basename %s', |
| 233 | build, basename) |
| 234 | return basename |
Nicolas Norvez | b08f54d | 2016-12-05 17:58:54 -0800 | [diff] [blame] | 235 | |
| 236 | |
khmel@google.com | 50c42dd | 2018-05-11 11:33:35 -0700 | [diff] [blame] | 237 | def PackSdkTools(build_branch, build_id, targets, arc_bucket_url, acl): |
khmel@google.com | 778a1cd | 2018-04-13 11:11:58 -0700 | [diff] [blame] | 238 | """Creates static SDK tools pack from ARC++ specific bucket. |
| 239 | |
| 240 | Ebuild needs archives to process binaries natively. This collects static SDK |
| 241 | tools and packs them to tbz2 archive which can referenced from Android |
| 242 | container ebuild file. Pack is placed into the same bucket where SDK tools |
| 243 | exist. If pack already exists and up to date then copying is skipped. |
| 244 | Otherwise fresh pack is copied. |
| 245 | |
| 246 | Args: |
| 247 | build_branch: branch of Android builds |
| 248 | build_id: A string. The Android build id number to check. |
| 249 | targets: Dict from build key to (targe build suffix, artifact file pattern) |
| 250 | pair. |
| 251 | arc_bucket_url: URL of the target ARC build gs bucket |
khmel@google.com | 50c42dd | 2018-05-11 11:33:35 -0700 | [diff] [blame] | 252 | acl: ACL file to apply. |
khmel@google.com | 778a1cd | 2018-04-13 11:11:58 -0700 | [diff] [blame] | 253 | """ |
| 254 | |
| 255 | if not 'SDK_TOOLS' in targets: |
| 256 | return |
| 257 | |
| 258 | gs_context = gs.GSContext() |
| 259 | target, pattern = targets['SDK_TOOLS'] |
| 260 | build_dir = '%s-%s' % (build_branch, target) |
| 261 | arc_dir = os.path.join(arc_bucket_url, build_dir, build_id) |
| 262 | |
| 263 | sdk_tools_dir = tempfile.mkdtemp() |
| 264 | |
| 265 | try: |
| 266 | sdk_tools_bin_dir = os.path.join(sdk_tools_dir, 'bin') |
| 267 | os.mkdir(sdk_tools_bin_dir) |
| 268 | |
| 269 | for tool in gs_context.List(arc_dir): |
| 270 | if re.search(pattern, tool.url): |
| 271 | local_tool_path = os.path.join(sdk_tools_bin_dir, |
| 272 | os.path.basename(tool.url)) |
| 273 | gs_context.Copy(tool.url, local_tool_path, version=0) |
| 274 | file_time = int(gs_context.Stat(tool.url).creation_time.strftime('%s')) |
| 275 | os.utime(local_tool_path, (file_time, file_time)) |
| 276 | |
| 277 | # Fix ./ times to make tar file stable. |
| 278 | os.utime(sdk_tools_bin_dir, (0, 0)) |
| 279 | |
| 280 | sdk_tools_file_name = 'sdk_tools_%s.tbz2' % build_id |
| 281 | sdk_tools_local_path = os.path.join(sdk_tools_dir, sdk_tools_file_name) |
| 282 | sdk_tools_target_path = os.path.join(arc_dir, sdk_tools_file_name) |
| 283 | subprocess.call(['tar', '--group=root:0', '--owner=root:0', |
| 284 | '--create', '--bzip2', '--sort=name', |
| 285 | '--file=%s' % sdk_tools_local_path, |
| 286 | '--directory=%s' % sdk_tools_bin_dir, '.']) |
| 287 | |
| 288 | if gs_context.Exists(sdk_tools_target_path): |
| 289 | # Calculate local md5 |
| 290 | md5 = hashlib.md5() |
| 291 | with open(sdk_tools_local_path, 'rb') as f: |
| 292 | while True: |
| 293 | buf = f.read(4096) |
| 294 | if not buf: |
| 295 | break |
| 296 | md5.update(buf) |
| 297 | md5_local = md5.digest() |
| 298 | # Get target md5 |
| 299 | md5_target = base64.decodestring( |
| 300 | gs_context.Stat(sdk_tools_target_path).hash_md5) |
| 301 | if md5_local == md5_target: |
| 302 | logging.info('SDK tools pack %s is up to date', sdk_tools_target_path) |
| 303 | return |
| 304 | logging.warning('SDK tools pack %s invalid, removing', |
| 305 | sdk_tools_target_path) |
| 306 | gs_context.Remove(sdk_tools_target_path) |
| 307 | |
| 308 | logging.info('Creating SDK tools pack %s', sdk_tools_target_path) |
| 309 | gs_context.Copy(sdk_tools_local_path, sdk_tools_target_path, version=0) |
khmel@google.com | 50c42dd | 2018-05-11 11:33:35 -0700 | [diff] [blame] | 310 | gs_context.ChangeACL(sdk_tools_target_path, acl_args_file=acl) |
khmel@google.com | 778a1cd | 2018-04-13 11:11:58 -0700 | [diff] [blame] | 311 | finally: |
| 312 | shutil.rmtree(sdk_tools_dir) |
| 313 | |
| 314 | |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 315 | def CopyToArcBucket(android_bucket_url, build_branch, build_id, subpaths, |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 316 | targets, arc_bucket_url, acls): |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 317 | """Copies from source Android bucket to ARC++ specific bucket. |
| 318 | |
| 319 | Copies each build to the ARC bucket eliminating the subpath. |
| 320 | Applies build specific ACLs for each file. |
| 321 | |
| 322 | Args: |
| 323 | android_bucket_url: URL of Android build gs bucket |
| 324 | build_branch: branch of Android builds |
| 325 | build_id: A string. The Android build id number to check. |
| 326 | subpaths: Subpath dictionary for each build to copy. |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 327 | targets: Dict from build key to (targe build suffix, artifact file pattern) |
| 328 | pair. |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 329 | arc_bucket_url: URL of the target ARC build gs bucket |
| 330 | acls: ACLs dictionary for each build to copy. |
| 331 | """ |
| 332 | gs_context = gs.GSContext() |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 333 | for build, subpath in subpaths.items(): |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 334 | target, pattern = targets[build] |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 335 | build_dir = '%s-%s' % (build_branch, target) |
| 336 | android_dir = os.path.join(android_bucket_url, build_dir, build_id, subpath) |
| 337 | arc_dir = os.path.join(arc_bucket_url, build_dir, build_id) |
| 338 | |
Hidehiko Abe | 12727dd | 2016-05-27 23:23:45 +0900 | [diff] [blame] | 339 | # Copy all target files from android_dir to arc_dir, setting ACLs. |
| 340 | for targetfile in gs_context.List(android_dir): |
| 341 | if re.search(pattern, targetfile.url): |
| 342 | basename = os.path.basename(targetfile.url) |
Nicolas Norvez | b08f54d | 2016-12-05 17:58:54 -0800 | [diff] [blame] | 343 | arc_path = os.path.join(arc_dir, _GetArcBasename(build, basename)) |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 344 | acl = acls[build] |
| 345 | needs_copy = True |
khmel@google.com | 96c193e | 2018-05-10 14:00:38 -0700 | [diff] [blame] | 346 | retry_count = 2 |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 347 | |
khmel@google.com | 96c193e | 2018-05-10 14:00:38 -0700 | [diff] [blame] | 348 | # Retry in case race condition when several boards trying to copy the |
| 349 | # same resource |
| 350 | while True: |
| 351 | # Check a pre-existing file with the original source. |
| 352 | if gs_context.Exists(arc_path): |
| 353 | if (gs_context.Stat(targetfile.url).hash_crc32c != |
| 354 | gs_context.Stat(arc_path).hash_crc32c): |
| 355 | logging.warn('Removing incorrect file %s', arc_path) |
| 356 | gs_context.Remove(arc_path) |
| 357 | else: |
| 358 | logging.info('Skipping already copied file %s', arc_path) |
| 359 | needs_copy = False |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 360 | |
khmel@google.com | 96c193e | 2018-05-10 14:00:38 -0700 | [diff] [blame] | 361 | # Copy if necessary, and set the ACL unconditionally. |
| 362 | # The Stat() call above doesn't verify the ACL is correct and |
| 363 | # the ChangeACL should be relatively cheap compared to the copy. |
| 364 | # This covers the following caes: |
| 365 | # - handling an interrupted copy from a previous run. |
| 366 | # - rerunning the copy in case one of the googlestorage_acl_X.txt |
| 367 | # files changes (e.g. we add a new variant which reuses a build). |
| 368 | if needs_copy: |
| 369 | logging.info('Copying %s -> %s (acl %s)', |
| 370 | targetfile.url, arc_path, acl) |
| 371 | try: |
| 372 | gs_context.Copy(targetfile.url, arc_path, version=0) |
| 373 | except gs.GSContextPreconditionFailed as error: |
| 374 | if not retry_count: |
| 375 | raise error |
| 376 | # Retry one more time after a short delay |
| 377 | logging.warning('Will retry copying %s -> %s', |
| 378 | targetfile.url, arc_path) |
| 379 | time.sleep(5) |
| 380 | retry_count = retry_count - 1 |
| 381 | continue |
| 382 | gs_context.ChangeACL(arc_path, acl_args_file=acl) |
| 383 | break |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 384 | |
| 385 | |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 386 | def MirrorArtifacts(android_bucket_url, android_build_branch, arc_bucket_url, |
| 387 | acls, targets, version=None): |
| 388 | """Mirrors artifacts from Android bucket to ARC bucket. |
| 389 | |
| 390 | First, this function identifies which build version should be copied, |
| 391 | if not given. Please see GetLatestBuild() and IsBuildIdValid() for details. |
| 392 | |
| 393 | On build version identified, then copies target artifacts to the ARC bucket, |
| 394 | with setting ACLs. |
| 395 | |
| 396 | Args: |
| 397 | android_bucket_url: URL of Android build gs bucket |
| 398 | android_build_branch: branch of Android builds |
| 399 | arc_bucket_url: URL of the target ARC build gs bucket |
| 400 | acls: ACLs dictionary for each build to copy. |
| 401 | targets: Dict from build key to (targe build suffix, artifact file pattern) |
| 402 | pair. |
| 403 | version: (optional) A string. The Android build id number to check. |
| 404 | If not passed, detect latest good build version. |
| 405 | |
| 406 | Returns: |
| 407 | Mirrored version. |
| 408 | """ |
| 409 | if version: |
| 410 | subpaths = IsBuildIdValid( |
| 411 | android_bucket_url, android_build_branch, version, targets) |
| 412 | if not subpaths: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 413 | logging.error('Requested build %s is not valid', version) |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 414 | else: |
| 415 | version, subpaths = GetLatestBuild( |
| 416 | android_bucket_url, android_build_branch, targets) |
| 417 | |
| 418 | CopyToArcBucket(android_bucket_url, android_build_branch, version, subpaths, |
| 419 | targets, arc_bucket_url, acls) |
khmel@google.com | 50c42dd | 2018-05-11 11:33:35 -0700 | [diff] [blame] | 420 | PackSdkTools(android_build_branch, version, targets, arc_bucket_url, |
| 421 | acls['SDK_TOOLS']) |
khmel@google.com | 778a1cd | 2018-04-13 11:11:58 -0700 | [diff] [blame] | 422 | |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 423 | return version |
| 424 | |
| 425 | |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 426 | def MakeAclDict(package_dir): |
| 427 | """Creates a dictionary of acl files for each build type. |
| 428 | |
| 429 | Args: |
| 430 | package_dir: The path to where the package acl files are stored. |
| 431 | |
| 432 | Returns: |
| 433 | Returns acls dictionary. |
| 434 | """ |
| 435 | return dict( |
| 436 | (k, os.path.join(package_dir, v)) |
| 437 | for k, v in constants.ARC_BUCKET_ACLS.items() |
| 438 | ) |
| 439 | |
| 440 | |
Qijiang Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 441 | def MakeBuildTargetDict(package_name, build_branch): |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 442 | """Creates a dictionary of build targets. |
| 443 | |
Bernie Thompson | 63ed561 | 2017-08-16 12:27:34 -0700 | [diff] [blame] | 444 | Not all targets are common between branches, for example |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 445 | sdk_google_cheets_x86 only exists on N. |
| 446 | This generates a dictionary listing the available build targets for a |
| 447 | specific branch. |
| 448 | |
| 449 | Args: |
Qijiang Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 450 | package_name: package name of chromeos arc package. |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 451 | build_branch: branch of Android builds. |
| 452 | |
| 453 | Returns: |
| 454 | Returns build target dictionary. |
| 455 | |
| 456 | Raises: |
| 457 | ValueError: if the Android build branch is invalid. |
| 458 | """ |
Qijiang Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 459 | if constants.ANDROID_CONTAINER_PACKAGE_KEYWORD in package_name: |
Federico 'Morg' Pareschi | 041ee65 | 2020-03-10 15:09:42 +0900 | [diff] [blame] | 460 | target_list = { |
| 461 | constants.ANDROID_MST_BUILD_BRANCH: |
| 462 | constants.ANDROID_MST_BUILD_TARGETS, |
| 463 | constants.ANDROID_NYC_BUILD_BRANCH: |
| 464 | constants.ANDROID_NYC_BUILD_TARGETS, |
| 465 | constants.ANDROID_PI_BUILD_BRANCH: |
| 466 | constants.ANDROID_PI_BUILD_TARGETS, |
| 467 | constants.ANDROID_QT_BUILD_BRANCH: |
| 468 | constants.ANDROID_QT_BUILD_TARGETS, |
| 469 | constants.ANDROID_RVC_BUILD_BRANCH: |
| 470 | constants.ANDROID_RVC_BUILD_TARGETS, |
| 471 | } |
Qijiang Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 472 | elif constants.ANDROID_VM_PACKAGE_KEYWORD in package_name: |
Federico 'Morg' Pareschi | 041ee65 | 2020-03-10 15:09:42 +0900 | [diff] [blame] | 473 | target_list = { |
| 474 | constants.ANDROID_VMPI_BUILD_BRANCH: |
| 475 | constants.ANDROID_VMPI_BUILD_TARGETS, |
| 476 | constants.ANDROID_VMMST_BUILD_BRANCH: |
| 477 | constants.ANDROID_VMMST_BUILD_TARGETS, |
| 478 | constants.ANDROID_VMRVC_BUILD_BRANCH: |
| 479 | constants.ANDROID_VMRVC_BUILD_TARGETS, |
| 480 | } |
| 481 | else: |
| 482 | raise ValueError('Unknown package: %s' % package_name) |
| 483 | target = target_list.get(build_branch) |
| 484 | if not target: |
Qijiang Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 485 | raise ValueError('Unknown branch: %s' % build_branch) |
Federico 'Morg' Pareschi | 041ee65 | 2020-03-10 15:09:42 +0900 | [diff] [blame] | 486 | return target |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 487 | |
| 488 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 489 | def GetAndroidRevisionListLink(build_branch, old_android, new_android): |
| 490 | """Returns a link to the list of revisions between two Android versions |
| 491 | |
| 492 | Given two AndroidEBuilds, generate a link to a page that prints the |
| 493 | Android changes between those two revisions, inclusive. |
| 494 | |
| 495 | Args: |
| 496 | build_branch: branch of Android builds |
| 497 | old_android: ebuild for the version to diff from |
| 498 | new_android: ebuild for the version to which to diff |
| 499 | |
| 500 | Returns: |
| 501 | The desired URL. |
| 502 | """ |
| 503 | return _ANDROID_VERSION_URL % {'branch': build_branch, |
Hidehiko Abe | c9ecf26 | 2017-07-05 15:17:41 +0900 | [diff] [blame] | 504 | 'old': old_android.version_no_rev, |
| 505 | 'new': new_android.version_no_rev} |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 506 | |
| 507 | |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 508 | def MarkAndroidEBuildAsStable(stable_candidate, unstable_ebuild, |
| 509 | android_package, android_version, package_dir, |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 510 | build_branch, arc_bucket_url, build_targets): |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 511 | r"""Uprevs the Android ebuild. |
| 512 | |
| 513 | This is the main function that uprevs from a stable candidate |
| 514 | to its new version. |
| 515 | |
| 516 | Args: |
| 517 | stable_candidate: ebuild that corresponds to the stable ebuild we are |
| 518 | revving from. If None, builds the a new ebuild given the version |
| 519 | with revision set to 1. |
| 520 | unstable_ebuild: ebuild corresponding to the unstable ebuild for Android. |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 521 | android_package: android package name. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 522 | android_version: The \d+ build id of Android. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 523 | package_dir: Path to the android-container package dir. |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 524 | build_branch: branch of Android builds. |
| 525 | arc_bucket_url: URL of the target ARC build gs bucket. |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 526 | build_targets: build targets for this particular Android branch. |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 527 | |
| 528 | Returns: |
| 529 | Full portage version atom (including rc's, etc) that was revved. |
| 530 | """ |
| 531 | def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild): |
| 532 | """Returns True if the new ebuild is redundant. |
| 533 | |
| 534 | This is True if there if the current stable ebuild is the exact same copy |
| 535 | of the new one. |
| 536 | """ |
| 537 | if not stable_ebuild: |
| 538 | return False |
| 539 | |
David Riley | 676f540 | 2016-02-12 17:24:23 -0800 | [diff] [blame] | 540 | if stable_candidate.version_no_rev == new_ebuild.version_no_rev: |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 541 | return filecmp.cmp( |
| 542 | new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False) |
| 543 | |
| 544 | # Case where we have the last stable candidate with same version just rev. |
David Riley | 676f540 | 2016-02-12 17:24:23 -0800 | [diff] [blame] | 545 | if stable_candidate and stable_candidate.version_no_rev == android_version: |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 546 | new_ebuild_path = '%s-r%d.ebuild' % ( |
| 547 | stable_candidate.ebuild_path_no_revision, |
| 548 | stable_candidate.current_revision + 1) |
| 549 | else: |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 550 | pf = '%s-%s-r1' % (android_package, android_version) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 551 | new_ebuild_path = os.path.join(package_dir, '%s.ebuild' % pf) |
| 552 | |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 553 | variables = {'BASE_URL': arc_bucket_url} |
Mike Frysinger | 0bdbc10 | 2019-06-13 15:27:29 -0400 | [diff] [blame] | 554 | for build, (target, _) in build_targets.items(): |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 555 | variables[build + '_TARGET'] = '%s-%s' % (build_branch, target) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 556 | |
| 557 | portage_util.EBuild.MarkAsStable( |
| 558 | unstable_ebuild.ebuild_path, new_ebuild_path, |
| 559 | variables, make_stable=True) |
| 560 | new_ebuild = portage_util.EBuild(new_ebuild_path) |
| 561 | |
| 562 | # Determine whether this is ebuild is redundant. |
| 563 | if IsTheNewEBuildRedundant(new_ebuild, stable_candidate): |
| 564 | msg = 'Previous ebuild with same version found and ebuild is redundant.' |
| 565 | logging.info(msg) |
| 566 | os.unlink(new_ebuild_path) |
| 567 | return None |
| 568 | |
| 569 | if stable_candidate: |
| 570 | logging.PrintBuildbotLink('Android revisions', |
| 571 | GetAndroidRevisionListLink(build_branch, |
| 572 | stable_candidate, |
| 573 | new_ebuild)) |
| 574 | |
| 575 | git.RunGit(package_dir, ['add', new_ebuild_path]) |
| 576 | if stable_candidate and not stable_candidate.IsSticky(): |
| 577 | git.RunGit(package_dir, ['rm', stable_candidate.ebuild_path]) |
| 578 | |
| 579 | # Update ebuild manifest and git add it. |
| 580 | gen_manifest_cmd = ['ebuild', new_ebuild_path, 'manifest', '--force'] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 581 | cros_build_lib.run(gen_manifest_cmd, extra_env=None, print_cmd=True) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 582 | git.RunGit(package_dir, ['add', 'Manifest']) |
| 583 | |
| 584 | portage_util.EBuild.CommitChange( |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 585 | _GIT_COMMIT_MESSAGE % {'android_package': android_package, |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 586 | 'android_version': android_version}, |
| 587 | package_dir) |
| 588 | |
| 589 | return '%s-%s' % (new_ebuild.package, new_ebuild.version) |
| 590 | |
| 591 | |
| 592 | def GetParser(): |
| 593 | """Creates the argument parser.""" |
| 594 | parser = commandline.ArgumentParser() |
| 595 | parser.add_argument('-b', '--boards') |
| 596 | parser.add_argument('--android_bucket_url', |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 597 | default=constants.ANDROID_BUCKET_URL, |
| 598 | type='gs_path') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 599 | parser.add_argument('--android_build_branch', |
Shuhei Takahashi | 6d02c19 | 2017-04-05 14:01:24 +0900 | [diff] [blame] | 600 | required=True, |
| 601 | help='Android branch to import from. ' |
| 602 | 'Ex: git_mnc-dr-arc-dev') |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 603 | parser.add_argument('--android_package', |
| 604 | default=constants.ANDROID_PACKAGE_NAME) |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 605 | parser.add_argument('--arc_bucket_url', |
| 606 | default=constants.ARC_BUCKET_URL, |
| 607 | type='gs_path') |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 608 | parser.add_argument('-f', '--force_version', |
| 609 | help='Android build id to use') |
| 610 | parser.add_argument('-s', '--srcroot', |
| 611 | default=os.path.join(os.environ['HOME'], 'trunk', 'src'), |
| 612 | help='Path to the src directory') |
| 613 | parser.add_argument('-t', '--tracking_branch', default='cros/master', |
| 614 | help='Branch we are tracking changes against') |
| 615 | return parser |
| 616 | |
| 617 | |
| 618 | def main(argv): |
Hidehiko Abe | c9ecf26 | 2017-07-05 15:17:41 +0900 | [diff] [blame] | 619 | logging.EnableBuildbotMarkers() |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 620 | parser = GetParser() |
| 621 | options = parser.parse_args(argv) |
| 622 | options.Freeze() |
| 623 | |
| 624 | overlay_dir = os.path.abspath(_OVERLAY_DIR % {'srcroot': options.srcroot}) |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 625 | android_package_dir = os.path.join( |
| 626 | overlay_dir, |
| 627 | portage_util.GetFullAndroidPortagePackageName(options.android_package)) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 628 | version_to_uprev = None |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 629 | |
| 630 | (unstable_ebuild, stable_ebuilds) = FindAndroidCandidates(android_package_dir) |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 631 | acls = MakeAclDict(android_package_dir) |
Qijiang Fan | 6588cc9 | 2019-11-20 13:26:04 +0900 | [diff] [blame] | 632 | build_targets = MakeBuildTargetDict(options.android_package, |
| 633 | options.android_build_branch) |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 634 | # Mirror artifacts, i.e., images and some sdk tools (e.g., adb, aapt). |
| 635 | version_to_uprev = MirrorArtifacts(options.android_bucket_url, |
| 636 | options.android_build_branch, |
| 637 | options.arc_bucket_url, acls, |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 638 | build_targets, |
Hidehiko Abe | 1ebc25d | 2016-07-28 02:24:37 +0900 | [diff] [blame] | 639 | options.force_version) |
| 640 | |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 641 | stable_candidate = portage_util.BestEBuild(stable_ebuilds) |
| 642 | |
| 643 | if stable_candidate: |
Lann Martin | ffb9516 | 2018-08-28 12:02:54 -0600 | [diff] [blame] | 644 | logging.info('Stable candidate found %s', stable_candidate.version) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 645 | else: |
| 646 | logging.info('No stable candidate found.') |
| 647 | |
| 648 | tracking_branch = 'remotes/m/%s' % os.path.basename(options.tracking_branch) |
| 649 | existing_branch = git.GetCurrentBranch(android_package_dir) |
| 650 | work_branch = cros_mark_as_stable.GitBranch(constants.STABLE_EBUILD_BRANCH, |
| 651 | tracking_branch, |
| 652 | android_package_dir) |
| 653 | work_branch.CreateBranch() |
| 654 | |
| 655 | # In the case of uprevving overlays that have patches applied to them, |
| 656 | # include the patched changes in the stabilizing branch. |
| 657 | if existing_branch: |
| 658 | git.RunGit(overlay_dir, ['rebase', existing_branch]) |
| 659 | |
| 660 | android_version_atom = MarkAndroidEBuildAsStable( |
Hidehiko Abe | 4fd94ae | 2017-01-24 18:59:55 +0900 | [diff] [blame] | 661 | stable_candidate, unstable_ebuild, options.android_package, |
David Riley | 73f00d9 | 2016-02-16 18:54:20 -0800 | [diff] [blame] | 662 | version_to_uprev, android_package_dir, |
Nicolas Norvez | 4bd854f | 2017-05-23 10:04:45 -0700 | [diff] [blame] | 663 | options.android_build_branch, options.arc_bucket_url, build_targets) |
David Riley | c0da9d9 | 2016-02-01 12:11:01 -0800 | [diff] [blame] | 664 | if android_version_atom: |
| 665 | if options.boards: |
| 666 | cros_mark_as_stable.CleanStalePackages(options.srcroot, |
| 667 | options.boards.split(':'), |
| 668 | [android_version_atom]) |
| 669 | |
| 670 | # Explicit print to communicate to caller. |
| 671 | print('ANDROID_VERSION_ATOM=%s' % android_version_atom) |