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