| Kevin Cheng | 3087af5 | 2018-08-13 13:26:50 -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"""Create entry point. |
| 17 | |
| 18 | Create will handle all the logic related to creating a local/remote instance |
| 19 | an Android Virtual Device and the logic related to prepping the local/remote |
| 20 | image artifacts. |
| 21 | """ |
| 22 | |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 23 | from __future__ import print_function |
| 24 | |
| Kevin Cheng | 835a415 | 2018-10-11 10:46:57 -0700 | [diff] [blame] | 25 | import os |
| 26 | import subprocess |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 27 | import sys |
| 28 | |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 29 | from acloud import errors |
| Kevin Cheng | c3d0d5e | 2018-08-14 14:22:44 -0700 | [diff] [blame] | 30 | from acloud.create import avd_spec |
| Richard Fung | 97503b2 | 2019-02-06 13:43:38 -0800 | [diff] [blame] | 31 | from acloud.create import cheeps_remote_image_remote_instance |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 32 | from acloud.create import gce_local_image_remote_instance |
| 33 | from acloud.create import gce_remote_image_remote_instance |
| Hsin-Yi Chen | 7ba4996 | 2019-09-25 18:58:56 +0800 | [diff] [blame] | 34 | from acloud.create import goldfish_local_image_local_instance |
| herbertxue | 494891e | 2019-01-19 13:50:53 +0800 | [diff] [blame] | 35 | from acloud.create import goldfish_remote_image_remote_instance |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 36 | from acloud.create import local_image_local_instance |
| 37 | from acloud.create import local_image_remote_instance |
| cylan | d43f293 | 2019-11-26 17:37:28 +0800 | [diff] [blame] | 38 | from acloud.create import local_image_remote_host |
| herbertxue | df01c42 | 2018-09-06 19:52:52 +0800 | [diff] [blame] | 39 | from acloud.create import remote_image_remote_instance |
| chojoyce | cd004bc | 2018-09-13 10:39:00 +0800 | [diff] [blame] | 40 | from acloud.create import remote_image_local_instance |
| cylan | d43f293 | 2019-11-26 17:37:28 +0800 | [diff] [blame] | 41 | from acloud.create import remote_image_remote_host |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 42 | from acloud.internal import constants |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 43 | from acloud.internal.lib import utils |
| 44 | from acloud.setup import setup |
| 45 | from acloud.setup import gcp_setup_runner |
| 46 | from acloud.setup import host_setup_runner |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 47 | |
| herbertxue | 1512f8a | 2019-06-27 13:56:23 +0800 | [diff] [blame] | 48 | |
| Kevin Cheng | 835a415 | 2018-10-11 10:46:57 -0700 | [diff] [blame] | 49 | _MAKE_CMD = "build/soong/soong_ui.bash" |
| 50 | _MAKE_ARG = "--make-mode" |
| 51 | |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 52 | _CREATOR_CLASS_DICT = { |
| 53 | # GCE types |
| 54 | (constants.TYPE_GCE, constants.IMAGE_SRC_LOCAL, constants.INSTANCE_TYPE_REMOTE): |
| 55 | gce_local_image_remote_instance.GceLocalImageRemoteInstance, |
| 56 | (constants.TYPE_GCE, constants.IMAGE_SRC_REMOTE, constants.INSTANCE_TYPE_REMOTE): |
| 57 | gce_remote_image_remote_instance.GceRemoteImageRemoteInstance, |
| 58 | # CF types |
| 59 | (constants.TYPE_CF, constants.IMAGE_SRC_LOCAL, constants.INSTANCE_TYPE_LOCAL): |
| 60 | local_image_local_instance.LocalImageLocalInstance, |
| 61 | (constants.TYPE_CF, constants.IMAGE_SRC_LOCAL, constants.INSTANCE_TYPE_REMOTE): |
| 62 | local_image_remote_instance.LocalImageRemoteInstance, |
| cylan | d43f293 | 2019-11-26 17:37:28 +0800 | [diff] [blame] | 63 | (constants.TYPE_CF, constants.IMAGE_SRC_LOCAL, constants.INSTANCE_TYPE_HOST): |
| 64 | local_image_remote_host.LocalImageRemoteHost, |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 65 | (constants.TYPE_CF, constants.IMAGE_SRC_REMOTE, constants.INSTANCE_TYPE_REMOTE): |
| 66 | remote_image_remote_instance.RemoteImageRemoteInstance, |
| 67 | (constants.TYPE_CF, constants.IMAGE_SRC_REMOTE, constants.INSTANCE_TYPE_LOCAL): |
| 68 | remote_image_local_instance.RemoteImageLocalInstance, |
| cylan | d43f293 | 2019-11-26 17:37:28 +0800 | [diff] [blame] | 69 | (constants.TYPE_CF, constants.IMAGE_SRC_REMOTE, constants.INSTANCE_TYPE_HOST): |
| 70 | remote_image_remote_host.RemoteImageRemoteHost, |
| Richard Fung | 97503b2 | 2019-02-06 13:43:38 -0800 | [diff] [blame] | 71 | # Cheeps types |
| 72 | (constants.TYPE_CHEEPS, constants.IMAGE_SRC_REMOTE, constants.INSTANCE_TYPE_REMOTE): |
| 73 | cheeps_remote_image_remote_instance.CheepsRemoteImageRemoteInstance, |
| herbertxue | 494891e | 2019-01-19 13:50:53 +0800 | [diff] [blame] | 74 | # GF types |
| 75 | (constants.TYPE_GF, constants.IMAGE_SRC_REMOTE, constants.INSTANCE_TYPE_REMOTE): |
| 76 | goldfish_remote_image_remote_instance.GoldfishRemoteImageRemoteInstance, |
| Hsin-Yi Chen | 7ba4996 | 2019-09-25 18:58:56 +0800 | [diff] [blame] | 77 | (constants.TYPE_GF, constants.IMAGE_SRC_LOCAL, constants.INSTANCE_TYPE_LOCAL): |
| 78 | goldfish_local_image_local_instance.GoldfishLocalImageLocalInstance, |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 79 | } |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 80 | |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 81 | |
| 82 | def GetAvdCreatorClass(avd_type, instance_type, image_source): |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 83 | """Return the creator class for the specified spec. |
| 84 | |
| 85 | Based on the image source and the instance type, return the proper |
| 86 | creator class. |
| 87 | |
| 88 | Args: |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 89 | avd_type: String, the AVD type(cuttlefish, gce). |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 90 | instance_type: String, the AVD instance type (local or remote). |
| 91 | image_source: String, the source of the image (local or remote). |
| 92 | |
| 93 | Returns: |
| 94 | An AVD creator class (e.g. LocalImageRemoteInstance). |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 95 | |
| 96 | Raises: |
| 97 | UnsupportedInstanceImageType if argments didn't match _CREATOR_CLASS_DICT. |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 98 | """ |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 99 | creator_class = _CREATOR_CLASS_DICT.get( |
| 100 | (avd_type, image_source, instance_type)) |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 101 | |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 102 | if not creator_class: |
| 103 | raise errors.UnsupportedInstanceImageType( |
| 104 | "unsupported creation of avd type: %s, instance type: %s, " |
| 105 | "image source: %s" % (avd_type, instance_type, image_source)) |
| 106 | return creator_class |
| Kevin Cheng | 3087af5 | 2018-08-13 13:26:50 -0700 | [diff] [blame] | 107 | |
| Kevin Cheng | 835a415 | 2018-10-11 10:46:57 -0700 | [diff] [blame] | 108 | def _CheckForAutoconnect(args): |
| 109 | """Check that we have all prerequisites for autoconnect. |
| 110 | |
| 111 | Autoconnect requires adb and ssh, we'll just check for adb for now and |
| 112 | assume ssh is everywhere. If adb isn't around, ask the user if they want us |
| 113 | to build it, if not we'll disable autoconnect. |
| 114 | |
| 115 | Args: |
| 116 | args: Namespace object from argparse.parse_args. |
| 117 | """ |
| chojoyce | 6e30cc5 | 2019-11-04 16:18:09 +0800 | [diff] [blame] | 118 | if not args.autoconnect or utils.FindExecutable(constants.ADB_BIN): |
| Kevin Cheng | 835a415 | 2018-10-11 10:46:57 -0700 | [diff] [blame] | 119 | return |
| 120 | |
| 121 | disable_autoconnect = False |
| 122 | answer = utils.InteractWithQuestion( |
| 123 | "adb is required for autoconnect, without it autoconnect will be " |
| Sam Chiu | 705b901 | 2019-01-19 12:11:35 +0800 | [diff] [blame] | 124 | "disabled, would you like acloud to build it[y/N]? ") |
| Kevin Cheng | 835a415 | 2018-10-11 10:46:57 -0700 | [diff] [blame] | 125 | if answer in constants.USER_ANSWER_YES: |
| 126 | utils.PrintColorString("Building adb ... ", end="") |
| 127 | android_build_top = os.environ.get( |
| 128 | constants.ENV_ANDROID_BUILD_TOP) |
| 129 | if not android_build_top: |
| 130 | utils.PrintColorString("Fail! (Not in a lunch'd env)", |
| 131 | utils.TextColors.FAIL) |
| 132 | disable_autoconnect = True |
| 133 | else: |
| 134 | make_cmd = os.path.join(android_build_top, _MAKE_CMD) |
| 135 | build_adb_cmd = [make_cmd, _MAKE_ARG, "adb"] |
| 136 | try: |
| 137 | with open(os.devnull, "w") as dev_null: |
| 138 | subprocess.check_call(build_adb_cmd, stderr=dev_null, |
| 139 | stdout=dev_null) |
| 140 | utils.PrintColorString("OK!", utils.TextColors.OKGREEN) |
| 141 | except subprocess.CalledProcessError: |
| 142 | utils.PrintColorString("Fail! (build failed)", |
| 143 | utils.TextColors.FAIL) |
| 144 | disable_autoconnect = True |
| 145 | else: |
| 146 | disable_autoconnect = True |
| 147 | |
| 148 | if disable_autoconnect: |
| 149 | utils.PrintColorString("Disabling autoconnect", |
| 150 | utils.TextColors.WARNING) |
| 151 | args.autoconnect = False |
| 152 | |
| 153 | |
| 154 | def _CheckForSetup(args): |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 155 | """Check that host is setup to run the create commands. |
| 156 | |
| 157 | We'll check we have the necessary bits setup to do what the user wants, and |
| 158 | if not, tell them what they need to do before running create again. |
| 159 | |
| 160 | Args: |
| 161 | args: Namespace object from argparse.parse_args. |
| 162 | """ |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 163 | # Need to set all these so if we need to run setup, it won't barf on us |
| 164 | # because of some missing fields. |
| 165 | args.gcp_init = False |
| 166 | args.host = False |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 167 | args.host_base = False |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 168 | args.force = False |
| 169 | # Remote image/instance requires the GCP config setup. |
| 170 | if not args.local_instance or args.local_image == "": |
| 171 | gcp_setup = gcp_setup_runner.GcpTaskRunner(args.config_file) |
| 172 | if gcp_setup.ShouldRun(): |
| 173 | args.gcp_init = True |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 174 | |
| Kevin Cheng | 2aa41da | 2018-10-11 15:00:48 -0700 | [diff] [blame] | 175 | # Local instance requires host to be setup. We'll assume that if the |
| 176 | # packages were installed, then the user was added into the groups. This |
| 177 | # avoids the scenario where a user runs setup and creates a local instance. |
| 178 | # The following local instance create will trigger this if statment and go |
| 179 | # through the whole setup again even though it's already done because the |
| 180 | # user groups aren't set until the user logs out and back in. |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 181 | if args.local_instance: |
| 182 | host_pkg_setup = host_setup_runner.AvdPkgInstaller() |
| Kevin Cheng | 2aa41da | 2018-10-11 15:00:48 -0700 | [diff] [blame] | 183 | if host_pkg_setup.ShouldRun(): |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 184 | args.host = True |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 185 | |
| 186 | # Install base packages if we haven't already. |
| 187 | host_base_setup = host_setup_runner.HostBasePkgInstaller() |
| 188 | if host_base_setup.ShouldRun(): |
| 189 | args.host_base = True |
| 190 | |
| 191 | run_setup = args.force or args.gcp_init or args.host or args.host_base |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 192 | |
| 193 | if run_setup: |
| 194 | answer = utils.InteractWithQuestion("Missing necessary acloud setup, " |
| Sam Chiu | 705b901 | 2019-01-19 12:11:35 +0800 | [diff] [blame] | 195 | "would you like to run setup[y/N]?") |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 196 | if answer in constants.USER_ANSWER_YES: |
| 197 | setup.Run(args) |
| 198 | else: |
| 199 | print("Please run '#acloud setup' so we can get your host setup") |
| Sam Chiu | e791f60 | 2019-05-03 15:18:10 +0800 | [diff] [blame] | 200 | sys.exit(constants.EXIT_BY_USER) |
| Kevin Cheng | cc6bf0d | 2018-10-10 14:18:47 -0700 | [diff] [blame] | 201 | |
| 202 | |
| Kevin Cheng | 835a415 | 2018-10-11 10:46:57 -0700 | [diff] [blame] | 203 | def PreRunCheck(args): |
| 204 | """Do some pre-run checks to ensure a smooth create experience. |
| 205 | |
| 206 | Args: |
| 207 | args: Namespace object from argparse.parse_args. |
| 208 | """ |
| 209 | _CheckForSetup(args) |
| 210 | _CheckForAutoconnect(args) |
| 211 | |
| 212 | |
| Kevin Cheng | c3d0d5e | 2018-08-14 14:22:44 -0700 | [diff] [blame] | 213 | def Run(args): |
| Kevin Cheng | 3087af5 | 2018-08-13 13:26:50 -0700 | [diff] [blame] | 214 | """Run create. |
| 215 | |
| 216 | Args: |
| 217 | args: Namespace object from argparse.parse_args. |
| herbertxue | 48bba08 | 2019-12-31 12:12:16 +0800 | [diff] [blame^] | 218 | |
| 219 | Returns: |
| 220 | A Report instance. |
| Kevin Cheng | 3087af5 | 2018-08-13 13:26:50 -0700 | [diff] [blame] | 221 | """ |
| Kevin Cheng | fab8cfc | 2019-06-27 14:01:37 -0700 | [diff] [blame] | 222 | if not args.skip_pre_run_check: |
| 223 | PreRunCheck(args) |
| Kevin Cheng | c3d0d5e | 2018-08-14 14:22:44 -0700 | [diff] [blame] | 224 | spec = avd_spec.AVDSpec(args) |
| chojoyce | 7a36173 | 2018-11-26 16:26:13 +0800 | [diff] [blame] | 225 | avd_creator_class = GetAvdCreatorClass(spec.avd_type, |
| 226 | spec.instance_type, |
| Kevin Cheng | 3ce4b45 | 2018-08-23 14:47:22 -0700 | [diff] [blame] | 227 | spec.image_source) |
| 228 | avd_creator = avd_creator_class() |
| herbertxue | 7ef23b2 | 2019-02-27 09:34:24 +0800 | [diff] [blame] | 229 | report = avd_creator.Create(spec, args.no_prompt) |
| herbertxue | 48bba08 | 2019-12-31 12:12:16 +0800 | [diff] [blame^] | 230 | return report |