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