blob: c749b14195a185f2a4126b80bedebe3a7e953dfb [file] [log] [blame]
Kevin Chengee6030f2018-06-26 10:55:30 -07001#!/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.
16r"""Setup entry point.
17
18Setup will handle all of the necessary steps to enable acloud to create a local
19or remote instance of an Android Virtual Device.
20"""
21
22from __future__ import print_function
Kevin Cheng99cf3d32018-10-05 02:09:21 -070023import os
24import subprocess
Sam Chiud22127a2018-11-21 09:23:34 +080025import sys
Kevin Chengee6030f2018-06-26 10:55:30 -070026
Sam Chiud22127a2018-11-21 09:23:34 +080027from acloud.internal import constants
Sam Chiu81bdc652018-06-29 18:45:08 +080028from acloud.internal.lib import utils
29from acloud.setup import host_setup_runner
herbertxue34776bb2018-07-03 21:57:48 +080030from acloud.setup import gcp_setup_runner
Kevin Chengee6030f2018-06-26 10:55:30 -070031
Sam Chiu81bdc652018-06-29 18:45:08 +080032
herbertxue34776bb2018-07-03 21:57:48 +080033def Run(args):
Kevin Chengee6030f2018-06-26 10:55:30 -070034 """Run setup.
35
herbertxue34776bb2018-07-03 21:57:48 +080036 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 Chengee6030f2018-06-26 10:55:30 -070041 Args:
42 args: Namespace object from argparse.parse_args.
43 """
Kevin Cheng99cf3d32018-10-05 02:09:21 -070044 _RunPreSetup()
45
Sam Chiu81bdc652018-06-29 18:45:08 +080046 # 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.
chojoyce1b818bb2018-10-03 16:34:57 +080051 host_runner = host_setup_runner.AvdPkgInstaller()
herbertxue34776bb2018-07-03 21:57:48 +080052 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 Chiu81bdc652018-06-29 18:45:08 +080060
61 for subtask in task_queue:
Kevin Cheng58b7e792018-10-05 02:26:53 -070062 subtask.Run(force_setup=args.force)
Sam Chiu81bdc652018-06-29 18:45:08 +080063
64 # 3.Print the usage hints.
65 _PrintUsage()
66
67
68def _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
83def _PrintUsage():
84 """Print cmd usage hints when acloud setup been finished."""
herbertxue34776bb2018-07-03 21:57:48 +080085 utils.PrintColorString("")
86 utils.PrintColorString("Setup process finished")
Kevin Cheng99cf3d32018-10-05 02:09:21 -070087
88
89def _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 Chiud22127a2018-11-21 09:23:34 +080096 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 Cheng99cf3d32018-10-05 02:09:21 -0700108 if os.path.exists(pre_setup_sh):
109 subprocess.call([pre_setup_sh])