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