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.
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
21import os
22import subprocess
23import sys
24
25from acloud.internal import constants
26from acloud.internal.lib import utils
27from acloud.setup import host_setup_runner
28from acloud.setup import gcp_setup_runner
29
30
31def Run(args):
32    """Run setup.
33
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
39    Args:
40        args: Namespace object from argparse.parse_args.
41    """
42    _RunPreSetup()
43
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.
49    host_base_runner = host_setup_runner.HostBasePkgInstaller()
50    host_avd_runner = host_setup_runner.AvdPkgInstaller()
51    host_cf_common_runner = host_setup_runner.CuttlefishCommonPkgInstaller()
52    host_env_runner = host_setup_runner.CuttlefishHostSetup()
53    gcp_runner = gcp_setup_runner.GcpTaskRunner(args.config_file)
54    task_queue = []
55    # User must explicitly specify --host to install the avd host packages.
56    if args.host:
57        task_queue.append(host_base_runner)
58        task_queue.append(host_avd_runner)
59        task_queue.append(host_cf_common_runner)
60        task_queue.append(host_env_runner)
61    # We should do these setup tasks if specified or if no args were used.
62    if args.host_base or (not args.host and not args.gcp_init):
63        task_queue.append(host_base_runner)
64    if args.gcp_init or (not args.host and not args.host_base):
65        task_queue.append(gcp_runner)
66
67    for subtask in task_queue:
68        subtask.Run(force_setup=args.force)
69
70    # 3.Print the usage hints.
71    _PrintUsage()
72
73
74def _PrintWelcomeMessage():
75    """Print welcome message when acloud setup been called."""
76
77    # pylint: disable=anomalous-backslash-in-string
78    asc_art = "                                    \n" \
79            "   ___  _______   ____  __  _____ \n" \
80            "  / _ |/ ___/ /  / __ \/ / / / _ \\ \n" \
81            " / __ / /__/ /__/ /_/ / /_/ / // /  \n" \
82            "/_/ |_\\___/____/\\____/\\____/____/ \n" \
83            "                                  \n"
84
85    print("\nWelcome to")
86    print(asc_art)
87
88
89def _PrintUsage():
90    """Print cmd usage hints when acloud setup been finished."""
91    utils.PrintColorString("")
92    utils.PrintColorString("Setup process finished")
93
94
95def _RunPreSetup():
96    """This will run any pre-setup scripts.
97
98    If we can find any pre-setup scripts, run it and don't care about the
99    results. Pre-setup scripts will do any special setup before actual
100    setup occurs (e.g. copying configs).
101    """
102    if constants.ENV_ANDROID_BUILD_TOP not in os.environ:
103        print("Can't find $%s." % constants.ENV_ANDROID_BUILD_TOP)
104        print("Please run '#source build/envsetup.sh && lunch <target>' first.")
105        sys.exit(constants.EXIT_BY_USER)
106
107    pre_setup_sh = os.path.join(os.environ.get(constants.ENV_ANDROID_BUILD_TOP),
108                                "tools",
109                                "acloud",
110                                "setup",
111                                "pre_setup_sh",
112                                "acloud_pre_setup.sh")
113
114    if os.path.exists(pre_setup_sh):
115        subprocess.call([pre_setup_sh])
116