blob: 5047ef977819f9ef0223f282cb76bf09788d94a7 [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 Leed66bc992022-10-26 22:35:50 +090047 build_id, _ = android.GetLatestBuild(input_proto.android_package)
Alex Klein1699fab2022-09-08 08:46:06 -060048 output_proto.android_version = build_id
Shao-Chuan Lee01dee222021-04-09 15:28:08 +090049
50
Michael Mortensen25626442019-11-22 10:06:59 -070051def _MarkStableResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060052 """Add fake status to a successful response."""
53 output_proto.android_atom.category = "category"
54 output_proto.android_atom.package_name = "android-package-name"
55 output_proto.android_atom.version = "1.2"
56 output_proto.status = android_pb2.MARK_STABLE_STATUS_SUCCESS
Michael Mortensen25626442019-11-22 10:06:59 -070057
58
59@faux.success(_MarkStableResponse)
60@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060061@validate.require("package_name")
Alex Klein231d2da2019-07-22 16:44:45 -060062@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -060063def MarkStable(
64 input_proto: android_pb2.MarkStableRequest,
65 output_proto: android_pb2.MarkStableResponse,
66 _config: "api_config.ApiConfig",
67) -> None:
68 """Uprev Android, if able.
LaMont Jones8a1644f2019-04-16 14:30:17 -060069
Alex Klein1699fab2022-09-08 08:46:06 -060070 Uprev Android, verify that the newly uprevved package can be emerged, and
71 return the new package info.
LaMont Jones8a1644f2019-04-16 14:30:17 -060072
Alex Klein1699fab2022-09-08 08:46:06 -060073 See AndroidService documentation in api/proto/android.proto.
LaMont Jones8a1644f2019-04-16 14:30:17 -060074
Alex Klein1699fab2022-09-08 08:46:06 -060075 Args:
Alex Klein611dddd2022-10-11 17:02:01 -060076 input_proto: The input proto.
77 output_proto: The output proto.
78 _config: The call config.
Alex Klein1699fab2022-09-08 08:46:06 -060079 """
80 chroot = controller_util.ParseChroot(input_proto.chroot)
81 build_targets = controller_util.ParseBuildTargets(input_proto.build_targets)
82 package_name = input_proto.package_name
83 android_build_branch = input_proto.android_build_branch
84 android_version = input_proto.android_version
85 skip_commit = input_proto.skip_commit
LaMont Jones8a1644f2019-04-16 14:30:17 -060086
Alex Klein1699fab2022-09-08 08:46:06 -060087 # Assume success.
88 output_proto.status = android_pb2.MARK_STABLE_STATUS_SUCCESS
89 # TODO(crbug/904939): This should move to service/android.py and the port
90 # should be finished.
91 android_atom_to_build = None
92 try:
93 result = packages.uprev_android(
94 android_package=package_name,
95 chroot=chroot,
96 build_targets=build_targets,
97 android_build_branch=android_build_branch,
98 android_version=android_version,
99 skip_commit=skip_commit,
100 )
101 if result.revved:
102 android_atom_to_build = result.android_atom
103 except packages.AndroidIsPinnedUprevError as e:
104 # If the uprev failed due to a pin, CI needs to unpin and retry.
105 android_atom_to_build = e.new_android_atom
106 output_proto.status = android_pb2.MARK_STABLE_STATUS_PINNED
LaMont Jones8a1644f2019-04-16 14:30:17 -0600107
Alex Klein1699fab2022-09-08 08:46:06 -0600108 if android_atom_to_build:
109 pkg = package_info.parse(android_atom_to_build)
110 controller_util.serialize_package_info(pkg, output_proto.android_atom)
111 else:
112 output_proto.status = android_pb2.MARK_STABLE_STATUS_EARLY_EXIT
LaMont Jones8a1644f2019-04-16 14:30:17 -0600113
Alex Klein2b236722019-06-19 15:44:26 -0600114
Michael Mortensen25626442019-11-22 10:06:59 -0700115# We don't use @faux.success for UnpinVersion because output_proto is unused.
Alex Klein076841b2019-08-29 15:19:39 -0600116@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600117@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600118def UnpinVersion(
119 _input_proto: android_pb2.UnpinVersionRequest,
120 _output_proto: android_pb2.UnpinVersionResponse,
121 _config: "api_config.ApiConfig",
122) -> None:
123 """Unpin the Android version.
LaMont Jones8a1644f2019-04-16 14:30:17 -0600124
Alex Klein1699fab2022-09-08 08:46:06 -0600125 See AndroidService documentation in api/proto/android.proto.
LaMont Jones8a1644f2019-04-16 14:30:17 -0600126
Alex Klein1699fab2022-09-08 08:46:06 -0600127 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600128 _input_proto: The input proto. (not used.)
129 _output_proto: The output proto. (not used.)
130 _config: The call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600131 """
132 osutils.SafeUnlink(ANDROIDPIN_MASK_PATH)
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900133
134
135def _WriteLKGBResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600136 """Fake WriteLKGB response."""
137 output_proto.modified_files.append("fake_file")
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900138
139
140@faux.success(_WriteLKGBResponse)
141@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600142@validate.require("android_package", "android_version")
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900143@validate.validation_complete
144def WriteLKGB(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600145 android_package = input_proto.android_package
146 android_version = input_proto.android_version
147 android_package_dir = android.GetAndroidPackageDir(android_package)
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900148
Alex Klein1699fab2022-09-08 08:46:06 -0600149 # Attempt to read current LKGB, if available.
150 current_lkgb = None
151 try:
152 current_lkgb = android.ReadLKGB(android_package_dir)
153 except android.MissingLKGBError:
154 logging.info("LKGB file is missing, creating a new one.")
155 except android.InvalidLKGBError:
156 logging.warning("Current LKGB file is invalid, overwriting.")
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900157
Alex Klein1699fab2022-09-08 08:46:06 -0600158 # Do nothing if LKGB is already set to the requested version.
159 if current_lkgb == android_version:
160 logging.warning(
161 "LKGB of %s is already %s, doing nothing.",
162 android_package,
163 android_version,
164 )
165 return
Shao-Chuan Leeddcca812021-12-10 15:11:16 +0900166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 # Actually update LKGB.
168 lkgb = android.WriteLKGB(android_package_dir, android_version)
169 output_proto.modified_files.append(lkgb)