blob: 6597113e79095f96296d5654d72998c44f13011e [file] [log] [blame]
Keun Soo Yimb293fdb2016-09-21 16:03:44 -07001#!/usr/bin/env python
2#
3# Copyright 2016 - 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.
Sam Chiu42ac7c52018-10-22 12:27:34 +080016r"""
17Welcome to
18 ___ _______ ____ __ _____
19 / _ |/ ___/ / / __ \/ / / / _ \
20 / __ / /__/ /__/ /_/ / /_/ / // /
21/_/ |_\___/____/\____/\____/____/
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070022
Sam Chiu42ac7c52018-10-22 12:27:34 +080023
24This a tool to create Android Virtual Devices locally/remotely.
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070025
26- Prerequisites:
Sam Chiu42ac7c52018-10-22 12:27:34 +080027 The manual will be available at
28 https://android.googlesource.com/platform/tools/acloud/+/master/README.md
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070029
Sam Chiu42ac7c52018-10-22 12:27:34 +080030- To get started:
31 - Create instances:
32 1) To create a remote cuttlefish instance with the local built image.
33 Example:
chojoyce1c4ecc92018-12-06 14:16:44 +080034 $ acloud create --local-image
35 Or specify built image dir:
36 $ acloud create --local-image /tmp/image_dir
Sam Chiu42ac7c52018-10-22 12:27:34 +080037 2) To create a local cuttlefish instance using the image which has been
38 built out in your workspace.
39 Example:
40 $ acloud create --local-instance --local-image
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070041
Sam Chiu42ac7c52018-10-22 12:27:34 +080042 - Delete instances:
43 $ acloud delete
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070044
Sam Chiua263b9e2019-08-08 13:52:54 +080045 - Reconnect:
46 To reconnect adb/vnc to an existing instance that's been disconnected:
47 $ acloud reconnect
48 Or to specify a specific instance:
49 $ acloud reconnect --instance-names <instance_name like ins-123-cf-x86-phone>
50
51 - List:
52 List will retrieve all the remote instances you've created in addition to any
53 local instances created as well.
54 To show device IP address, adb port and instance name:
55 $ acloud list
56 To show more detail info on the list.
57 $ acloud list -vv
58
Sam Chiu42ac7c52018-10-22 12:27:34 +080059Try $acloud [cmd] --help for further details.
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070060
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070061"""
Kevin Cheng1ea015f2019-01-08 09:10:58 -080062
63from __future__ import print_function
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070064import argparse
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070065import logging
Kevin Cheng1ea015f2019-01-08 09:10:58 -080066import platform
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070067import sys
Sam Chiue791f602019-05-03 15:18:10 +080068import traceback
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070069
Kevin Cheng1ea015f2019-01-08 09:10:58 -080070# TODO: Remove this once we switch over to embedded launcher.
71# Exit out if python version is < 2.7.13 due to b/120883119.
72if (sys.version_info.major == 2
73 and sys.version_info.minor == 7
74 and sys.version_info.micro < 13):
75 print("Acloud requires python version 2.7.13+ (currently @ %d.%d.%d)" %
76 (sys.version_info.major, sys.version_info.minor,
77 sys.version_info.micro))
78 print("Update your 2.7 python with:")
79 # pylint: disable=invalid-name
80 os_type = platform.system().lower()
81 if os_type == "linux":
82 print(" apt-get install python2.7")
83 elif os_type == "darwin":
84 print(" brew install python@2 (and then follow instructions at "
85 "https://docs.python-guide.org/starting/install/osx/)")
86 print(" - or -")
87 print(" POSIXLY_CORRECT=1 port -N install python27")
88 sys.exit(1)
89
Sam Chiue791f602019-05-03 15:18:10 +080090# By Default silence root logger's stream handler since 3p lib may initial
91# root logger no matter what level we're using. The acloud logger behavior will
92# be defined in _SetupLogging(). This also could workaround to get rid of below
93# oauth2client warning:
Sam Chiu29d858f2018-08-14 20:06:25 +080094# 'No handlers could be found for logger "oauth2client.contrib.multistore_file'
Sam Chiue791f602019-05-03 15:18:10 +080095DEFAULT_STREAM_HANDLER = logging.StreamHandler()
96DEFAULT_STREAM_HANDLER.setLevel(logging.CRITICAL)
97logging.getLogger().addHandler(DEFAULT_STREAM_HANDLER)
Kevin Chengb5963882018-05-09 00:06:27 -070098
Kevin Chengf4137c62018-05-22 16:06:58 -070099# pylint: disable=wrong-import-position
Sam Chiu7de3b232018-12-06 19:45:52 +0800100from acloud import errors
Kevin Cheng6001db32018-10-23 12:34:20 -0700101from acloud.create import create
102from acloud.create import create_args
103from acloud.delete import delete
104from acloud.delete import delete_args
Sam Chiue791f602019-05-03 15:18:10 +0800105from acloud.internal import constants
cylan4569dca2018-11-02 12:12:53 +0800106from acloud.reconnect import reconnect
107from acloud.reconnect import reconnect_args
Sam Chiu56c58892018-10-25 09:53:19 +0800108from acloud.list import list as list_instances
109from acloud.list import list_args
Kevin Cheng6001db32018-10-23 12:34:20 -0700110from acloud.metrics import metrics
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700111from acloud.public import acloud_common
112from acloud.public import config
113from acloud.public import device_driver
Kevin Chengb5963882018-05-09 00:06:27 -0700114from acloud.public.actions import create_cuttlefish_action
115from acloud.public.actions import create_goldfish_action
Kevin Chengee6030f2018-06-26 10:55:30 -0700116from acloud.setup import setup
117from acloud.setup import setup_args
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700118
herbertxue1512f8a2019-06-27 13:56:23 +0800119
Sam Chiu445941f2018-10-04 11:54:40 +0800120LOGGING_FMT = "%(asctime)s |%(levelname)s| %(module)s:%(lineno)s| %(message)s"
Sam Chiu29d858f2018-08-14 20:06:25 +0800121ACLOUD_LOGGER = "acloud"
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700122
123# Commands
Kevin Chengb5963882018-05-09 00:06:27 -0700124CMD_CREATE_CUTTLEFISH = "create_cf"
125CMD_CREATE_GOLDFISH = "create_gf"
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700126CMD_CLEANUP = "cleanup"
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700127
128
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700129# pylint: disable=too-many-statements
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700130def _ParseArgs(args):
131 """Parse args.
132
133 Args:
134 args: Argument list passed from main.
135
136 Returns:
137 Parsed args.
138 """
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700139 usage = ",".join([
Sam Chiue669ef72018-10-16 16:23:37 +0800140 setup_args.CMD_SETUP,
141 create_args.CMD_CREATE,
Sam Chiu56c58892018-10-25 09:53:19 +0800142 list_args.CMD_LIST,
Kevin Chengeb85e862018-10-09 15:35:13 -0700143 delete_args.CMD_DELETE,
cylan4569dca2018-11-02 12:12:53 +0800144 reconnect_args.CMD_RECONNECT,
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700145 ])
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700146 parser = argparse.ArgumentParser(
147 description=__doc__,
148 formatter_class=argparse.RawDescriptionHelpFormatter,
Sam Chiu42ac7c52018-10-22 12:27:34 +0800149 usage="acloud {" + usage + "} ...")
Kevin Cheng6bd8d132019-02-25 23:00:38 -0800150 subparsers = parser.add_subparsers(metavar="{" + usage + "}")
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700151 subparser_list = []
152
Kevin Chengb5963882018-05-09 00:06:27 -0700153 # Command "create_cf", create cuttlefish instances
154 create_cf_parser = subparsers.add_parser(CMD_CREATE_CUTTLEFISH)
155 create_cf_parser.required = False
156 create_cf_parser.set_defaults(which=CMD_CREATE_CUTTLEFISH)
157 create_cf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700158 "--branch",
159 type=str,
160 dest="branch",
161 help="Android branch, e.g. git_master")
162 create_cf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700163 "--kernel_build_id",
Kevin Chengcf5bbf52019-05-09 16:17:08 -0700164 "--kernel-build-id",
Kevin Chengb5963882018-05-09 00:06:27 -0700165 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"
Kevin Chengcf5bbf52019-05-09 16:17:08 -0700169 " kernel build with a particular Android build (--build_id). If neither"
170 " kernel_branch nor kernel_build_id are specified, the kernel that's"
171 " bundled with the Android build would be used.")
172 create_cf_parser.add_argument(
173 "--kernel_branch",
174 "--kernel-branch",
175 type=str,
176 dest="kernel_branch",
177 required=False,
178 help="Android kernel build branch name, e.g."
179 " kernel-common-android-4.14. This is to test a new kernel build with a"
180 " particular Android build (--build-id). If specified without"
181 " specifying kernel_build_id, the last green build in the branch will"
182 " be used. If neither kernel_branch nor kernel_build_id are specified,"
183 " the kernel that's bundled with the Android build would be used.")
Kevin Cheng4eeb9d42019-06-05 10:17:18 -0700184 create_cf_parser.add_argument(
Kevin Cheng85987fc2019-06-19 10:51:21 -0700185 "--kernel_build_target",
186 type=str,
187 dest="kernel_build_target",
188 default="kernel",
189 help="Kernel build target, specify if different from 'kernel'")
190 create_cf_parser.add_argument(
Kevin Cheng4eeb9d42019-06-05 10:17:18 -0700191 "--system_branch",
192 type=str,
193 dest="system_branch",
194 help="Branch to consume the system image (system.img) from, will "
195 "default to what is defined by --branch. "
196 "That feature allows to (automatically) test various combinations "
197 "of vendor.img (CF, e.g.) and system images (GSI, e.g.). ",
198 required=False)
199 create_cf_parser.add_argument(
200 "--system_build_id",
201 type=str,
202 dest="system_build_id",
203 help="System image build id, e.g. 2145099, P2804227",
204 required=False)
205 create_cf_parser.add_argument(
206 "--system_build_target",
207 type=str,
208 dest="system_build_target",
209 help="System image build target, specify if different from "
210 "--build_target",
211 required=False)
Kevin Chengb5963882018-05-09 00:06:27 -0700212
Kevin Cheng3087af52018-08-13 13:26:50 -0700213 create_args.AddCommonCreateArgs(create_cf_parser)
Kevin Chengb5963882018-05-09 00:06:27 -0700214 subparser_list.append(create_cf_parser)
215
216 # Command "create_gf", create goldfish instances
217 # In order to create a goldfish device we need the following parameters:
218 # 1. The emulator build we wish to use, this is the binary that emulates
219 # an android device. See go/emu-dev for more
220 # 2. A system-image. This is the android release we wish to run on the
221 # emulated hardware.
222 create_gf_parser = subparsers.add_parser(CMD_CREATE_GOLDFISH)
223 create_gf_parser.required = False
224 create_gf_parser.set_defaults(which=CMD_CREATE_GOLDFISH)
225 create_gf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700226 "--branch",
227 type=str,
228 dest="branch",
229 help="Android branch, e.g. git_master")
230 create_gf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700231 "--emulator_build_id",
232 type=str,
233 dest="emulator_build_id",
234 required=False,
235 help="Emulator build used to run the images. e.g. 4669466.")
236 create_gf_parser.add_argument(
Kevin Cheng85187b72019-06-04 15:38:45 -0700237 "--emulator_branch",
238 type=str,
239 dest="emulator_branch",
240 required=False,
241 help="Emulator build branch name, e.g. aosp-emu-master-dev. If specified"
242 " without emulator_build_id, the last green build will be used.")
243 create_gf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700244 "--gpu",
245 type=str,
246 dest="gpu",
247 required=False,
248 default=None,
249 help="GPU accelerator to use if any."
250 " e.g. nvidia-tesla-k80, omit to use swiftshader")
251 create_gf_parser.add_argument(
Kevin Chengbced4af2018-06-26 10:35:01 -0700252 "--base_image",
253 type=str,
254 dest="base_image",
255 required=False,
256 help="Name of the goldfish base image to be used to create the instance. "
257 "This will override stable_goldfish_host_image_name from config. "
258 "e.g. emu-dev-cts-061118")
Erwin Jansenf39798d2019-05-14 21:06:44 -0700259 create_gf_parser.add_argument(
260 "--tags",
261 dest="tags",
262 nargs="*",
263 required=False,
264 default=None,
265 help="Tags to be set on to the created instance. e.g. https-server.")
Kevin Cheng7be06d62019-06-14 16:00:15 -0700266 create_gf_parser.add_argument(
267 "--kernel_build_id",
268 type=str,
269 dest="kernel_build_id",
270 help="Android kernel build id, e.g. 4586590. This is to test a new"
271 " kernel build with a particular Android build (--build_id). If neither"
272 " kernel_branch nor kernel_build_id are specified, the kernel that's"
273 " bundled with the Android build would be used.")
274 create_gf_parser.add_argument(
275 "--kernel_branch",
276 type=str,
277 dest="kernel_branch",
278 help="Android kernel build branch name, "
279 "e.g. kernel-common-android-4.14. This is to test a new kernel build "
280 "with a particular Android build (--build_id). If specified without "
281 "specifying kernel_build_id, the last green build in the branch will "
282 "be used. If neither kernel_branch nor kernel_build_id are specified, "
283 "the kernel that's bundled with the Android build would be used.")
Kevin Chengb5963882018-05-09 00:06:27 -0700284
Kevin Cheng3087af52018-08-13 13:26:50 -0700285 create_args.AddCommonCreateArgs(create_gf_parser)
Kevin Chengb5963882018-05-09 00:06:27 -0700286 subparser_list.append(create_gf_parser)
287
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700288 # Command "cleanup"
289 cleanup_parser = subparsers.add_parser(CMD_CLEANUP)
290 cleanup_parser.required = False
291 cleanup_parser.set_defaults(which=CMD_CLEANUP)
292 cleanup_parser.add_argument(
293 "--expiration_mins",
294 type=int,
295 dest="expiration_mins",
296 required=True,
297 help="Garbage collect all gce instances, gce images, cached disk "
298 "images that are older than |expiration_mins|.")
299 subparser_list.append(cleanup_parser)
300
Kevin Chengeb85e862018-10-09 15:35:13 -0700301 # Command "create"
302 subparser_list.append(create_args.GetCreateArgParser(subparsers))
303
Kevin Chengee6030f2018-06-26 10:55:30 -0700304 # Command "setup"
305 subparser_list.append(setup_args.GetSetupArgParser(subparsers))
306
Sam Chiu56c58892018-10-25 09:53:19 +0800307 # Command "delete"
Kevin Chengeb85e862018-10-09 15:35:13 -0700308 subparser_list.append(delete_args.GetDeleteArgParser(subparsers))
309
Sam Chiu56c58892018-10-25 09:53:19 +0800310 # Command "list"
311 subparser_list.append(list_args.GetListArgParser(subparsers))
312
cylan4569dca2018-11-02 12:12:53 +0800313 # Command "Reconnect"
314 subparser_list.append(reconnect_args.GetReconnectArgParser(subparsers))
315
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700316 # Add common arguments.
Kevin Chengb21d7712018-05-24 14:54:55 -0700317 for subparser in subparser_list:
318 acloud_common.AddCommonArguments(subparser)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700319
320 return parser.parse_args(args)
321
322
herbertxue2625b042018-08-16 23:28:20 +0800323# pylint: disable=too-many-branches
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700324def _VerifyArgs(parsed_args):
325 """Verify args.
326
327 Args:
328 parsed_args: Parsed args.
329
330 Raises:
331 errors.CommandArgError: If args are invalid.
332 """
herbertxue2625b042018-08-16 23:28:20 +0800333 if parsed_args.which == create_args.CMD_CREATE:
334 create_args.VerifyArgs(parsed_args)
Kevin Cheng84d3eed2018-08-16 15:16:00 -0700335 if parsed_args.which == CMD_CREATE_CUTTLEFISH:
Kevin Chengd1671ce2019-07-23 01:13:52 -0700336 if not parsed_args.build_id and not parsed_args.branch:
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700337 raise errors.CommandArgError(
Kevin Chengd1671ce2019-07-23 01:13:52 -0700338 "Must specify --build_id or --branch")
Kevin Chengb5963882018-05-09 00:06:27 -0700339 if parsed_args.which == CMD_CREATE_GOLDFISH:
Kevin Cheng85187b72019-06-04 15:38:45 -0700340 if not parsed_args.emulator_build_id and not parsed_args.build_id and (
341 not parsed_args.emulator_branch and not parsed_args.branch):
342 raise errors.CommandArgError(
343 "Must specify either --build_id or --branch or "
344 "--emulator_branch or --emulator_build_id")
Kevin Cheng84d3eed2018-08-16 15:16:00 -0700345 if not parsed_args.build_target:
346 raise errors.CommandArgError("Must specify --build_target")
Kevin Chengb5963882018-05-09 00:06:27 -0700347
348 if parsed_args.which in [
Kevin Cheng3087af52018-08-13 13:26:50 -0700349 create_args.CMD_CREATE, CMD_CREATE_CUTTLEFISH, CMD_CREATE_GOLDFISH
Kevin Chengb5963882018-05-09 00:06:27 -0700350 ]:
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700351 if (parsed_args.serial_log_file
352 and not parsed_args.serial_log_file.endswith(".tar.gz")):
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700353 raise errors.CommandArgError(
354 "--serial_log_file must ends with .tar.gz")
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700355 if (parsed_args.logcat_file
356 and not parsed_args.logcat_file.endswith(".tar.gz")):
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700357 raise errors.CommandArgError(
358 "--logcat_file must ends with .tar.gz")
359
360
Sam Chiu29d858f2018-08-14 20:06:25 +0800361def _SetupLogging(log_file, verbose):
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700362 """Setup logging.
363
Sam Chiu29d858f2018-08-14 20:06:25 +0800364 This function define the logging policy in below manners.
365 - without -v , -vv ,--log_file:
366 Only display critical log and print() message on screen.
367
368 - with -v:
369 Display INFO log and set StreamHandler to acloud parent logger to turn on
370 ONLY acloud modules logging.(silence all 3p libraries)
371
372 - with -vv:
373 Display INFO/DEBUG log and set StreamHandler to root logger to turn on all
374 acloud modules and 3p libraries logging.
375
376 - with --log_file.
377 Dump logs to FileHandler with DEBUG level.
378
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700379 Args:
Sam Chiu29d858f2018-08-14 20:06:25 +0800380 log_file: String, if not None, dump the log to log file.
381 verbose: Int, if verbose = 1(-v), log at INFO level and turn on
382 logging on libraries to a StreamHandler.
383 If verbose = 2(-vv), log at DEBUG level and turn on logging on
384 all libraries and 3rd party libraries to a StreamHandler.
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700385 """
Sam Chiu29d858f2018-08-14 20:06:25 +0800386 # Define logging level and hierarchy by verbosity.
387 shandler_level = None
388 logger = None
389 if verbose == 0:
390 shandler_level = logging.CRITICAL
391 logger = logging.getLogger(ACLOUD_LOGGER)
392 elif verbose == 1:
393 shandler_level = logging.INFO
394 logger = logging.getLogger(ACLOUD_LOGGER)
395 elif verbose > 1:
396 shandler_level = logging.DEBUG
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700397 logger = logging.getLogger()
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700398
Sam Chiu29d858f2018-08-14 20:06:25 +0800399 # Add StreamHandler by default.
400 shandler = logging.StreamHandler()
401 shandler.setFormatter(logging.Formatter(LOGGING_FMT))
402 shandler.setLevel(shandler_level)
403 logger.addHandler(shandler)
404 # Set the default level to DEBUG, the other handlers will handle
405 # their own levels via the args supplied (-v and --log_file).
406 logger.setLevel(logging.DEBUG)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700407
Sam Chiu29d858f2018-08-14 20:06:25 +0800408 # Add FileHandler if log_file is provided.
Sam Chiufde41e92018-08-07 18:37:02 +0800409 if log_file:
Sam Chiu29d858f2018-08-14 20:06:25 +0800410 fhandler = logging.FileHandler(filename=log_file)
411 fhandler.setFormatter(logging.Formatter(LOGGING_FMT))
412 fhandler.setLevel(logging.DEBUG)
413 logger.addHandler(fhandler)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700414
415
Erwin Jansen95559242018-11-08 15:38:18 -0800416def main(argv=None):
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700417 """Main entry.
418
419 Args:
420 argv: A list of system arguments.
421
422 Returns:
423 0 if success. None-zero if fails.
424 """
Erwin Jansen95559242018-11-08 15:38:18 -0800425 if argv is None:
426 argv = sys.argv[1:]
427
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700428 args = _ParseArgs(argv)
Sam Chiu29d858f2018-08-14 20:06:25 +0800429 _SetupLogging(args.log_file, args.verbose)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700430 _VerifyArgs(args)
431
Sam Chiuc64f3432018-08-17 11:19:06 +0800432 cfg = config.GetAcloudConfig(args)
Kevin Cheng3087af52018-08-13 13:26:50 -0700433 # TODO: Move this check into the functions it is actually needed.
Fang Dengcef4b112017-03-02 11:20:17 -0800434 # Check access.
Kevin Cheng3087af52018-08-13 13:26:50 -0700435 # device_driver.CheckAccess(cfg)
Fang Dengcef4b112017-03-02 11:20:17 -0800436
Kevin Chengee6030f2018-06-26 10:55:30 -0700437 report = None
chojoyce7a361732018-11-26 16:26:13 +0800438 if args.which == create_args.CMD_CREATE:
Kevin Chengc3d0d5e2018-08-14 14:22:44 -0700439 create.Run(args)
Kevin Chengb5963882018-05-09 00:06:27 -0700440 elif args.which == CMD_CREATE_CUTTLEFISH:
441 report = create_cuttlefish_action.CreateDevices(
442 cfg=cfg,
443 build_target=args.build_target,
444 build_id=args.build_id,
Kevin Cheng81d43952019-06-07 11:37:45 -0700445 branch=args.branch,
Kevin Chengb5963882018-05-09 00:06:27 -0700446 kernel_build_id=args.kernel_build_id,
Kevin Chengcf5bbf52019-05-09 16:17:08 -0700447 kernel_branch=args.kernel_branch,
Kevin Cheng85987fc2019-06-19 10:51:21 -0700448 kernel_build_target=args.kernel_build_target,
Kevin Cheng4eeb9d42019-06-05 10:17:18 -0700449 system_branch=args.system_branch,
450 system_build_id=args.system_build_id,
451 system_build_target=args.system_build_target,
Kevin Chengb5963882018-05-09 00:06:27 -0700452 num=args.num,
453 serial_log_file=args.serial_log_file,
454 logcat_file=args.logcat_file,
Kevin Cheng86d43c72018-08-30 10:59:14 -0700455 autoconnect=args.autoconnect,
cylanbab76b12019-07-16 16:36:39 +0800456 report_internal_ip=args.report_internal_ip,
457 boot_timeout_secs=args.boot_timeout_secs)
Kevin Chengb5963882018-05-09 00:06:27 -0700458 elif args.which == CMD_CREATE_GOLDFISH:
459 report = create_goldfish_action.CreateDevices(
460 cfg=cfg,
461 build_target=args.build_target,
462 build_id=args.build_id,
463 emulator_build_id=args.emulator_build_id,
Kevin Cheng85187b72019-06-04 15:38:45 -0700464 branch=args.branch,
465 emulator_branch=args.emulator_branch,
Kevin Cheng7be06d62019-06-14 16:00:15 -0700466 kernel_build_id=args.kernel_build_id,
467 kernel_branch=args.kernel_branch,
Kevin Chengb5963882018-05-09 00:06:27 -0700468 gpu=args.gpu,
469 num=args.num,
470 serial_log_file=args.serial_log_file,
471 logcat_file=args.logcat_file,
Kevin Cheng84d3eed2018-08-16 15:16:00 -0700472 autoconnect=args.autoconnect,
Erwin Jansenf39798d2019-05-14 21:06:44 -0700473 tags=args.tags,
Kevin Cheng86d43c72018-08-30 10:59:14 -0700474 report_internal_ip=args.report_internal_ip)
Sam Chiu56c58892018-10-25 09:53:19 +0800475 elif args.which == delete_args.CMD_DELETE:
Kevin Chengeb85e862018-10-09 15:35:13 -0700476 report = delete.Run(args)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700477 elif args.which == CMD_CLEANUP:
478 report = device_driver.Cleanup(cfg, args.expiration_mins)
Sam Chiu56c58892018-10-25 09:53:19 +0800479 elif args.which == list_args.CMD_LIST:
480 list_instances.Run(args)
cylan4569dca2018-11-02 12:12:53 +0800481 elif args.which == reconnect_args.CMD_RECONNECT:
482 reconnect.Run(args)
Kevin Chengee6030f2018-06-26 10:55:30 -0700483 elif args.which == setup_args.CMD_SETUP:
herbertxue34776bb2018-07-03 21:57:48 +0800484 setup.Run(args)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700485 else:
486 sys.stderr.write("Invalid command %s" % args.which)
487 return 2
488
herbertxue07293a32018-11-05 20:40:11 +0800489 if report and args.report_file:
Kevin Chengee6030f2018-06-26 10:55:30 -0700490 report.Dump(args.report_file)
491 if report.errors:
492 msg = "\n".join(report.errors)
493 sys.stderr.write("Encountered the following errors:\n%s\n" % msg)
494 return 1
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700495 return 0
Tri Vo8e292532016-10-01 16:55:51 -0700496
497
498if __name__ == "__main__":
Sam Chiue791f602019-05-03 15:18:10 +0800499 EXIT_CODE = None
500 EXCEPTION_STACKTRACE = None
501 EXCEPTION_LOG = None
Sam Chiu37b1ee32019-06-20 10:49:56 +0800502 LOG_METRICS = metrics.LogUsage(sys.argv[1:])
Sam Chiue791f602019-05-03 15:18:10 +0800503 try:
504 EXIT_CODE = main(sys.argv[1:])
505 except Exception as e:
506 EXIT_CODE = constants.EXIT_BY_ERROR
507 EXCEPTION_STACKTRACE = traceback.format_exc()
508 EXCEPTION_LOG = str(e)
509 raise
510 finally:
511 # Log Exit event here to calculate the consuming time.
Sam Chiu37b1ee32019-06-20 10:49:56 +0800512 if LOG_METRICS:
513 metrics.LogExitEvent(EXIT_CODE,
514 stacktrace=EXCEPTION_STACKTRACE,
515 logs=EXCEPTION_LOG)