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 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | @validate.require_any("android_build_branch", "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): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | branch = ( |
| 48 | input_proto.android_build_branch |
| 49 | or android.GetAndroidBranchForPackage(input_proto.android_package) |
| 50 | ) |
| 51 | build_id, _ = android.GetLatestBuild(branch) |
| 52 | output_proto.android_version = build_id |
Shao-Chuan Lee | 01dee22 | 2021-04-09 15:28:08 +0900 | [diff] [blame] | 53 | |
| 54 | |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 55 | def _MarkStableResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | """Add fake status to a successful response.""" |
| 57 | output_proto.android_atom.category = "category" |
| 58 | output_proto.android_atom.package_name = "android-package-name" |
| 59 | output_proto.android_atom.version = "1.2" |
| 60 | output_proto.status = android_pb2.MARK_STABLE_STATUS_SUCCESS |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 61 | |
| 62 | |
| 63 | @faux.success(_MarkStableResponse) |
| 64 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | @validate.require("package_name") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 66 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | def MarkStable( |
| 68 | input_proto: android_pb2.MarkStableRequest, |
| 69 | output_proto: android_pb2.MarkStableResponse, |
| 70 | _config: "api_config.ApiConfig", |
| 71 | ) -> None: |
| 72 | """Uprev Android, if able. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | Uprev Android, verify that the newly uprevved package can be emerged, and |
| 75 | return the new package info. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | See AndroidService documentation in api/proto/android.proto. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 78 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 80 | input_proto: The input proto. |
| 81 | output_proto: The output proto. |
| 82 | _config: The call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 83 | """ |
| 84 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 85 | build_targets = controller_util.ParseBuildTargets(input_proto.build_targets) |
| 86 | package_name = input_proto.package_name |
| 87 | android_build_branch = input_proto.android_build_branch |
| 88 | android_version = input_proto.android_version |
| 89 | skip_commit = input_proto.skip_commit |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | # Assume success. |
| 92 | output_proto.status = android_pb2.MARK_STABLE_STATUS_SUCCESS |
| 93 | # TODO(crbug/904939): This should move to service/android.py and the port |
| 94 | # should be finished. |
| 95 | android_atom_to_build = None |
| 96 | try: |
| 97 | result = packages.uprev_android( |
| 98 | android_package=package_name, |
| 99 | chroot=chroot, |
| 100 | build_targets=build_targets, |
| 101 | android_build_branch=android_build_branch, |
| 102 | android_version=android_version, |
| 103 | skip_commit=skip_commit, |
| 104 | ) |
| 105 | if result.revved: |
| 106 | android_atom_to_build = result.android_atom |
| 107 | except packages.AndroidIsPinnedUprevError as e: |
| 108 | # If the uprev failed due to a pin, CI needs to unpin and retry. |
| 109 | android_atom_to_build = e.new_android_atom |
| 110 | output_proto.status = android_pb2.MARK_STABLE_STATUS_PINNED |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 111 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | if android_atom_to_build: |
| 113 | pkg = package_info.parse(android_atom_to_build) |
| 114 | controller_util.serialize_package_info(pkg, output_proto.android_atom) |
| 115 | else: |
| 116 | output_proto.status = android_pb2.MARK_STABLE_STATUS_EARLY_EXIT |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 117 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 118 | |
Michael Mortensen | 2562644 | 2019-11-22 10:06:59 -0700 | [diff] [blame] | 119 | # 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] | 120 | @faux.all_empty |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 121 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 122 | def UnpinVersion( |
| 123 | _input_proto: android_pb2.UnpinVersionRequest, |
| 124 | _output_proto: android_pb2.UnpinVersionResponse, |
| 125 | _config: "api_config.ApiConfig", |
| 126 | ) -> None: |
| 127 | """Unpin the Android version. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | See AndroidService documentation in api/proto/android.proto. |
LaMont Jones | 8a1644f | 2019-04-16 14:30:17 -0600 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 132 | _input_proto: The input proto. (not used.) |
| 133 | _output_proto: The output proto. (not used.) |
| 134 | _config: The call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 135 | """ |
| 136 | osutils.SafeUnlink(ANDROIDPIN_MASK_PATH) |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def _WriteLKGBResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 140 | """Fake WriteLKGB response.""" |
| 141 | output_proto.modified_files.append("fake_file") |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 142 | |
| 143 | |
| 144 | @faux.success(_WriteLKGBResponse) |
| 145 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | @validate.require("android_package", "android_version") |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 147 | @validate.validation_complete |
| 148 | def WriteLKGB(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | android_package = input_proto.android_package |
| 150 | android_version = input_proto.android_version |
| 151 | android_package_dir = android.GetAndroidPackageDir(android_package) |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 152 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | # Attempt to read current LKGB, if available. |
| 154 | current_lkgb = None |
| 155 | try: |
| 156 | current_lkgb = android.ReadLKGB(android_package_dir) |
| 157 | except android.MissingLKGBError: |
| 158 | logging.info("LKGB file is missing, creating a new one.") |
| 159 | except android.InvalidLKGBError: |
| 160 | logging.warning("Current LKGB file is invalid, overwriting.") |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 162 | # Do nothing if LKGB is already set to the requested version. |
| 163 | if current_lkgb == android_version: |
| 164 | logging.warning( |
| 165 | "LKGB of %s is already %s, doing nothing.", |
| 166 | android_package, |
| 167 | android_version, |
| 168 | ) |
| 169 | return |
Shao-Chuan Lee | ddcca81 | 2021-12-10 15:11:16 +0900 | [diff] [blame] | 170 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | # Actually update LKGB. |
| 172 | lkgb = android.WriteLKGB(android_package_dir, android_version) |
| 173 | output_proto.modified_files.append(lkgb) |