blob: d5eaeb1eaab8e8fb186a1eefc1bf6263d9a38880 [file] [log] [blame]
Sam Chiu81bdc652018-06-29 18:45:08 +08001#!/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.
16r"""host setup runner
17
18A setup sub task runner to support setting up the local host for AVD local
19instance.
20"""
21
22from __future__ import print_function
23
24import getpass
25import grp
26import logging
27import os
28import platform
29
30from acloud.internal import constants
31from acloud.internal.lib import utils
32from acloud.setup import base_task_runner
33from acloud.setup import setup_common
34
35logger = logging.getLogger(__name__)
36
37# Install cuttlefish-common will probably not work now.
38# TODO: update this to pull from the proper repo.
chojoyce1b818bb2018-10-03 16:34:57 +080039_AVD_REQUIRED_PKGS = ["cuttlefish-common", "ssvnc"]
Sam Chiu81bdc652018-06-29 18:45:08 +080040# dict of supported system and their distributions.
41_SUPPORTED_SYSTEMS_AND_DISTS = {"Linux": ["Ubuntu", "Debian"]}
Sam Chiu81bdc652018-06-29 18:45:08 +080042_LIST_OF_MODULES = ["kvm_intel", "kvm"]
43
44
45def _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
chojoyce1b818bb2018-10-03 16:34:57 +080066class AvdPkgInstaller(base_task_runner.BaseTaskRunner):
Sam Chiu81bdc652018-06-29 18:45:08 +080067 """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 "
chojoyce1b818bb2018-10-03 16:34:57 +080072 "running Android cuttlefish devices and vnc on your host.")
Sam Chiu81bdc652018-06-29 18:45:08 +080073
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.
chojoyce1b818bb2018-10-03 16:34:57 +080085 for pkg_name in _AVD_REQUIRED_PKGS:
Sam Chiu81bdc652018-06-29 18:45:08 +080086 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 ",
chojoyce1b818bb2018-10-03 16:34:57 +080095 _AVD_REQUIRED_PKGS)
Sam Chiu81bdc652018-06-29 18:45:08 +080096
chojoyce1b818bb2018-10-03 16:34:57 +080097 for pkg in _AVD_REQUIRED_PKGS:
Sam Chiu81bdc652018-06-29 18:45:08 +080098 setup_common.InstallPackage(pkg)
99
100 logger.info("All required package are installed now.")
101
102
103class 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 Chiuafbc6582018-09-04 20:47:13 +0800123 return not (self.CheckUserInGroups(constants.LIST_CF_USER_GROUPS)
Sam Chiu81bdc652018-06-29 18:45:08 +0800124 and self._CheckLoadedModules(_LIST_OF_MODULES))
125
126 @staticmethod
Sam Chiuafbc6582018-09-04 20:47:13 +0800127 def CheckUserInGroups(group_name_list):
Sam Chiu81bdc652018-06-29 18:45:08 +0800128 """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 Chiuafbc6582018-09-04 20:47:13 +0800172 for group in constants.LIST_CF_USER_GROUPS:
Sam Chiu81bdc652018-06-29 18:45:08 +0800173 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