Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -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 | """Utility functions that are useful for controllers.""" |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 6 | |
| 7 | import glob |
Alex Klein | 28e59a6 | 2021-09-09 15:45:14 -0600 | [diff] [blame] | 8 | import logging |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 9 | import os |
Lizzy Presland | 084c20f | 2022-03-30 19:13:50 +0000 | [diff] [blame] | 10 | from typing import Iterable, Optional, TYPE_CHECKING, Union |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 11 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 12 | from chromite.api.gen.chromite.api import sysroot_pb2 |
| 13 | from chromite.api.gen.chromite.api import test_pb2 |
Alex Klein | 1f67cf3 | 2019-10-09 11:13:42 -0600 | [diff] [blame] | 14 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 15 | from chromite.lib import build_target_lib |
George Engelbrecht | 35b6e8d | 2021-06-18 09:43:28 -0600 | [diff] [blame] | 16 | from chromite.lib import chroot_lib |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 17 | from chromite.lib import constants |
Ram Chandrasekar | e08e3ba | 2022-04-04 21:42:27 +0000 | [diff] [blame] | 18 | from chromite.lib import goma_lib |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 19 | from chromite.lib import remoteexec_util |
George Engelbrecht | 35b6e8d | 2021-06-18 09:43:28 -0600 | [diff] [blame] | 20 | from chromite.lib import sysroot_lib |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 21 | from chromite.lib.parser import package_info |
| 22 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 23 | |
Alex Klein | 46c30f3 | 2021-11-10 13:12:50 -0700 | [diff] [blame] | 24 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | from chromite.api.gen.chromiumos.build.api import portage_pb2 |
| 26 | |
Alex Klein | 46c30f3 | 2021-11-10 13:12:50 -0700 | [diff] [blame] | 27 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 28 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | """Base error class for the module.""" |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class InvalidMessageError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | """Invalid message.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 34 | |
| 35 | |
Kevin Shelton | a805636 | 2022-04-04 16:19:23 -0700 | [diff] [blame] | 36 | def ParseChroot(chroot_message: common_pb2.Chroot) -> chroot_lib.Chroot: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | """Create a chroot object from the chroot message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 38 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | Args: |
| 40 | chroot_message: The chroot message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 41 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | Returns: |
| 43 | Chroot: The parsed chroot object. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | Raises: |
| 46 | AssertionError: When the message is not a Chroot message. |
| 47 | """ |
| 48 | assert isinstance(chroot_message, common_pb2.Chroot) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | path = chroot_message.path or constants.DEFAULT_CHROOT_PATH |
| 51 | cache_dir = chroot_message.cache_dir |
| 52 | chrome_root = chroot_message.chrome_dir |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | use_flags = [u.flag for u in chroot_message.env.use_flags] |
| 55 | features = [f.feature for f in chroot_message.env.features] |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | env = {} |
| 58 | if use_flags: |
| 59 | env["USE"] = " ".join(use_flags) |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 60 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 61 | # Make sure it'll use the local source to build chrome when we have it. |
| 62 | if chrome_root: |
| 63 | env["CHROME_ORIGIN"] = "LOCAL_SOURCE" |
Alex Klein | b7485bb | 2019-09-19 13:23:37 -0600 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | if features: |
| 66 | env["FEATURES"] = " ".join(features) |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | chroot = chroot_lib.Chroot( |
| 69 | path=path, cache_dir=cache_dir, chrome_root=chrome_root, env=env |
| 70 | ) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 71 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | return chroot |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 73 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 74 | |
Kevin Shelton | a805636 | 2022-04-04 16:19:23 -0700 | [diff] [blame] | 75 | def ParseSysroot(sysroot_message: sysroot_pb2.Sysroot) -> sysroot_lib.Sysroot: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | """Create a sysroot object from the sysroot message. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 77 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | Args: |
| 79 | sysroot_message: The sysroot message. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | Returns: |
| 82 | Sysroot: The parsed sysroot object. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | Raises: |
| 85 | AssertionError: When the message is not a Sysroot message. |
| 86 | """ |
| 87 | assert isinstance(sysroot_message, sysroot_pb2.Sysroot) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 88 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | return sysroot_lib.Sysroot(sysroot_message.path) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 90 | |
| 91 | |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 92 | def ParseRemoteexecConfig(remoteexec_message: common_pb2.RemoteexecConfig): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | """Parse a remoteexec config message.""" |
| 94 | assert isinstance(remoteexec_message, common_pb2.RemoteexecConfig) |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | if not ( |
| 97 | remoteexec_message.reclient_dir or remoteexec_message.reproxy_cfg_file |
| 98 | ): |
| 99 | return None |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 100 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | return remoteexec_util.Remoteexec( |
| 102 | remoteexec_message.reclient_dir, remoteexec_message.reproxy_cfg_file |
| 103 | ) |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 104 | |
| 105 | |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 106 | def ParseGomaConfig(goma_message, chroot_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | """Parse a goma config message.""" |
| 108 | assert isinstance(goma_message, common_pb2.GomaConfig) |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 109 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | if not goma_message.goma_dir: |
| 111 | return None |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | # Parse the goma config. |
| 114 | chromeos_goma_dir = goma_message.chromeos_goma_dir or None |
| 115 | if goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING: |
| 116 | goma_approach = goma_lib.GomaApproach( |
| 117 | "?staging", "staging-goma.chromium.org", True |
| 118 | ) |
| 119 | elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD: |
| 120 | goma_approach = goma_lib.GomaApproach( |
| 121 | "?prod", "goma.chromium.org", True |
| 122 | ) |
| 123 | else: |
| 124 | goma_approach = goma_lib.GomaApproach( |
| 125 | "?cros", "goma.chromium.org", True |
| 126 | ) |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 127 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 128 | # Note that we are not specifying the goma log_dir so that goma will create |
| 129 | # and use a tmp dir for the logs. |
| 130 | stats_filename = goma_message.stats_file or None |
| 131 | counterz_filename = goma_message.counterz_file or None |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | return goma_lib.Goma( |
| 134 | goma_message.goma_dir, |
| 135 | goma_message.goma_client_json, |
| 136 | stage_name="BuildAPI", |
| 137 | chromeos_goma_dir=chromeos_goma_dir, |
| 138 | chroot_dir=chroot_path, |
| 139 | goma_approach=goma_approach, |
| 140 | stats_filename=stats_filename, |
| 141 | counterz_filename=counterz_filename, |
| 142 | ) |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 143 | |
| 144 | |
Kevin Shelton | a805636 | 2022-04-04 16:19:23 -0700 | [diff] [blame] | 145 | def ParseBuildTarget( |
| 146 | build_target_message: common_pb2.BuildTarget, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | profile_message: Optional[sysroot_pb2.Profile] = None, |
Kevin Shelton | a805636 | 2022-04-04 16:19:23 -0700 | [diff] [blame] | 148 | ) -> build_target_lib.BuildTarget: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | """Create a BuildTarget object from a build_target message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 150 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | Args: |
| 152 | build_target_message: The BuildTarget message. |
| 153 | profile_message: The profile message. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 154 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 155 | Returns: |
| 156 | BuildTarget: The parsed instance. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 157 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | Raises: |
| 159 | AssertionError: When the field is not a BuildTarget message. |
| 160 | """ |
| 161 | assert isinstance(build_target_message, common_pb2.BuildTarget) |
| 162 | assert profile_message is None or isinstance( |
| 163 | profile_message, sysroot_pb2.Profile |
| 164 | ) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 165 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 166 | profile_name = profile_message.name if profile_message else None |
| 167 | return build_target_lib.BuildTarget( |
| 168 | build_target_message.name, profile=profile_name |
| 169 | ) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 170 | |
| 171 | |
| 172 | def ParseBuildTargets(repeated_build_target_field): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | """Create a BuildTarget for each entry in the repeated field. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 174 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | Args: |
| 176 | repeated_build_target_field: The repeated BuildTarget field. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | Returns: |
| 179 | list[BuildTarget]: The parsed BuildTargets. |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 180 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 181 | Raises: |
| 182 | AssertionError: When the field contains non-BuildTarget messages. |
| 183 | """ |
| 184 | return [ParseBuildTarget(target) for target in repeated_build_target_field] |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 185 | |
| 186 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 187 | def serialize_package_info( |
| 188 | pkg_info: package_info.PackageInfo, |
| 189 | pkg_info_msg: Union[common_pb2.PackageInfo, "portage_pb2.Portage.Package"], |
| 190 | ): |
| 191 | """Serialize a PackageInfo object to a PackageInfo proto.""" |
| 192 | if not isinstance(pkg_info, package_info.PackageInfo): |
| 193 | # Allows us to swap everything to serialize_package_info, and search the |
| 194 | # logs for usages that aren't passing though a PackageInfo yet. |
| 195 | logging.warning( |
| 196 | "serialize_package_info: Got a %s instead of a PackageInfo.", |
| 197 | type(pkg_info), |
| 198 | ) |
| 199 | pkg_info = package_info.parse(pkg_info) |
| 200 | pkg_info_msg.package_name = pkg_info.package |
| 201 | if pkg_info.category: |
| 202 | pkg_info_msg.category = pkg_info.category |
| 203 | if pkg_info.vr: |
| 204 | pkg_info_msg.version = pkg_info.vr |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def deserialize_package_info(pkg_info_msg): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | """Deserialize a PackageInfo message to a PackageInfo object.""" |
| 209 | return package_info.parse(PackageInfoToString(pkg_info_msg)) |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 210 | |
| 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | def retrieve_package_log_paths( |
| 213 | packages: Iterable[package_info.PackageInfo], |
| 214 | output_proto: Union[ |
| 215 | sysroot_pb2.InstallPackagesResponse, |
| 216 | sysroot_pb2.InstallToolchainResponse, |
| 217 | test_pb2.BuildTargetUnitTestResponse, |
| 218 | ], |
| 219 | target_sysroot: sysroot_lib.Sysroot, |
| 220 | ) -> None: |
| 221 | """Get the path to the log file for each package that failed to build. |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 222 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 223 | Args: |
| 224 | packages: A list of packages which failed to build. |
| 225 | output_proto: The Response message for a given API call. This response proto |
| 226 | must contain a failed_package_data field. |
| 227 | target_sysroot: The sysroot used by the build step. |
| 228 | """ |
| 229 | for pkg_info in packages: |
| 230 | # Grab the paths to the log files for each failed package from the |
| 231 | # sysroot. |
| 232 | # Logs currently exist within the sysroot in the form of: |
| 233 | # /build/${BOARD}/tmp/portage/logs/$CATEGORY:$PF:$TIMESTAMP.log |
| 234 | failed_pkg_data_msg = output_proto.failed_package_data.add() |
| 235 | serialize_package_info(pkg_info, failed_pkg_data_msg.name) |
| 236 | glob_path = os.path.join( |
| 237 | target_sysroot.portage_logdir, |
| 238 | f"{pkg_info.category}:{pkg_info.pvr}:*.log", |
| 239 | ) |
| 240 | log_files = glob.glob(glob_path) |
| 241 | log_files.sort(reverse=True) |
| 242 | # Omit path if files don't exist for some reason. |
| 243 | if not log_files: |
| 244 | logging.warning( |
| 245 | "Log file for %s was not found. Search path: %s", |
| 246 | pkg_info.cpvr, |
| 247 | glob_path, |
| 248 | ) |
| 249 | continue |
| 250 | failed_pkg_data_msg.log_path.path = log_files[0] |
| 251 | failed_pkg_data_msg.log_path.location = common_pb2.Path.INSIDE |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 252 | |
| 253 | |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 254 | def PackageInfoToCPV(package_info_msg): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | """Helper to translate a PackageInfo message into a CPV.""" |
| 256 | if not package_info_msg or not package_info_msg.package_name: |
| 257 | return None |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 258 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 259 | return package_info.SplitCPV( |
| 260 | PackageInfoToString(package_info_msg), strict=False |
| 261 | ) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 262 | |
| 263 | |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 264 | def PackageInfoToString(package_info_msg): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 265 | """Combine the components into the full package string.""" |
| 266 | # TODO: Use the lib.parser.package_info.PackageInfo class instead. |
| 267 | if not package_info_msg.package_name: |
| 268 | raise ValueError("Invalid PackageInfo message.") |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 269 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | c = ("%s/" % package_info_msg.category) if package_info_msg.category else "" |
| 271 | p = package_info_msg.package_name |
| 272 | v = ("-%s" % package_info_msg.version) if package_info_msg.version else "" |
| 273 | return "%s%s%s" % (c, p, v) |