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