Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -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 | |
Alex Klein | e0fa642 | 2019-06-21 12:01:39 -0600 | [diff] [blame] | 5 | """Router class for the Build API. |
| 6 | |
| 7 | Handles routing requests to the appropriate controller and handles service |
| 8 | registration. |
| 9 | """ |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 10 | |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 11 | import collections |
Mike Frysinger | c2e72b4 | 2023-03-03 22:34:04 -0500 | [diff] [blame] | 12 | import contextlib |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 13 | import importlib |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 14 | import logging |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 15 | import os |
Tomasz Tylenda | b429230 | 2021-08-08 18:59:36 +0900 | [diff] [blame] | 16 | from types import ModuleType |
| 17 | from typing import Callable, List, TYPE_CHECKING |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 18 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 19 | from chromite.third_party.google.protobuf import symbol_database |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 20 | |
| 21 | from chromite.api import controller |
| 22 | from chromite.api import field_handler |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 23 | from chromite.api.gen.chromite.api import android_pb2 |
Alex Klein | 54e38e3 | 2019-06-21 14:54:17 -0600 | [diff] [blame] | 24 | from chromite.api.gen.chromite.api import api_pb2 |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 25 | from chromite.api.gen.chromite.api import artifacts_pb2 |
| 26 | from chromite.api.gen.chromite.api import binhost_pb2 |
| 27 | from chromite.api.gen.chromite.api import build_api_pb2 |
Tristan Honscheid | 52ba4d2 | 2023-02-09 11:59:29 -0700 | [diff] [blame] | 28 | from chromite.api.gen.chromite.api import copybot_pb2 |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 29 | from chromite.api.gen.chromite.api import depgraph_pb2 |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 30 | from chromite.api.gen.chromite.api import firmware_pb2 |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 31 | from chromite.api.gen.chromite.api import image_pb2 |
Alex Klein | d4d9caa | 2021-11-10 15:44:52 -0700 | [diff] [blame] | 32 | from chromite.api.gen.chromite.api import metadata_pb2 |
Lizzy Presland | e723c01 | 2022-06-10 05:05:37 +0000 | [diff] [blame] | 33 | from chromite.api.gen.chromite.api import observability_pb2 |
Alex Klein | eb77ffa | 2019-05-28 14:47:44 -0600 | [diff] [blame] | 34 | from chromite.api.gen.chromite.api import packages_pb2 |
George Engelbrecht | fe63c8c | 2019-08-31 22:51:29 -0600 | [diff] [blame] | 35 | from chromite.api.gen.chromite.api import payload_pb2 |
Alexander Liu | 008389c | 2022-06-27 18:30:11 +0000 | [diff] [blame] | 36 | from chromite.api.gen.chromite.api import portage_explorer_pb2 |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 37 | from chromite.api.gen.chromite.api import sdk_pb2 |
| 38 | from chromite.api.gen.chromite.api import sysroot_pb2 |
| 39 | from chromite.api.gen.chromite.api import test_pb2 |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 40 | from chromite.api.gen.chromite.api import toolchain_pb2 |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame^] | 41 | from chromite.lib import constants |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 42 | from chromite.lib import cros_build_lib |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 43 | from chromite.lib import osutils |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 44 | from chromite.utils import memoize |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 45 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 46 | |
Tomasz Tylenda | b429230 | 2021-08-08 18:59:36 +0900 | [diff] [blame] | 47 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | from chromite.third_party import google |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | from chromite.api import api_config |
| 51 | from chromite.api import message_util |
Mike Frysinger | 88770ef | 2021-05-21 11:04:00 -0400 | [diff] [blame] | 52 | |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 53 | MethodData = collections.namedtuple( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | "MethodData", ("service_descriptor", "module_name", "method_descriptor") |
| 55 | ) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 56 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 57 | |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 58 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | """Base error class for the module.""" |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 60 | |
| 61 | |
Alex Klein | d3394c2 | 2020-06-16 14:05:06 -0600 | [diff] [blame] | 62 | class InvalidSdkError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 63 | """Raised when the SDK is invalid or does not exist.""" |
Alex Klein | d3394c2 | 2020-06-16 14:05:06 -0600 | [diff] [blame] | 64 | |
| 65 | |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 66 | class CrosSdkNotRunError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | """Raised when the cros_sdk command could not be run to enter the chroot.""" |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 68 | |
| 69 | |
| 70 | # API Service Errors. |
| 71 | class UnknownServiceError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | """Error raised when the requested service has not been registered.""" |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 73 | |
| 74 | |
| 75 | class ControllerModuleNotDefinedError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | """Error class for when no controller has been defined for a service.""" |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 77 | |
| 78 | |
| 79 | class ServiceControllerNotFoundError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 80 | """Error raised when the service's controller cannot be imported.""" |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 81 | |
| 82 | |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame^] | 83 | class TotSdkError(Error): |
| 84 | """When attempting to run a ToT endpoint inside the SDK.""" |
| 85 | |
| 86 | |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 87 | # API Method Errors. |
| 88 | class UnknownMethodError(Error): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | """The service has been defined in the proto, but the method has not.""" |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 90 | |
| 91 | |
| 92 | class MethodNotFoundError(Error): |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 93 | """The method's implementation cannot be found in the controller.""" |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 94 | |
| 95 | |
| 96 | class Router(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | """Encapsulates the request dispatching logic.""" |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 98 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | REEXEC_INPUT_FILE = "input_proto" |
| 100 | REEXEC_OUTPUT_FILE = "output_proto" |
| 101 | REEXEC_CONFIG_FILE = "config_proto" |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | def __init__(self): |
| 104 | self._services = {} |
| 105 | self._aliases = {} |
| 106 | # All imported generated messages get added to this symbol db. |
| 107 | self._sym_db = symbol_database.Default() |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | # Save the service and method extension info for looking up |
| 110 | # configured extension data. |
| 111 | extensions = build_api_pb2.DESCRIPTOR.extensions_by_name |
| 112 | self._svc_options_ext = extensions["service_options"] |
| 113 | self._method_options_ext = extensions["method_options"] |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 114 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 115 | @memoize.Memoize |
| 116 | def _get_method_data(self, service_name, method_name): |
| 117 | """Get the descriptors and module name for the given Service/Method.""" |
| 118 | try: |
| 119 | svc, module_name = self._services[service_name] |
| 120 | except KeyError: |
| 121 | raise UnknownServiceError( |
| 122 | "The %s service has not been registered." % service_name |
| 123 | ) |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | try: |
| 126 | method_desc = svc.methods_by_name[method_name] |
| 127 | except KeyError: |
| 128 | raise UnknownMethodError( |
| 129 | "The %s method has not been defined in the %s " |
| 130 | "service." % (method_name, service_name) |
| 131 | ) |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | return MethodData( |
| 134 | service_descriptor=svc, |
| 135 | module_name=module_name, |
| 136 | method_descriptor=method_desc, |
| 137 | ) |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 138 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | def get_input_message_instance(self, service_name, method_name): |
| 140 | """Get an empty input message instance for the specified method.""" |
| 141 | method_data = self._get_method_data(service_name, method_name) |
| 142 | return self._sym_db.GetPrototype( |
| 143 | method_data.method_descriptor.input_type |
| 144 | )() |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | def _get_output_message_instance(self, service_name, method_name): |
| 147 | """Get an empty output message instance for the specified method.""" |
| 148 | method_data = self._get_method_data(service_name, method_name) |
| 149 | return self._sym_db.GetPrototype( |
| 150 | method_data.method_descriptor.output_type |
| 151 | )() |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 152 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | def _get_module_name(self, service_name, method_name): |
| 154 | """Get the name of the module containing the endpoint implementation.""" |
| 155 | return self._get_method_data(service_name, method_name).module_name |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | def _get_service_options(self, service_name, method_name): |
| 158 | """Get the configured service options for the endpoint.""" |
| 159 | method_data = self._get_method_data(service_name, method_name) |
| 160 | svc_extensions = method_data.service_descriptor.GetOptions().Extensions |
| 161 | return svc_extensions[self._svc_options_ext] |
Alex Klein | 92341cd | 2020-02-27 14:11:04 -0700 | [diff] [blame] | 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | def _get_method_options(self, service_name, method_name): |
| 164 | """Get the configured method options for the endpoint.""" |
| 165 | method_data = self._get_method_data(service_name, method_name) |
| 166 | method_extensions = ( |
| 167 | method_data.method_descriptor.GetOptions().Extensions |
| 168 | ) |
| 169 | return method_extensions[self._method_options_ext] |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 170 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | def Register(self, proto_module: ModuleType): |
| 172 | """Register the services from a generated proto module. |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 173 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 175 | proto_module: The generated proto module to register. |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 176 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 177 | Raises: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 178 | ServiceModuleNotDefinedError when the service cannot be found in the |
| 179 | provided module. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | """ |
| 181 | services = proto_module.DESCRIPTOR.services_by_name |
| 182 | for service_name, svc in services.items(): |
| 183 | module_name = ( |
| 184 | svc.GetOptions().Extensions[self._svc_options_ext].module |
| 185 | ) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 186 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 187 | if not module_name: |
| 188 | raise ControllerModuleNotDefinedError( |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 189 | "The module must be defined in the service definition: " |
| 190 | f"{proto_module}.{service_name}" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 191 | ) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | self._services[svc.full_name] = (svc, module_name) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | def ListMethods(self): |
| 196 | """List all methods registered with the router.""" |
| 197 | services = [] |
| 198 | for service_name, (svc, _module) in self._services.items(): |
| 199 | svc_visibility = getattr( |
| 200 | svc.GetOptions().Extensions[self._svc_options_ext], |
| 201 | "service_visibility", |
| 202 | build_api_pb2.LV_VISIBLE, |
| 203 | ) |
| 204 | if svc_visibility == build_api_pb2.LV_HIDDEN: |
| 205 | continue |
Alex Klein | 6cce6f6 | 2021-03-02 14:24:05 -0700 | [diff] [blame] | 206 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 207 | for method_name in svc.methods_by_name.keys(): |
| 208 | method_options = self._get_method_options( |
| 209 | service_name, method_name |
| 210 | ) |
| 211 | method_visibility = getattr( |
| 212 | method_options, |
| 213 | "method_visibility", |
| 214 | build_api_pb2.LV_VISIBLE, |
| 215 | ) |
| 216 | if method_visibility == build_api_pb2.LV_HIDDEN: |
| 217 | continue |
| 218 | |
| 219 | services.append("%s/%s" % (service_name, method_name)) |
| 220 | |
| 221 | return sorted(services) |
| 222 | |
| 223 | def Route( |
| 224 | self, |
| 225 | service_name: str, |
| 226 | method_name: str, |
| 227 | config: "api_config.ApiConfig", |
| 228 | input_handler: "message_util.MessageHandler", |
| 229 | output_handlers: List["message_util.MessageHandler"], |
| 230 | config_handler: "message_util.MessageHandler", |
| 231 | ) -> int: |
| 232 | """Dispatch the request. |
| 233 | |
| 234 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 235 | service_name: The fully qualified service name. |
| 236 | method_name: The name of the method being called. |
| 237 | config: The call configs. |
| 238 | input_handler: The request message handler. |
| 239 | output_handlers: The response message handlers. |
| 240 | config_handler: The config message handler. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 241 | |
| 242 | Returns: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 243 | The return code. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 244 | |
| 245 | Raises: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 246 | InvalidInputFileError when the input file cannot be read. |
| 247 | InvalidOutputFileError when the output file cannot be written. |
| 248 | ServiceModuleNotFoundError when the service module cannot be |
| 249 | imported. |
| 250 | MethodNotFoundError when the method cannot be retrieved from the |
| 251 | module. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 252 | """ |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame^] | 253 | # Fetch the method options for chroot and method name overrides. |
| 254 | method_options = self._get_method_options(service_name, method_name) |
| 255 | |
| 256 | # Check the chroot settings before running. |
| 257 | service_options = self._get_service_options(service_name, method_name) |
| 258 | |
| 259 | if self._needs_branch_reexecution( |
| 260 | service_options, method_options, config |
| 261 | ): |
| 262 | logging.info("Re-executing the endpoint on the branched BAPI.") |
| 263 | return self._reexecute_branched( |
| 264 | input_handler, |
| 265 | output_handlers, |
| 266 | config_handler, |
| 267 | service_name, |
| 268 | method_name, |
| 269 | ) |
| 270 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 271 | input_msg = self.get_input_message_instance(service_name, method_name) |
| 272 | input_handler.read_into(input_msg) |
| 273 | |
| 274 | # Get an empty output message instance. |
| 275 | output_msg = self._get_output_message_instance( |
| 276 | service_name, method_name |
| 277 | ) |
| 278 | |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame^] | 279 | chroot_reexec = self._needs_chroot_reexecution( |
| 280 | service_options, method_options, config |
| 281 | ) |
Alex Klein | 6cce6f6 | 2021-03-02 14:24:05 -0700 | [diff] [blame] | 282 | |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame^] | 283 | if chroot_reexec and not constants.IS_BRANCHED_CHROMITE: |
| 284 | # Can't run inside the SDK with ToT chromite. |
| 285 | raise TotSdkError( |
| 286 | f"Cannot run ToT {service_name}/{method_name} inside the SDK." |
| 287 | ) |
| 288 | elif chroot_reexec: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 289 | logging.info("Re-executing the endpoint inside the chroot.") |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame^] | 290 | return self._reexecute_inside( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 291 | input_msg, |
| 292 | output_msg, |
| 293 | config, |
| 294 | input_handler, |
| 295 | output_handlers, |
| 296 | config_handler, |
| 297 | service_name, |
| 298 | method_name, |
| 299 | ) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 300 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 301 | # Allow proto-based method name override. |
| 302 | if method_options.HasField("implementation_name"): |
| 303 | implementation_name = method_options.implementation_name |
| 304 | else: |
| 305 | implementation_name = method_name |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 306 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 307 | # Import the module and get the method. |
| 308 | module_name = self._get_module_name(service_name, method_name) |
| 309 | method_impl = self._GetMethod(module_name, implementation_name) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 310 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 311 | # Successfully located; call and return. |
| 312 | return_code = method_impl(input_msg, output_msg, config) |
| 313 | if return_code is None: |
| 314 | return_code = controller.RETURN_CODE_SUCCESS |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 315 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 316 | for h in output_handlers: |
| 317 | h.write_from(output_msg) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 318 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 319 | return return_code |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 320 | |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame^] | 321 | def _needs_branch_reexecution( |
| 322 | self, |
| 323 | service_options: "google.protobuf.Message", |
| 324 | method_options: "google.protobuf.Message", |
| 325 | config: "api_config.ApiConfig", |
| 326 | ) -> bool: |
| 327 | """Check if the call needs to be re-executed on the branched BAPI.""" |
| 328 | if not config.run_endpoint: |
| 329 | # Do not re-exec for validate only and mock calls. |
| 330 | return False |
| 331 | |
| 332 | if method_options.HasField("method_branched_execution"): |
| 333 | # Prefer the method option when set. |
| 334 | branched_exec = method_options.method_branched_execution |
| 335 | elif service_options.HasField("service_branched_execution"): |
| 336 | # Fall back to the service option. |
| 337 | branched_exec = service_options.service_branched_execution |
| 338 | else: |
| 339 | branched_exec = build_api_pb2.EXECUTE_BRANCHED |
| 340 | |
| 341 | if branched_exec in ( |
| 342 | build_api_pb2.EXECUTE_NOT_SPECIFIED, |
| 343 | build_api_pb2.EXECUTE_BRANCHED, |
| 344 | ): |
| 345 | return not constants.IS_BRANCHED_CHROMITE |
| 346 | |
| 347 | return False |
| 348 | |
| 349 | def _needs_chroot_reexecution( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 350 | self, |
| 351 | service_options: "google.protobuf.Message", |
| 352 | method_options: "google.protobuf.Message", |
| 353 | config: "api_config.ApiConfig", |
| 354 | ) -> bool: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 355 | """Check the chroot options; execute assertion or note reexec as needed. |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 356 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 357 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 358 | service_options: The service options. |
| 359 | method_options: The method options. |
| 360 | config: The Build API call config instance. |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 361 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 362 | Returns: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 363 | True iff it needs to be reexeced inside the chroot. |
Alex Klein | bd6edf8 | 2019-07-18 10:30:49 -0600 | [diff] [blame] | 364 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 365 | Raises: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 366 | cros_build_lib.DieSystemExit when the chroot setting cannot be |
| 367 | satisfied. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 368 | """ |
| 369 | if not config.run_endpoint: |
| 370 | # Do not enter the chroot for validate only and mock calls. |
| 371 | return False |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 372 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 373 | chroot_assert = build_api_pb2.NO_ASSERTION |
| 374 | if method_options.HasField("method_chroot_assert"): |
| 375 | # Prefer the method option when set. |
| 376 | chroot_assert = method_options.method_chroot_assert |
| 377 | elif service_options.HasField("service_chroot_assert"): |
| 378 | # Fall back to the service option. |
| 379 | chroot_assert = service_options.service_chroot_assert |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | if chroot_assert == build_api_pb2.INSIDE: |
| 382 | return not cros_build_lib.IsInsideChroot() |
| 383 | elif chroot_assert == build_api_pb2.OUTSIDE: |
| 384 | # If it must be run outside we have to already be outside. |
| 385 | cros_build_lib.AssertOutsideChroot() |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 386 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 387 | return False |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 388 | |
Alex Klein | 12fc7ca | 2023-04-26 18:23:18 -0600 | [diff] [blame^] | 389 | def _reexecute_branched( |
| 390 | self, |
| 391 | input_handler: "message_util.MessageHandler", |
| 392 | output_handlers: List["message_util.MessageHandler"], |
| 393 | config_handler: "message_util.MessageHandler", |
| 394 | service_name: str, |
| 395 | method_name: str, |
| 396 | ): |
| 397 | """Re-execute the call on the branched BAPI. |
| 398 | |
| 399 | Args: |
| 400 | input_handler: Input message handler. |
| 401 | output_handlers: Output message handlers. |
| 402 | config_handler: Config message handler. |
| 403 | service_name: The name of the service to run. |
| 404 | method_name: The name of the method to run. |
| 405 | """ |
| 406 | cmd = [ |
| 407 | constants.BRANCHED_CHROMITE_DIR / "bin" / "build_api", |
| 408 | f"{service_name}/{method_name}", |
| 409 | input_handler.input_arg, |
| 410 | input_handler.path, |
| 411 | config_handler.config_arg, |
| 412 | config_handler.path, |
| 413 | "--debug", |
| 414 | ] |
| 415 | for output_handler in output_handlers: |
| 416 | cmd += [ |
| 417 | output_handler.output_arg, |
| 418 | output_handler.path, |
| 419 | ] |
| 420 | |
| 421 | try: |
| 422 | result = cros_build_lib.run( |
| 423 | cmd, |
| 424 | check=False, |
| 425 | ) |
| 426 | except cros_build_lib.RunCommandError: |
| 427 | # A non-zero return code will not result in an error, but one |
| 428 | # is still thrown when the command cannot be run in the first |
| 429 | # place. This is known to happen at least when the PATH does |
| 430 | # not include the chromite bin dir. |
| 431 | raise CrosSdkNotRunError("Unable to execute the branched BAPI.") |
| 432 | |
| 433 | logging.info( |
| 434 | "Endpoint execution completed, return code: %d", |
| 435 | result.returncode, |
| 436 | ) |
| 437 | |
| 438 | return result.returncode |
| 439 | |
| 440 | def _reexecute_inside( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | self, |
| 442 | input_msg: "google.protobuf.Message", |
| 443 | output_msg: "google.protobuf.Message", |
| 444 | config: "api_config.ApiConfig", |
| 445 | input_handler: "message_util.MessageHandler", |
| 446 | output_handlers: List["message_util.MessageHandler"], |
| 447 | config_handler: "message_util.MessageHandler", |
| 448 | service_name: str, |
| 449 | method_name: str, |
| 450 | ): |
| 451 | """Re-execute the service inside the chroot. |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 452 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 454 | input_msg: The parsed input message. |
| 455 | output_msg: The empty output message instance. |
| 456 | config: The call configs. |
| 457 | input_handler: Input message handler. |
| 458 | output_handlers: Output message handlers. |
| 459 | config_handler: Config message handler. |
| 460 | service_name: The name of the service to run. |
| 461 | method_name: The name of the method to run. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 462 | """ |
| 463 | # Parse the chroot and clear the chroot field in the input message. |
| 464 | chroot = field_handler.handle_chroot(input_msg) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 465 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 466 | if not chroot.exists(): |
| 467 | raise InvalidSdkError("Chroot does not exist.") |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 468 | |
Mike Frysinger | c2e72b4 | 2023-03-03 22:34:04 -0500 | [diff] [blame] | 469 | # Use a ExitStack to avoid the deep nesting this many context managers |
| 470 | # introduces. |
| 471 | with contextlib.ExitStack() as stack: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 472 | # TempDirs setup. |
Mike Frysinger | c2e72b4 | 2023-03-03 22:34:04 -0500 | [diff] [blame] | 473 | tempdir = stack.enter_context(chroot.tempdir()) |
| 474 | sync_tempdir = stack.enter_context(chroot.tempdir()) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 475 | # The copy-paths-in context manager to handle Path messages. |
Mike Frysinger | c2e72b4 | 2023-03-03 22:34:04 -0500 | [diff] [blame] | 476 | stack.enter_context( |
| 477 | field_handler.copy_paths_in( |
| 478 | input_msg, |
| 479 | chroot.tmp, |
| 480 | prefix=chroot.path, |
| 481 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 482 | ) |
| 483 | # The sync-directories context manager to handle SyncedDir messages. |
Mike Frysinger | c2e72b4 | 2023-03-03 22:34:04 -0500 | [diff] [blame] | 484 | stack.enter_context( |
| 485 | field_handler.sync_dirs( |
| 486 | input_msg, |
| 487 | sync_tempdir, |
| 488 | prefix=chroot.path, |
| 489 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 490 | ) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 491 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 492 | # Parse goma. |
Brian Norris | d0dfeae | 2023-03-09 13:06:47 -0800 | [diff] [blame] | 493 | chroot.goma = field_handler.handle_goma( |
| 494 | input_msg, chroot.path, chroot.out_path |
| 495 | ) |
Alex Klein | d1e9e5c | 2020-12-14 12:32:32 -0700 | [diff] [blame] | 496 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 497 | # Parse remoteexec. |
| 498 | chroot.remoteexec = field_handler.handle_remoteexec(input_msg) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 499 | |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 500 | # Build inside-chroot paths for the input, output, and config |
| 501 | # messages. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 502 | new_input = os.path.join(tempdir, self.REEXEC_INPUT_FILE) |
Brian Norris | 8faf47b | 2023-03-03 16:19:08 -0800 | [diff] [blame] | 503 | chroot_input = chroot.chroot_path(new_input) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 504 | new_output = os.path.join(tempdir, self.REEXEC_OUTPUT_FILE) |
Brian Norris | 8faf47b | 2023-03-03 16:19:08 -0800 | [diff] [blame] | 505 | chroot_output = chroot.chroot_path(new_output) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 506 | new_config = os.path.join(tempdir, self.REEXEC_CONFIG_FILE) |
Brian Norris | 8faf47b | 2023-03-03 16:19:08 -0800 | [diff] [blame] | 507 | chroot_config = chroot.chroot_path(new_config) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 508 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 509 | # Setup the inside-chroot message files. |
| 510 | logging.info("Writing input message to: %s", new_input) |
| 511 | input_handler.write_from(input_msg, path=new_input) |
| 512 | osutils.Touch(new_output) |
| 513 | logging.info("Writing config message to: %s", new_config) |
| 514 | config_handler.write_from(config.get_proto(), path=new_config) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 515 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 516 | # We can use a single output to write the rest of them. Use the |
| 517 | # first one as the reexec output and just translate its output in |
| 518 | # the rest of the handlers after. |
| 519 | output_handler = output_handlers[0] |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 520 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 521 | cmd = [ |
| 522 | "build_api", |
| 523 | "%s/%s" % (service_name, method_name), |
| 524 | input_handler.input_arg, |
| 525 | chroot_input, |
| 526 | output_handler.output_arg, |
| 527 | chroot_output, |
| 528 | config_handler.config_arg, |
| 529 | chroot_config, |
| 530 | "--debug", |
| 531 | ] |
Alex Klein | 915cce9 | 2019-12-17 14:19:50 -0700 | [diff] [blame] | 532 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | try: |
| 534 | result = cros_build_lib.run( |
| 535 | cmd, |
| 536 | enter_chroot=True, |
| 537 | chroot_args=chroot.get_enter_args(), |
| 538 | check=False, |
| 539 | extra_env=chroot.env, |
| 540 | ) |
| 541 | except cros_build_lib.RunCommandError: |
| 542 | # A non-zero return code will not result in an error, but one |
| 543 | # is still thrown when the command cannot be run in the first |
| 544 | # place. This is known to happen at least when the PATH does |
| 545 | # not include the chromite bin dir. |
| 546 | raise CrosSdkNotRunError("Unable to enter the chroot.") |
Alex Klein | d3394c2 | 2020-06-16 14:05:06 -0600 | [diff] [blame] | 547 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 548 | logging.info( |
| 549 | "Endpoint execution completed, return code: %d", |
| 550 | result.returncode, |
| 551 | ) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 552 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 553 | # Transfer result files out of the chroot. |
| 554 | output_handler.read_into(output_msg, path=new_output) |
| 555 | field_handler.extract_results(input_msg, output_msg, chroot) |
Alex Klein | 9b7331e | 2019-12-30 14:37:21 -0700 | [diff] [blame] | 556 | |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 557 | # Write out all the response formats. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 558 | for handler in output_handlers: |
| 559 | handler.write_from(output_msg) |
Joanna Wang | 92cad81 | 2021-11-03 14:52:08 -0700 | [diff] [blame] | 560 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 561 | return result.returncode |
Alex Klein | 9b7331e | 2019-12-30 14:37:21 -0700 | [diff] [blame] | 562 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 563 | def _GetMethod(self, module_name: str, method_name: str) -> Callable: |
| 564 | """Get the implementation of the method for the service module. |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 565 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 566 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 567 | module_name: The name of the service module. |
| 568 | method_name: The name of the method. |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 569 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 570 | Returns: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 571 | The method. |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 572 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 573 | Raises: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 574 | MethodNotFoundError when the method cannot be found in the module. |
| 575 | ServiceModuleNotFoundError when the service module cannot be |
| 576 | imported. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 577 | """ |
| 578 | try: |
| 579 | module = importlib.import_module( |
| 580 | controller.IMPORT_PATTERN % module_name |
| 581 | ) |
| 582 | except ImportError as e: |
| 583 | raise ServiceControllerNotFoundError(str(e)) |
| 584 | try: |
| 585 | return getattr(module, method_name) |
| 586 | except AttributeError as e: |
| 587 | raise MethodNotFoundError(str(e)) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 588 | |
| 589 | |
Tomasz Tylenda | b429230 | 2021-08-08 18:59:36 +0900 | [diff] [blame] | 590 | def RegisterServices(router: Router): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 591 | """Register all the services. |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 592 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 593 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 594 | router: The router. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 595 | """ |
| 596 | router.Register(android_pb2) |
| 597 | router.Register(api_pb2) |
| 598 | router.Register(artifacts_pb2) |
| 599 | router.Register(binhost_pb2) |
Tristan Honscheid | 52ba4d2 | 2023-02-09 11:59:29 -0700 | [diff] [blame] | 600 | router.Register(copybot_pb2) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 601 | router.Register(depgraph_pb2) |
| 602 | router.Register(firmware_pb2) |
| 603 | router.Register(image_pb2) |
| 604 | router.Register(metadata_pb2) |
Lizzy Presland | e723c01 | 2022-06-10 05:05:37 +0000 | [diff] [blame] | 605 | router.Register(observability_pb2) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 606 | router.Register(packages_pb2) |
| 607 | router.Register(payload_pb2) |
| 608 | router.Register(portage_explorer_pb2) |
| 609 | router.Register(sdk_pb2) |
| 610 | router.Register(sysroot_pb2) |
| 611 | router.Register(test_pb2) |
| 612 | router.Register(toolchain_pb2) |
| 613 | logging.debug("Services registered successfully.") |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 614 | |
| 615 | |
| 616 | def GetRouter(): |
Alex Klein | 54c891a | 2023-01-24 10:45:41 -0700 | [diff] [blame] | 617 | """Get a router that has had all the services registered.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 618 | router = Router() |
| 619 | RegisterServices(router) |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 620 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 621 | return router |