1# Copyright 2018 - The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14r"""Create args. 15 16Defines the create arg parser that holds create specific args. 17""" 18 19import argparse 20import os 21 22from acloud import errors 23from acloud.create import create_common 24from acloud.internal import constants 25 26 27CMD_CREATE = "create" 28 29 30# TODO: Add this into main create args once create_cf/gf is deprecated. 31def AddCommonCreateArgs(parser): 32 """Adds arguments common to create parsers. 33 34 Args: 35 parser: ArgumentParser object, used to parse flags. 36 """ 37 parser.add_argument( 38 "--num", 39 type=int, 40 dest="num", 41 required=False, 42 default=1, 43 help="Number of instances to create.") 44 parser.add_argument( 45 "--serial-log-file", 46 type=str, 47 dest="serial_log_file", 48 required=False, 49 help="Path to a *tar.gz file where serial logs will be saved " 50 "when a device fails on boot.") 51 parser.add_argument( 52 "--logcat-file", 53 type=str, 54 dest="logcat_file", 55 required=False, 56 help="Path to a *tar.gz file where logcat logs will be saved " 57 "when a device fails on boot.") 58 parser.add_argument( 59 "--autoconnect", 60 action="store_true", 61 dest="autoconnect", 62 required=False, 63 help="For each instance created, we will automatically create both 2 " 64 "ssh tunnels forwarding both adb & vnc. Then add the device to " 65 "adb.") 66 parser.add_argument( 67 "--no-autoconnect", 68 action="store_false", 69 dest="autoconnect", 70 required=False, 71 help="Will not automatically create ssh tunnels forwarding adb & vnc " 72 "when instance created.") 73 parser.set_defaults(autoconnect=True) 74 parser.add_argument( 75 "--report-internal-ip", 76 action="store_true", 77 dest="report_internal_ip", 78 required=False, 79 help="Report internal ip of the created instance instead of external " 80 "ip. Using the internal ip is used when connecting from another " 81 "GCE instance.") 82 parser.add_argument( 83 "--network", 84 type=str, 85 dest="network", 86 required=False, 87 help="Set the network the GCE instance will utilize.") 88 89 # TODO(b/118439885): Old arg formats to support transition, delete when 90 # transistion is done. 91 parser.add_argument( 92 "--serial_log_file", 93 type=str, 94 dest="serial_log_file", 95 required=False, 96 help=argparse.SUPPRESS) 97 parser.add_argument( 98 "--logcat_file", 99 type=str, 100 dest="logcat_file", 101 required=False, 102 help=argparse.SUPPRESS) 103 parser.add_argument( 104 "--build_id", 105 type=str, 106 dest="build_id", 107 required=False, 108 help=argparse.SUPPRESS) 109 parser.add_argument( 110 "--build_target", 111 type=str, 112 dest="build_target", 113 required=False, 114 help=argparse.SUPPRESS) 115 116 117def GetCreateArgParser(subparser): 118 """Return the create arg parser. 119 120 Args: 121 subparser: argparse.ArgumentParser that is attached to main acloud cmd. 122 123 Returns: 124 argparse.ArgumentParser with create options defined. 125 """ 126 create_parser = subparser.add_parser(CMD_CREATE) 127 create_parser.required = False 128 create_parser.set_defaults(which=CMD_CREATE) 129 create_parser.add_argument( 130 "--local-instance", 131 action="store_true", 132 dest="local_instance", 133 required=False, 134 help="Create a local instance of the AVD.") 135 create_parser.add_argument( 136 "--avd-type", 137 type=str, 138 dest="avd_type", 139 default=constants.TYPE_CF, 140 choices=[constants.TYPE_GCE, constants.TYPE_CF, constants.TYPE_GF, constants.TYPE_CHEEPS], 141 help="Android Virtual Device type (default %s)." % constants.TYPE_CF) 142 create_parser.add_argument( 143 "--flavor", 144 type=str, 145 dest="flavor", 146 help="The device flavor of the AVD (default %s)." % constants.FLAVOR_PHONE) 147 create_parser.add_argument( 148 "--build-target", 149 type=str, 150 dest="build_target", 151 help="Android build target, e.g. aosp_cf_x86_phone-userdebug, " 152 "or short names: phone, tablet, or tablet_mobile.") 153 create_parser.add_argument( 154 "--branch", 155 type=str, 156 dest="branch", 157 help="Android branch, e.g. mnc-dev or git_mnc-dev") 158 create_parser.add_argument( 159 "--build-id", 160 type=str, 161 dest="build_id", 162 help="Android build id, e.g. 2145099, P2804227") 163 create_parser.add_argument( 164 "--kernel-build-id", 165 type=str, 166 dest="kernel_build_id", 167 required=False, 168 help=("Android kernel build id, e.g. 4586590. This is to test a new" 169 " kernel build with a particular Android build (--build_id). If" 170 "not specified, the kernel that's bundled with the Android build" 171 "would be used.")) 172 create_parser.add_argument( 173 "--local-image", 174 type=str, 175 dest="local_image", 176 nargs="?", 177 default="", 178 required=False, 179 help="Use the locally built image for the AVD. Look for the image " 180 "artifact in $ANDROID_PRODUCT_OUT if no args value is provided." 181 "e.g --local-image or --local-image /path/to/dir or --local-image " 182 "/path/to/file") 183 create_parser.add_argument( 184 "--image-download-dir", 185 type=str, 186 dest="image_download_dir", 187 required=False, 188 help="Define remote image download directory, e.g. /usr/local/dl.") 189 create_parser.add_argument( 190 "--yes", "-y", 191 action="store_true", 192 dest="no_prompt", 193 required=False, 194 help=("Automatic yes to prompts. Assume 'yes' as answer to all prompts " 195 "and run non-interactively.")) 196 # User should not specify --spec and --hw_property at the same time. 197 hw_spec_group = create_parser.add_mutually_exclusive_group() 198 hw_spec_group.add_argument( 199 "--hw-property", 200 type=str, 201 dest="hw_property", 202 required=False, 203 help="Supported HW properties and example values: %s" % 204 constants.HW_PROPERTIES_CMD_EXAMPLE) 205 hw_spec_group.add_argument( 206 "--spec", 207 type=str, 208 dest="spec", 209 required=False, 210 choices=constants.SPEC_NAMES, 211 help="The name of a pre-configured device spec that we are " 212 "going to use.") 213 # Arguments for goldfish type. 214 # TODO(b/118439885): Verify args that are used in wrong avd_type. 215 # e.g. $acloud create --avd-type cuttlefish --emulator-build-id 216 create_parser.add_argument( 217 "--gpu", 218 type=str, 219 dest="gpu", 220 required=False, 221 default=None, 222 help="'goldfish only' GPU accelerator to use if any. " 223 "e.g. nvidia-tesla-k80, omit to use swiftshader") 224 create_parser.add_argument( 225 "--emulator-build-id", 226 type=int, 227 dest="emulator_build_id", 228 required=False, 229 help="'goldfish only' Emulator build used to run the images. " 230 "e.g. 4669466.") 231 232 AddCommonCreateArgs(create_parser) 233 return create_parser 234 235 236def VerifyArgs(args): 237 """Verify args. 238 239 Args: 240 args: Namespace object from argparse.parse_args. 241 242 Raises: 243 errors.CheckPathError: Zipped image path doesn't exist. 244 errors.UnsupportedFlavor: Flavor doesn't support. 245 """ 246 # Verify that user specified flavor name is in support list. 247 # We don't use argparse's builtin validation because we need to be able to 248 # tell when a user doesn't specify a flavor. 249 if args.flavor and args.flavor not in constants.ALL_FLAVORS: 250 raise errors.UnsupportedFlavor( 251 "Flavor[%s] isn't in support list: %s" % (args.flavor, 252 constants.ALL_FLAVORS)) 253 254 if args.local_image and not os.path.exists(args.local_image): 255 raise errors.CheckPathError( 256 "Specified path doesn't exist: %s" % args.local_image) 257 258 hw_properties = create_common.ParseHWPropertyArgs(args.hw_property) 259 for key in hw_properties: 260 if key not in constants.HW_PROPERTIES: 261 raise errors.InvalidHWPropertyError( 262 "[%s] is an invalid hw property, supported values are:%s. " 263 % (key, constants.HW_PROPERTIES)) 264