Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Android operations.""" |
| 6 | |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 7 | import logging |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 8 | import os |
Eric Lin | 63d3971 | 2021-08-19 10:11:27 +0000 | [diff] [blame] | 9 | from typing import TYPE_CHECKING |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 10 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 11 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 12 | from chromite.api import validate |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 13 | from chromite.api.controller import controller_util |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 14 | from chromite.api.gen.chromite.api import android_pb2 |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 15 | from chromite.lib import constants |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 16 | from chromite.lib import osutils |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 17 | from chromite.lib.parser import package_info |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame] | 18 | from chromite.service import android |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 19 | from chromite.service import packages |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 20 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 21 | |
Eric Lin | 63d3971 | 2021-08-19 10:11:27 +0000 | [diff] [blame] | 22 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 23 | from chromite.api import api_config |
Eric Lin | 63d3971 | 2021-08-19 10:11:27 +0000 | [diff] [blame] | 24 | |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 25 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | ANDROIDPIN_MASK_PATH = os.path.join( |
| 27 | constants.SOURCE_ROOT, |
| 28 | constants.CHROMIUMOS_OVERLAY_DIR, |
| 29 | "profiles", |
| 30 | "default", |
| 31 | "linux", |
| 32 | "package.mask", |
| 33 | "androidpin", |
| 34 | ) |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 35 | |
| 36 | |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame] | 37 | def _GetLatestBuildResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | """Fake GetLatestBuild response.""" |
| 39 | output_proto.android_version = "7123456" |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame] | 40 | |
| 41 | |
| 42 | @faux.success(_GetLatestBuildResponse) |
| 43 | @faux.empty_error |
Shao-Chuan Lee | d66bc99 | 2022-10-26 22:35:50 +0900 | [diff] [blame] | 44 | @validate.require("android_package") |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame] | 45 | @validate.validation_complete |
| 46 | def GetLatestBuild(input_proto, output_proto, _config): |
Shao-Chuan Lee | 81d3c43 | 2022-11-15 12:44:37 +0900 | [diff] [blame] | 47 | build_id, _ = android.GetLatestBuild( |
| 48 | input_proto.android_package, |
| 49 | build_branch=input_proto.android_build_branch, |
| 50 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | output_proto.android_version = build_id |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame] | 52 | |
| 53 | |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 54 | def _MarkStableResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | """Add fake status to a successful response.""" |
| 56 | output_proto.android_atom.category = "category" |
| 57 | output_proto.android_atom.package_name = "android-package-name" |
| 58 | output_proto.android_atom.version = "1.2" |
| 59 | output_proto.status = android_pb2.MARK_STABLE_STATUS_SUCCESS |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 60 | |
| 61 | |
| 62 | @faux.success(_MarkStableResponse) |
| 63 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | @validate.require("package_name") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 65 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | def MarkStable( |
| 67 | input_proto: android_pb2.MarkStableRequest, |
| 68 | output_proto: android_pb2.MarkStableResponse, |
| 69 | _config: "api_config.ApiConfig", |
| 70 | ) -> None: |
| 71 | """Uprev Android, if able. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | Uprev Android, verify that the newly uprevved package can be emerged, and |
| 74 | return the new package info. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | See AndroidService documentation in api/proto/android.proto. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 77 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 79 | input_proto: The input proto. |
| 80 | output_proto: The output proto. |
| 81 | _config: The call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | """ |
| 83 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 84 | build_targets = controller_util.ParseBuildTargets(input_proto.build_targets) |
| 85 | package_name = input_proto.package_name |
| 86 | android_build_branch = input_proto.android_build_branch |
| 87 | android_version = input_proto.android_version |
| 88 | skip_commit = input_proto.skip_commit |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | # Assume success. |
| 91 | output_proto.status = android_pb2.MARK_STABLE_STATUS_SUCCESS |
| 92 | # TODO(crbug/904939): This should move to service/android.py and the port |
| 93 | # should be finished. |
| 94 | android_atom_to_build = None |
| 95 | try: |
| 96 | result = packages.uprev_android( |
| 97 | android_package=package_name, |
| 98 | chroot=chroot, |
| 99 | build_targets=build_targets, |
| 100 | android_build_branch=android_build_branch, |
| 101 | android_version=android_version, |
| 102 | skip_commit=skip_commit, |
| 103 | ) |
| 104 | if result.revved: |
| 105 | android_atom_to_build = result.android_atom |
| 106 | except packages.AndroidIsPinnedUprevError as e: |
| 107 | # If the uprev failed due to a pin, CI needs to unpin and retry. |
| 108 | android_atom_to_build = e.new_android_atom |
| 109 | output_proto.status = android_pb2.MARK_STABLE_STATUS_PINNED |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 110 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | if android_atom_to_build: |
| 112 | pkg = package_info.parse(android_atom_to_build) |
| 113 | controller_util.serialize_package_info(pkg, output_proto.android_atom) |
| 114 | else: |
| 115 | output_proto.status = android_pb2.MARK_STABLE_STATUS_EARLY_EXIT |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 116 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 117 | |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 118 | # We don't use @faux.success for UnpinVersion because output_proto is unused. |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 119 | @faux.all_empty |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 120 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | def UnpinVersion( |
| 122 | _input_proto: android_pb2.UnpinVersionRequest, |
| 123 | _output_proto: android_pb2.UnpinVersionResponse, |
| 124 | _config: "api_config.ApiConfig", |
| 125 | ) -> None: |
| 126 | """Unpin the Android version. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 127 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 128 | See AndroidService documentation in api/proto/android.proto. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 129 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 130 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 131 | _input_proto: The input proto. (not used.) |
| 132 | _output_proto: The output proto. (not used.) |
| 133 | _config: The call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | """ |
| 135 | osutils.SafeUnlink(ANDROIDPIN_MASK_PATH) |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 136 | |
| 137 | |
| 138 | def _WriteLKGBResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | """Fake WriteLKGB response.""" |
| 140 | output_proto.modified_files.append("fake_file") |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 141 | |
| 142 | |
| 143 | @faux.success(_WriteLKGBResponse) |
| 144 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | @validate.require("android_package", "android_version") |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 146 | @validate.validation_complete |
| 147 | def WriteLKGB(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | android_package = input_proto.android_package |
| 149 | android_version = input_proto.android_version |
| 150 | android_package_dir = android.GetAndroidPackageDir(android_package) |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 151 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | # Attempt to read current LKGB, if available. |
| 153 | current_lkgb = None |
| 154 | try: |
| 155 | current_lkgb = android.ReadLKGB(android_package_dir) |
| 156 | except android.MissingLKGBError: |
| 157 | logging.info("LKGB file is missing, creating a new one.") |
| 158 | except android.InvalidLKGBError: |
| 159 | logging.warning("Current LKGB file is invalid, overwriting.") |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 160 | |
Shao-Chuan Lee | e0b9ba9 | 2023-01-18 19:35:36 +0900 | [diff] [blame] | 161 | # For release branches: check if the runtime artifacts pin exists. |
| 162 | milestone = packages.determine_milestone_version() |
| 163 | runtime_artifacts_pin = android.FindRuntimeArtifactsPin( |
| 164 | android_package, milestone |
| 165 | ) |
| 166 | if runtime_artifacts_pin is not None: |
| 167 | logging.info( |
| 168 | "Found runtime artifacts pin for M%s: %s", |
| 169 | milestone, |
| 170 | runtime_artifacts_pin, |
| 171 | ) |
| 172 | |
| 173 | lkgb = android.LKGB( |
| 174 | build_id=android_version, runtime_artifacts_pin=runtime_artifacts_pin |
| 175 | ) |
| 176 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 177 | # Do nothing if LKGB is already set to the requested version. |
Shao-Chuan Lee | e0b9ba9 | 2023-01-18 19:35:36 +0900 | [diff] [blame] | 178 | if current_lkgb == lkgb: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 179 | logging.warning( |
| 180 | "LKGB of %s is already %s, doing nothing.", |
| 181 | android_package, |
| 182 | android_version, |
| 183 | ) |
| 184 | return |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 185 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | # Actually update LKGB. |
Shao-Chuan Lee | e0b9ba9 | 2023-01-18 19:35:36 +0900 | [diff] [blame] | 187 | modified = android.WriteLKGB(android_package_dir, lkgb) |
| 188 | output_proto.modified_files.append(modified) |