| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [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"""host setup runner |
| 17 | |
| 18 | A setup sub task runner to support setting up the local host for AVD local |
| 19 | instance. |
| 20 | """ |
| 21 | |
| 22 | from __future__ import print_function |
| 23 | |
| 24 | import getpass |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 25 | import logging |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 26 | import os |
| 27 | import shutil |
| Sam Chiu | aa703b6 | 2019-10-04 19:47:43 +0800 | [diff] [blame] | 28 | import sys |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 29 | import tempfile |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 30 | |
| 31 | from acloud.internal import constants |
| 32 | from acloud.internal.lib import utils |
| 33 | from acloud.setup import base_task_runner |
| 34 | from acloud.setup import setup_common |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 35 | from acloud.setup import mkcert |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 36 | |
| herbertxue | 1512f8a | 2019-06-27 13:56:23 +0800 | [diff] [blame] | 37 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 38 | logger = logging.getLogger(__name__) |
| 39 | |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 40 | _CF_COMMOM_FOLDER = "cf-common" |
| chojoyce | 345c2c0 | 2021-08-12 18:24:05 +0800 | [diff] [blame] | 41 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 42 | _LIST_OF_MODULES = ["kvm_intel", "kvm"] |
| Kevin Cheng | f756bbd | 2018-10-11 13:50:00 -0700 | [diff] [blame] | 43 | _UPDATE_APT_GET_CMD = "sudo apt-get update" |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 44 | _INSTALL_CUTTLEFISH_COMMOM_CMD = [ |
| 45 | "git clone https://github.com/google/android-cuttlefish.git {git_folder}", |
| 46 | "cd {git_folder}", |
| chojoyce | 1f78dee | 2022-02-09 17:50:24 +0800 | [diff] [blame] | 47 | "debuild -i -us -uc -b", |
| 48 | "sudo dpkg -i ../cuttlefish-common_*_*64.deb || sudo apt-get install -f"] |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 49 | |
| 50 | |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 51 | class BasePkgInstaller(base_task_runner.BaseTaskRunner): |
| 52 | """Subtask base runner class for installing packages.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 53 | |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 54 | # List of packages for child classes to override. |
| 55 | PACKAGES = [] |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 56 | |
| 57 | def ShouldRun(self): |
| 58 | """Check if required packages are all installed. |
| 59 | |
| 60 | Returns: |
| 61 | Boolean, True if required packages are not installed. |
| 62 | """ |
| Sam Chiu | 6c738d6 | 2018-12-04 10:29:02 +0800 | [diff] [blame] | 63 | if not utils.IsSupportedPlatform(): |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 64 | return False |
| 65 | |
| 66 | # Any required package is not installed or not up-to-date will need to |
| 67 | # run installation task. |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 68 | for pkg_name in self.PACKAGES: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 69 | if not setup_common.PackageInstalled(pkg_name): |
| 70 | return True |
| 71 | |
| 72 | return False |
| 73 | |
| 74 | def _Run(self): |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 75 | """Install specified packages.""" |
| Sam Chiu | aa703b6 | 2019-10-04 19:47:43 +0800 | [diff] [blame] | 76 | cmd = "\n".join( |
| 77 | [setup_common.PKG_INSTALL_CMD % pkg |
| 78 | for pkg in self.PACKAGES |
| 79 | if not setup_common.PackageInstalled(pkg)]) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 80 | |
| Sam Chiu | aa703b6 | 2019-10-04 19:47:43 +0800 | [diff] [blame] | 81 | if not utils.GetUserAnswerYes("\nStart to install package(s):\n%s" |
| herbertxue | 97af4a6 | 2020-11-05 17:12:56 +0800 | [diff] [blame] | 82 | "\nEnter 'y' to continue, otherwise N or " |
| 83 | "enter to exit: " % cmd): |
| Sam Chiu | aa703b6 | 2019-10-04 19:47:43 +0800 | [diff] [blame] | 84 | sys.exit(constants.EXIT_BY_USER) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 85 | |
| Kevin Cheng | f756bbd | 2018-10-11 13:50:00 -0700 | [diff] [blame] | 86 | setup_common.CheckCmdOutput(_UPDATE_APT_GET_CMD, shell=True) |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 87 | for pkg in self.PACKAGES: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 88 | setup_common.InstallPackage(pkg) |
| 89 | |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 90 | logger.info("All package(s) installed now.") |
| 91 | |
| 92 | |
| 93 | class AvdPkgInstaller(BasePkgInstaller): |
| 94 | """Subtask runner class for installing packages for local instances.""" |
| 95 | |
| 96 | WELCOME_MESSAGE_TITLE = ("Install required packages for host setup for " |
| 97 | "local instances") |
| 98 | WELCOME_MESSAGE = ("This step will walk you through the required packages " |
| 99 | "installation for running Android cuttlefish devices " |
| 100 | "on your host.") |
| chojoyce | 8faf885 | 2021-07-12 12:26:28 +0800 | [diff] [blame] | 101 | PACKAGES = constants.AVD_REQUIRED_PKGS |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 102 | |
| 103 | |
| 104 | class HostBasePkgInstaller(BasePkgInstaller): |
| 105 | """Subtask runner class for installing base host packages.""" |
| 106 | |
| 107 | WELCOME_MESSAGE_TITLE = "Install base packages on the host" |
| 108 | WELCOME_MESSAGE = ("This step will walk you through the base packages " |
| 109 | "installation for your host.") |
| chojoyce | 8faf885 | 2021-07-12 12:26:28 +0800 | [diff] [blame] | 110 | PACKAGES = constants.BASE_REQUIRED_PKGS |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 111 | |
| 112 | |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 113 | class CuttlefishCommonPkgInstaller(base_task_runner.BaseTaskRunner): |
| 114 | """Subtask base runner class for installing cuttlefish-common.""" |
| 115 | |
| 116 | WELCOME_MESSAGE_TITLE = "Install cuttlefish-common packages on the host" |
| 117 | WELCOME_MESSAGE = ("This step will walk you through the cuttlefish-common " |
| 118 | "packages installation for your host.") |
| 119 | |
| 120 | def ShouldRun(self): |
| 121 | """Check if cuttlefish-common package is installed. |
| 122 | |
| 123 | Returns: |
| 124 | Boolean, True if cuttlefish-common is not installed. |
| 125 | """ |
| 126 | if not utils.IsSupportedPlatform(): |
| 127 | return False |
| 128 | |
| 129 | # Any required package is not installed or not up-to-date will need to |
| 130 | # run installation task. |
| chojoyce | 8faf885 | 2021-07-12 12:26:28 +0800 | [diff] [blame] | 131 | if not setup_common.PackageInstalled(constants.CUTTLEFISH_COMMOM_PKG): |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 132 | return True |
| 133 | return False |
| 134 | |
| 135 | def _Run(self): |
| 136 | """Install cuttlefilsh-common packages.""" |
| 137 | cf_common_path = os.path.join(tempfile.mkdtemp(), _CF_COMMOM_FOLDER) |
| 138 | logger.debug("cuttlefish-common path: %s", cf_common_path) |
| 139 | cmd = "\n".join(sub_cmd.format(git_folder=cf_common_path) |
| 140 | for sub_cmd in _INSTALL_CUTTLEFISH_COMMOM_CMD) |
| 141 | |
| 142 | if not utils.GetUserAnswerYes("\nStart to install cuttlefish-common :\n%s" |
| herbertxue | 97af4a6 | 2020-11-05 17:12:56 +0800 | [diff] [blame] | 143 | "\nEnter 'y' to continue, otherwise N or " |
| 144 | "enter to exit: " % cmd): |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 145 | sys.exit(constants.EXIT_BY_USER) |
| 146 | try: |
| 147 | setup_common.CheckCmdOutput(cmd, shell=True) |
| 148 | finally: |
| 149 | shutil.rmtree(os.path.dirname(cf_common_path)) |
| 150 | logger.info("Cuttlefish-common package installed now.") |
| 151 | |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 152 | |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 153 | class LocalCAHostSetup(base_task_runner.BaseTaskRunner): |
| 154 | """Subtask class that setup host for setup local CA.""" |
| 155 | |
| 156 | WELCOME_MESSAGE_TITLE = "Local CA Host Environment Setup" |
| 157 | WELCOME_MESSAGE = ("This step will walk you through the local CA setup " |
| 158 | "to your host for assuring a secure localhost url " |
| 159 | "connection when launching an AVD over webrtc.") |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 160 | |
| 161 | def ShouldRun(self): |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 162 | """Check if the local CA is setup or not. |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 163 | |
| 164 | Returns: |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 165 | Boolean, True if local CA is ready. |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 166 | """ |
| 167 | if not utils.IsSupportedPlatform(): |
| 168 | return False |
| 169 | |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 170 | return not mkcert.IsRootCAReady() |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 171 | |
| 172 | def _Run(self): |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 173 | """Setup host environment for the local CA.""" |
| 174 | if not utils.GetUserAnswerYes("\nStart to setup the local CA:\n" |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 175 | "\nEnter 'y' to continue, otherwise N or " |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 176 | "enter to exit: "): |
| chojoyce | 3c5ad5c | 2021-07-05 10:44:14 +0800 | [diff] [blame] | 177 | sys.exit(constants.EXIT_BY_USER) |
| 178 | |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 179 | mkcert.Install() |
| 180 | logger.info("The local CA '%s.pem' is installed now.", |
| 181 | constants.SSL_CA_NAME) |
| 182 | |
| herbertxue | 975b887 | 2020-02-12 14:41:41 +0800 | [diff] [blame] | 183 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 184 | class CuttlefishHostSetup(base_task_runner.BaseTaskRunner): |
| 185 | """Subtask class that setup host for cuttlefish.""" |
| 186 | |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 187 | WELCOME_MESSAGE_TITLE = "Host Environment Setup" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 188 | WELCOME_MESSAGE = ( |
| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 189 | "This step will help you to setup environment for running Android " |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 190 | "cuttlefish devices on your host. That includes adding user to kvm " |
| 191 | "related groups and checking required linux modules." |
| 192 | ) |
| 193 | |
| 194 | def ShouldRun(self): |
| 195 | """Check host user groups and modules. |
| 196 | |
| 197 | Returns: |
| 198 | Boolean: False if user is in all required groups and all modules |
| 199 | are reloaded. |
| 200 | """ |
| Sam Chiu | 6c738d6 | 2018-12-04 10:29:02 +0800 | [diff] [blame] | 201 | if not utils.IsSupportedPlatform(): |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 202 | return False |
| 203 | |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 204 | return not (utils.CheckUserInGroups(constants.LIST_CF_USER_GROUPS) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 205 | and self._CheckLoadedModules(_LIST_OF_MODULES)) |
| 206 | |
| 207 | @staticmethod |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 208 | def _CheckLoadedModules(module_list): |
| 209 | """Check if the modules are all in use. |
| 210 | |
| 211 | Args: |
| 212 | module_list: The list of module name. |
| 213 | Returns: |
| 214 | True if all modules are in use. |
| 215 | """ |
| 216 | logger.info("Checking if modules are loaded: %s", module_list) |
| 217 | lsmod_output = setup_common.CheckCmdOutput("lsmod", print_cmd=False) |
| 218 | current_modules = [r.split()[0] for r in lsmod_output.splitlines()] |
| 219 | all_modules_present = True |
| 220 | for module in module_list: |
| 221 | if module not in current_modules: |
| 222 | logger.info("missing module: %s", module) |
| 223 | all_modules_present = False |
| 224 | return all_modules_present |
| 225 | |
| 226 | def _Run(self): |
| 227 | """Setup host environment for local cuttlefish instance support.""" |
| 228 | # TODO: provide --uid args to let user use prefered username |
| 229 | username = getpass.getuser() |
| 230 | setup_cmds = [ |
| 231 | "sudo rmmod kvm_intel", |
| 232 | "sudo rmmod kvm", |
| 233 | "sudo modprobe kvm", |
| 234 | "sudo modprobe kvm_intel"] |
| Sam Chiu | afbc658 | 2018-09-04 20:47:13 +0800 | [diff] [blame] | 235 | for group in constants.LIST_CF_USER_GROUPS: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 236 | setup_cmds.append("sudo usermod -aG %s % s" % (group, username)) |
| 237 | |
| 238 | print("Below commands will be run:") |
| 239 | for setup_cmd in setup_cmds: |
| 240 | print(setup_cmd) |
| 241 | |
| 242 | if self._ConfirmContinue(): |
| 243 | for setup_cmd in setup_cmds: |
| 244 | setup_common.CheckCmdOutput(setup_cmd, shell=True) |
| 245 | print("Host environment setup has done!") |
| 246 | |
| 247 | @staticmethod |
| 248 | def _ConfirmContinue(): |
| 249 | """Ask user if they want to continue. |
| 250 | |
| 251 | Returns: |
| 252 | True if user answer yes. |
| 253 | """ |
| 254 | answer_client = utils.InteractWithQuestion( |
| herbertxue | 97af4a6 | 2020-11-05 17:12:56 +0800 | [diff] [blame] | 255 | "\nEnter 'y' to continue, otherwise N or enter to exit: ", |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 256 | utils.TextColors.WARNING) |
| 257 | return answer_client in constants.USER_ANSWER_YES |