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