blob: c581a70af353764d17b949720510858cc232118f [file] [log] [blame]
Kevin Cheng3087af52018-08-13 13:26:50 -07001# 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
Kevin Cheng9aeed4d2018-12-18 14:37:34 -080019import argparse
herbertxue36b99f12021-03-25 14:47:28 +080020import logging
herbertxue2625b042018-08-16 23:28:20 +080021import os
22
23from acloud import errors
Sam Chiuc64f3432018-08-17 11:19:06 +080024from acloud.create import create_common
Kevin Cheng3087af52018-08-13 13:26:50 -070025from acloud.internal import constants
herbertxue6ef54a52019-05-02 11:38:58 +080026from acloud.internal.lib import utils
Kevin Cheng3087af52018-08-13 13:26:50 -070027
herbertxue36b99f12021-03-25 14:47:28 +080028logger = logging.getLogger(__name__)
herbertxue93630f52020-03-06 16:06:56 +080029_DEFAULT_GPU = "default"
Kevin Cheng3087af52018-08-13 13:26:50 -070030CMD_CREATE = "create"
31
32
33# TODO: Add this into main create args once create_cf/gf is deprecated.
herbertxue1b784292021-11-03 13:38:10 +080034# pylint: disable=too-many-statements
Kevin Cheng3087af52018-08-13 13:26:50 -070035def AddCommonCreateArgs(parser):
36 """Adds arguments common to create parsers.
37
38 Args:
39 parser: ArgumentParser object, used to parse flags.
40 """
41 parser.add_argument(
42 "--num",
43 type=int,
44 dest="num",
45 required=False,
46 default=1,
47 help="Number of instances to create.")
48 parser.add_argument(
Sam Chiue669ef72018-10-16 16:23:37 +080049 "--serial-log-file",
Kevin Cheng3087af52018-08-13 13:26:50 -070050 type=str,
51 dest="serial_log_file",
52 required=False,
53 help="Path to a *tar.gz file where serial logs will be saved "
Kevin Cheng86d43c72018-08-30 10:59:14 -070054 "when a device fails on boot.")
Kevin Cheng3087af52018-08-13 13:26:50 -070055 parser.add_argument(
Kevin Cheng3087af52018-08-13 13:26:50 -070056 "--autoconnect",
chojoycee86730a2019-12-11 15:37:58 +080057 type=str,
58 nargs="?",
chojoyce2d116892021-09-27 15:56:41 +080059 const=constants.INS_KEY_WEBRTC,
Kevin Cheng3087af52018-08-13 13:26:50 -070060 dest="autoconnect",
61 required=False,
chojoyce4e2d9222020-01-03 17:04:05 +080062 choices=[constants.INS_KEY_VNC, constants.INS_KEY_ADB,
63 constants.INS_KEY_WEBRTC],
64 help="Determines to establish a tunnel forwarding adb/vnc and "
65 "launch VNC/webrtc. Establish a tunnel forwarding adb and vnc "
66 "then launch vnc if --autoconnect vnc is provided. Establish a "
67 "tunnel forwarding adb if --autoconnect adb is provided. "
68 "Establish a tunnel forwarding adb and auto-launch on the browser "
69 "if --autoconnect webrtc is provided. For local goldfish "
70 "instance, create a window.")
Kevin Cheng4a8f5cf2018-08-15 08:59:28 -070071 parser.add_argument(
72 "--no-autoconnect",
73 action="store_false",
74 dest="autoconnect",
daviddclo53fd8472018-12-27 16:24:06 +080075 required=False,
76 help="Will not automatically create ssh tunnels forwarding adb & vnc "
77 "when instance created.")
herbertxuecdd8d792021-11-09 14:13:59 +080078 parser.add_argument(
79 "--mkcert",
80 action="store_true",
81 dest="mkcert",
82 required=False,
83 help="Install mkcert package on the host. It helps to create the "
84 "certification files for the WEB browser.")
chojoyce2d116892021-09-27 15:56:41 +080085 parser.set_defaults(autoconnect=constants.INS_KEY_WEBRTC)
Kevin Cheng86d43c72018-08-30 10:59:14 -070086 parser.add_argument(
chojoycec60fa582019-07-11 16:09:02 +080087 "--unlock",
88 action="store_true",
89 dest="unlock_screen",
90 required=False,
91 default=False,
92 help="This can unlock screen after invoke vnc client.")
93 parser.add_argument(
Sam Chiue669ef72018-10-16 16:23:37 +080094 "--report-internal-ip",
Kevin Cheng86d43c72018-08-30 10:59:14 -070095 action="store_true",
96 dest="report_internal_ip",
97 required=False,
98 help="Report internal ip of the created instance instead of external "
Kevin Cheng1f582bc2018-10-02 10:37:02 -070099 "ip. Using the internal ip is used when connecting from another "
Kevin Cheng86d43c72018-08-30 10:59:14 -0700100 "GCE instance.")
Kevin Chengc9424a82018-10-25 11:34:55 -0700101 parser.add_argument(
Spencer Chuafb541b2021-05-27 20:32:31 +0000102 "--disable-external-ip",
103 action="store_true",
104 dest="disable_external_ip",
105 required=False,
106 help="Disable the external ip of the created instance.")
107 parser.add_argument(
herbertxue59fc96c2021-10-05 15:31:06 +0800108 "--extra-files",
109 nargs='+',
110 type=str,
111 dest="extra_files",
112 required=False,
113 help="Upload the extra files into GCE instance. e.g. "
114 "/path/to/file_in_local,/path/to/file_in_gce")
115 parser.add_argument(
Kevin Chengc9424a82018-10-25 11:34:55 -0700116 "--network",
117 type=str,
118 dest="network",
119 required=False,
120 help="Set the network the GCE instance will utilize.")
Kevin Chengfab8cfc2019-06-27 14:01:37 -0700121 parser.add_argument(
122 "--skip-pre-run-check",
123 action="store_true",
124 dest="skip_pre_run_check",
125 required=False,
126 help="Skip the pre-run check.")
cylan6bc90ff2019-07-10 13:40:56 +0800127 parser.add_argument(
128 "--boot-timeout",
129 dest="boot_timeout_secs",
130 type=int,
131 required=False,
herbertxue5804bc12021-09-15 19:45:30 +0800132 help="The maximum time in seconds used to wait for the AVD to download "
133 "artifacts and boot.")
herbertxue23b2a962019-07-24 22:39:20 +0800134 parser.add_argument(
herbertxue40e7efb2019-12-13 11:47:24 +0800135 "--wait-for-ins-stable",
136 dest="ins_timeout_secs",
137 type=int,
138 required=False,
139 help="The maximum time in seconds used to wait for the instance boot "
140 "up. The default value to wait for instance up time is 300 secs.")
141 parser.add_argument(
herbertxuec6f0c262019-07-31 16:59:49 +0800142 "--build-target",
143 type=str,
144 dest="build_target",
Ram Muthiahe313c432021-05-26 12:35:02 -0700145 help="Android build target, e.g. aosp_cf_x86_64_phone-userdebug, "
herbertxuec6f0c262019-07-31 16:59:49 +0800146 "or short names: phone, tablet, or tablet_mobile.")
147 parser.add_argument(
148 "--branch",
149 type=str,
150 dest="branch",
151 help="Android branch, e.g. mnc-dev or git_mnc-dev")
152 parser.add_argument(
153 "--build-id",
154 type=str,
155 dest="build_id",
156 help="Android build id, e.g. 2145099, P2804227")
157 parser.add_argument(
herbertxuee659d152020-10-15 18:52:06 +0800158 "--bootloader-branch",
159 type=str,
160 dest="bootloader_branch",
161 help="'cuttlefish only' Branch to consume the bootloader from.",
162 required=False)
163 parser.add_argument(
164 "--bootloader-build-id",
165 type=str,
166 dest="bootloader_build_id",
167 help="'cuttlefish only' Bootloader build id, e.g. P2804227",
168 required=False)
169 parser.add_argument(
170 "--bootloader-build-target",
171 type=str,
172 dest="bootloader_build_target",
173 help="'cuttlefish only' Bootloader build target.",
174 required=False)
175 parser.add_argument(
herbertxuec6f0c262019-07-31 16:59:49 +0800176 "--kernel-build-id",
177 type=str,
178 dest="kernel_build_id",
179 required=False,
180 help="Android kernel build id, e.g. 4586590. This is to test a new"
herbertxue03eb5d72019-09-10 14:52:10 +0800181 " kernel build with a particular Android build (--build-id). If neither"
182 " kernel-branch nor kernel-build-id are specified, the kernel that's"
herbertxuec6f0c262019-07-31 16:59:49 +0800183 " bundled with the Android build would be used.")
184 parser.add_argument(
185 "--kernel-branch",
186 type=str,
187 dest="kernel_branch",
188 required=False,
189 help="Android kernel build branch name, e.g."
190 " kernel-common-android-4.14. This is to test a new kernel build with a"
191 " particular Android build (--build-id). If specified without"
herbertxue03eb5d72019-09-10 14:52:10 +0800192 " specifying kernel-build-id, the last green build in the branch will"
193 " be used. If neither kernel-branch nor kernel-build-id are specified,"
herbertxuec6f0c262019-07-31 16:59:49 +0800194 " the kernel that's bundled with the Android build would be used.")
195 parser.add_argument(
196 "--kernel-build-target",
197 type=str,
198 dest="kernel_build_target",
199 default="kernel",
200 help="Kernel build target, specify if different from 'kernel'")
201 parser.add_argument(
Hsin-Yi Chendd8de5d2021-10-21 12:18:23 +0800202 "--kernel-artifact",
203 type=str,
204 dest="kernel_artifact",
205 required=False,
206 help="Goldfish remote host only. The name of the boot image to be "
207 "retrieved from Android build, e.g., boot-5.10.img.")
208 parser.add_argument(
herbertxue1b784292021-11-03 13:38:10 +0800209 "--ota-branch",
210 type=str,
211 dest="ota_branch",
212 required=False,
213 help="'cuttlefish only' OTA tools branch name. e.g. aosp-master")
214 parser.add_argument(
215 "--ota-build-id",
216 type=str,
217 dest="ota_build_id",
218 required=False,
219 help="'cuttlefish only' OTA tools build id, e.g. 2145099, P2804227")
220 parser.add_argument(
221 "--ota-build-target",
222 type=str,
223 dest="ota_build_target",
224 required=False,
225 help="'cuttlefish only' OTA tools build target, e.g. "
226 "cf_x86_64_phone-userdebug.")
227 parser.add_argument(
herbertxue23b2a962019-07-24 22:39:20 +0800228 "--system-branch",
229 type=str,
230 dest="system_branch",
231 help="'cuttlefish only' Branch to consume the system image (system.img) "
232 "from, will default to what is defined by --branch. "
233 "That feature allows to (automatically) test various combinations "
234 "of vendor.img (CF, e.g.) and system images (GSI, e.g.). ",
235 required=False)
236 parser.add_argument(
237 "--system-build-id",
238 type=str,
239 dest="system_build_id",
240 help="'cuttlefish only' System image build id, e.g. 2145099, P2804227",
241 required=False)
242 parser.add_argument(
243 "--system-build-target",
244 type=str,
245 dest="system_build_target",
246 help="'cuttlefish only' System image build target, specify if different "
herbertxue03eb5d72019-09-10 14:52:10 +0800247 "from --build-target",
herbertxue23b2a962019-07-24 22:39:20 +0800248 required=False)
herbertxue877df862021-04-14 13:49:50 +0800249 parser.add_argument(
250 "--launch-args",
251 type=str,
252 dest="launch_args",
253 help="'cuttlefish only' Add extra args to launch_cvd command.",
254 required=False)
herbertxue7caa30f2021-05-20 10:14:09 +0800255 parser.add_argument(
256 "--gce-metadata",
257 type=str,
258 dest="gce_metadata",
259 default=None,
260 help="'GCE instance only' Record data into GCE instance metadata with "
261 "key-value pair format. e.g. id:12,name:unknown.")
herbertxueda2619b2019-12-06 12:09:47 +0800262 # TODO(146314062): Remove --multi-stage-launch after infra don't use this
263 # args.
Cody Schuffelen102b3b52019-07-17 10:26:35 -0700264 parser.add_argument(
265 "--multi-stage-launch",
266 dest="multi_stage_launch",
herbertxue6e546122020-01-06 23:35:25 +0800267 action="store_true",
Cody Schuffelen102b3b52019-07-17 10:26:35 -0700268 required=False,
herbertxueda2619b2019-12-06 12:09:47 +0800269 default=True,
Cody Schuffelen102b3b52019-07-17 10:26:35 -0700270 help="Enable the multi-stage cuttlefish launch.")
271 parser.add_argument(
272 "--no-multi-stage-launch",
273 dest="multi_stage_launch",
herbertxue6e546122020-01-06 23:35:25 +0800274 action="store_false",
Cody Schuffelen102b3b52019-07-17 10:26:35 -0700275 required=False,
276 default=None,
277 help="Disable the multi-stage cuttlefish launch.")
herbertxue6e546122020-01-06 23:35:25 +0800278 parser.add_argument(
279 "--no-pull-log",
280 dest="no_pull_log",
281 action="store_true",
282 required=False,
283 default=None,
284 help="Disable auto download logs when AVD booting up failed.")
herbertxue9a651d62020-01-08 21:58:34 +0800285 # TODO(147335651): Add gpu in user config.
286 # TODO(147335651): Support "--gpu" without giving any value.
287 parser.add_argument(
288 "--gpu",
289 type=str,
herbertxue93630f52020-03-06 16:06:56 +0800290 const=_DEFAULT_GPU,
291 nargs="?",
herbertxue9a651d62020-01-08 21:58:34 +0800292 dest="gpu",
293 required=False,
294 default=None,
herbertxue93630f52020-03-06 16:06:56 +0800295 help="GPU accelerator to use if any. e.g. nvidia-tesla-k80. For local "
296 "instances, this arg without assigning any value is to enable "
297 "local gpu support.")
herbertxueffbcbc22020-07-07 14:35:34 +0800298 # Hide following args for users, it is only used in infra.
herbertxue408dba82020-03-19 19:23:32 +0800299 parser.add_argument(
Hsin-Yi Chene0bc5392020-09-08 00:48:43 +0800300 "--local-instance-dir",
301 dest="local_instance_dir",
302 required=False,
303 help=argparse.SUPPRESS)
304 parser.add_argument(
herbertxue408dba82020-03-19 19:23:32 +0800305 "--num-avds-per-instance",
306 type=int,
307 dest="num_avds_per_instance",
308 required=False,
309 default=1,
310 help=argparse.SUPPRESS)
herbertxueffbcbc22020-07-07 14:35:34 +0800311 parser.add_argument(
herbertxueb1574472021-04-08 14:44:47 +0800312 "--oxygen",
313 action="store_true",
314 dest="oxygen",
315 required=False,
316 help=argparse.SUPPRESS)
317 parser.add_argument(
herbertxueffbcbc22020-07-07 14:35:34 +0800318 "--zone",
319 type=str,
320 dest="zone",
321 required=False,
322 help=argparse.SUPPRESS)
Kevin Cheng3087af52018-08-13 13:26:50 -0700323
Kevin Cheng85a20f62018-10-25 11:53:11 -0700324 # TODO(b/118439885): Old arg formats to support transition, delete when
325 # transistion is done.
326 parser.add_argument(
327 "--serial_log_file",
328 type=str,
329 dest="serial_log_file",
330 required=False,
Kevin Cheng9aeed4d2018-12-18 14:37:34 -0800331 help=argparse.SUPPRESS)
Kevin Cheng85a20f62018-10-25 11:53:11 -0700332 parser.add_argument(
Richard Fung97503b22019-02-06 13:43:38 -0800333 "--build_id",
334 type=str,
335 dest="build_id",
336 required=False,
337 help=argparse.SUPPRESS)
338 parser.add_argument(
339 "--build_target",
340 type=str,
341 dest="build_target",
342 required=False,
343 help=argparse.SUPPRESS)
herbertxue23b2a962019-07-24 22:39:20 +0800344 parser.add_argument(
345 "--system_branch",
346 type=str,
347 dest="system_branch",
348 required=False,
349 help=argparse.SUPPRESS)
350 parser.add_argument(
351 "--system_build_id",
352 type=str,
353 dest="system_build_id",
354 required=False,
355 help=argparse.SUPPRESS)
356 parser.add_argument(
357 "--system_build_target",
358 type=str,
359 dest="system_build_target",
360 required=False,
361 help=argparse.SUPPRESS)
herbertxuec6f0c262019-07-31 16:59:49 +0800362 parser.add_argument(
363 "--kernel_build_id",
364 type=str,
365 dest="kernel_build_id",
366 required=False,
367 help=argparse.SUPPRESS)
368 parser.add_argument(
369 "--kernel_branch",
370 type=str,
371 dest="kernel_branch",
372 required=False,
373 help=argparse.SUPPRESS)
374 parser.add_argument(
375 "--kernel_build_target",
376 type=str,
377 dest="kernel_build_target",
378 default="kernel",
379 help=argparse.SUPPRESS)
herbertxuee659d152020-10-15 18:52:06 +0800380 parser.add_argument(
381 "--bootloader_branch",
382 type=str,
383 dest="bootloader_branch",
384 help=argparse.SUPPRESS,
385 required=False)
386 parser.add_argument(
387 "--bootloader_build_id",
388 type=str,
389 dest="bootloader_build_id",
390 help=argparse.SUPPRESS,
391 required=False)
392 parser.add_argument(
393 "--bootloader_build_target",
394 type=str,
395 dest="bootloader_build_target",
396 help=argparse.SUPPRESS,
397 required=False)
Kevin Cheng85a20f62018-10-25 11:53:11 -0700398
Kevin Cheng3087af52018-08-13 13:26:50 -0700399
400def GetCreateArgParser(subparser):
401 """Return the create arg parser.
402
403 Args:
404 subparser: argparse.ArgumentParser that is attached to main acloud cmd.
405
406 Returns:
407 argparse.ArgumentParser with create options defined.
408 """
409 create_parser = subparser.add_parser(CMD_CREATE)
410 create_parser.required = False
411 create_parser.set_defaults(which=CMD_CREATE)
Hsin-Yi Chencaec52f2020-07-16 14:59:26 +0800412 # Use default=None to distinguish remote instance or local. The instance
413 # type will be remote if the arg is not provided.
Kevin Cheng3087af52018-08-13 13:26:50 -0700414 create_parser.add_argument(
Sam Chiue669ef72018-10-16 16:23:37 +0800415 "--local-instance",
Hsin-Yi Chencaec52f2020-07-16 14:59:26 +0800416 type=_PositiveInteger,
417 const=0,
418 metavar="ID",
Sam Chiu59120542019-08-15 09:32:32 +0800419 nargs="?",
cylanabe0a3a2018-08-16 18:50:09 +0800420 dest="local_instance",
421 required=False,
Hsin-Yi Chencaec52f2020-07-16 14:59:26 +0800422 help="Create a local AVD instance using the resources associated with "
423 "the ID. Choose an unused ID automatically if the value is "
424 "not specified (primarily for infra usage).")
cylanabe0a3a2018-08-16 18:50:09 +0800425 create_parser.add_argument(
herbertxue6ef54a52019-05-02 11:38:58 +0800426 "--adb-port", "-p",
427 type=int,
428 default=None,
429 dest="adb_port",
430 required=False,
431 help="Specify port for adb forwarding.")
432 create_parser.add_argument(
Sam Chiue669ef72018-10-16 16:23:37 +0800433 "--avd-type",
Kevin Cheng3087af52018-08-13 13:26:50 -0700434 type=str,
435 dest="avd_type",
436 default=constants.TYPE_CF,
Peter Collingbournedce6d722020-05-07 15:29:21 -0700437 choices=[constants.TYPE_GCE, constants.TYPE_CF, constants.TYPE_GF, constants.TYPE_CHEEPS,
herbertxueb8f22d92021-10-27 11:51:15 +0800438 constants.TYPE_FVP, constants.TYPE_OPENWRT],
Kevin Cheng3087af52018-08-13 13:26:50 -0700439 help="Android Virtual Device type (default %s)." % constants.TYPE_CF)
440 create_parser.add_argument(
herbertxue60d70c92021-03-31 20:56:37 +0800441 "--config", "--flavor",
cylanabe0a3a2018-08-16 18:50:09 +0800442 type=str,
443 dest="flavor",
herbertxue60d70c92021-03-31 20:56:37 +0800444 help="The device flavor of the AVD (default %s). e.g. phone, tv, foldable."
445 % constants.FLAVOR_PHONE)
cylanabe0a3a2018-08-16 18:50:09 +0800446 create_parser.add_argument(
Sam Chiue669ef72018-10-16 16:23:37 +0800447 "--local-image",
Hsin-Yi Chenc1f69642020-12-28 12:44:43 +0800448 const=constants.FIND_IN_BUILD_ENV,
herbertxue2625b042018-08-16 23:28:20 +0800449 type=str,
450 dest="local_image",
451 nargs="?",
herbertxue2625b042018-08-16 23:28:20 +0800452 required=False,
Kevin Cheng01b58bd2018-10-17 16:03:10 -0700453 help="Use the locally built image for the AVD. Look for the image "
Sam Chiu96172ae2019-01-31 14:30:30 +0800454 "artifact in $ANDROID_PRODUCT_OUT if no args value is provided."
455 "e.g --local-image or --local-image /path/to/dir or --local-image "
456 "/path/to/file")
chojoycef23e7d52018-10-15 17:36:35 +0800457 create_parser.add_argument(
Hsin-Yi Chen6a658472021-06-03 11:57:03 +0800458 "--local-kernel-image", "--local-boot-image",
Hsin-Yi Chen469d0dc2021-04-14 19:21:41 +0800459 const=constants.FIND_IN_BUILD_ENV,
460 type=str,
461 dest="local_kernel_image",
462 nargs="?",
463 required=False,
464 help="Use the locally built kernel image for the AVD. Look for "
465 "boot.img or boot-*.img if the argument is a directory. Look for the "
466 "image in $ANDROID_PRODUCT_OUT if no argument is provided. e.g., "
467 "--local-kernel-image, --local-kernel-image /path/to/dir, or "
468 "--local-kernel-image /path/to/img")
469 create_parser.add_argument(
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800470 "--local-system-image",
Hsin-Yi Chenc1f69642020-12-28 12:44:43 +0800471 const=constants.FIND_IN_BUILD_ENV,
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800472 type=str,
473 dest="local_system_image",
474 nargs="?",
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800475 required=False,
476 help="Use the locally built system images for the AVD. Look for the "
477 "images in $ANDROID_PRODUCT_OUT if no args value is provided. "
Hsin-Yi Chenbef87362021-04-13 16:59:02 +0800478 "e.g., --local-system-image, --local-system-image /path/to/dir, or "
479 "--local-system-image /path/to/img")
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800480 create_parser.add_argument(
Hsin-Yi Chene76c1bf2019-12-23 15:15:47 +0800481 "--local-tool",
482 type=str,
483 dest="local_tool",
484 action="append",
485 default=[],
486 required=False,
487 help="Use the tools in the specified directory to create local "
herbertxue4f9d1162021-07-19 12:05:21 +0800488 "instances. The directory structure follows $ANDROID_SOONG_HOST_OUT "
489 "or $ANDROID_EMULATOR_PREBUILTS.")
490 create_parser.add_argument(
491 "--cvd-host-package",
492 type=str,
493 dest="cvd_host_package",
494 required=False,
495 help="Use the specified path of the cvd host package to create "
496 "instances. e.g. /path/cvd-host_package_v1.tar.gz")
Hsin-Yi Chene76c1bf2019-12-23 15:15:47 +0800497 create_parser.add_argument(
chojoycef23e7d52018-10-15 17:36:35 +0800498 "--image-download-dir",
499 type=str,
500 dest="image_download_dir",
501 required=False,
502 help="Define remote image download directory, e.g. /usr/local/dl.")
herbertxue7ef23b22019-02-27 09:34:24 +0800503 create_parser.add_argument(
504 "--yes", "-y",
505 action="store_true",
506 dest="no_prompt",
507 required=False,
508 help=("Automatic yes to prompts. Assume 'yes' as answer to all prompts "
509 "and run non-interactively."))
cylan2d633512019-09-09 20:33:42 +0800510 create_parser.add_argument(
511 "--reuse-gce",
512 type=str,
513 const=constants.SELECT_ONE_GCE_INSTANCE,
514 nargs="?",
515 dest="reuse_gce",
516 required=False,
517 help="'cuttlefish only' This can help users use their own instance. "
518 "Reusing specific gce instance if --reuse-gce [instance_name] is "
519 "provided. Select one gce instance to reuse if --reuse-gce is "
520 "provided.")
cyland43f2932019-11-26 17:37:28 +0800521 create_parser.add_argument(
herbertxue102a7ac2021-11-30 11:08:59 +0800522 "--openwrt",
523 action="store_true",
524 dest="openwrt",
525 required=False,
526 help="'cuttlefish only' Create OpenWrt device when launching cuttlefish "
527 "device.")
528 create_parser.add_argument(
cyland43f2932019-11-26 17:37:28 +0800529 "--host",
530 type=str,
531 dest="remote_host",
532 default=None,
cylan0f61af62019-12-30 22:31:10 +0800533 help="'cuttlefish only' Provide host name to clean up the remote host. "
534 "For example: '--host 1.1.1.1'")
cyland43f2932019-11-26 17:37:28 +0800535 create_parser.add_argument(
536 "--host-user",
537 type=str,
538 dest="host_user",
cylan0f61af62019-12-30 22:31:10 +0800539 default=constants.GCE_USER,
540 help="'remote host only' Provide host user for logging in to the host. "
541 "The default value is vsoc-01. For example: '--host 1.1.1.1 --host-user "
542 "vsoc-02'")
cyland43f2932019-11-26 17:37:28 +0800543 create_parser.add_argument(
544 "--host-ssh-private-key-path",
545 type=str,
546 dest="host_ssh_private_key_path",
547 default=None,
548 help="'remote host only' Provide host key for login on on this host.")
Sam Chiuc64f3432018-08-17 11:19:06 +0800549 # User should not specify --spec and --hw_property at the same time.
550 hw_spec_group = create_parser.add_mutually_exclusive_group()
551 hw_spec_group.add_argument(
Sam Chiue669ef72018-10-16 16:23:37 +0800552 "--hw-property",
Sam Chiuc64f3432018-08-17 11:19:06 +0800553 type=str,
554 dest="hw_property",
555 required=False,
556 help="Supported HW properties and example values: %s" %
557 constants.HW_PROPERTIES_CMD_EXAMPLE)
558 hw_spec_group.add_argument(
559 "--spec",
560 type=str,
561 dest="spec",
562 required=False,
563 choices=constants.SPEC_NAMES,
564 help="The name of a pre-configured device spec that we are "
565 "going to use.")
Yuchen Hec4a8f252021-10-21 18:15:29 +0000566 create_parser.add_argument(
567 "--disk-type",
568 type=str,
569 dest="disk_type",
570 required=False,
571 help="This is used to customize the GCE instance disk type, the "
572 "default disk type is from the stable host image. Use pd-ssd or "
573 "pd-standard to specify instance disk type.")
herbertxue06214492021-10-12 19:43:32 +0800574 create_parser.add_argument(
575 "--stable-host-image-name",
576 type=str,
577 dest="stable_host_image_name",
578 required=False,
579 default=None,
580 help=("'cuttlefish only' The Cuttlefish host image from which instances "
581 "are launched. If specified here, the value set in Acloud config "
582 "file will be overridden."))
Yuchen Hec4a8f252021-10-21 18:15:29 +0000583
herbertxue494891e2019-01-19 13:50:53 +0800584 # Arguments for goldfish type.
herbertxue494891e2019-01-19 13:50:53 +0800585 create_parser.add_argument(
herbertxue494891e2019-01-19 13:50:53 +0800586 "--emulator-build-id",
587 type=int,
588 dest="emulator_build_id",
589 required=False,
Hsin-Yi Chenc7e76992021-07-26 18:37:36 +0800590 help="'goldfish only' Emulator build ID used to run the images. "
herbertxue494891e2019-01-19 13:50:53 +0800591 "e.g. 4669466.")
Hsin-Yi Chenc7e76992021-07-26 18:37:36 +0800592 create_parser.add_argument(
593 "--emulator-build-target",
594 dest="emulator_build_target",
595 required=False,
596 help="'goldfish remote host only' Emulator build target used to run "
597 "the images. e.g. sdk_tools_linux.")
Kevin Cheng3087af52018-08-13 13:26:50 -0700598
Richard Fung8d3979e2019-06-12 16:00:34 -0700599 # Arguments for cheeps type.
600 create_parser.add_argument(
Shao-Chuan Lee80244112020-02-04 17:14:07 +0900601 "--stable-cheeps-host-image-name",
602 type=str,
603 dest="stable_cheeps_host_image_name",
604 required=False,
605 default=None,
606 help=("'cheeps only' The Cheeps host image from which instances are "
607 "launched. If specified here, the value set in Acloud config "
608 "file will be overridden."))
609 create_parser.add_argument(
610 "--stable-cheeps-host-image-project",
611 type=str,
612 dest="stable_cheeps_host_image_project",
613 required=False,
614 default=None,
615 help=("'cheeps only' The project hosting the specified Cheeps host "
616 "image. If specified here, the value set in Acloud config file "
617 "will be overridden."))
618 create_parser.add_argument(
Richard Fung8d3979e2019-06-12 16:00:34 -0700619 "--user",
620 type=str,
621 dest="username",
622 required=False,
623 default=None,
624 help="'cheeps only' username to log in to Chrome OS as.")
625 create_parser.add_argument(
626 "--password",
627 type=str,
628 dest="password",
629 required=False,
630 default=None,
631 help="'cheeps only' password to log in to Chrome OS with.")
Richard Fungd7d57f42020-04-09 12:58:49 -0700632 create_parser.add_argument(
633 "--betty-image",
634 type=str,
635 dest="cheeps_betty_image",
636 required=False,
637 default=None,
638 help=("'cheeps only' The L1 betty version to use. Only makes sense "
639 "when launching a controller image with "
640 "stable-cheeps-host-image"))
Richard Fung8d3979e2019-06-12 16:00:34 -0700641
Kevin Cheng3087af52018-08-13 13:26:50 -0700642 AddCommonCreateArgs(create_parser)
643 return create_parser
herbertxue2625b042018-08-16 23:28:20 +0800644
645
Hsin-Yi Chencaec52f2020-07-16 14:59:26 +0800646def _PositiveInteger(arg):
647 """Convert an argument from a string to a positive integer."""
648 try:
649 value = int(arg)
Hsin-Yi Chenc1f69642020-12-28 12:44:43 +0800650 except ValueError as e:
651 raise argparse.ArgumentTypeError(arg + " is not an integer.") from e
Hsin-Yi Chencaec52f2020-07-16 14:59:26 +0800652 if value <= 0:
653 raise argparse.ArgumentTypeError(arg + " is not positive.")
654 return value
655
656
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800657def _VerifyLocalArgs(args):
658 """Verify args starting with --local.
659
660 Args:
661 args: Namespace object from argparse.parse_args.
662
663 Raises:
664 errors.CheckPathError: Image path doesn't exist.
665 errors.UnsupportedCreateArgs: The specified avd type does not support
666 --local-system-image.
667 errors.UnsupportedLocalInstanceId: Local instance ID is invalid.
668 """
669 if args.local_image and not os.path.exists(args.local_image):
670 raise errors.CheckPathError(
671 "Specified path doesn't exist: %s" % args.local_image)
672
Hsin-Yi Chene0bc5392020-09-08 00:48:43 +0800673 if args.local_instance_dir and not os.path.exists(args.local_instance_dir):
674 raise errors.CheckPathError(
675 "Specified path doesn't exist: %s" % args.local_instance_dir)
676
Hsin-Yi Chenc1f69642020-12-28 12:44:43 +0800677 if not (args.local_system_image is None or
Hsin-Yi Chen19d665c2020-12-28 18:13:22 +0800678 args.avd_type in (constants.TYPE_CF, constants.TYPE_GF)):
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800679 raise errors.UnsupportedCreateArgs("%s instance does not support "
680 "--local-system-image" %
681 args.avd_type)
chojoyced71e03c2021-11-03 13:36:36 +0800682 # TODO(b/179340595): To support local image remote instance with kernel build.
683 if args.local_instance is None and args.local_image is not None and (
chojoycec223ec12021-11-10 15:25:03 +0800684 args.kernel_branch or args.kernel_build_id):
chojoyced71e03c2021-11-03 13:36:36 +0800685 raise errors.UnsupportedCreateArgs(
686 "Acloud didn't support local image with specific kernel. "
687 "Please download the specific kernel and put it into "
688 "your local image folder: '%s'." % (
689 args.local_image if args.local_image else
690 utils.GetBuildEnvironmentVariable(constants.ENV_ANDROID_PRODUCT_OUT)))
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800691
692 if (args.local_system_image and
693 not os.path.exists(args.local_system_image)):
694 raise errors.CheckPathError(
695 "Specified path doesn't exist: %s" % args.local_system_image)
696
Hsin-Yi Chene76c1bf2019-12-23 15:15:47 +0800697 for tool_dir in args.local_tool:
698 if not os.path.exists(tool_dir):
699 raise errors.CheckPathError(
700 "Specified path doesn't exist: %s" % tool_dir)
701
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800702
cyland43f2932019-11-26 17:37:28 +0800703def _VerifyHostArgs(args):
704 """Verify args starting with --host.
705
706 Args:
707 args: Namespace object from argparse.parse_args.
708
709 Raises:
710 errors.UnsupportedCreateArgs: When a create arg is specified but
711 unsupported for remote host mode.
712 """
713 if args.remote_host and args.local_instance is not None:
714 raise errors.UnsupportedCreateArgs(
715 "--host is not supported for local instance.")
716
717 if args.remote_host and args.num > 1:
718 raise errors.UnsupportedCreateArgs(
719 "--num is not supported for remote host.")
720
cylan30ab4d62020-01-06 14:01:09 +0800721 if args.host_user != constants.GCE_USER and args.remote_host is None:
cyland43f2932019-11-26 17:37:28 +0800722 raise errors.UnsupportedCreateArgs(
723 "--host-user only support for remote host.")
724
725 if args.host_ssh_private_key_path and args.remote_host is None:
726 raise errors.UnsupportedCreateArgs(
727 "--host-ssh-private-key-path only support for remote host.")
728
729
Hsin-Yi Chenc7e76992021-07-26 18:37:36 +0800730def _VerifyGoldfishArgs(args):
731 """Verify goldfish args.
732
733 Args:
734 args: Namespace object from argparse.parse_args.
735
736 Raises:
737 errors.UnsupportedCreateArgs: When a create arg is specified but
738 unsupported for goldfish.
739 """
Hsin-Yi Chendd8de5d2021-10-21 12:18:23 +0800740 goldfish_only_flags = [
741 args.emulator_build_id,
742 args.emulator_build_target,
743 args.kernel_artifact
744 ]
Hsin-Yi Chenc7e76992021-07-26 18:37:36 +0800745 if args.avd_type != constants.TYPE_GF and any(goldfish_only_flags):
746 raise errors.UnsupportedCreateArgs(
Hsin-Yi Chendd8de5d2021-10-21 12:18:23 +0800747 "--emulator-* and --kernel-artifact are only valid with "
748 "avd_type == %s" % constants.TYPE_GF)
Hsin-Yi Chenc7e76992021-07-26 18:37:36 +0800749
Hsin-Yi Chen725ca6f2021-10-06 17:58:58 +0800750 # Exclude kernel_build_target because the default value isn't empty.
Hsin-Yi Chendd8de5d2021-10-21 12:18:23 +0800751 remote_kernel_flags = [
752 args.kernel_build_id,
753 args.kernel_branch,
754 args.kernel_artifact,
755 ]
756 if (args.avd_type == constants.TYPE_GF and any(remote_kernel_flags) and
757 not all(remote_kernel_flags)):
758 raise errors.UnsupportedCreateArgs(
759 "Either none or all of --kernel-branch, --kernel-build-target, "
760 "--kernel-build-id, and --kernel-artifact must be specified for "
761 "goldfish.")
762
763 remote_system_flags = [
Hsin-Yi Chen725ca6f2021-10-06 17:58:58 +0800764 args.system_build_target,
765 args.system_build_id,
766 args.system_branch,
Hsin-Yi Chen725ca6f2021-10-06 17:58:58 +0800767 ]
Hsin-Yi Chendd8de5d2021-10-21 12:18:23 +0800768 if (args.avd_type == constants.TYPE_GF and any(remote_system_flags) and
769 not all(remote_system_flags)):
770 raise errors.UnsupportedCreateArgs(
771 "Either none or all of --system-branch, --system-build-target, "
772 "and --system-build-id must be specified for goldfish.")
773
774 remote_host_only_flags = ([args.emulator_build_target] +
775 remote_kernel_flags + remote_system_flags)
Hsin-Yi Chen725ca6f2021-10-06 17:58:58 +0800776 if args.avd_type == constants.TYPE_GF and args.remote_host is None and any(
777 remote_host_only_flags):
Hsin-Yi Chenc7e76992021-07-26 18:37:36 +0800778 raise errors.UnsupportedCreateArgs(
Hsin-Yi Chen725ca6f2021-10-06 17:58:58 +0800779 "--kernel-*, --system-*, and --emulator-build-target for goldfish "
Hsin-Yi Chendd8de5d2021-10-21 12:18:23 +0800780 "are only supported for remote host.")
Hsin-Yi Chenc7e76992021-07-26 18:37:36 +0800781
782
herbertxue2625b042018-08-16 23:28:20 +0800783def VerifyArgs(args):
784 """Verify args.
785
786 Args:
787 args: Namespace object from argparse.parse_args.
788
789 Raises:
herbertxue6ef54a52019-05-02 11:38:58 +0800790 errors.UnsupportedMultiAdbPort: multi adb port doesn't support.
herbertxue23b2a962019-07-24 22:39:20 +0800791 errors.UnsupportedCreateArgs: When a create arg is specified but
792 unsupported for a particular avd type.
793 (e.g. --system-build-id for gf)
herbertxue2625b042018-08-16 23:28:20 +0800794 """
herbertxuefd15dfd2018-12-04 11:26:27 +0800795 # Verify that user specified flavor name is in support list.
796 # We don't use argparse's builtin validation because we need to be able to
797 # tell when a user doesn't specify a flavor.
798 if args.flavor and args.flavor not in constants.ALL_FLAVORS:
herbertxue36b99f12021-03-25 14:47:28 +0800799 logger.debug("Flavor[%s] isn't in default support list: %s",
800 args.flavor, constants.ALL_FLAVORS)
801
Hsin-Yi Chen725ca6f2021-10-06 17:58:58 +0800802 if args.avd_type not in (constants.TYPE_CF, constants.TYPE_GF):
herbertxue23b2a962019-07-24 22:39:20 +0800803 if args.system_branch or args.system_build_id or args.system_build_target:
804 raise errors.UnsupportedCreateArgs(
805 "--system-* args are not supported for AVD type: %s"
806 % args.avd_type)
herbertxuefd15dfd2018-12-04 11:26:27 +0800807
herbertxue6ef54a52019-05-02 11:38:58 +0800808 if args.num > 1 and args.adb_port:
809 raise errors.UnsupportedMultiAdbPort(
810 "--adb-port is not supported for multi-devices.")
811
herbertxue755ad482019-11-21 11:31:29 +0800812 if args.num > 1 and args.local_instance is not None:
813 raise errors.UnsupportedCreateArgs(
814 "--num is not supported for local instance.")
815
herbertxue93630f52020-03-06 16:06:56 +0800816 if args.local_instance is None and args.gpu == _DEFAULT_GPU:
817 raise errors.UnsupportedCreateArgs(
818 "Please assign one gpu model for GCE instance. Reference: "
819 "https://cloud.google.com/compute/docs/gpus")
820
herbertxue6ef54a52019-05-02 11:38:58 +0800821 if args.adb_port:
822 utils.CheckPortFree(args.adb_port)
823
herbertxuec58d2812021-01-21 15:02:27 +0800824 hw_properties = create_common.ParseKeyValuePairArgs(args.hw_property)
Sam Chiuc64f3432018-08-17 11:19:06 +0800825 for key in hw_properties:
826 if key not in constants.HW_PROPERTIES:
827 raise errors.InvalidHWPropertyError(
828 "[%s] is an invalid hw property, supported values are:%s. "
829 % (key, constants.HW_PROPERTIES))
Richard Fung8d3979e2019-06-12 16:00:34 -0700830
Richard Fungd7d57f42020-04-09 12:58:49 -0700831 cheeps_only_flags = [args.stable_cheeps_host_image_name,
832 args.stable_cheeps_host_image_project,
833 args.username,
834 args.password,
835 args.cheeps_betty_image]
836 if args.avd_type != constants.TYPE_CHEEPS and any(cheeps_only_flags):
Shao-Chuan Lee80244112020-02-04 17:14:07 +0900837 raise errors.UnsupportedCreateArgs(
Richard Fungd7d57f42020-04-09 12:58:49 -0700838 "--stable-cheeps-*, --betty-image, --username and --password are "
839 "only valid with avd_type == %s" % constants.TYPE_CHEEPS)
Richard Fung8d3979e2019-06-12 16:00:34 -0700840 if (args.username or args.password) and not (args.username and args.password):
841 raise ValueError("--username and --password must both be set")
chojoycec60fa582019-07-11 16:09:02 +0800842 if not args.autoconnect and args.unlock_screen:
843 raise ValueError("--no-autoconnect and --unlock couldn't be "
844 "passed in together.")
Sam Chiu59120542019-08-15 09:32:32 +0800845
Hsin-Yi Chenc7e76992021-07-26 18:37:36 +0800846 _VerifyGoldfishArgs(args)
Hsin-Yi Chen2a609332019-10-07 19:39:52 +0800847 _VerifyLocalArgs(args)
cyland43f2932019-11-26 17:37:28 +0800848 _VerifyHostArgs(args)