• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.public import config
28from acloud.setup import host_setup_runner
29from acloud.setup import gcp_setup_runner
30
31
32def Run(args):
33    """Run setup.
34
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
40    Args:
41        args: Namespace object from argparse.parse_args.
42    """
43    if args.update_config:
44        _UpdateConfig(args.config_file, args.update_config[0], args.update_config[1])
45        return
46
47    _RunPreSetup()
48
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.
54    host_base_runner = host_setup_runner.HostBasePkgInstaller()
55    host_avd_runner = host_setup_runner.AvdPkgInstaller()
56    host_cf_common_runner = host_setup_runner.CuttlefishCommonPkgInstaller()
57    host_localca_runner = host_setup_runner.LocalCAHostSetup()
58    host_env_runner = host_setup_runner.CuttlefishHostSetup()
59    gcp_runner = gcp_setup_runner.GcpTaskRunner(args.config_file)
60    task_queue = []
61    # User must explicitly specify --host to install the avd host packages.
62    if args.host and utils.IsSupportedKvm():
63        task_queue.append(host_base_runner)
64        task_queue.append(host_avd_runner)
65        task_queue.append(host_cf_common_runner)
66        task_queue.append(host_env_runner)
67
68    task_queue.append(host_localca_runner)
69
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):
74        task_queue.append(gcp_runner)
75
76    for subtask in task_queue:
77        subtask.Run(force_setup=args.force)
78
79    # 3.Print the usage hints.
80    _PrintUsage()
81
82
83def _PrintWelcomeMessage():
84    """Print welcome message when acloud setup been called."""
85
86    asc_art = r"                                   " + "\n" \
87              r"   ___  _______   ____  __  _____  " + "\n" \
88              r"  / _ |/ ___/ /  / __ \/ / / / _ \ " + "\n" \
89              r" / __ / /__/ /__/ /_/ / /_/ / // / " + "\n" \
90              r"/_/ |_\___/____/\____/\____/____/  " + "\n" \
91              r"                                   " + "\n"
92
93    print("\nWelcome to")
94    print(asc_art)
95
96
97def _PrintUsage():
98    """Print cmd usage hints when acloud setup been finished."""
99    utils.PrintColorString("")
100    utils.PrintColorString("Setup process finished")
101
102
103def _RunPreSetup():
104    """This will run any pre-setup scripts.
105
106    If we can find any pre-setup scripts, run it and don't care about the
107    results. Pre-setup scripts will do any special setup before actual
108    setup occurs (e.g. copying configs).
109    """
110    if constants.ENV_ANDROID_BUILD_TOP not in os.environ:
111        print(f"Can't find ${constants.ENV_ANDROID_BUILD_TOP}.")
112        print("Please run '#source build/envsetup.sh && lunch <target>' first.")
113        sys.exit(constants.EXIT_BY_USER)
114
115    pre_setup_sh = os.path.join(os.environ.get(constants.ENV_ANDROID_BUILD_TOP),
116                                "tools",
117                                "acloud",
118                                "setup",
119                                "pre_setup_sh",
120                                "acloud_pre_setup.sh")
121
122    if os.path.exists(pre_setup_sh):
123        subprocess.call([pre_setup_sh])
124
125def _UpdateConfig(config_file, field, value):
126    """Update the user config.
127
128    Args:
129        config_file: String of config file path.
130        field: String, field name in user config.
131        value: String, the value of field.
132    """
133    config_mgr = config.AcloudConfigManager(config_file)
134    config_mgr.Load()
135    user_config = config_mgr.user_config_path
136    print(f"Your config ({user_config}) is updated.")
137    gcp_setup_runner.UpdateConfigFile(user_config, field, value)
138    _PrintUsage()
139