blob: ab8111f35b07c523c2047a55d33761165b44da46 [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
Shao-Chuan Leed66bc992022-10-26 22:35:50 +090044@validate.require("android_package")
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090045@validate.validation_complete
46def GetLatestBuild(input_proto, output_proto, _config):
Shao-Chuan Lee81d3c432022-11-15 12:44:37 +090047 build_id, _ = android.GetLatestBuild(
48 input_proto.android_package,
49 build_branch=input_proto.android_build_branch,
50 )
Alex Klein1699fab2022-09-08 08:46:06 -060051 output_proto.android_version = build_id
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090052
53
Michael Mortensen25626442019-11-22 10:06:59 -070054def _MarkStableResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060055 """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 Mortensen25626442019-11-22 10:06:59 -070060
61
62@faux.success(_MarkStableResponse)
63@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060064@validate.require("package_name")
Alex Klein231d2da2019-07-22 16:44:45 -060065@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -060066def 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 Jones8a1644f2019-04-16 14:30:17 -060072
Alex Klein1699fab2022-09-08 08:46:06 -060073 Uprev Android, verify that the newly uprevved package can be emerged, and
74 return the new package info.
LaMont Jones8a1644f2019-04-16 14:30:17 -060075
Alex Klein1699fab2022-09-08 08:46:06 -060076 See AndroidService documentation in api/proto/android.proto.
LaMont Jones8a1644f2019-04-16 14:30:17 -060077
Alex Klein1699fab2022-09-08 08:46:06 -060078 Args:
Alex Klein611dddd2022-10-11 17:02:01 -060079 input_proto: The input proto.
80 output_proto: The output proto.
81 _config: The call config.
Alex Klein1699fab2022-09-08 08:46:06 -060082 """
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 Jones8a1644f2019-04-16 14:30:17 -060089
Alex Klein1699fab2022-09-08 08:46:06 -060090 # 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 Jones8a1644f2019-04-16 14:30:17 -0600110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 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 Jones8a1644f2019-04-16 14:30:17 -0600116
Alex Klein2b236722019-06-19 15:44:26 -0600117
Michael Mortensen25626442019-11-22 10:06:59 -0700118# We don't use @faux.success for UnpinVersion because output_proto is unused.
Alex Klein076841b2019-08-29 15:19:39 -0600119@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600120@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600121def 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 Jones8a1644f2019-04-16 14:30:17 -0600127
Alex Klein1699fab2022-09-08 08:46:06 -0600128 See AndroidService documentation in api/proto/android.proto.
LaMont Jones8a1644f2019-04-16 14:30:17 -0600129
Alex Klein1699fab2022-09-08 08:46:06 -0600130 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600131 _input_proto: The input proto. (not used.)
132 _output_proto: The output proto. (not used.)
133 _config: The call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600134 """
135 osutils.SafeUnlink(ANDROIDPIN_MASK_PATH)
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900136
137
138def _WriteLKGBResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600139 """Fake WriteLKGB response."""
140 output_proto.modified_files.append("fake_file")
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900141
142
143@faux.success(_WriteLKGBResponse)
144@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600145@validate.require("android_package", "android_version")
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900146@validate.validation_complete
147def WriteLKGB(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600148 android_package = input_proto.android_package
149 android_version = input_proto.android_version
Shao-Chuan Lee293389c2023-01-20 17:44:35 +0900150 android_branch = (
151 input_proto.android_branch
152 or android.GetAndroidBranchForPackage(android_package)
153 )
Alex Klein1699fab2022-09-08 08:46:06 -0600154 android_package_dir = android.GetAndroidPackageDir(android_package)
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900155
Alex Klein1699fab2022-09-08 08:46:06 -0600156 # Attempt to read current LKGB, if available.
157 current_lkgb = None
158 try:
159 current_lkgb = android.ReadLKGB(android_package_dir)
160 except android.MissingLKGBError:
161 logging.info("LKGB file is missing, creating a new one.")
162 except android.InvalidLKGBError:
163 logging.warning("Current LKGB file is invalid, overwriting.")
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900164
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900165 # For release branches: check if the runtime artifacts pin exists.
166 milestone = packages.determine_milestone_version()
167 runtime_artifacts_pin = android.FindRuntimeArtifactsPin(
168 android_package, milestone
169 )
170 if runtime_artifacts_pin is not None:
171 logging.info(
172 "Found runtime artifacts pin for M%s: %s",
173 milestone,
174 runtime_artifacts_pin,
175 )
176
177 lkgb = android.LKGB(
Shao-Chuan Lee293389c2023-01-20 17:44:35 +0900178 build_id=android_version,
179 branch=android_branch,
180 runtime_artifacts_pin=runtime_artifacts_pin,
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900181 )
182
Alex Klein1699fab2022-09-08 08:46:06 -0600183 # Do nothing if LKGB is already set to the requested version.
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900184 if current_lkgb == lkgb:
Alex Klein1699fab2022-09-08 08:46:06 -0600185 logging.warning(
186 "LKGB of %s is already %s, doing nothing.",
187 android_package,
188 android_version,
189 )
190 return
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900191
Alex Klein1699fab2022-09-08 08:46:06 -0600192 # Actually update LKGB.
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900193 modified = android.WriteLKGB(android_package_dir, lkgb)
194 output_proto.modified_files.append(modified)