| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 1 | # 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. |
| 14 | r"""Setup entry point. |
| 15 | |
| 16 | Setup will handle all of the necessary steps to enable acloud to create a local |
| 17 | or remote instance of an Android Virtual Device. |
| 18 | """ |
| 19 | |
| 20 | from __future__ import print_function |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 21 | import os |
| 22 | import subprocess |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame] | 23 | import sys |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 24 | |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame] | 25 | from acloud.internal import constants |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 26 | from acloud.internal.lib import utils |
| herbertxue | c0f98f3 | 2020-05-20 14:00:56 +0800 | [diff] [blame] | 27 | from acloud.public import config |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 28 | from acloud.setup import host_setup_runner |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 29 | from acloud.setup import gcp_setup_runner |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 30 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 31 | |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 32 | def Run(args): |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 33 | """Run setup. |
| 34 | |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 35 | 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 Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 40 | Args: |
| 41 | args: Namespace object from argparse.parse_args. |
| 42 | """ |
| herbertxue | c0f98f3 | 2020-05-20 14:00:56 +0800 | [diff] [blame] | 43 | if args.update_config: |
| 44 | _UpdateConfig(args.config_file, args.update_config[0], args.update_config[1]) |
| 45 | return |
| 46 | |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 47 | _RunPreSetup() |
| 48 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 49 | # 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 Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 54 | host_base_runner = host_setup_runner.HostBasePkgInstaller() |
| 55 | host_avd_runner = host_setup_runner.AvdPkgInstaller() |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 56 | host_cf_common_runner = host_setup_runner.CuttlefishCommonPkgInstaller() |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 57 | host_mkcert_runner = host_setup_runner.MkcertPkgInstaller() |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 58 | host_env_runner = host_setup_runner.CuttlefishHostSetup() |
| 59 | gcp_runner = gcp_setup_runner.GcpTaskRunner(args.config_file) |
| 60 | task_queue = [] |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 61 | # User must explicitly specify --host to install the avd host packages. |
| Kevin Cheng | 92532b4 | 2019-02-12 10:03:13 -0800 | [diff] [blame] | 62 | if args.host: |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 63 | task_queue.append(host_base_runner) |
| 64 | task_queue.append(host_avd_runner) |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 65 | task_queue.append(host_cf_common_runner) |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 66 | task_queue.append(host_env_runner) |
| chojoyce | 574f8f0 | 2021-10-27 12:21:03 +0800 | [diff] [blame^] | 67 | if args.autoconnect == constants.INS_KEY_WEBRTC: |
| 68 | task_queue.append(host_mkcert_runner) |
| 69 | |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 70 | # We should do these setup tasks if specified or if no args were used. |
| 71 | if args.host_base or (not args.host and not args.gcp_init): |
| 72 | task_queue.append(host_base_runner) |
| 73 | if args.gcp_init or (not args.host and not args.host_base): |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 74 | task_queue.append(gcp_runner) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 75 | |
| 76 | for subtask in task_queue: |
| Kevin Cheng | 58b7e79 | 2018-10-05 02:26:53 -0700 | [diff] [blame] | 77 | subtask.Run(force_setup=args.force) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 78 | |
| 79 | # 3.Print the usage hints. |
| 80 | _PrintUsage() |
| 81 | |
| 82 | |
| 83 | def _PrintWelcomeMessage(): |
| 84 | """Print welcome message when acloud setup been called.""" |
| 85 | |
| 86 | # pylint: disable=anomalous-backslash-in-string |
| 87 | asc_art = " \n" \ |
| 88 | " ___ _______ ____ __ _____ \n" \ |
| 89 | " / _ |/ ___/ / / __ \/ / / / _ \\ \n" \ |
| 90 | " / __ / /__/ /__/ /_/ / /_/ / // / \n" \ |
| 91 | "/_/ |_\\___/____/\\____/\\____/____/ \n" \ |
| 92 | " \n" |
| 93 | |
| 94 | print("\nWelcome to") |
| 95 | print(asc_art) |
| 96 | |
| 97 | |
| 98 | def _PrintUsage(): |
| 99 | """Print cmd usage hints when acloud setup been finished.""" |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 100 | utils.PrintColorString("") |
| 101 | utils.PrintColorString("Setup process finished") |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def _RunPreSetup(): |
| 105 | """This will run any pre-setup scripts. |
| 106 | |
| 107 | If we can find any pre-setup scripts, run it and don't care about the |
| 108 | results. Pre-setup scripts will do any special setup before actual |
| 109 | setup occurs (e.g. copying configs). |
| 110 | """ |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame] | 111 | if constants.ENV_ANDROID_BUILD_TOP not in os.environ: |
| 112 | print("Can't find $%s." % constants.ENV_ANDROID_BUILD_TOP) |
| 113 | print("Please run '#source build/envsetup.sh && lunch <target>' first.") |
| Sam Chiu | e791f60 | 2019-05-03 15:18:10 +0800 | [diff] [blame] | 114 | sys.exit(constants.EXIT_BY_USER) |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame] | 115 | |
| 116 | pre_setup_sh = os.path.join(os.environ.get(constants.ENV_ANDROID_BUILD_TOP), |
| 117 | "tools", |
| 118 | "acloud", |
| 119 | "setup", |
| 120 | "pre_setup_sh", |
| 121 | "acloud_pre_setup.sh") |
| 122 | |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 123 | if os.path.exists(pre_setup_sh): |
| 124 | subprocess.call([pre_setup_sh]) |
| herbertxue | c0f98f3 | 2020-05-20 14:00:56 +0800 | [diff] [blame] | 125 | |
| 126 | def _UpdateConfig(config_file, field, value): |
| 127 | """Update the user config. |
| 128 | |
| 129 | Args: |
| 130 | config_file: String of config file path. |
| 131 | field: String, field name in user config. |
| 132 | value: String, the value of field. |
| 133 | """ |
| 134 | config_mgr = config.AcloudConfigManager(config_file) |
| 135 | config_mgr.Load() |
| 136 | user_config = config_mgr.user_config_path |
| 137 | print("Your config (%s) is updated." % user_config) |
| 138 | gcp_setup_runner.UpdateConfigFile(user_config, field, value) |
| 139 | _PrintUsage() |