blob: fef6b4160d48291731ebac02209127124589c663 [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
Sam Chiu81bdc652018-06-29 18:45:08 +080025import logging
herbertxue975b8872020-02-12 14:41:41 +080026import os
27import shutil
Sam Chiuaa703b62019-10-04 19:47:43 +080028import sys
herbertxue975b8872020-02-12 14:41:41 +080029import tempfile
Sam Chiu81bdc652018-06-29 18:45:08 +080030
31from acloud.internal import constants
32from acloud.internal.lib import utils
33from acloud.setup import base_task_runner
34from acloud.setup import setup_common
chojoyceb0817032022-01-12 18:29:36 +080035from acloud.setup import mkcert
Sam Chiu81bdc652018-06-29 18:45:08 +080036
herbertxue1512f8a2019-06-27 13:56:23 +080037
Sam Chiu81bdc652018-06-29 18:45:08 +080038logger = logging.getLogger(__name__)
39
herbertxue975b8872020-02-12 14:41:41 +080040_CF_COMMOM_FOLDER = "cf-common"
chojoyce345c2c02021-08-12 18:24:05 +080041
Sam Chiu81bdc652018-06-29 18:45:08 +080042_LIST_OF_MODULES = ["kvm_intel", "kvm"]
Kevin Chengf756bbd2018-10-11 13:50:00 -070043_UPDATE_APT_GET_CMD = "sudo apt-get update"
herbertxue975b8872020-02-12 14:41:41 +080044_INSTALL_CUTTLEFISH_COMMOM_CMD = [
45 "git clone https://github.com/google/android-cuttlefish.git {git_folder}",
46 "cd {git_folder}",
47 "yes | sudo mk-build-deps -i -r -B",
48 "dpkg-buildpackage -uc -us",
49 "sudo apt-get install -y -f ../cuttlefish-common_*_amd64.deb"]
Sam Chiu81bdc652018-06-29 18:45:08 +080050
51
Kevin Chengeb997272019-06-05 14:53:18 -070052class BasePkgInstaller(base_task_runner.BaseTaskRunner):
53 """Subtask base runner class for installing packages."""
Sam Chiu81bdc652018-06-29 18:45:08 +080054
Kevin Chengeb997272019-06-05 14:53:18 -070055 # List of packages for child classes to override.
56 PACKAGES = []
Sam Chiu81bdc652018-06-29 18:45:08 +080057
58 def ShouldRun(self):
59 """Check if required packages are all installed.
60
61 Returns:
62 Boolean, True if required packages are not installed.
63 """
Sam Chiu6c738d62018-12-04 10:29:02 +080064 if not utils.IsSupportedPlatform():
Sam Chiu81bdc652018-06-29 18:45:08 +080065 return False
66
67 # Any required package is not installed or not up-to-date will need to
68 # run installation task.
Kevin Chengeb997272019-06-05 14:53:18 -070069 for pkg_name in self.PACKAGES:
Sam Chiu81bdc652018-06-29 18:45:08 +080070 if not setup_common.PackageInstalled(pkg_name):
71 return True
72
73 return False
74
75 def _Run(self):
Kevin Chengeb997272019-06-05 14:53:18 -070076 """Install specified packages."""
Sam Chiuaa703b62019-10-04 19:47:43 +080077 cmd = "\n".join(
78 [setup_common.PKG_INSTALL_CMD % pkg
79 for pkg in self.PACKAGES
80 if not setup_common.PackageInstalled(pkg)])
Sam Chiu81bdc652018-06-29 18:45:08 +080081
Sam Chiuaa703b62019-10-04 19:47:43 +080082 if not utils.GetUserAnswerYes("\nStart to install package(s):\n%s"
herbertxue97af4a62020-11-05 17:12:56 +080083 "\nEnter 'y' to continue, otherwise N or "
84 "enter to exit: " % cmd):
Sam Chiuaa703b62019-10-04 19:47:43 +080085 sys.exit(constants.EXIT_BY_USER)
Sam Chiu81bdc652018-06-29 18:45:08 +080086
Kevin Chengf756bbd2018-10-11 13:50:00 -070087 setup_common.CheckCmdOutput(_UPDATE_APT_GET_CMD, shell=True)
Kevin Chengeb997272019-06-05 14:53:18 -070088 for pkg in self.PACKAGES:
Sam Chiu81bdc652018-06-29 18:45:08 +080089 setup_common.InstallPackage(pkg)
90
Kevin Chengeb997272019-06-05 14:53:18 -070091 logger.info("All package(s) installed now.")
92
93
94class AvdPkgInstaller(BasePkgInstaller):
95 """Subtask runner class for installing packages for local instances."""
96
97 WELCOME_MESSAGE_TITLE = ("Install required packages for host setup for "
98 "local instances")
99 WELCOME_MESSAGE = ("This step will walk you through the required packages "
100 "installation for running Android cuttlefish devices "
101 "on your host.")
chojoyce8faf8852021-07-12 12:26:28 +0800102 PACKAGES = constants.AVD_REQUIRED_PKGS
Kevin Chengeb997272019-06-05 14:53:18 -0700103
104
105class HostBasePkgInstaller(BasePkgInstaller):
106 """Subtask runner class for installing base host packages."""
107
108 WELCOME_MESSAGE_TITLE = "Install base packages on the host"
109 WELCOME_MESSAGE = ("This step will walk you through the base packages "
110 "installation for your host.")
chojoyce8faf8852021-07-12 12:26:28 +0800111 PACKAGES = constants.BASE_REQUIRED_PKGS
Sam Chiu81bdc652018-06-29 18:45:08 +0800112
113
herbertxue975b8872020-02-12 14:41:41 +0800114class CuttlefishCommonPkgInstaller(base_task_runner.BaseTaskRunner):
115 """Subtask base runner class for installing cuttlefish-common."""
116
117 WELCOME_MESSAGE_TITLE = "Install cuttlefish-common packages on the host"
118 WELCOME_MESSAGE = ("This step will walk you through the cuttlefish-common "
119 "packages installation for your host.")
120
121 def ShouldRun(self):
122 """Check if cuttlefish-common package is installed.
123
124 Returns:
125 Boolean, True if cuttlefish-common is not installed.
126 """
127 if not utils.IsSupportedPlatform():
128 return False
129
130 # Any required package is not installed or not up-to-date will need to
131 # run installation task.
chojoyce8faf8852021-07-12 12:26:28 +0800132 if not setup_common.PackageInstalled(constants.CUTTLEFISH_COMMOM_PKG):
herbertxue975b8872020-02-12 14:41:41 +0800133 return True
134 return False
135
136 def _Run(self):
137 """Install cuttlefilsh-common packages."""
138 cf_common_path = os.path.join(tempfile.mkdtemp(), _CF_COMMOM_FOLDER)
139 logger.debug("cuttlefish-common path: %s", cf_common_path)
140 cmd = "\n".join(sub_cmd.format(git_folder=cf_common_path)
141 for sub_cmd in _INSTALL_CUTTLEFISH_COMMOM_CMD)
142
143 if not utils.GetUserAnswerYes("\nStart to install cuttlefish-common :\n%s"
herbertxue97af4a62020-11-05 17:12:56 +0800144 "\nEnter 'y' to continue, otherwise N or "
145 "enter to exit: " % cmd):
herbertxue975b8872020-02-12 14:41:41 +0800146 sys.exit(constants.EXIT_BY_USER)
147 try:
148 setup_common.CheckCmdOutput(cmd, shell=True)
149 finally:
150 shutil.rmtree(os.path.dirname(cf_common_path))
151 logger.info("Cuttlefish-common package installed now.")
152
chojoyce3c5ad5c2021-07-05 10:44:14 +0800153
chojoyceb0817032022-01-12 18:29:36 +0800154class LocalCAHostSetup(base_task_runner.BaseTaskRunner):
155 """Subtask class that setup host for setup local CA."""
156
157 WELCOME_MESSAGE_TITLE = "Local CA Host Environment Setup"
158 WELCOME_MESSAGE = ("This step will walk you through the local CA setup "
159 "to your host for assuring a secure localhost url "
160 "connection when launching an AVD over webrtc.")
chojoyce3c5ad5c2021-07-05 10:44:14 +0800161
162 def ShouldRun(self):
chojoyceb0817032022-01-12 18:29:36 +0800163 """Check if the local CA is setup or not.
chojoyce3c5ad5c2021-07-05 10:44:14 +0800164
165 Returns:
chojoyceb0817032022-01-12 18:29:36 +0800166 Boolean, True if local CA is ready.
chojoyce3c5ad5c2021-07-05 10:44:14 +0800167 """
168 if not utils.IsSupportedPlatform():
169 return False
170
chojoyceb0817032022-01-12 18:29:36 +0800171 return not mkcert.IsRootCAReady()
chojoyce3c5ad5c2021-07-05 10:44:14 +0800172
173 def _Run(self):
chojoyceb0817032022-01-12 18:29:36 +0800174 """Setup host environment for the local CA."""
175 if not utils.GetUserAnswerYes("\nStart to setup the local CA:\n"
chojoyce3c5ad5c2021-07-05 10:44:14 +0800176 "\nEnter 'y' to continue, otherwise N or "
chojoyceb0817032022-01-12 18:29:36 +0800177 "enter to exit: "):
chojoyce3c5ad5c2021-07-05 10:44:14 +0800178 sys.exit(constants.EXIT_BY_USER)
179
chojoyceb0817032022-01-12 18:29:36 +0800180 mkcert.Install()
181 logger.info("The local CA '%s.pem' is installed now.",
182 constants.SSL_CA_NAME)
183
herbertxue975b8872020-02-12 14:41:41 +0800184
Sam Chiu81bdc652018-06-29 18:45:08 +0800185class CuttlefishHostSetup(base_task_runner.BaseTaskRunner):
186 """Subtask class that setup host for cuttlefish."""
187
chojoyceb0817032022-01-12 18:29:36 +0800188 WELCOME_MESSAGE_TITLE = "Host Environment Setup"
Sam Chiu81bdc652018-06-29 18:45:08 +0800189 WELCOME_MESSAGE = (
chojoyceb0817032022-01-12 18:29:36 +0800190 "This step will help you to setup environment for running Android "
Sam Chiu81bdc652018-06-29 18:45:08 +0800191 "cuttlefish devices on your host. That includes adding user to kvm "
192 "related groups and checking required linux modules."
193 )
194
195 def ShouldRun(self):
196 """Check host user groups and modules.
197
198 Returns:
199 Boolean: False if user is in all required groups and all modules
200 are reloaded.
201 """
Sam Chiu6c738d62018-12-04 10:29:02 +0800202 if not utils.IsSupportedPlatform():
Sam Chiu81bdc652018-06-29 18:45:08 +0800203 return False
204
herbertxue07293a32018-11-05 20:40:11 +0800205 return not (utils.CheckUserInGroups(constants.LIST_CF_USER_GROUPS)
Sam Chiu81bdc652018-06-29 18:45:08 +0800206 and self._CheckLoadedModules(_LIST_OF_MODULES))
207
208 @staticmethod
Sam Chiu81bdc652018-06-29 18:45:08 +0800209 def _CheckLoadedModules(module_list):
210 """Check if the modules are all in use.
211
212 Args:
213 module_list: The list of module name.
214 Returns:
215 True if all modules are in use.
216 """
217 logger.info("Checking if modules are loaded: %s", module_list)
218 lsmod_output = setup_common.CheckCmdOutput("lsmod", print_cmd=False)
219 current_modules = [r.split()[0] for r in lsmod_output.splitlines()]
220 all_modules_present = True
221 for module in module_list:
222 if module not in current_modules:
223 logger.info("missing module: %s", module)
224 all_modules_present = False
225 return all_modules_present
226
227 def _Run(self):
228 """Setup host environment for local cuttlefish instance support."""
229 # TODO: provide --uid args to let user use prefered username
230 username = getpass.getuser()
231 setup_cmds = [
232 "sudo rmmod kvm_intel",
233 "sudo rmmod kvm",
234 "sudo modprobe kvm",
235 "sudo modprobe kvm_intel"]
Sam Chiuafbc6582018-09-04 20:47:13 +0800236 for group in constants.LIST_CF_USER_GROUPS:
Sam Chiu81bdc652018-06-29 18:45:08 +0800237 setup_cmds.append("sudo usermod -aG %s % s" % (group, username))
238
239 print("Below commands will be run:")
240 for setup_cmd in setup_cmds:
241 print(setup_cmd)
242
243 if self._ConfirmContinue():
244 for setup_cmd in setup_cmds:
245 setup_common.CheckCmdOutput(setup_cmd, shell=True)
246 print("Host environment setup has done!")
247
248 @staticmethod
249 def _ConfirmContinue():
250 """Ask user if they want to continue.
251
252 Returns:
253 True if user answer yes.
254 """
255 answer_client = utils.InteractWithQuestion(
herbertxue97af4a62020-11-05 17:12:56 +0800256 "\nEnter 'y' to continue, otherwise N or enter to exit: ",
Sam Chiu81bdc652018-06-29 18:45:08 +0800257 utils.TextColors.WARNING)
258 return answer_client in constants.USER_ANSWER_YES