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