Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """The build API entry point.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import importlib |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 11 | import os |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 12 | import shutil |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 13 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 14 | from google.protobuf import json_format |
| 15 | from google.protobuf import symbol_database |
| 16 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 17 | from chromite.api import controller |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 18 | from chromite.api import field_handler |
Evan Hernandez | aeb556a | 2019-04-03 11:28:49 -0600 | [diff] [blame] | 19 | from chromite.api.gen.chromite.api import artifacts_pb2 |
| 20 | from chromite.api.gen.chromite.api import binhost_pb2 |
Alex Klein | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 21 | from chromite.api.gen.chromite.api import build_api_pb2 |
| 22 | from chromite.api.gen.chromite.api import depgraph_pb2 |
| 23 | from chromite.api.gen.chromite.api import image_pb2 |
| 24 | from chromite.api.gen.chromite.api import sdk_pb2 |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 25 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 26 | from chromite.api.gen.chromite.api import test_pb2 |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 27 | from chromite.lib import commandline |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 28 | from chromite.lib import cros_build_lib |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 29 | from chromite.lib import cros_logging as logging |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 30 | from chromite.lib import osutils |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 31 | from chromite.utils import matching |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | class Error(Exception): |
| 35 | """Base error class for the module.""" |
| 36 | |
| 37 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 38 | class InvalidInputFileError(Error): |
| 39 | """Raised when the input file cannot be read.""" |
| 40 | |
| 41 | |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 42 | class InvalidInputFormatError(Error): |
| 43 | """Raised when the passed input protobuf can't be parsed.""" |
| 44 | |
| 45 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 46 | class InvalidOutputFileError(Error): |
| 47 | """Raised when the output file cannot be written.""" |
| 48 | |
| 49 | |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 50 | class CrosSdkNotRunError(Error): |
| 51 | """Raised when the cros_sdk command could not be run to enter the chroot.""" |
| 52 | |
| 53 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 54 | # API Service Errors. |
| 55 | class UnknownServiceError(Error): |
| 56 | """Error raised when the requested service has not been registered.""" |
| 57 | |
| 58 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 59 | class ControllerModuleNotDefinedError(Error): |
| 60 | """Error class for when no controller is defined for a service.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 61 | |
| 62 | |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 63 | class ServiceControllerNotFoundError(Error): |
| 64 | """Error raised when the service's controller cannot be imported.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 65 | |
| 66 | |
| 67 | # API Method Errors. |
| 68 | class UnknownMethodError(Error): |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 69 | """The service is defined in the proto but the method is not.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | class MethodNotFoundError(Error): |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 73 | """The method's implementation cannot be found in the service's controller.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def GetParser(): |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 77 | """Build the argument parser.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 78 | parser = commandline.ArgumentParser(description=__doc__) |
| 79 | |
| 80 | parser.add_argument('service_method', |
| 81 | help='The "chromite.api.Service/Method" that is being ' |
| 82 | 'called.') |
| 83 | |
| 84 | parser.add_argument( |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 85 | '--input-json', type='path', required=True, |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 86 | help='Path to the JSON serialized input argument protobuf message.') |
| 87 | parser.add_argument( |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 88 | '--output-json', type='path', required=True, |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 89 | help='The path to which the result protobuf message should be written.') |
| 90 | |
| 91 | return parser |
| 92 | |
| 93 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 94 | def _ParseArgs(argv, router): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 95 | """Parse and validate arguments.""" |
| 96 | parser = GetParser() |
| 97 | opts = parser.parse_args(argv) |
| 98 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 99 | methods = router.ListMethods() |
| 100 | if opts.service_method not in methods: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 101 | # Unknown method, try to match against known methods and make a suggestion. |
| 102 | # This is just for developer sanity, e.g. misspellings when testing. |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 103 | matched = matching.GetMostLikelyMatchedObject(methods, opts.service_method, |
| 104 | matched_score_threshold=0.6) |
| 105 | error = 'Unrecognized service name.' |
| 106 | if matched: |
| 107 | error += '\nDid you mean: \n%s' % '\n'.join(matched) |
| 108 | parser.error(error) |
| 109 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 110 | parts = opts.service_method.split('/') |
| 111 | |
| 112 | if len(parts) != 2: |
| 113 | parser.error('Must pass "Service/Method".') |
| 114 | |
| 115 | opts.service = parts[0] |
| 116 | opts.method = parts[1] |
| 117 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 118 | if not os.path.exists(opts.input_json): |
| 119 | parser.error('Input file does not exist.') |
| 120 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 121 | opts.Freeze() |
| 122 | return opts |
| 123 | |
| 124 | |
| 125 | class Router(object): |
| 126 | """Encapsulates the request dispatching logic.""" |
| 127 | |
| 128 | def __init__(self): |
| 129 | self._services = {} |
| 130 | self._aliases = {} |
| 131 | # All imported generated messages get added to this symbol db. |
| 132 | self._sym_db = symbol_database.Default() |
| 133 | |
| 134 | extensions = build_api_pb2.DESCRIPTOR.extensions_by_name |
| 135 | self._service_options = extensions['service_options'] |
| 136 | self._method_options = extensions['method_options'] |
| 137 | |
| 138 | def Register(self, proto_module): |
| 139 | """Register the services from a generated proto module. |
| 140 | |
| 141 | Args: |
| 142 | proto_module (module): The generated proto module whose service is being |
| 143 | registered. |
| 144 | |
| 145 | Raises: |
| 146 | ServiceModuleNotDefinedError when the service cannot be found in the |
| 147 | provided module. |
| 148 | """ |
| 149 | services = proto_module.DESCRIPTOR.services_by_name |
| 150 | for service_name, svc in services.items(): |
| 151 | module_name = svc.GetOptions().Extensions[self._service_options].module |
| 152 | |
| 153 | if not module_name: |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 154 | raise ControllerModuleNotDefinedError( |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 155 | 'The module must be defined in the service definition: %s.%s' % |
| 156 | (proto_module, service_name)) |
| 157 | |
| 158 | self._services[svc.full_name] = (svc, module_name) |
| 159 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 160 | def ListMethods(self): |
| 161 | """List all methods registered with the router.""" |
| 162 | services = [] |
| 163 | for service_name, (svc, _module) in self._services.items(): |
| 164 | for method_name in svc.methods_by_name.keys(): |
| 165 | services.append('%s/%s' % (service_name, method_name)) |
| 166 | |
| 167 | return sorted(services) |
| 168 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 169 | def Route(self, service_name, method_name, input_path, output_path): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 170 | """Dispatch the request. |
| 171 | |
| 172 | Args: |
| 173 | service_name (str): The fully qualified service name. |
| 174 | method_name (str): The name of the method being called. |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 175 | input_path (str): The path to the input message file. |
| 176 | output_path (str): The path where the output message should be written. |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 177 | |
| 178 | Returns: |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 179 | int: The return code. |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 180 | |
| 181 | Raises: |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 182 | InvalidInputFileError when the input file cannot be read. |
| 183 | InvalidOutputFileError when the output file cannot be written. |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 184 | ServiceModuleNotFoundError when the service module cannot be imported. |
| 185 | MethodNotFoundError when the method cannot be retrieved from the module. |
| 186 | """ |
| 187 | try: |
Alex Klein | 8dffea4 | 2019-06-14 14:01:36 -0600 | [diff] [blame] | 188 | input_json = osutils.ReadFile(input_path).strip() |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 189 | except IOError as e: |
| 190 | raise InvalidInputFileError('Unable to read input file: %s' % e.message) |
| 191 | |
| 192 | try: |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 193 | svc, module_name = self._services[service_name] |
| 194 | except KeyError: |
| 195 | raise UnknownServiceError('The %s service has not been registered.' |
| 196 | % service_name) |
| 197 | |
| 198 | try: |
| 199 | method_desc = svc.methods_by_name[method_name] |
| 200 | except KeyError: |
| 201 | raise UnknownMethodError('The %s method has not been defined in the %s ' |
| 202 | 'service.' % (method_name, service_name)) |
| 203 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 204 | # Parse the input file to build an instance of the input message. |
| 205 | input_msg = self._sym_db.GetPrototype(method_desc.input_type)() |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 206 | try: |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 207 | json_format.Parse(input_json, input_msg, ignore_unknown_fields=True) |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 208 | except json_format.ParseError as e: |
| 209 | raise InvalidInputFormatError( |
| 210 | 'Unable to parse the input json: %s' % e.message) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 211 | |
| 212 | # Get an empty output message instance. |
| 213 | output_msg = self._sym_db.GetPrototype(method_desc.output_type)() |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 214 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 215 | # Allow proto-based method name override. |
| 216 | method_options = method_desc.GetOptions().Extensions[self._method_options] |
| 217 | if method_options.HasField('implementation_name'): |
| 218 | method_name = method_options.implementation_name |
| 219 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 220 | # Check the chroot settings before running. |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 221 | service_options = svc.GetOptions().Extensions[self._service_options] |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 222 | if self._ChrootCheck(service_options, method_options): |
| 223 | # Run inside the chroot instead. |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 224 | logging.info('Re-executing the endpoint inside the chroot.') |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 225 | return self._ReexecuteInside(input_msg, output_path, service_name, |
| 226 | method_name) |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 227 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 228 | # Import the module and get the method. |
| 229 | method_impl = self._GetMethod(module_name, method_name) |
| 230 | |
| 231 | # Successfully located; call and return. |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 232 | return_code = method_impl(input_msg, output_msg) |
| 233 | if return_code is None: |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 234 | return_code = controller.RETURN_CODE_SUCCESS |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 235 | |
| 236 | try: |
| 237 | osutils.WriteFile(output_path, json_format.MessageToJson(output_msg)) |
| 238 | except IOError as e: |
| 239 | raise InvalidOutputFileError('Cannot write output file: %s' % e.message) |
| 240 | |
| 241 | return return_code |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 242 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 243 | def _ChrootCheck(self, service_options, method_options): |
| 244 | """Check the chroot options, and execute assertion or note reexec as needed. |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 245 | |
| 246 | Args: |
| 247 | service_options (google.protobuf.Message): The service options. |
| 248 | method_options (google.protobuf.Message): The method options. |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 249 | |
| 250 | Returns: |
| 251 | bool - True iff it needs to be reexeced inside the chroot. |
| 252 | |
| 253 | Raises: |
| 254 | cros_build_lib.DieSystemExit when the chroot setting cannot be satisfied. |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 255 | """ |
| 256 | chroot_assert = build_api_pb2.NO_ASSERTION |
| 257 | if method_options.HasField('method_chroot_assert'): |
| 258 | # Prefer the method option when set. |
| 259 | chroot_assert = method_options.method_chroot_assert |
| 260 | elif service_options.HasField('service_chroot_assert'): |
| 261 | # Fall back to the service option. |
| 262 | chroot_assert = service_options.service_chroot_assert |
| 263 | |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 264 | if chroot_assert == build_api_pb2.INSIDE: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 265 | return not cros_build_lib.IsInsideChroot() |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 266 | elif chroot_assert == build_api_pb2.OUTSIDE: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 267 | # If it must be run outside we have to already be outside. |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 268 | cros_build_lib.AssertOutsideChroot() |
| 269 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 270 | return False |
| 271 | |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 272 | def _ReexecuteInside(self, input_msg, output_path, service_name, method_name): |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 273 | """Re-execute the service inside the chroot. |
| 274 | |
| 275 | Args: |
| 276 | input_msg (Message): The parsed input message. |
| 277 | output_path (str): The path for the serialized output. |
| 278 | service_name (str): The name of the service to run. |
| 279 | method_name (str): The name of the method to run. |
| 280 | """ |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 281 | # Parse the chroot and clear the chroot field in the input message. |
| 282 | chroot = field_handler.handle_chroot(input_msg) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 283 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 284 | base_dir = os.path.join(chroot.path, 'tmp') |
Alex Klein | 14ea8b7 | 2019-06-06 10:09:08 -0600 | [diff] [blame] | 285 | with field_handler.handle_paths(input_msg, base_dir, prefix=chroot.path): |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 286 | with osutils.TempDir(base_dir=base_dir) as tempdir: |
| 287 | new_input = os.path.join(tempdir, 'input.json') |
| 288 | chroot_input = '/%s' % os.path.relpath(new_input, chroot.path) |
| 289 | new_output = os.path.join(tempdir, 'output.json') |
| 290 | chroot_output = '/%s' % os.path.relpath(new_output, chroot.path) |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 291 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 292 | logging.info('Writing input message to: %s', new_input) |
| 293 | osutils.WriteFile(new_input, json_format.MessageToJson(input_msg)) |
| 294 | osutils.Touch(new_output) |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 295 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 296 | cmd = ['build_api', '%s/%s' % (service_name, method_name), |
| 297 | '--input-json', chroot_input, '--output-json', chroot_output] |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 298 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 299 | try: |
| 300 | result = cros_build_lib.RunCommand(cmd, enter_chroot=True, |
| 301 | chroot_args=chroot.GetEnterArgs(), |
| 302 | error_code_ok=True, |
| 303 | extra_env=chroot.env) |
| 304 | except cros_build_lib.RunCommandError: |
| 305 | # A non-zero return code will not result in an error, but one is still |
| 306 | # thrown when the command cannot be run in the first place. This is |
| 307 | # known to happen at least when the PATH does not include the chromite |
| 308 | # bin dir. |
| 309 | raise CrosSdkNotRunError('Unable to enter the chroot.') |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 310 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 311 | logging.info('Endpoint execution completed, return code: %d', |
| 312 | result.returncode) |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 313 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 314 | shutil.move(new_output, output_path) |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 315 | |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 316 | return result.returncode |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 317 | |
| 318 | def _GetChrootArgs(self, chroot): |
| 319 | """Translate a Chroot message to chroot enter args. |
| 320 | |
| 321 | Args: |
| 322 | chroot (chromiumos.Chroot): A chroot message. |
| 323 | |
| 324 | Returns: |
| 325 | list[str]: The cros_sdk args for the chroot. |
| 326 | """ |
| 327 | args = [] |
| 328 | if chroot.path: |
| 329 | args.extend(['--chroot', chroot.path]) |
| 330 | if chroot.cache_dir: |
| 331 | args.extend(['--cache-dir', chroot.cache_dir]) |
| 332 | |
| 333 | return args |
| 334 | |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 335 | def _GetChrootEnv(self, chroot): |
| 336 | """Get chroot environment variables that need to be set.""" |
| 337 | use_flags = [u.flag for u in chroot.env.use_flags] |
| 338 | features = [f.feature for f in chroot.env.features] |
| 339 | |
| 340 | env = {} |
| 341 | if use_flags: |
| 342 | env['USE'] = ' '.join(use_flags) |
Alex Klein | 40748dc | 2019-05-13 17:43:23 -0600 | [diff] [blame] | 343 | |
| 344 | # TODO(saklein) Remove the default when fully integrated in recipes. |
| 345 | env['FEATURES'] = 'separatedebug' |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 346 | if features: |
Alex Klein | 40748dc | 2019-05-13 17:43:23 -0600 | [diff] [blame] | 347 | env['FEATURES'] = ' '.join(features) |
Alex Klein | be0bae4 | 2019-05-06 13:01:49 -0600 | [diff] [blame] | 348 | |
| 349 | return env |
| 350 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 351 | def _GetMethod(self, module_name, method_name): |
| 352 | """Get the implementation of the method for the service module. |
| 353 | |
| 354 | Args: |
| 355 | module_name (str): The name of the service module. |
| 356 | method_name (str): The name of the method. |
| 357 | |
| 358 | Returns: |
| 359 | callable - The method. |
| 360 | |
| 361 | Raises: |
| 362 | MethodNotFoundError when the method cannot be found in the module. |
| 363 | ServiceModuleNotFoundError when the service module cannot be imported. |
| 364 | """ |
| 365 | try: |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 366 | module = importlib.import_module(controller.IMPORT_PATTERN % module_name) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 367 | except ImportError as e: |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 368 | raise ServiceControllerNotFoundError(e.message) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 369 | try: |
| 370 | return getattr(module, method_name) |
| 371 | except AttributeError as e: |
| 372 | raise MethodNotFoundError(e.message) |
| 373 | |
| 374 | |
| 375 | def RegisterServices(router): |
| 376 | """Register all the services. |
| 377 | |
| 378 | Args: |
| 379 | router (Router): The router. |
| 380 | """ |
Evan Hernandez | aeb556a | 2019-04-03 11:28:49 -0600 | [diff] [blame] | 381 | router.Register(artifacts_pb2) |
| 382 | router.Register(binhost_pb2) |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 383 | router.Register(depgraph_pb2) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 384 | router.Register(image_pb2) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 385 | router.Register(sdk_pb2) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 386 | router.Register(sysroot_pb2) |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 387 | router.Register(test_pb2) |
Alex Klein | 6db9cdf | 2019-05-03 14:59:10 -0600 | [diff] [blame] | 388 | logging.debug('Services registered successfully.') |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 389 | |
| 390 | |
| 391 | def main(argv): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 392 | router = Router() |
| 393 | RegisterServices(router) |
| 394 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 395 | opts = _ParseArgs(argv, router) |
| 396 | |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 397 | try: |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 398 | return router.Route(opts.service, opts.method, opts.input_json, |
| 399 | opts.output_json) |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 400 | except Error as e: |
| 401 | # Error derivatives are handled nicely, but let anything else bubble up. |
| 402 | cros_build_lib.Die(e.message) |