Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 1 | # Copyright 2018 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 | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 5 | """The Build API entry point.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 6 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 7 | import logging |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 8 | import os |
Cindy Lin | c88eba3 | 2022-08-10 04:45:03 +0000 | [diff] [blame] | 9 | import sys |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 10 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 11 | from chromite.api import api_config as api_config_lib |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 12 | from chromite.api import controller |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 13 | from chromite.api import message_util |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 14 | from chromite.api import router as router_lib |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 15 | from chromite.api.gen.chromite.api import build_api_config_pb2 |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 16 | from chromite.lib import commandline |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
Cindy Lin | c88eba3 | 2022-08-10 04:45:03 +0000 | [diff] [blame] | 18 | from chromite.lib import namespaces |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 19 | from chromite.utils import matching |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 20 | |
Mike Frysinger | 898265b | 2020-02-10 23:49:12 -0500 | [diff] [blame] | 21 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 22 | def GetParser(): |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 23 | """Build the argument parser.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 24 | parser = commandline.ArgumentParser(description=__doc__) |
| 25 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 26 | parser.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 27 | 'service_method', |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 28 | help='The "chromite.api.Service/Method" that is being called.') |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 29 | # Input arguments. |
| 30 | input_args = parser.add_mutually_exclusive_group(required=True) |
| 31 | input_args.add_argument( |
| 32 | '--input-binary', |
| 33 | type='path', |
| 34 | help='Path to the protobuf binary serialization of the input message.') |
| 35 | input_args.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 36 | '--input-json', |
| 37 | type='path', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 38 | help='Path to the JSON serialized input argument protobuf message.') |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 39 | # Output options. |
| 40 | parser.add_argument( |
| 41 | '--output-binary', |
| 42 | type='path', |
| 43 | help='The path to which the protobuf binary serialization of the ' |
| 44 | 'response message should be written.') |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 45 | parser.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 46 | '--output-json', |
| 47 | type='path', |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 48 | help='The path to which the JSON serialization of the response message ' |
| 49 | 'should be written.') |
| 50 | # Config options. |
| 51 | config_args = parser.add_mutually_exclusive_group() |
| 52 | config_args.add_argument( |
| 53 | '--config-binary', |
| 54 | type='path', |
| 55 | help='The path to the protobuf binary serialization of the Build API ' |
| 56 | 'call configs.') |
| 57 | config_args.add_argument( |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 58 | '--config-json', |
| 59 | type='path', |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 60 | help='The path to the JSON encoded Build API call configs.') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 61 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 62 | return parser |
| 63 | |
| 64 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 65 | def _ParseArgs(argv, router): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 66 | """Parse and validate arguments.""" |
| 67 | parser = GetParser() |
Alex Klein | 7cc434f | 2019-12-17 14:58:57 -0700 | [diff] [blame] | 68 | opts, unknown = parser.parse_known_args( |
| 69 | argv, namespace=commandline.ArgumentNamespace()) |
| 70 | parser.DoPostParseSetup(opts, unknown) |
| 71 | |
| 72 | if unknown: |
| 73 | logging.warning('Unknown args ignored: %s', ' '.join(unknown)) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 74 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 75 | methods = router.ListMethods() |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 76 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 77 | # Positional service_method argument validation. |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 78 | parts = opts.service_method.split('/') |
| 79 | if len(parts) != 2: |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 80 | parser.error('Invalid service/method specification format. It should be ' |
| 81 | 'something like chromite.api.SdkService/Create.') |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 82 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 83 | if opts.service_method not in methods: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 84 | # Unknown method, try to match against known methods and make a suggestion. |
Sloan Johnson | a1c89eb | 2022-06-07 22:49:05 +0000 | [diff] [blame] | 85 | # This is just for developer assistance, e.g. misspellings when testing. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 86 | matched = matching.GetMostLikelyMatchedObject( |
| 87 | methods, opts.service_method, matched_score_threshold=0.6) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 88 | error = 'Unrecognized service name.' |
| 89 | if matched: |
| 90 | error += '\nDid you mean: \n%s' % '\n'.join(matched) |
| 91 | parser.error(error) |
| 92 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 93 | opts.service = parts[0] |
| 94 | opts.method = parts[1] |
| 95 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 96 | # Input and output validation. |
| 97 | if not opts.output_binary and not opts.output_json: |
| 98 | parser.error('At least one output file must be specified.') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 99 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 100 | if not os.path.exists(opts.input_binary or opts.input_json): |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 101 | parser.error('Input file does not exist.') |
| 102 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 103 | config_msg = build_api_config_pb2.BuildApiConfig() |
| 104 | if opts.config_json: |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 105 | handler = message_util.get_message_handler(opts.config_json, |
| 106 | message_util.FORMAT_JSON) |
| 107 | else: |
| 108 | handler = message_util.get_message_handler(opts.config_binary, |
| 109 | message_util.FORMAT_BINARY) |
| 110 | |
| 111 | if opts.config_json or opts.config_binary: |
| 112 | # We have been given a config, so read it. |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 113 | try: |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 114 | handler.read_into(config_msg) |
| 115 | except message_util.Error as e: |
| 116 | parser.error(str(e)) |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 117 | |
| 118 | opts.config = api_config_lib.build_config_from_proto(config_msg) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 119 | opts.config_handler = handler |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 120 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 121 | opts.Freeze() |
| 122 | return opts |
| 123 | |
| 124 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 125 | def _get_io_handlers(opts): |
| 126 | """Build the input and output handlers.""" |
| 127 | if opts.input_binary: |
| 128 | input_handler = message_util.get_message_handler(opts.input_binary, |
| 129 | message_util.FORMAT_BINARY) |
| 130 | else: |
| 131 | input_handler = message_util.get_message_handler(opts.input_json, |
| 132 | message_util.FORMAT_JSON) |
| 133 | |
| 134 | output_handlers = [] |
| 135 | if opts.output_binary: |
| 136 | handler = message_util.get_message_handler(opts.output_binary, |
| 137 | message_util.FORMAT_BINARY) |
| 138 | output_handlers.append(handler) |
| 139 | if opts.output_json: |
| 140 | handler = message_util.get_message_handler(opts.output_json, |
| 141 | message_util.FORMAT_JSON) |
| 142 | output_handlers.append(handler) |
| 143 | |
| 144 | return input_handler, output_handlers |
| 145 | |
| 146 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 147 | def main(argv): |
Mike Frysinger | da928de | 2021-06-21 12:49:40 -0400 | [diff] [blame] | 148 | router = router_lib.GetRouter() |
| 149 | opts = _ParseArgs(argv, router) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 150 | |
Cindy Lin | c88eba3 | 2022-08-10 04:45:03 +0000 | [diff] [blame] | 151 | # For build_image, make sure we run with network disabled to prevent leakage. |
| 152 | if opts.service_method == 'chromite.api.ImageService/Create': |
| 153 | namespaces.ReExecuteWithNamespace(sys.argv) |
| 154 | |
Mike Frysinger | da928de | 2021-06-21 12:49:40 -0400 | [diff] [blame] | 155 | if opts.config.log_path: |
| 156 | logging.warning('Ignoring log_path config option') |
| 157 | if 'BUILD_API_TEE_LOG_FILE' in os.environ: |
| 158 | logging.warning('Ignoring $BUILD_API_TEE_LOG_FILE env var') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 159 | |
Mike Frysinger | da928de | 2021-06-21 12:49:40 -0400 | [diff] [blame] | 160 | if opts.config.mock_invalid: |
| 161 | # --mock-invalid handling. We print error messages, but no output is ever |
| 162 | # set for validation errors, so we can handle it by just giving back the |
| 163 | # correct return code here. |
| 164 | return controller.RETURN_CODE_INVALID_INPUT |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 165 | |
Mike Frysinger | da928de | 2021-06-21 12:49:40 -0400 | [diff] [blame] | 166 | input_handler, output_handlers = _get_io_handlers(opts) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame] | 167 | |
Mike Frysinger | da928de | 2021-06-21 12:49:40 -0400 | [diff] [blame] | 168 | try: |
| 169 | return router.Route(opts.service, opts.method, opts.config, input_handler, |
| 170 | output_handlers, opts.config_handler) |
| 171 | except router_lib.Error as e: |
| 172 | # Handle router_lib.Error derivatives nicely, but let anything else bubble |
| 173 | # up. |
| 174 | cros_build_lib.Die(e) |