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