| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2018 - The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | r"""Setup args. |
| 17 | |
| 18 | Defines the setup arg parser that holds setup specific args. |
| 19 | """ |
| 20 | |
| herbertxue | 36b19b2 | 2020-06-02 17:38:26 +0800 | [diff] [blame^] | 21 | from acloud import errors |
| 22 | # pylint: disable=no-name-in-module,import-error |
| 23 | from acloud.internal.proto.user_config_pb2 import UserConfig |
| 24 | |
| 25 | |
| 26 | _FIELD_NAMES = sorted([field.name for field in UserConfig.DESCRIPTOR.fields]) |
| 27 | _CONFIG_FIELD = 0 |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 28 | CMD_SETUP = "setup" |
| 29 | |
| 30 | |
| 31 | def GetSetupArgParser(subparser): |
| 32 | """Return the setup arg parser. |
| 33 | |
| 34 | Args: |
| 35 | subparser: argparse.ArgumentParser that is attached to main acloud cmd. |
| 36 | |
| 37 | Returns: |
| 38 | argparse.ArgumentParser with setup options defined. |
| 39 | """ |
| 40 | setup_parser = subparser.add_parser(CMD_SETUP) |
| 41 | setup_parser.required = False |
| 42 | setup_parser.set_defaults(which=CMD_SETUP) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 43 | setup_parser.add_argument( |
| 44 | "--host", |
| 45 | action="store_true", |
| 46 | dest="host", |
| 47 | required=False, |
| Kevin Cheng | 92532b4 | 2019-02-12 10:03:13 -0800 | [diff] [blame] | 48 | help="Setup host to run local instance of an Android Virtual Device. " |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 49 | "Must explicitly set to kick off host setup. Automatically installs " |
| 50 | "host base packages as well") |
| 51 | setup_parser.add_argument( |
| 52 | "--host-base", |
| 53 | action="store_true", |
| 54 | dest="host_base", |
| 55 | required=False, |
| 56 | help="Install base packages on the host.") |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 57 | setup_parser.add_argument( |
| cylan | 76b61c6 | 2019-01-19 13:59:09 +0800 | [diff] [blame] | 58 | "--gcp-init", |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 59 | action="store_true", |
| 60 | dest="gcp_init", |
| 61 | required=False, |
| 62 | help="Setup Google Cloud project name and enable required GCP APIs." |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 63 | "E.G. Google Cloud Storage/ Internal Android Build/ Compute Engine") |
| Kevin Cheng | 58b7e79 | 2018-10-05 02:26:53 -0700 | [diff] [blame] | 64 | setup_parser.add_argument( |
| 65 | "--force", |
| 66 | action="store_true", |
| 67 | dest="force", |
| 68 | required=False, |
| 69 | help="Force the setup steps even if it's not required.") |
| herbertxue | c0f98f3 | 2020-05-20 14:00:56 +0800 | [diff] [blame] | 70 | # TODO(157532869): Validate the field name. |
| 71 | setup_parser.add_argument( |
| 72 | "--update-config", |
| 73 | nargs=2, |
| 74 | dest="update_config", |
| 75 | required=False, |
| 76 | help="Update the acloud user config. The first arg is field name in " |
| 77 | "config, and the second arg is the value of the field. Command would " |
| herbertxue | 36b19b2 | 2020-06-02 17:38:26 +0800 | [diff] [blame^] | 78 | "like: 'acloud setup --config stable_host_image_family acloud-release'." |
| 79 | " The first arg can be one of following fields:%s" % _FIELD_NAMES) |
| Kevin Cheng | 58b7e79 | 2018-10-05 02:26:53 -0700 | [diff] [blame] | 80 | |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 81 | return setup_parser |
| herbertxue | 36b19b2 | 2020-06-02 17:38:26 +0800 | [diff] [blame^] | 82 | |
| 83 | |
| 84 | def VerifyArgs(args): |
| 85 | """Verify args. |
| 86 | |
| 87 | One example of command "acloud setup --update-config zone us-central1-c", |
| 88 | then this function would check "zone" is a valid field. |
| 89 | |
| 90 | Args: |
| 91 | args: Namespace object from argparse.parse_args. |
| 92 | |
| 93 | Raises: |
| 94 | errors.NotSupportedFieldName: The field name doesn't support in config. |
| 95 | """ |
| 96 | if args.update_config: |
| 97 | if args.update_config[_CONFIG_FIELD] not in _FIELD_NAMES: |
| 98 | raise errors.NotSupportedFieldName( |
| 99 | "Field[%s] isn't in support list: %s" % (args.update_config[0], |
| 100 | _FIELD_NAMES)) |