blob: a47606b3fc92ed7230cd84e483669dba618cd2e5 [file] [log] [blame]
Kevin Cheng3ce4b452018-08-23 14:47:22 -07001#!/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"""LocalImageLocalInstance class.
17
18Create class that is responsible for creating a local instance AVD with a
19local image.
20"""
21
Sam Chiuafbc6582018-09-04 20:47:13 +080022from __future__ import print_function
23import logging
24import os
25import subprocess
herbertxue61007092018-10-10 11:56:57 +080026import sys
Sam Chiuafbc6582018-09-04 20:47:13 +080027import time
28
29from acloud import errors
Kevin Cheng3ce4b452018-08-23 14:47:22 -070030from acloud.create import base_avd_create
Sam Chiuafbc6582018-09-04 20:47:13 +080031from acloud.create import create_common
32from acloud.internal import constants
33from acloud.internal.lib import utils
34from acloud.setup import host_setup_runner
35
36logger = logging.getLogger(__name__)
37
38_BOOT_COMPLETE = "VIRTUAL_DEVICE_BOOT_COMPLETED"
Sam Chiuafbc6582018-09-04 20:47:13 +080039# TODO(b/117366819): Currently --serial_number is not working.
40_CMD_LAUNCH_CVD_ARGS = (" --daemon --cpus %s --x_res %s --y_res %s --dpi %s "
41 "--memory_mb %s --blank_data_image_mb %s "
42 "--data_policy always_create "
43 "--system_image_dir %s "
44 "--vnc_server_port %s "
45 "--serial_number %s")
46_CMD_PGREP = "pgrep"
47_CMD_SG = "sg "
48_CMD_STOP_CVD = "stop_cvd"
49_CONFIRM_RELAUNCH = ("\nCuttlefish AVD is already running. \nPress 'y' to "
50 "terminate current instance and launch new instance \nor "
51 "anything else to exit out.")
52_CVD_SERIAL_PREFIX = "acloudCF"
53_ENV_ANDROID_HOST_OUT = "ANDROID_HOST_OUT"
Kevin Cheng3ce4b452018-08-23 14:47:22 -070054
55
56class LocalImageLocalInstance(base_avd_create.BaseAVDCreate):
57 """Create class for a local image local instance AVD."""
58
Kevin Cheng3ce4b452018-08-23 14:47:22 -070059 def Create(self, avd_spec):
60 """Create the AVD.
61
62 Args:
63 avd_spec: AVDSpec object that tells us what we're going to create.
64 """
Sam Chiuafbc6582018-09-04 20:47:13 +080065 self.PrintAvdDetails(avd_spec)
66 start = time.time()
67
68 local_image_path, launch_cvd_path = self.GetImageArtifactsPath(avd_spec)
69
70 cmd = self.PrepareLaunchCVDCmd(launch_cvd_path,
71 avd_spec.hw_property,
72 local_image_path,
73 avd_spec.flavor)
74 try:
herbertxue61007092018-10-10 11:56:57 +080075 self.CheckLaunchCVD(cmd, os.path.dirname(launch_cvd_path))
Sam Chiuafbc6582018-09-04 20:47:13 +080076 except errors.LaunchCVDFail as launch_error:
77 raise launch_error
78
79 utils.PrintColorString("\n")
80 utils.PrintColorString("Total time: %ds" % (time.time() - start),
81 utils.TextColors.WARNING)
82 # TODO(b/117366819): Should display the correct device serial
83 # according to the args --serial_number.
cylan4b8eacf2018-10-11 11:13:13 +080084 utils.PrintColorString("Device serial: %s" %
85 (constants.LOCALHOST_ADB_SERIAL %
Kevin Cheng065fdd62018-10-09 14:27:22 -070086 constants.DEFAULT_ADB_PORT),
Sam Chiuafbc6582018-09-04 20:47:13 +080087 utils.TextColors.WARNING)
88 if avd_spec.autoconnect:
89 self.LaunchVncClient()
90
91
92 @staticmethod
93 def GetImageArtifactsPath(avd_spec):
94 """Get image artifacts path.
95
96 This method will check if local image and launch_cvd are exist and
97 return the tuple path where they are located respectively.
98 For remote image, RemoteImageLocalInstance will override this method and
99 return the artifacts path which is extracted and downloaded from remote.
100
101 Args:
102 avd_spec: AVDSpec object that tells us what we're going to create.
103
104 Returns:
105 Tuple of (local image file, launch_cvd package) paths.
106 """
107 try:
108 # Check if local image is exist.
109 create_common.VerifyLocalImageArtifactsExist(
110 avd_spec.local_image_dir)
111
112 # TODO(b/117306227): help user to build out images and host package if
113 # anything needed is not found.
114 except errors.GetLocalImageError as imgerror:
115 logger.error(imgerror.message)
116 raise imgerror
117
118 # Check if launch_cvd is exist.
119 launch_cvd_path = os.path.join(
herbertxue61007092018-10-10 11:56:57 +0800120 os.environ.get(_ENV_ANDROID_HOST_OUT), "bin", constants.CMD_LAUNCH_CVD)
Sam Chiuafbc6582018-09-04 20:47:13 +0800121 if not os.path.exists(launch_cvd_path):
122 raise errors.GetCvdLocalHostPackageError(
123 "No launch_cvd found. Please run \"m launch_cvd\" first")
124
125 return avd_spec.local_image_dir, launch_cvd_path
126
127 @staticmethod
128 def PrepareLaunchCVDCmd(launch_cvd_path, hw_property, system_image_dir,
129 flavor):
130 """Prepare launch_cvd command.
131
132 Combine whole launch_cvd cmd including the hw property options and login
133 as the required groups if need. The reason using here-doc instead of
134 ampersand sign is all operations need to be ran in ths same pid.
135 The example of cmd:
136 $ sg kvm << EOF
137 sg libvirt
138 sg cvdnetwork
139 launch_cvd --cpus 2 --x_res 1280 --y_res 720 --dpi 160 --memory_mb 4096
140 EOF
141
142 Args:
143 launch_cvd_path: String of launch_cvd path.
144 hw_property: dict object of hw property.
145 system_image_dir: String of local images path.
146 flavor: String of flavor type.
147
148 Returns:
149 String, launch_cvd cmd.
150 """
151 launch_cvd_w_args = launch_cvd_path + _CMD_LAUNCH_CVD_ARGS % (
152 hw_property["cpu"], hw_property["x_res"], hw_property["y_res"],
153 hw_property["dpi"], hw_property["memory"], hw_property["disk"],
cylan66713722018-10-06 01:38:26 +0800154 system_image_dir, constants.DEFAULT_VNC_PORT,
155 _CVD_SERIAL_PREFIX+flavor)
Sam Chiuafbc6582018-09-04 20:47:13 +0800156
157 combined_launch_cmd = ""
158 host_setup = host_setup_runner.CuttlefishHostSetup()
159 if not host_setup.CheckUserInGroups(constants.LIST_CF_USER_GROUPS):
160 # As part of local host setup to enable local instance support,
161 # the user is added to certain groups. For those settings to
162 # take effect systemwide requires the user to log out and
163 # log back in. In the scenario where the user has run setup and
164 # hasn't logged out, we still want them to be able to launch a
165 # local instance so add the user to the groups as part of the
166 # command to ensure success.
167 logger.debug("User group is not ready for cuttlefish")
168 for idx, group in enumerate(constants.LIST_CF_USER_GROUPS):
169 combined_launch_cmd += _CMD_SG + group
170 if idx == 0:
171 combined_launch_cmd += " <<EOF\n"
172 else:
173 combined_launch_cmd += "\n"
174 launch_cvd_w_args += "\nEOF"
175
176 combined_launch_cmd += launch_cvd_w_args
177 logger.debug("launch_cvd cmd:\n %s", combined_launch_cmd)
178 return combined_launch_cmd
179
herbertxue61007092018-10-10 11:56:57 +0800180 @utils.TimeExecute(function_description="Waiting for AVD(s) to boot up")
181 def CheckLaunchCVD(self, cmd, host_pack_dir):
Sam Chiuafbc6582018-09-04 20:47:13 +0800182 """Execute launch_cvd command and wait for boot up completed.
183
184 Args:
185 cmd: String, launch_cvd command.
herbertxue61007092018-10-10 11:56:57 +0800186 host_pack_dir: String of host package directory.
Sam Chiuafbc6582018-09-04 20:47:13 +0800187 """
Sam Chiuafbc6582018-09-04 20:47:13 +0800188 # Cuttlefish support launch single AVD at one time currently.
189 if self._IsLaunchCVDInUse():
190 logger.info("Cuttlefish AVD is already running.")
191 if utils.GetUserAnswerYes(_CONFIRM_RELAUNCH):
herbertxue61007092018-10-10 11:56:57 +0800192 stop_cvd_cmd = os.path.join(host_pack_dir, _CMD_STOP_CVD)
193 with open(os.devnull, "w") as dev_null:
194 subprocess.check_call(stop_cvd_cmd, stderr=dev_null,
195 stdout=dev_null)
Sam Chiuafbc6582018-09-04 20:47:13 +0800196
herbertxue61007092018-10-10 11:56:57 +0800197 else:
198 print("Exiting out")
199 sys.exit()
Sam Chiuafbc6582018-09-04 20:47:13 +0800200
201 process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
202 stderr=subprocess.STDOUT)
203
204 boot_complete = False
205 for line in iter(process.stdout.readline, b''):
206 logger.debug(line.strip())
207 # cvd is still running and got boot complete.
208 if _BOOT_COMPLETE in line:
Sam Chiuafbc6582018-09-04 20:47:13 +0800209 boot_complete = True
210 break
211
212 if not boot_complete:
Sam Chiuafbc6582018-09-04 20:47:13 +0800213 raise errors.LaunchCVDFail(
214 "Can't launch cuttlefish AVD. No %s found" % _BOOT_COMPLETE)
215
216 @staticmethod
217 def _IsLaunchCVDInUse():
218 """Check if launch_cvd is running.
219
220 Returns:
221 Boolean, True if launch_cvd is running. False otherwise.
222 """
223 try:
herbertxue61007092018-10-10 11:56:57 +0800224 with open(os.devnull, "w") as dev_null:
225 subprocess.check_call([_CMD_PGREP, constants.CMD_LAUNCH_CVD],
226 stderr=dev_null, stdout=dev_null)
Sam Chiuafbc6582018-09-04 20:47:13 +0800227 return True
228 except subprocess.CalledProcessError:
229 # launch_cvd process is not in use.
230 return False