blob: 603c312c4dae6cc6c3ee88fb0146019146481a4f [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
LaMont Jones8a1644f2019-04-16 14:30:17 -06002# 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 Leeddcca812021-12-10 15:11:16 +09007import logging
LaMont Jones8a1644f2019-04-16 14:30:17 -06008import os
Eric Lin63d39712021-08-19 10:11:27 +00009from typing import TYPE_CHECKING
LaMont Jones8a1644f2019-04-16 14:30:17 -060010
Alex Klein076841b2019-08-29 15:19:39 -060011from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060012from chromite.api import validate
Alex Klein4de25e82019-08-05 15:58:39 -060013from chromite.api.controller import controller_util
LaMont Jones8a1644f2019-04-16 14:30:17 -060014from chromite.api.gen.chromite.api import android_pb2
LaMont Jones8a1644f2019-04-16 14:30:17 -060015from chromite.lib import constants
LaMont Jones8a1644f2019-04-16 14:30:17 -060016from chromite.lib import osutils
Alex Klein18a60af2020-06-11 12:08:47 -060017from chromite.lib.parser import package_info
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090018from chromite.service import android
Alex Klein4de25e82019-08-05 15:58:39 -060019from chromite.service import packages
LaMont Jones8a1644f2019-04-16 14:30:17 -060020
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040021
Eric Lin63d39712021-08-19 10:11:27 +000022if TYPE_CHECKING:
Alex Klein1699fab2022-09-08 08:46:06 -060023 from chromite.api import api_config
Eric Lin63d39712021-08-19 10:11:27 +000024
LaMont Jones8a1644f2019-04-16 14:30:17 -060025
Alex Klein1699fab2022-09-08 08:46:06 -060026ANDROIDPIN_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 Jones8a1644f2019-04-16 14:30:17 -060035
36
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090037def _GetLatestBuildResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060038 """Fake GetLatestBuild response."""
39 output_proto.android_version = "7123456"
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090040
41
42@faux.success(_GetLatestBuildResponse)
43@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060044@validate.require_any("android_build_branch", "android_package")
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090045@validate.validation_complete
46def GetLatestBuild(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060047 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 Lee01dee222021-04-09 15:28:08 +090053
54
Michael Mortensen25626442019-11-22 10:06:59 -070055def _MarkStableResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060056 """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 Mortensen25626442019-11-22 10:06:59 -070061
62
63@faux.success(_MarkStableResponse)
64@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060065@validate.require("package_name")
Alex Klein231d2da2019-07-22 16:44:45 -060066@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -060067def 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 Jones8a1644f2019-04-16 14:30:17 -060073
Alex Klein1699fab2022-09-08 08:46:06 -060074 Uprev Android, verify that the newly uprevved package can be emerged, and
75 return the new package info.
LaMont Jones8a1644f2019-04-16 14:30:17 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 See AndroidService documentation in api/proto/android.proto.
LaMont Jones8a1644f2019-04-16 14:30:17 -060078
Alex Klein1699fab2022-09-08 08:46:06 -060079 Args:
Alex Klein611dddd2022-10-11 17:02:01 -060080 input_proto: The input proto.
81 output_proto: The output proto.
82 _config: The call config.
Alex Klein1699fab2022-09-08 08:46:06 -060083 """
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 Jones8a1644f2019-04-16 14:30:17 -060090
Alex Klein1699fab2022-09-08 08:46:06 -060091 # 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 Jones8a1644f2019-04-16 14:30:17 -0600111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 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 Jones8a1644f2019-04-16 14:30:17 -0600117
Alex Klein2b236722019-06-19 15:44:26 -0600118
Michael Mortensen25626442019-11-22 10:06:59 -0700119# We don't use @faux.success for UnpinVersion because output_proto is unused.
Alex Klein076841b2019-08-29 15:19:39 -0600120@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600121@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600122def 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 Jones8a1644f2019-04-16 14:30:17 -0600128
Alex Klein1699fab2022-09-08 08:46:06 -0600129 See AndroidService documentation in api/proto/android.proto.
LaMont Jones8a1644f2019-04-16 14:30:17 -0600130
Alex Klein1699fab2022-09-08 08:46:06 -0600131 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600132 _input_proto: The input proto. (not used.)
133 _output_proto: The output proto. (not used.)
134 _config: The call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600135 """
136 osutils.SafeUnlink(ANDROIDPIN_MASK_PATH)
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900137
138
139def _WriteLKGBResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600140 """Fake WriteLKGB response."""
141 output_proto.modified_files.append("fake_file")
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900142
143
144@faux.success(_WriteLKGBResponse)
145@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600146@validate.require("android_package", "android_version")
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900147@validate.validation_complete
148def WriteLKGB(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600149 android_package = input_proto.android_package
150 android_version = input_proto.android_version
151 android_package_dir = android.GetAndroidPackageDir(android_package)
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900152
Alex Klein1699fab2022-09-08 08:46:06 -0600153 # 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 Leeddcca812021-12-10 15:11:16 +0900161
Alex Klein1699fab2022-09-08 08:46:06 -0600162 # 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 Leeddcca812021-12-10 15:11:16 +0900170
Alex Klein1699fab2022-09-08 08:46:06 -0600171 # Actually update LKGB.
172 lkgb = android.WriteLKGB(android_package_dir, android_version)
173 output_proto.modified_files.append(lkgb)