blob: 36b6ffd9382fb52a1f29afe233fd0372e822fe2b [file] [log] [blame]
Kevin Chengee6030f2018-06-26 10:55:30 -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"""Setup entry point.
15
16Setup will handle all of the necessary steps to enable acloud to create a local
17or remote instance of an Android Virtual Device.
18"""
19
20from __future__ import print_function
Kevin Cheng99cf3d32018-10-05 02:09:21 -070021import os
22import subprocess
Sam Chiud22127a2018-11-21 09:23:34 +080023import sys
Kevin Chengee6030f2018-06-26 10:55:30 -070024
Sam Chiud22127a2018-11-21 09:23:34 +080025from acloud.internal import constants
Sam Chiu81bdc652018-06-29 18:45:08 +080026from acloud.internal.lib import utils
27from acloud.setup import host_setup_runner
herbertxue34776bb2018-07-03 21:57:48 +080028from acloud.setup import gcp_setup_runner
Kevin Chengee6030f2018-06-26 10:55:30 -070029
Sam Chiu81bdc652018-06-29 18:45:08 +080030
herbertxue34776bb2018-07-03 21:57:48 +080031def Run(args):
Kevin Chengee6030f2018-06-26 10:55:30 -070032 """Run setup.
33
herbertxue34776bb2018-07-03 21:57:48 +080034 Setup options:
35 -host: Setup host settings.
36 -gcp_init: Setup gcp settings.
37 -None, default behavior will setup host and gcp settings.
38
Kevin Chengee6030f2018-06-26 10:55:30 -070039 Args:
40 args: Namespace object from argparse.parse_args.
41 """
Kevin Cheng99cf3d32018-10-05 02:09:21 -070042 _RunPreSetup()
43
Sam Chiu81bdc652018-06-29 18:45:08 +080044 # Setup process will be in the following manner:
45 # 1.Print welcome message.
46 _PrintWelcomeMessage()
47
48 # 2.Init all subtasks in queue and traverse them.
Kevin Chengeb997272019-06-05 14:53:18 -070049 host_base_runner = host_setup_runner.HostBasePkgInstaller()
50 host_avd_runner = host_setup_runner.AvdPkgInstaller()
herbertxue34776bb2018-07-03 21:57:48 +080051 host_env_runner = host_setup_runner.CuttlefishHostSetup()
52 gcp_runner = gcp_setup_runner.GcpTaskRunner(args.config_file)
53 task_queue = []
Kevin Chengeb997272019-06-05 14:53:18 -070054 # User must explicitly specify --host to install the avd host packages.
Kevin Cheng92532b42019-02-12 10:03:13 -080055 if args.host:
Kevin Chengeb997272019-06-05 14:53:18 -070056 task_queue.append(host_base_runner)
57 task_queue.append(host_avd_runner)
herbertxue34776bb2018-07-03 21:57:48 +080058 task_queue.append(host_env_runner)
Kevin Chengeb997272019-06-05 14:53:18 -070059 # We should do these setup tasks if specified or if no args were used.
60 if args.host_base or (not args.host and not args.gcp_init):
61 task_queue.append(host_base_runner)
62 if args.gcp_init or (not args.host and not args.host_base):
herbertxue34776bb2018-07-03 21:57:48 +080063 task_queue.append(gcp_runner)
Sam Chiu81bdc652018-06-29 18:45:08 +080064
65 for subtask in task_queue:
Kevin Cheng58b7e792018-10-05 02:26:53 -070066 subtask.Run(force_setup=args.force)
Sam Chiu81bdc652018-06-29 18:45:08 +080067
68 # 3.Print the usage hints.
69 _PrintUsage()
70
71
72def _PrintWelcomeMessage():
73 """Print welcome message when acloud setup been called."""
74
75 # pylint: disable=anomalous-backslash-in-string
76 asc_art = " \n" \
77 " ___ _______ ____ __ _____ \n" \
78 " / _ |/ ___/ / / __ \/ / / / _ \\ \n" \
79 " / __ / /__/ /__/ /_/ / /_/ / // / \n" \
80 "/_/ |_\\___/____/\\____/\\____/____/ \n" \
81 " \n"
82
83 print("\nWelcome to")
84 print(asc_art)
85
86
87def _PrintUsage():
88 """Print cmd usage hints when acloud setup been finished."""
herbertxue34776bb2018-07-03 21:57:48 +080089 utils.PrintColorString("")
90 utils.PrintColorString("Setup process finished")
Kevin Cheng99cf3d32018-10-05 02:09:21 -070091
92
93def _RunPreSetup():
94 """This will run any pre-setup scripts.
95
96 If we can find any pre-setup scripts, run it and don't care about the
97 results. Pre-setup scripts will do any special setup before actual
98 setup occurs (e.g. copying configs).
99 """
Sam Chiud22127a2018-11-21 09:23:34 +0800100 if constants.ENV_ANDROID_BUILD_TOP not in os.environ:
101 print("Can't find $%s." % constants.ENV_ANDROID_BUILD_TOP)
102 print("Please run '#source build/envsetup.sh && lunch <target>' first.")
Sam Chiue791f602019-05-03 15:18:10 +0800103 sys.exit(constants.EXIT_BY_USER)
Sam Chiud22127a2018-11-21 09:23:34 +0800104
105 pre_setup_sh = os.path.join(os.environ.get(constants.ENV_ANDROID_BUILD_TOP),
106 "tools",
107 "acloud",
108 "setup",
109 "pre_setup_sh",
110 "acloud_pre_setup.sh")
111
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700112 if os.path.exists(pre_setup_sh):
113 subprocess.call([pre_setup_sh])