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 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 10 | import os |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 11 | import sys |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 12 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 13 | from chromite.api import api_config as api_config_lib |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 14 | from chromite.api import controller |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 15 | from chromite.api import router as router_lib |
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 |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_logging as logging |
| 19 | from chromite.lib import tee |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 20 | from chromite.utils import matching |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 21 | |
| 22 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 23 | def GetParser(): |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 24 | """Build the argument parser.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 25 | parser = commandline.ArgumentParser(description=__doc__) |
| 26 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 27 | call_group = parser.add_argument_group( |
| 28 | 'API Call Options', |
| 29 | 'These options are used to execute an endpoint. When making a call every ' |
| 30 | 'argument in this group is required.') |
| 31 | call_group.add_argument( |
| 32 | 'service_method', |
| 33 | nargs='?', |
| 34 | help='The "chromite.api.Service/Method" that is being called.') |
| 35 | call_group.add_argument( |
| 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 | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 39 | call_group.add_argument( |
| 40 | '--output-json', |
| 41 | type='path', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 42 | help='The path to which the result protobuf message should be written.') |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 43 | call_group.add_argument( |
| 44 | '--tee-log', |
| 45 | type='path', |
| 46 | help='The path to which stdout and stderr should be teed to.') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 47 | |
| 48 | ux_group = parser.add_argument_group('Developer Options', |
| 49 | 'Options to help developers.') |
| 50 | # Lists the full chromite.api.Service/Method, has both names to match |
| 51 | # whichever mental model people prefer. |
| 52 | ux_group.add_argument( |
| 53 | '--list-methods', |
| 54 | '--list-services', |
| 55 | action='store_true', |
| 56 | dest='list_services', |
| 57 | help='List the name of each registered "chromite.api.Service/Method".') |
| 58 | |
| 59 | # Run configuration options. |
| 60 | test_group = parser.add_argument_group( |
| 61 | 'Testing Options', |
| 62 | 'These options are used to execute various tests against the API. These ' |
| 63 | 'options are mutually exclusive. Calling code can use these options to ' |
| 64 | 'validate inputs and test their handling of each return code case for ' |
| 65 | 'each endpoint.') |
| 66 | call_modifications = test_group.add_mutually_exclusive_group() |
| 67 | call_modifications.add_argument( |
| 68 | '--validate-only', |
| 69 | action='store_true', |
| 70 | default=False, |
| 71 | help='When set, only runs the argument validation logic. Calls produce ' |
| 72 | 'a return code of 0 iff the input proto comprises arguments that ' |
| 73 | 'are a valid call to the endpoint, or 1 otherwise.') |
| 74 | # See: api/faux.py for the mock call and error implementations. |
| 75 | call_modifications.add_argument( |
| 76 | '--mock-call', |
| 77 | action='store_true', |
| 78 | default=False, |
| 79 | help='When set, returns a valid, mock response rather than running the ' |
| 80 | 'endpoint. This allows API consumers to more easily test their ' |
| 81 | 'implementations against the version of the API being called. ' |
| 82 | 'This argument will always result in a return code of 0.') |
| 83 | call_modifications.add_argument( |
| 84 | '--mock-error', |
| 85 | action='store_true', |
| 86 | default=False, |
| 87 | help='When set, return a valid, mock error response rather than running ' |
| 88 | 'the endpoint. This allows API consumers to test their error ' |
| 89 | 'handling semantics against the version of the API being called. ' |
| 90 | 'This argument will always result in a return code of 2 iff the ' |
| 91 | 'endpoint ever produces a return code of 2, otherwise will always' |
| 92 | 'produce a return code of 1.') |
| 93 | call_modifications.add_argument( |
| 94 | '--mock-invalid', |
| 95 | action='store_true', |
| 96 | default=False, |
| 97 | help='When set, return a mock validation error response rather than ' |
| 98 | 'running the endpoint. This allows API consumers to test their ' |
| 99 | 'validation error handling semantics against the version of the API ' |
| 100 | 'being called without having to understand how to construct an ' |
| 101 | 'invalid request. ' |
| 102 | 'This argument will always result in a return code of 1.') |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 103 | |
| 104 | return parser |
| 105 | |
| 106 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 107 | def _ParseArgs(argv, router): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 108 | """Parse and validate arguments.""" |
| 109 | parser = GetParser() |
| 110 | opts = parser.parse_args(argv) |
| 111 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 112 | methods = router.ListMethods() |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 113 | |
| 114 | if opts.list_services: |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 115 | # We just need to print the methods and we're done. |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 116 | for method in methods: |
| 117 | print(method) |
| 118 | sys.exit(0) |
| 119 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 120 | # Positional service_method argument validation. |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 121 | if not opts.service_method: |
| 122 | parser.error('Must pass "Service/Method".') |
| 123 | |
| 124 | parts = opts.service_method.split('/') |
| 125 | if len(parts) != 2: |
| 126 | parser.error( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 127 | 'Must pass the correct format: (e.g. chromite.api.SdkService/Create).' |
| 128 | 'Use --list-methods to see a full list.') |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 129 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 130 | if opts.service_method not in methods: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 131 | # Unknown method, try to match against known methods and make a suggestion. |
| 132 | # This is just for developer sanity, e.g. misspellings when testing. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 133 | matched = matching.GetMostLikelyMatchedObject( |
| 134 | methods, opts.service_method, matched_score_threshold=0.6) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 135 | error = 'Unrecognized service name.' |
| 136 | if matched: |
| 137 | error += '\nDid you mean: \n%s' % '\n'.join(matched) |
| 138 | parser.error(error) |
| 139 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 140 | opts.service = parts[0] |
| 141 | opts.method = parts[1] |
| 142 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 143 | # --input-json and --output-json validation. |
| 144 | if not opts.input_json or not opts.output_json: |
| 145 | parser.error('--input-json and --output-json are both required.') |
| 146 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 147 | if not os.path.exists(opts.input_json): |
| 148 | parser.error('Input file does not exist.') |
| 149 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 150 | # Build the config object from the options. |
| 151 | opts.config = api_config_lib.ApiConfig( |
| 152 | validate_only=opts.validate_only, |
| 153 | mock_call=opts.mock_call, |
| 154 | mock_error=opts.mock_error) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 155 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 156 | opts.Freeze() |
| 157 | return opts |
| 158 | |
| 159 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 160 | def main(argv): |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 161 | with cros_build_lib.ContextManagerStack() as stack: |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 162 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 163 | router = router_lib.GetRouter() |
| 164 | opts = _ParseArgs(argv, router) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 165 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 166 | if opts.tee_log: |
| 167 | stack.Add(tee.Tee, opts.tee_log) |
| 168 | logging.info('Teeing stdout and stderr to %s', opts.tee_log) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 169 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 170 | if opts.mock_invalid: |
| 171 | # --mock-invalid handling. We print error messages, but no output is ever |
| 172 | # set for validation errors, so we can handle it by just giving back the |
| 173 | # correct return code here. |
| 174 | return controller.RETURN_CODE_INVALID_INPUT |
| 175 | |
| 176 | try: |
| 177 | return router.Route(opts.service, opts.method, opts.input_json, |
| 178 | opts.output_json, opts.config) |
| 179 | except router_lib.Error as e: |
| 180 | # Handle router_lib.Error derivatives nicely, but let anything else bubble |
| 181 | # up. |
| 182 | cros_build_lib.Die(e) |