blob: c424318b97c03083e60d2b51c27384c8c788fcfc [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
herbertxuec0f98f32020-05-20 14:00:56 +080027from acloud.public import config
Sam Chiu81bdc652018-06-29 18:45:08 +080028from acloud.setup import host_setup_runner
herbertxue34776bb2018-07-03 21:57:48 +080029from acloud.setup import gcp_setup_runner
Kevin Chengee6030f2018-06-26 10:55:30 -070030
Sam Chiu81bdc652018-06-29 18:45:08 +080031
herbertxue34776bb2018-07-03 21:57:48 +080032def Run(args):
Kevin Chengee6030f2018-06-26 10:55:30 -070033 """Run setup.
34
herbertxue34776bb2018-07-03 21:57:48 +080035 Setup options:
36 -host: Setup host settings.
37 -gcp_init: Setup gcp settings.
38 -None, default behavior will setup host and gcp settings.
39
Kevin Chengee6030f2018-06-26 10:55:30 -070040 Args:
41 args: Namespace object from argparse.parse_args.
42 """
herbertxuec0f98f32020-05-20 14:00:56 +080043 if args.update_config:
44 _UpdateConfig(args.config_file, args.update_config[0], args.update_config[1])
45 return
46
Kevin Cheng99cf3d32018-10-05 02:09:21 -070047 _RunPreSetup()
48
Sam Chiu81bdc652018-06-29 18:45:08 +080049 # Setup process will be in the following manner:
50 # 1.Print welcome message.
51 _PrintWelcomeMessage()
52
53 # 2.Init all subtasks in queue and traverse them.
Kevin Chengeb997272019-06-05 14:53:18 -070054 host_base_runner = host_setup_runner.HostBasePkgInstaller()
55 host_avd_runner = host_setup_runner.AvdPkgInstaller()
herbertxue975b8872020-02-12 14:41:41 +080056 host_cf_common_runner = host_setup_runner.CuttlefishCommonPkgInstaller()
herbertxue34776bb2018-07-03 21:57:48 +080057 host_env_runner = host_setup_runner.CuttlefishHostSetup()
58 gcp_runner = gcp_setup_runner.GcpTaskRunner(args.config_file)
59 task_queue = []
Kevin Chengeb997272019-06-05 14:53:18 -070060 # User must explicitly specify --host to install the avd host packages.
Kevin Cheng92532b42019-02-12 10:03:13 -080061 if args.host:
Kevin Chengeb997272019-06-05 14:53:18 -070062 task_queue.append(host_base_runner)
63 task_queue.append(host_avd_runner)
herbertxue975b8872020-02-12 14:41:41 +080064 task_queue.append(host_cf_common_runner)
herbertxue34776bb2018-07-03 21:57:48 +080065 task_queue.append(host_env_runner)
Kevin Chengeb997272019-06-05 14:53:18 -070066 # We should do these setup tasks if specified or if no args were used.
67 if args.host_base or (not args.host and not args.gcp_init):
68 task_queue.append(host_base_runner)
69 if args.gcp_init or (not args.host and not args.host_base):
herbertxue34776bb2018-07-03 21:57:48 +080070 task_queue.append(gcp_runner)
Sam Chiu81bdc652018-06-29 18:45:08 +080071
72 for subtask in task_queue:
Kevin Cheng58b7e792018-10-05 02:26:53 -070073 subtask.Run(force_setup=args.force)
Sam Chiu81bdc652018-06-29 18:45:08 +080074
75 # 3.Print the usage hints.
76 _PrintUsage()
77
78
79def _PrintWelcomeMessage():
80 """Print welcome message when acloud setup been called."""
81
82 # pylint: disable=anomalous-backslash-in-string
83 asc_art = " \n" \
84 " ___ _______ ____ __ _____ \n" \
85 " / _ |/ ___/ / / __ \/ / / / _ \\ \n" \
86 " / __ / /__/ /__/ /_/ / /_/ / // / \n" \
87 "/_/ |_\\___/____/\\____/\\____/____/ \n" \
88 " \n"
89
90 print("\nWelcome to")
91 print(asc_art)
92
93
94def _PrintUsage():
95 """Print cmd usage hints when acloud setup been finished."""
herbertxue34776bb2018-07-03 21:57:48 +080096 utils.PrintColorString("")
97 utils.PrintColorString("Setup process finished")
Kevin Cheng99cf3d32018-10-05 02:09:21 -070098
99
100def _RunPreSetup():
101 """This will run any pre-setup scripts.
102
103 If we can find any pre-setup scripts, run it and don't care about the
104 results. Pre-setup scripts will do any special setup before actual
105 setup occurs (e.g. copying configs).
106 """
Sam Chiud22127a2018-11-21 09:23:34 +0800107 if constants.ENV_ANDROID_BUILD_TOP not in os.environ:
108 print("Can't find $%s." % constants.ENV_ANDROID_BUILD_TOP)
109 print("Please run '#source build/envsetup.sh && lunch <target>' first.")
Sam Chiue791f602019-05-03 15:18:10 +0800110 sys.exit(constants.EXIT_BY_USER)
Sam Chiud22127a2018-11-21 09:23:34 +0800111
112 pre_setup_sh = os.path.join(os.environ.get(constants.ENV_ANDROID_BUILD_TOP),
113 "tools",
114 "acloud",
115 "setup",
116 "pre_setup_sh",
117 "acloud_pre_setup.sh")
118
Kevin Cheng99cf3d32018-10-05 02:09:21 -0700119 if os.path.exists(pre_setup_sh):
120 subprocess.call([pre_setup_sh])
herbertxuec0f98f32020-05-20 14:00:56 +0800121
122def _UpdateConfig(config_file, field, value):
123 """Update the user config.
124
125 Args:
126 config_file: String of config file path.
127 field: String, field name in user config.
128 value: String, the value of field.
129 """
130 config_mgr = config.AcloudConfigManager(config_file)
131 config_mgr.Load()
132 user_config = config_mgr.user_config_path
133 print("Your config (%s) is updated." % user_config)
134 gcp_setup_runner.UpdateConfigFile(user_config, field, value)
135 _PrintUsage()