blob: a7fe194a2d29689efb624fe9b589983f40a3dfbc [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 Chiu42ac7c52018-10-22 12:27:34 +080045Try $acloud [cmd] --help for further details.
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070046
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070047"""
Kevin Cheng1ea015f2019-01-08 09:10:58 -080048
49from __future__ import print_function
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070050import argparse
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070051import logging
Kevin Cheng1ea015f2019-01-08 09:10:58 -080052import platform
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070053import sys
Sam Chiue791f602019-05-03 15:18:10 +080054import traceback
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070055
Kevin Cheng1ea015f2019-01-08 09:10:58 -080056# TODO: Remove this once we switch over to embedded launcher.
57# Exit out if python version is < 2.7.13 due to b/120883119.
58if (sys.version_info.major == 2
59 and sys.version_info.minor == 7
60 and sys.version_info.micro < 13):
61 print("Acloud requires python version 2.7.13+ (currently @ %d.%d.%d)" %
62 (sys.version_info.major, sys.version_info.minor,
63 sys.version_info.micro))
64 print("Update your 2.7 python with:")
65 # pylint: disable=invalid-name
66 os_type = platform.system().lower()
67 if os_type == "linux":
68 print(" apt-get install python2.7")
69 elif os_type == "darwin":
70 print(" brew install python@2 (and then follow instructions at "
71 "https://docs.python-guide.org/starting/install/osx/)")
72 print(" - or -")
73 print(" POSIXLY_CORRECT=1 port -N install python27")
74 sys.exit(1)
75
Sam Chiue791f602019-05-03 15:18:10 +080076# By Default silence root logger's stream handler since 3p lib may initial
77# root logger no matter what level we're using. The acloud logger behavior will
78# be defined in _SetupLogging(). This also could workaround to get rid of below
79# oauth2client warning:
Sam Chiu29d858f2018-08-14 20:06:25 +080080# 'No handlers could be found for logger "oauth2client.contrib.multistore_file'
Sam Chiue791f602019-05-03 15:18:10 +080081DEFAULT_STREAM_HANDLER = logging.StreamHandler()
82DEFAULT_STREAM_HANDLER.setLevel(logging.CRITICAL)
83logging.getLogger().addHandler(DEFAULT_STREAM_HANDLER)
Kevin Chengb5963882018-05-09 00:06:27 -070084
Kevin Chengf4137c62018-05-22 16:06:58 -070085# pylint: disable=wrong-import-position
Sam Chiu7de3b232018-12-06 19:45:52 +080086from acloud import errors
Kevin Cheng6001db32018-10-23 12:34:20 -070087from acloud.create import create
88from acloud.create import create_args
89from acloud.delete import delete
90from acloud.delete import delete_args
Sam Chiue791f602019-05-03 15:18:10 +080091from acloud.internal import constants
cylan4569dca2018-11-02 12:12:53 +080092from acloud.reconnect import reconnect
93from acloud.reconnect import reconnect_args
Sam Chiu56c58892018-10-25 09:53:19 +080094from acloud.list import list as list_instances
95from acloud.list import list_args
Kevin Cheng6001db32018-10-23 12:34:20 -070096from acloud.metrics import metrics
Keun Soo Yimb293fdb2016-09-21 16:03:44 -070097from acloud.public import acloud_common
98from acloud.public import config
99from acloud.public import device_driver
Kevin Chengb5963882018-05-09 00:06:27 -0700100from acloud.public.actions import create_cuttlefish_action
101from acloud.public.actions import create_goldfish_action
Kevin Chengee6030f2018-06-26 10:55:30 -0700102from acloud.setup import setup
103from acloud.setup import setup_args
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700104
Sam Chiu445941f2018-10-04 11:54:40 +0800105LOGGING_FMT = "%(asctime)s |%(levelname)s| %(module)s:%(lineno)s| %(message)s"
Sam Chiu29d858f2018-08-14 20:06:25 +0800106ACLOUD_LOGGER = "acloud"
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700107
108# Commands
Kevin Chengb5963882018-05-09 00:06:27 -0700109CMD_CREATE_CUTTLEFISH = "create_cf"
110CMD_CREATE_GOLDFISH = "create_gf"
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700111CMD_CLEANUP = "cleanup"
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700112
113
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700114# pylint: disable=too-many-statements
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700115def _ParseArgs(args):
116 """Parse args.
117
118 Args:
119 args: Argument list passed from main.
120
121 Returns:
122 Parsed args.
123 """
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700124 usage = ",".join([
Sam Chiue669ef72018-10-16 16:23:37 +0800125 setup_args.CMD_SETUP,
126 create_args.CMD_CREATE,
Sam Chiu56c58892018-10-25 09:53:19 +0800127 list_args.CMD_LIST,
Kevin Chengeb85e862018-10-09 15:35:13 -0700128 delete_args.CMD_DELETE,
cylan4569dca2018-11-02 12:12:53 +0800129 reconnect_args.CMD_RECONNECT,
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700130 ])
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700131 parser = argparse.ArgumentParser(
132 description=__doc__,
133 formatter_class=argparse.RawDescriptionHelpFormatter,
Sam Chiu42ac7c52018-10-22 12:27:34 +0800134 usage="acloud {" + usage + "} ...")
Kevin Cheng6bd8d132019-02-25 23:00:38 -0800135 subparsers = parser.add_subparsers(metavar="{" + usage + "}")
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700136 subparser_list = []
137
Kevin Chengb5963882018-05-09 00:06:27 -0700138 # Command "create_cf", create cuttlefish instances
139 create_cf_parser = subparsers.add_parser(CMD_CREATE_CUTTLEFISH)
140 create_cf_parser.required = False
141 create_cf_parser.set_defaults(which=CMD_CREATE_CUTTLEFISH)
142 create_cf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700143 "--branch",
144 type=str,
145 dest="branch",
146 help="Android branch, e.g. git_master")
147 create_cf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700148 "--kernel_build_id",
Kevin Chengcf5bbf52019-05-09 16:17:08 -0700149 "--kernel-build-id",
Kevin Chengb5963882018-05-09 00:06:27 -0700150 type=str,
151 dest="kernel_build_id",
152 required=False,
153 help="Android kernel build id, e.g. 4586590. This is to test a new"
Kevin Chengcf5bbf52019-05-09 16:17:08 -0700154 " kernel build with a particular Android build (--build_id). If neither"
155 " kernel_branch nor kernel_build_id are specified, the kernel that's"
156 " bundled with the Android build would be used.")
157 create_cf_parser.add_argument(
158 "--kernel_branch",
159 "--kernel-branch",
160 type=str,
161 dest="kernel_branch",
162 required=False,
163 help="Android kernel build branch name, e.g."
164 " kernel-common-android-4.14. This is to test a new kernel build with a"
165 " particular Android build (--build-id). If specified without"
166 " specifying kernel_build_id, the last green build in the branch will"
167 " be used. If neither kernel_branch nor kernel_build_id are specified,"
168 " the kernel that's bundled with the Android build would be used.")
Kevin Cheng4eeb9d42019-06-05 10:17:18 -0700169 create_cf_parser.add_argument(
170 "--system_branch",
171 type=str,
172 dest="system_branch",
173 help="Branch to consume the system image (system.img) from, will "
174 "default to what is defined by --branch. "
175 "That feature allows to (automatically) test various combinations "
176 "of vendor.img (CF, e.g.) and system images (GSI, e.g.). ",
177 required=False)
178 create_cf_parser.add_argument(
179 "--system_build_id",
180 type=str,
181 dest="system_build_id",
182 help="System image build id, e.g. 2145099, P2804227",
183 required=False)
184 create_cf_parser.add_argument(
185 "--system_build_target",
186 type=str,
187 dest="system_build_target",
188 help="System image build target, specify if different from "
189 "--build_target",
190 required=False)
Kevin Chengb5963882018-05-09 00:06:27 -0700191
Kevin Cheng3087af52018-08-13 13:26:50 -0700192 create_args.AddCommonCreateArgs(create_cf_parser)
Kevin Chengb5963882018-05-09 00:06:27 -0700193 subparser_list.append(create_cf_parser)
194
195 # Command "create_gf", create goldfish instances
196 # In order to create a goldfish device we need the following parameters:
197 # 1. The emulator build we wish to use, this is the binary that emulates
198 # an android device. See go/emu-dev for more
199 # 2. A system-image. This is the android release we wish to run on the
200 # emulated hardware.
201 create_gf_parser = subparsers.add_parser(CMD_CREATE_GOLDFISH)
202 create_gf_parser.required = False
203 create_gf_parser.set_defaults(which=CMD_CREATE_GOLDFISH)
204 create_gf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700205 "--branch",
206 type=str,
207 dest="branch",
208 help="Android branch, e.g. git_master")
209 create_gf_parser.add_argument(
Kevin Chengb5963882018-05-09 00:06:27 -0700210 "--emulator_build_id",
211 type=str,
212 dest="emulator_build_id",
213 required=False,
214 help="Emulator build used to run the images. e.g. 4669466.")
215 create_gf_parser.add_argument(
216 "--gpu",
217 type=str,
218 dest="gpu",
219 required=False,
220 default=None,
221 help="GPU accelerator to use if any."
222 " e.g. nvidia-tesla-k80, omit to use swiftshader")
223 create_gf_parser.add_argument(
Kevin Chengbced4af2018-06-26 10:35:01 -0700224 "--base_image",
225 type=str,
226 dest="base_image",
227 required=False,
228 help="Name of the goldfish base image to be used to create the instance. "
229 "This will override stable_goldfish_host_image_name from config. "
230 "e.g. emu-dev-cts-061118")
Erwin Jansenf39798d2019-05-14 21:06:44 -0700231 create_gf_parser.add_argument(
232 "--tags",
233 dest="tags",
234 nargs="*",
235 required=False,
236 default=None,
237 help="Tags to be set on to the created instance. e.g. https-server.")
Kevin Chengb5963882018-05-09 00:06:27 -0700238
Kevin Cheng3087af52018-08-13 13:26:50 -0700239 create_args.AddCommonCreateArgs(create_gf_parser)
Kevin Chengb5963882018-05-09 00:06:27 -0700240 subparser_list.append(create_gf_parser)
241
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700242 # Command "cleanup"
243 cleanup_parser = subparsers.add_parser(CMD_CLEANUP)
244 cleanup_parser.required = False
245 cleanup_parser.set_defaults(which=CMD_CLEANUP)
246 cleanup_parser.add_argument(
247 "--expiration_mins",
248 type=int,
249 dest="expiration_mins",
250 required=True,
251 help="Garbage collect all gce instances, gce images, cached disk "
252 "images that are older than |expiration_mins|.")
253 subparser_list.append(cleanup_parser)
254
Kevin Chengeb85e862018-10-09 15:35:13 -0700255 # Command "create"
256 subparser_list.append(create_args.GetCreateArgParser(subparsers))
257
Kevin Chengee6030f2018-06-26 10:55:30 -0700258 # Command "setup"
259 subparser_list.append(setup_args.GetSetupArgParser(subparsers))
260
Sam Chiu56c58892018-10-25 09:53:19 +0800261 # Command "delete"
Kevin Chengeb85e862018-10-09 15:35:13 -0700262 subparser_list.append(delete_args.GetDeleteArgParser(subparsers))
263
Sam Chiu56c58892018-10-25 09:53:19 +0800264 # Command "list"
265 subparser_list.append(list_args.GetListArgParser(subparsers))
266
cylan4569dca2018-11-02 12:12:53 +0800267 # Command "Reconnect"
268 subparser_list.append(reconnect_args.GetReconnectArgParser(subparsers))
269
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700270 # Add common arguments.
Kevin Chengb21d7712018-05-24 14:54:55 -0700271 for subparser in subparser_list:
272 acloud_common.AddCommonArguments(subparser)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700273
274 return parser.parse_args(args)
275
276
herbertxue2625b042018-08-16 23:28:20 +0800277# pylint: disable=too-many-branches
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700278def _VerifyArgs(parsed_args):
279 """Verify args.
280
281 Args:
282 parsed_args: Parsed args.
283
284 Raises:
285 errors.CommandArgError: If args are invalid.
286 """
herbertxue2625b042018-08-16 23:28:20 +0800287 if parsed_args.which == create_args.CMD_CREATE:
288 create_args.VerifyArgs(parsed_args)
Kevin Cheng84d3eed2018-08-16 15:16:00 -0700289 if parsed_args.which == CMD_CREATE_CUTTLEFISH:
Kevin Chengb5963882018-05-09 00:06:27 -0700290 if not parsed_args.build_id or not parsed_args.build_target:
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700291 raise errors.CommandArgError(
292 "Must specify --build_id and --build_target")
Kevin Chengb5963882018-05-09 00:06:27 -0700293 if parsed_args.which == CMD_CREATE_GOLDFISH:
Kevin Cheng84d3eed2018-08-16 15:16:00 -0700294 if not parsed_args.emulator_build_id and not parsed_args.build_id:
295 raise errors.CommandArgError("Must specify either "
296 "--emulator_build_id or --build_id")
297 if not parsed_args.build_target:
298 raise errors.CommandArgError("Must specify --build_target")
Kevin Chengb5963882018-05-09 00:06:27 -0700299
300 if parsed_args.which in [
Kevin Cheng3087af52018-08-13 13:26:50 -0700301 create_args.CMD_CREATE, CMD_CREATE_CUTTLEFISH, CMD_CREATE_GOLDFISH
Kevin Chengb5963882018-05-09 00:06:27 -0700302 ]:
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700303 if (parsed_args.serial_log_file
304 and not parsed_args.serial_log_file.endswith(".tar.gz")):
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700305 raise errors.CommandArgError(
306 "--serial_log_file must ends with .tar.gz")
Kevin Cheng3031f8a2018-05-16 13:21:51 -0700307 if (parsed_args.logcat_file
308 and not parsed_args.logcat_file.endswith(".tar.gz")):
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700309 raise errors.CommandArgError(
310 "--logcat_file must ends with .tar.gz")
311
312
Sam Chiu29d858f2018-08-14 20:06:25 +0800313def _SetupLogging(log_file, verbose):
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700314 """Setup logging.
315
Sam Chiu29d858f2018-08-14 20:06:25 +0800316 This function define the logging policy in below manners.
317 - without -v , -vv ,--log_file:
318 Only display critical log and print() message on screen.
319
320 - with -v:
321 Display INFO log and set StreamHandler to acloud parent logger to turn on
322 ONLY acloud modules logging.(silence all 3p libraries)
323
324 - with -vv:
325 Display INFO/DEBUG log and set StreamHandler to root logger to turn on all
326 acloud modules and 3p libraries logging.
327
328 - with --log_file.
329 Dump logs to FileHandler with DEBUG level.
330
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700331 Args:
Sam Chiu29d858f2018-08-14 20:06:25 +0800332 log_file: String, if not None, dump the log to log file.
333 verbose: Int, if verbose = 1(-v), log at INFO level and turn on
334 logging on libraries to a StreamHandler.
335 If verbose = 2(-vv), log at DEBUG level and turn on logging on
336 all libraries and 3rd party libraries to a StreamHandler.
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700337 """
Sam Chiu29d858f2018-08-14 20:06:25 +0800338 # Define logging level and hierarchy by verbosity.
339 shandler_level = None
340 logger = None
341 if verbose == 0:
342 shandler_level = logging.CRITICAL
343 logger = logging.getLogger(ACLOUD_LOGGER)
344 elif verbose == 1:
345 shandler_level = logging.INFO
346 logger = logging.getLogger(ACLOUD_LOGGER)
347 elif verbose > 1:
348 shandler_level = logging.DEBUG
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700349 logger = logging.getLogger()
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700350
Sam Chiu29d858f2018-08-14 20:06:25 +0800351 # Add StreamHandler by default.
352 shandler = logging.StreamHandler()
353 shandler.setFormatter(logging.Formatter(LOGGING_FMT))
354 shandler.setLevel(shandler_level)
355 logger.addHandler(shandler)
356 # Set the default level to DEBUG, the other handlers will handle
357 # their own levels via the args supplied (-v and --log_file).
358 logger.setLevel(logging.DEBUG)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700359
Sam Chiu29d858f2018-08-14 20:06:25 +0800360 # Add FileHandler if log_file is provided.
Sam Chiufde41e92018-08-07 18:37:02 +0800361 if log_file:
Sam Chiu29d858f2018-08-14 20:06:25 +0800362 fhandler = logging.FileHandler(filename=log_file)
363 fhandler.setFormatter(logging.Formatter(LOGGING_FMT))
364 fhandler.setLevel(logging.DEBUG)
365 logger.addHandler(fhandler)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700366
367
Erwin Jansen95559242018-11-08 15:38:18 -0800368def main(argv=None):
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700369 """Main entry.
370
371 Args:
372 argv: A list of system arguments.
373
374 Returns:
375 0 if success. None-zero if fails.
376 """
Erwin Jansen95559242018-11-08 15:38:18 -0800377 if argv is None:
378 argv = sys.argv[1:]
379
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700380 args = _ParseArgs(argv)
Sam Chiu29d858f2018-08-14 20:06:25 +0800381 _SetupLogging(args.log_file, args.verbose)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700382 _VerifyArgs(args)
383
Sam Chiuc64f3432018-08-17 11:19:06 +0800384 cfg = config.GetAcloudConfig(args)
Kevin Cheng3087af52018-08-13 13:26:50 -0700385 # TODO: Move this check into the functions it is actually needed.
Fang Dengcef4b112017-03-02 11:20:17 -0800386 # Check access.
Kevin Cheng3087af52018-08-13 13:26:50 -0700387 # device_driver.CheckAccess(cfg)
Fang Dengcef4b112017-03-02 11:20:17 -0800388
Kevin Chengee6030f2018-06-26 10:55:30 -0700389 report = None
chojoyce7a361732018-11-26 16:26:13 +0800390 if args.which == create_args.CMD_CREATE:
Kevin Chengc3d0d5e2018-08-14 14:22:44 -0700391 create.Run(args)
Kevin Chengb5963882018-05-09 00:06:27 -0700392 elif args.which == CMD_CREATE_CUTTLEFISH:
393 report = create_cuttlefish_action.CreateDevices(
394 cfg=cfg,
395 build_target=args.build_target,
396 build_id=args.build_id,
Kevin Cheng81d43952019-06-07 11:37:45 -0700397 branch=args.branch,
Kevin Chengb5963882018-05-09 00:06:27 -0700398 kernel_build_id=args.kernel_build_id,
Kevin Chengcf5bbf52019-05-09 16:17:08 -0700399 kernel_branch=args.kernel_branch,
Kevin Cheng4eeb9d42019-06-05 10:17:18 -0700400 system_branch=args.system_branch,
401 system_build_id=args.system_build_id,
402 system_build_target=args.system_build_target,
Kevin Chengb5963882018-05-09 00:06:27 -0700403 num=args.num,
404 serial_log_file=args.serial_log_file,
405 logcat_file=args.logcat_file,
Kevin Cheng86d43c72018-08-30 10:59:14 -0700406 autoconnect=args.autoconnect,
407 report_internal_ip=args.report_internal_ip)
Kevin Chengb5963882018-05-09 00:06:27 -0700408 elif args.which == CMD_CREATE_GOLDFISH:
409 report = create_goldfish_action.CreateDevices(
410 cfg=cfg,
411 build_target=args.build_target,
412 build_id=args.build_id,
413 emulator_build_id=args.emulator_build_id,
414 gpu=args.gpu,
415 num=args.num,
416 serial_log_file=args.serial_log_file,
417 logcat_file=args.logcat_file,
Kevin Cheng84d3eed2018-08-16 15:16:00 -0700418 autoconnect=args.autoconnect,
Kevin Cheng86d43c72018-08-30 10:59:14 -0700419 branch=args.branch,
Erwin Jansenf39798d2019-05-14 21:06:44 -0700420 tags=args.tags,
Kevin Cheng86d43c72018-08-30 10:59:14 -0700421 report_internal_ip=args.report_internal_ip)
Sam Chiu56c58892018-10-25 09:53:19 +0800422 elif args.which == delete_args.CMD_DELETE:
Kevin Chengeb85e862018-10-09 15:35:13 -0700423 report = delete.Run(args)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700424 elif args.which == CMD_CLEANUP:
425 report = device_driver.Cleanup(cfg, args.expiration_mins)
Sam Chiu56c58892018-10-25 09:53:19 +0800426 elif args.which == list_args.CMD_LIST:
427 list_instances.Run(args)
cylan4569dca2018-11-02 12:12:53 +0800428 elif args.which == reconnect_args.CMD_RECONNECT:
429 reconnect.Run(args)
Kevin Chengee6030f2018-06-26 10:55:30 -0700430 elif args.which == setup_args.CMD_SETUP:
herbertxue34776bb2018-07-03 21:57:48 +0800431 setup.Run(args)
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700432 else:
433 sys.stderr.write("Invalid command %s" % args.which)
434 return 2
435
herbertxue07293a32018-11-05 20:40:11 +0800436 if report and args.report_file:
Kevin Chengee6030f2018-06-26 10:55:30 -0700437 report.Dump(args.report_file)
438 if report.errors:
439 msg = "\n".join(report.errors)
440 sys.stderr.write("Encountered the following errors:\n%s\n" % msg)
441 return 1
Keun Soo Yimb293fdb2016-09-21 16:03:44 -0700442 return 0
Tri Vo8e292532016-10-01 16:55:51 -0700443
444
445if __name__ == "__main__":
Sam Chiue791f602019-05-03 15:18:10 +0800446 EXIT_CODE = None
447 EXCEPTION_STACKTRACE = None
448 EXCEPTION_LOG = None
449 metrics.LogUsage(sys.argv[1:])
450 try:
451 EXIT_CODE = main(sys.argv[1:])
452 except Exception as e:
453 EXIT_CODE = constants.EXIT_BY_ERROR
454 EXCEPTION_STACKTRACE = traceback.format_exc()
455 EXCEPTION_LOG = str(e)
456 raise
457 finally:
458 # Log Exit event here to calculate the consuming time.
459 metrics.LogExitEvent(EXIT_CODE,
460 stacktrace=EXCEPTION_STACKTRACE,
461 logs=EXCEPTION_LOG)