| 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 |
| 25 | import grp |
| 26 | import logging |
| 27 | import os |
| 28 | import platform |
| 29 | |
| 30 | from acloud.internal import constants |
| 31 | from acloud.internal.lib import utils |
| 32 | from acloud.setup import base_task_runner |
| 33 | from acloud.setup import setup_common |
| 34 | |
| 35 | logger = logging.getLogger(__name__) |
| 36 | |
| 37 | # Install cuttlefish-common will probably not work now. |
| 38 | # TODO: update this to pull from the proper repo. |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 39 | _AVD_REQUIRED_PKGS = ["cuttlefish-common", "ssvnc"] |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 40 | # dict of supported system and their distributions. |
| 41 | _SUPPORTED_SYSTEMS_AND_DISTS = {"Linux": ["Ubuntu", "Debian"]} |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 42 | _LIST_OF_MODULES = ["kvm_intel", "kvm"] |
| 43 | |
| 44 | |
| 45 | def _IsSupportedPlatform(): |
| 46 | """Check if user's os is the supported platform. |
| 47 | |
| 48 | Returns: |
| 49 | Boolean, True if user is using supported platform. |
| 50 | """ |
| 51 | system = platform.system() |
| 52 | dist = platform.linux_distribution()[0] |
| 53 | platform_supported = (system in _SUPPORTED_SYSTEMS_AND_DISTS and |
| 54 | dist in _SUPPORTED_SYSTEMS_AND_DISTS[system]) |
| 55 | |
| 56 | logger.info("supported system and dists: %s", |
| 57 | _SUPPORTED_SYSTEMS_AND_DISTS) |
| 58 | logger.info("%s[%s] %s supported platform", |
| 59 | system, |
| 60 | dist, |
| 61 | "is a" if platform_supported else "is not a") |
| 62 | |
| 63 | return platform_supported |
| 64 | |
| 65 | |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 66 | class AvdPkgInstaller(base_task_runner.BaseTaskRunner): |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 67 | """Subtask runner class for installing required packages.""" |
| 68 | |
| 69 | WELCOME_MESSAGE_TITLE = "Install required package for host setup" |
| 70 | WELCOME_MESSAGE = ( |
| 71 | "This step will walk you through the required packages installation for " |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 72 | "running Android cuttlefish devices and vnc on your host.") |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 73 | |
| 74 | def ShouldRun(self): |
| 75 | """Check if required packages are all installed. |
| 76 | |
| 77 | Returns: |
| 78 | Boolean, True if required packages are not installed. |
| 79 | """ |
| 80 | if not _IsSupportedPlatform(): |
| 81 | return False |
| 82 | |
| 83 | # Any required package is not installed or not up-to-date will need to |
| 84 | # run installation task. |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 85 | for pkg_name in _AVD_REQUIRED_PKGS: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 86 | if not setup_common.PackageInstalled(pkg_name): |
| 87 | return True |
| 88 | |
| 89 | return False |
| 90 | |
| 91 | def _Run(self): |
| 92 | """Install Cuttlefish-common package.""" |
| 93 | |
| 94 | logger.info("Start to install required package: %s ", |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 95 | _AVD_REQUIRED_PKGS) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 96 | |
| chojoyce | 1b818bb | 2018-10-03 16:34:57 +0800 | [diff] [blame] | 97 | for pkg in _AVD_REQUIRED_PKGS: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 98 | setup_common.InstallPackage(pkg) |
| 99 | |
| 100 | logger.info("All required package are installed now.") |
| 101 | |
| 102 | |
| 103 | class CuttlefishHostSetup(base_task_runner.BaseTaskRunner): |
| 104 | """Subtask class that setup host for cuttlefish.""" |
| 105 | |
| 106 | WELCOME_MESSAGE_TITLE = "Host Enviornment Setup" |
| 107 | WELCOME_MESSAGE = ( |
| 108 | "This step will help you to setup enviornment for running Android " |
| 109 | "cuttlefish devices on your host. That includes adding user to kvm " |
| 110 | "related groups and checking required linux modules." |
| 111 | ) |
| 112 | |
| 113 | def ShouldRun(self): |
| 114 | """Check host user groups and modules. |
| 115 | |
| 116 | Returns: |
| 117 | Boolean: False if user is in all required groups and all modules |
| 118 | are reloaded. |
| 119 | """ |
| 120 | if not _IsSupportedPlatform(): |
| 121 | return False |
| 122 | |
| Sam Chiu | afbc658 | 2018-09-04 20:47:13 +0800 | [diff] [blame^] | 123 | return not (self.CheckUserInGroups(constants.LIST_CF_USER_GROUPS) |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 124 | and self._CheckLoadedModules(_LIST_OF_MODULES)) |
| 125 | |
| 126 | @staticmethod |
| Sam Chiu | afbc658 | 2018-09-04 20:47:13 +0800 | [diff] [blame^] | 127 | def CheckUserInGroups(group_name_list): |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 128 | """Check if the current user is in the group. |
| 129 | |
| 130 | Args: |
| 131 | group_name_list: The list of group name. |
| 132 | Returns: |
| 133 | True if current user is in all the groups. |
| 134 | """ |
| 135 | logger.info("Checking if user is in following groups: %s", group_name_list) |
| 136 | current_groups = [grp.getgrgid(g).gr_name for g in os.getgroups()] |
| 137 | all_groups_present = True |
| 138 | for group in group_name_list: |
| 139 | if group not in current_groups: |
| 140 | all_groups_present = False |
| 141 | logger.info("missing group: %s", group) |
| 142 | return all_groups_present |
| 143 | |
| 144 | @staticmethod |
| 145 | def _CheckLoadedModules(module_list): |
| 146 | """Check if the modules are all in use. |
| 147 | |
| 148 | Args: |
| 149 | module_list: The list of module name. |
| 150 | Returns: |
| 151 | True if all modules are in use. |
| 152 | """ |
| 153 | logger.info("Checking if modules are loaded: %s", module_list) |
| 154 | lsmod_output = setup_common.CheckCmdOutput("lsmod", print_cmd=False) |
| 155 | current_modules = [r.split()[0] for r in lsmod_output.splitlines()] |
| 156 | all_modules_present = True |
| 157 | for module in module_list: |
| 158 | if module not in current_modules: |
| 159 | logger.info("missing module: %s", module) |
| 160 | all_modules_present = False |
| 161 | return all_modules_present |
| 162 | |
| 163 | def _Run(self): |
| 164 | """Setup host environment for local cuttlefish instance support.""" |
| 165 | # TODO: provide --uid args to let user use prefered username |
| 166 | username = getpass.getuser() |
| 167 | setup_cmds = [ |
| 168 | "sudo rmmod kvm_intel", |
| 169 | "sudo rmmod kvm", |
| 170 | "sudo modprobe kvm", |
| 171 | "sudo modprobe kvm_intel"] |
| Sam Chiu | afbc658 | 2018-09-04 20:47:13 +0800 | [diff] [blame^] | 172 | for group in constants.LIST_CF_USER_GROUPS: |
| Sam Chiu | 81bdc65 | 2018-06-29 18:45:08 +0800 | [diff] [blame] | 173 | setup_cmds.append("sudo usermod -aG %s % s" % (group, username)) |
| 174 | |
| 175 | print("Below commands will be run:") |
| 176 | for setup_cmd in setup_cmds: |
| 177 | print(setup_cmd) |
| 178 | |
| 179 | if self._ConfirmContinue(): |
| 180 | for setup_cmd in setup_cmds: |
| 181 | setup_common.CheckCmdOutput(setup_cmd, shell=True) |
| 182 | print("Host environment setup has done!") |
| 183 | |
| 184 | @staticmethod |
| 185 | def _ConfirmContinue(): |
| 186 | """Ask user if they want to continue. |
| 187 | |
| 188 | Returns: |
| 189 | True if user answer yes. |
| 190 | """ |
| 191 | answer_client = utils.InteractWithQuestion( |
| 192 | "\nPress 'y' to continue or anything else to do it myself:[y]", |
| 193 | utils.TextColors.WARNING) |
| 194 | return answer_client in constants.USER_ANSWER_YES |