| 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 |
| 27 | from acloud.setup import host_setup_runner |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 28 | from acloud.setup import gcp_setup_runner |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 29 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 30 | |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 31 | def Run(args): |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 32 | """Run setup. |
| 33 | |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 34 | 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 Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 39 | Args: |
| 40 | args: Namespace object from argparse.parse_args. |
| 41 | """ |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 42 | _RunPreSetup() |
| 43 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 44 | # 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. |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 49 | host_runner = host_setup_runner.AvdPkgInstaller() |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 50 | host_env_runner = host_setup_runner.CuttlefishHostSetup() |
| 51 | gcp_runner = gcp_setup_runner.GcpTaskRunner(args.config_file) |
| 52 | task_queue = [] |
| Kevin Cheng | 92532b4 | 2019-02-12 10:03:13 -0800 | [diff] [blame] | 53 | # User must explicitly specify --host to install the host packages. |
| 54 | if args.host: |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 55 | task_queue.append(host_runner) |
| 56 | task_queue.append(host_env_runner) |
| 57 | if args.gcp_init or not args.host: |
| 58 | task_queue.append(gcp_runner) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 59 | |
| 60 | for subtask in task_queue: |
| Kevin Cheng | 58b7e79 | 2018-10-05 02:26:53 -0700 | [diff] [blame] | 61 | subtask.Run(force_setup=args.force) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 62 | |
| 63 | # 3.Print the usage hints. |
| 64 | _PrintUsage() |
| 65 | |
| 66 | |
| 67 | def _PrintWelcomeMessage(): |
| 68 | """Print welcome message when acloud setup been called.""" |
| 69 | |
| 70 | # pylint: disable=anomalous-backslash-in-string |
| 71 | asc_art = " \n" \ |
| 72 | " ___ _______ ____ __ _____ \n" \ |
| 73 | " / _ |/ ___/ / / __ \/ / / / _ \\ \n" \ |
| 74 | " / __ / /__/ /__/ /_/ / /_/ / // / \n" \ |
| 75 | "/_/ |_\\___/____/\\____/\\____/____/ \n" \ |
| 76 | " \n" |
| 77 | |
| 78 | print("\nWelcome to") |
| 79 | print(asc_art) |
| 80 | |
| 81 | |
| 82 | def _PrintUsage(): |
| 83 | """Print cmd usage hints when acloud setup been finished.""" |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 84 | utils.PrintColorString("") |
| 85 | utils.PrintColorString("Setup process finished") |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def _RunPreSetup(): |
| 89 | """This will run any pre-setup scripts. |
| 90 | |
| 91 | If we can find any pre-setup scripts, run it and don't care about the |
| 92 | results. Pre-setup scripts will do any special setup before actual |
| 93 | setup occurs (e.g. copying configs). |
| 94 | """ |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame] | 95 | if constants.ENV_ANDROID_BUILD_TOP not in os.environ: |
| 96 | print("Can't find $%s." % constants.ENV_ANDROID_BUILD_TOP) |
| 97 | print("Please run '#source build/envsetup.sh && lunch <target>' first.") |
| Sam Chiu | e791f60 | 2019-05-03 15:18:10 +0800 | [diff] [blame^] | 98 | sys.exit(constants.EXIT_BY_USER) |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame] | 99 | |
| 100 | pre_setup_sh = os.path.join(os.environ.get(constants.ENV_ANDROID_BUILD_TOP), |
| 101 | "tools", |
| 102 | "acloud", |
| 103 | "setup", |
| 104 | "pre_setup_sh", |
| 105 | "acloud_pre_setup.sh") |
| 106 | |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 107 | if os.path.exists(pre_setup_sh): |
| 108 | subprocess.call([pre_setup_sh]) |