| 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 |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 26 | |
| 27 | from acloud.internal import constants |
| 28 | from acloud.internal.lib import utils |
| 29 | from acloud.setup import base_task_runner |
| 30 | from acloud.setup import setup_common |
| 31 | |
| herbertxue | 1512f8a | 2019-06-27 13:56:23 +0800 | [diff] [blame] | 32 | |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 33 | logger = logging.getLogger(__name__) |
| 34 | |
| 35 | # Install cuttlefish-common will probably not work now. |
| 36 | # TODO: update this to pull from the proper repo. |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 37 | _AVD_REQUIRED_PKGS = ["cuttlefish-common", |
| Kevin Cheng | f756bbd | 2018-10-11 13:50:00 -0700 | [diff] [blame] | 38 | # TODO(b/117613492): This is all qemu related, take this |
| 39 | # out once they are added back in as deps for |
| 40 | # cuttlefish-common. |
| 41 | "qemu-kvm", "qemu-system-common", "qemu-system-x86", |
| 42 | "qemu-utils", "libvirt-clients", "libvirt-daemon-system"] |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 43 | _BASE_REQUIRED_PKGS = ["ssvnc", "lzop"] |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 44 | _LIST_OF_MODULES = ["kvm_intel", "kvm"] |
| Kevin Cheng | f756bbd | 2018-10-11 13:50:00 -0700 | [diff] [blame] | 45 | _UPDATE_APT_GET_CMD = "sudo apt-get update" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 46 | |
| 47 | |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 48 | class BasePkgInstaller(base_task_runner.BaseTaskRunner): |
| 49 | """Subtask base runner class for installing packages.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 50 | |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 51 | # List of packages for child classes to override. |
| 52 | PACKAGES = [] |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 53 | |
| 54 | def ShouldRun(self): |
| 55 | """Check if required packages are all installed. |
| 56 | |
| 57 | Returns: |
| 58 | Boolean, True if required packages are not installed. |
| 59 | """ |
| Sam Chiu | 6c738d6 | 2018-12-04 10:29:02 +0800 | [diff] [blame] | 60 | if not utils.IsSupportedPlatform(): |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 61 | return False |
| 62 | |
| 63 | # Any required package is not installed or not up-to-date will need to |
| 64 | # run installation task. |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 65 | for pkg_name in self.PACKAGES: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 66 | if not setup_common.PackageInstalled(pkg_name): |
| 67 | return True |
| 68 | |
| 69 | return False |
| 70 | |
| 71 | def _Run(self): |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 72 | """Install specified packages.""" |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 73 | |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 74 | logger.info("Start to install package(s): %s ", |
| 75 | self.PACKAGES) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 76 | |
| Kevin Cheng | f756bbd | 2018-10-11 13:50:00 -0700 | [diff] [blame] | 77 | setup_common.CheckCmdOutput(_UPDATE_APT_GET_CMD, shell=True) |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 78 | for pkg in self.PACKAGES: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 79 | setup_common.InstallPackage(pkg) |
| 80 | |
| Kevin Cheng | eb99727 | 2019-06-05 14:53:18 -0700 | [diff] [blame] | 81 | logger.info("All package(s) installed now.") |
| 82 | |
| 83 | |
| 84 | class AvdPkgInstaller(BasePkgInstaller): |
| 85 | """Subtask runner class for installing packages for local instances.""" |
| 86 | |
| 87 | WELCOME_MESSAGE_TITLE = ("Install required packages for host setup for " |
| 88 | "local instances") |
| 89 | WELCOME_MESSAGE = ("This step will walk you through the required packages " |
| 90 | "installation for running Android cuttlefish devices " |
| 91 | "on your host.") |
| 92 | PACKAGES = _AVD_REQUIRED_PKGS |
| 93 | |
| 94 | |
| 95 | class HostBasePkgInstaller(BasePkgInstaller): |
| 96 | """Subtask runner class for installing base host packages.""" |
| 97 | |
| 98 | WELCOME_MESSAGE_TITLE = "Install base packages on the host" |
| 99 | WELCOME_MESSAGE = ("This step will walk you through the base packages " |
| 100 | "installation for your host.") |
| 101 | PACKAGES = _BASE_REQUIRED_PKGS |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 102 | |
| 103 | |
| 104 | class CuttlefishHostSetup(base_task_runner.BaseTaskRunner): |
| 105 | """Subtask class that setup host for cuttlefish.""" |
| 106 | |
| 107 | WELCOME_MESSAGE_TITLE = "Host Enviornment Setup" |
| 108 | WELCOME_MESSAGE = ( |
| 109 | "This step will help you to setup enviornment for running Android " |
| 110 | "cuttlefish devices on your host. That includes adding user to kvm " |
| 111 | "related groups and checking required linux modules." |
| 112 | ) |
| 113 | |
| 114 | def ShouldRun(self): |
| 115 | """Check host user groups and modules. |
| 116 | |
| 117 | Returns: |
| 118 | Boolean: False if user is in all required groups and all modules |
| 119 | are reloaded. |
| 120 | """ |
| Sam Chiu | 6c738d6 | 2018-12-04 10:29:02 +0800 | [diff] [blame] | 121 | if not utils.IsSupportedPlatform(): |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 122 | return False |
| 123 | |
| herbertxue | 07293a3 | 2018-11-05 20:40:11 +0800 | [diff] [blame] | 124 | return not (utils.CheckUserInGroups(constants.LIST_CF_USER_GROUPS) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 125 | and self._CheckLoadedModules(_LIST_OF_MODULES)) |
| 126 | |
| 127 | @staticmethod |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 128 | def _CheckLoadedModules(module_list): |
| 129 | """Check if the modules are all in use. |
| 130 | |
| 131 | Args: |
| 132 | module_list: The list of module name. |
| 133 | Returns: |
| 134 | True if all modules are in use. |
| 135 | """ |
| 136 | logger.info("Checking if modules are loaded: %s", module_list) |
| 137 | lsmod_output = setup_common.CheckCmdOutput("lsmod", print_cmd=False) |
| 138 | current_modules = [r.split()[0] for r in lsmod_output.splitlines()] |
| 139 | all_modules_present = True |
| 140 | for module in module_list: |
| 141 | if module not in current_modules: |
| 142 | logger.info("missing module: %s", module) |
| 143 | all_modules_present = False |
| 144 | return all_modules_present |
| 145 | |
| 146 | def _Run(self): |
| 147 | """Setup host environment for local cuttlefish instance support.""" |
| 148 | # TODO: provide --uid args to let user use prefered username |
| 149 | username = getpass.getuser() |
| 150 | setup_cmds = [ |
| 151 | "sudo rmmod kvm_intel", |
| 152 | "sudo rmmod kvm", |
| 153 | "sudo modprobe kvm", |
| 154 | "sudo modprobe kvm_intel"] |
| Sam Chiu | afbc658 | 2018-09-04 20:47:13 +0800 | [diff] [blame] | 155 | for group in constants.LIST_CF_USER_GROUPS: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 156 | setup_cmds.append("sudo usermod -aG %s % s" % (group, username)) |
| 157 | |
| 158 | print("Below commands will be run:") |
| 159 | for setup_cmd in setup_cmds: |
| 160 | print(setup_cmd) |
| 161 | |
| 162 | if self._ConfirmContinue(): |
| 163 | for setup_cmd in setup_cmds: |
| 164 | setup_common.CheckCmdOutput(setup_cmd, shell=True) |
| 165 | print("Host environment setup has done!") |
| 166 | |
| 167 | @staticmethod |
| 168 | def _ConfirmContinue(): |
| 169 | """Ask user if they want to continue. |
| 170 | |
| 171 | Returns: |
| 172 | True if user answer yes. |
| 173 | """ |
| 174 | answer_client = utils.InteractWithQuestion( |
| Sam Chiu | 705b901 | 2019-01-19 12:11:35 +0800 | [diff] [blame] | 175 | "\nPress 'y' to continue or anything else to do it myself[y/N]: ", |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 176 | utils.TextColors.WARNING) |
| 177 | return answer_client in constants.USER_ANSWER_YES |