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