blob: f994eb105b6d5a1f750e85544911c4f3df9493bb [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
Sam Chiu81bdc652018-06-29 18:45:08 +080026
27from acloud.internal import constants
28from acloud.internal.lib import utils
29from acloud.setup import base_task_runner
30from acloud.setup import setup_common
31
32logger = logging.getLogger(__name__)
33
34# Install cuttlefish-common will probably not work now.
35# TODO: update this to pull from the proper repo.
Kevin Chengeb997272019-06-05 14:53:18 -070036_AVD_REQUIRED_PKGS = ["cuttlefish-common",
Kevin Chengf756bbd2018-10-11 13:50:00 -070037 # TODO(b/117613492): This is all qemu related, take this
38 # out once they are added back in as deps for
39 # cuttlefish-common.
40 "qemu-kvm", "qemu-system-common", "qemu-system-x86",
41 "qemu-utils", "libvirt-clients", "libvirt-daemon-system"]
Kevin Chengeb997272019-06-05 14:53:18 -070042_BASE_REQUIRED_PKGS = ["ssvnc", "lzop"]
Sam Chiu81bdc652018-06-29 18:45:08 +080043_LIST_OF_MODULES = ["kvm_intel", "kvm"]
Kevin Chengf756bbd2018-10-11 13:50:00 -070044_UPDATE_APT_GET_CMD = "sudo apt-get update"
Sam Chiu81bdc652018-06-29 18:45:08 +080045
46
Kevin Chengeb997272019-06-05 14:53:18 -070047class BasePkgInstaller(base_task_runner.BaseTaskRunner):
48 """Subtask base runner class for installing packages."""
Sam Chiu81bdc652018-06-29 18:45:08 +080049
Kevin Chengeb997272019-06-05 14:53:18 -070050 # List of packages for child classes to override.
51 PACKAGES = []
Sam Chiu81bdc652018-06-29 18:45:08 +080052
53 def ShouldRun(self):
54 """Check if required packages are all installed.
55
56 Returns:
57 Boolean, True if required packages are not installed.
58 """
Sam Chiu6c738d62018-12-04 10:29:02 +080059 if not utils.IsSupportedPlatform():
Sam Chiu81bdc652018-06-29 18:45:08 +080060 return False
61
62 # Any required package is not installed or not up-to-date will need to
63 # run installation task.
Kevin Chengeb997272019-06-05 14:53:18 -070064 for pkg_name in self.PACKAGES:
Sam Chiu81bdc652018-06-29 18:45:08 +080065 if not setup_common.PackageInstalled(pkg_name):
66 return True
67
68 return False
69
70 def _Run(self):
Kevin Chengeb997272019-06-05 14:53:18 -070071 """Install specified packages."""
Sam Chiu81bdc652018-06-29 18:45:08 +080072
Kevin Chengeb997272019-06-05 14:53:18 -070073 logger.info("Start to install package(s): %s ",
74 self.PACKAGES)
Sam Chiu81bdc652018-06-29 18:45:08 +080075
Kevin Chengf756bbd2018-10-11 13:50:00 -070076 setup_common.CheckCmdOutput(_UPDATE_APT_GET_CMD, shell=True)
Kevin Chengeb997272019-06-05 14:53:18 -070077 for pkg in self.PACKAGES:
Sam Chiu81bdc652018-06-29 18:45:08 +080078 setup_common.InstallPackage(pkg)
79
Kevin Chengeb997272019-06-05 14:53:18 -070080 logger.info("All package(s) installed now.")
81
82
83class AvdPkgInstaller(BasePkgInstaller):
84 """Subtask runner class for installing packages for local instances."""
85
86 WELCOME_MESSAGE_TITLE = ("Install required packages for host setup for "
87 "local instances")
88 WELCOME_MESSAGE = ("This step will walk you through the required packages "
89 "installation for running Android cuttlefish devices "
90 "on your host.")
91 PACKAGES = _AVD_REQUIRED_PKGS
92
93
94class HostBasePkgInstaller(BasePkgInstaller):
95 """Subtask runner class for installing base host packages."""
96
97 WELCOME_MESSAGE_TITLE = "Install base packages on the host"
98 WELCOME_MESSAGE = ("This step will walk you through the base packages "
99 "installation for your host.")
100 PACKAGES = _BASE_REQUIRED_PKGS
Sam Chiu81bdc652018-06-29 18:45:08 +0800101
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 """
Sam Chiu6c738d62018-12-04 10:29:02 +0800120 if not utils.IsSupportedPlatform():
Sam Chiu81bdc652018-06-29 18:45:08 +0800121 return False
122
herbertxue07293a32018-11-05 20:40:11 +0800123 return not (utils.CheckUserInGroups(constants.LIST_CF_USER_GROUPS)
Sam Chiu81bdc652018-06-29 18:45:08 +0800124 and self._CheckLoadedModules(_LIST_OF_MODULES))
125
126 @staticmethod
Sam Chiu81bdc652018-06-29 18:45:08 +0800127 def _CheckLoadedModules(module_list):
128 """Check if the modules are all in use.
129
130 Args:
131 module_list: The list of module name.
132 Returns:
133 True if all modules are in use.
134 """
135 logger.info("Checking if modules are loaded: %s", module_list)
136 lsmod_output = setup_common.CheckCmdOutput("lsmod", print_cmd=False)
137 current_modules = [r.split()[0] for r in lsmod_output.splitlines()]
138 all_modules_present = True
139 for module in module_list:
140 if module not in current_modules:
141 logger.info("missing module: %s", module)
142 all_modules_present = False
143 return all_modules_present
144
145 def _Run(self):
146 """Setup host environment for local cuttlefish instance support."""
147 # TODO: provide --uid args to let user use prefered username
148 username = getpass.getuser()
149 setup_cmds = [
150 "sudo rmmod kvm_intel",
151 "sudo rmmod kvm",
152 "sudo modprobe kvm",
153 "sudo modprobe kvm_intel"]
Sam Chiuafbc6582018-09-04 20:47:13 +0800154 for group in constants.LIST_CF_USER_GROUPS:
Sam Chiu81bdc652018-06-29 18:45:08 +0800155 setup_cmds.append("sudo usermod -aG %s % s" % (group, username))
156
157 print("Below commands will be run:")
158 for setup_cmd in setup_cmds:
159 print(setup_cmd)
160
161 if self._ConfirmContinue():
162 for setup_cmd in setup_cmds:
163 setup_common.CheckCmdOutput(setup_cmd, shell=True)
164 print("Host environment setup has done!")
165
166 @staticmethod
167 def _ConfirmContinue():
168 """Ask user if they want to continue.
169
170 Returns:
171 True if user answer yes.
172 """
173 answer_client = utils.InteractWithQuestion(
Sam Chiu705b9012019-01-19 12:11:35 +0800174 "\nPress 'y' to continue or anything else to do it myself[y/N]: ",
Sam Chiu81bdc652018-06-29 18:45:08 +0800175 utils.TextColors.WARNING)
176 return answer_client in constants.USER_ANSWER_YES