| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2018 - 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. |
| 16 | r"""Setup entry point. |
| 17 | |
| 18 | Setup will handle all of the necessary steps to enable acloud to create a local |
| 19 | or remote instance of an Android Virtual Device. |
| 20 | """ |
| 21 | |
| 22 | from __future__ import print_function |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 23 | import os |
| 24 | import subprocess |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame^] | 25 | import sys |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 26 | |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame^] | 27 | from acloud.internal import constants |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 28 | from acloud.internal.lib import utils |
| 29 | from acloud.setup import host_setup_runner |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 30 | from acloud.setup import gcp_setup_runner |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 31 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 32 | |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 33 | def Run(args): |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 34 | """Run setup. |
| 35 | |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 36 | Setup options: |
| 37 | -host: Setup host settings. |
| 38 | -gcp_init: Setup gcp settings. |
| 39 | -None, default behavior will setup host and gcp settings. |
| 40 | |
| Kevin Cheng | ee6030f | 2018-06-26 10:55:30 -0700 | [diff] [blame] | 41 | Args: |
| 42 | args: Namespace object from argparse.parse_args. |
| 43 | """ |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 44 | _RunPreSetup() |
| 45 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 46 | # Setup process will be in the following manner: |
| 47 | # 1.Print welcome message. |
| 48 | _PrintWelcomeMessage() |
| 49 | |
| 50 | # 2.Init all subtasks in queue and traverse them. |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 51 | host_runner = host_setup_runner.AvdPkgInstaller() |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 52 | host_env_runner = host_setup_runner.CuttlefishHostSetup() |
| 53 | gcp_runner = gcp_setup_runner.GcpTaskRunner(args.config_file) |
| 54 | task_queue = [] |
| 55 | if args.host or not args.gcp_init: |
| 56 | task_queue.append(host_runner) |
| 57 | task_queue.append(host_env_runner) |
| 58 | if args.gcp_init or not args.host: |
| 59 | task_queue.append(gcp_runner) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 60 | |
| 61 | for subtask in task_queue: |
| Kevin Cheng | 58b7e79 | 2018-10-05 02:26:53 -0700 | [diff] [blame] | 62 | subtask.Run(force_setup=args.force) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 63 | |
| 64 | # 3.Print the usage hints. |
| 65 | _PrintUsage() |
| 66 | |
| 67 | |
| 68 | def _PrintWelcomeMessage(): |
| 69 | """Print welcome message when acloud setup been called.""" |
| 70 | |
| 71 | # pylint: disable=anomalous-backslash-in-string |
| 72 | asc_art = " \n" \ |
| 73 | " ___ _______ ____ __ _____ \n" \ |
| 74 | " / _ |/ ___/ / / __ \/ / / / _ \\ \n" \ |
| 75 | " / __ / /__/ /__/ /_/ / /_/ / // / \n" \ |
| 76 | "/_/ |_\\___/____/\\____/\\____/____/ \n" \ |
| 77 | " \n" |
| 78 | |
| 79 | print("\nWelcome to") |
| 80 | print(asc_art) |
| 81 | |
| 82 | |
| 83 | def _PrintUsage(): |
| 84 | """Print cmd usage hints when acloud setup been finished.""" |
| herbertxue | 34776bb | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 85 | utils.PrintColorString("") |
| 86 | utils.PrintColorString("Setup process finished") |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 87 | |
| 88 | |
| 89 | def _RunPreSetup(): |
| 90 | """This will run any pre-setup scripts. |
| 91 | |
| 92 | If we can find any pre-setup scripts, run it and don't care about the |
| 93 | results. Pre-setup scripts will do any special setup before actual |
| 94 | setup occurs (e.g. copying configs). |
| 95 | """ |
| Sam Chiu | d22127a | 2018-11-21 09:23:34 +0800 | [diff] [blame^] | 96 | if constants.ENV_ANDROID_BUILD_TOP not in os.environ: |
| 97 | print("Can't find $%s." % constants.ENV_ANDROID_BUILD_TOP) |
| 98 | print("Please run '#source build/envsetup.sh && lunch <target>' first.") |
| 99 | sys.exit(1) |
| 100 | |
| 101 | pre_setup_sh = os.path.join(os.environ.get(constants.ENV_ANDROID_BUILD_TOP), |
| 102 | "tools", |
| 103 | "acloud", |
| 104 | "setup", |
| 105 | "pre_setup_sh", |
| 106 | "acloud_pre_setup.sh") |
| 107 | |
| Kevin Cheng | 99cf3d3 | 2018-10-05 02:09:21 -0700 | [diff] [blame] | 108 | if os.path.exists(pre_setup_sh): |
| 109 | subprocess.call([pre_setup_sh]) |