xref: /aosp_15_r20/tools/acloud/internal/lib/cheeps_compute_client.py (revision 800a58d989c669b8eb8a71d8df53b1ba3d411444)
1*800a58d9SAndroid Build Coastguard Worker# Copyright 2019 - The Android Open Source Project
2*800a58d9SAndroid Build Coastguard Worker#
3*800a58d9SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*800a58d9SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*800a58d9SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*800a58d9SAndroid Build Coastguard Worker#
7*800a58d9SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*800a58d9SAndroid Build Coastguard Worker#
9*800a58d9SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*800a58d9SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*800a58d9SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*800a58d9SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*800a58d9SAndroid Build Coastguard Worker# limitations under the License.
14*800a58d9SAndroid Build Coastguard Worker"""A client that manages Cheeps Virtual Device on compute engine.
15*800a58d9SAndroid Build Coastguard Worker
16*800a58d9SAndroid Build Coastguard Worker** CheepsComputeClient **
17*800a58d9SAndroid Build Coastguard Worker
18*800a58d9SAndroid Build Coastguard WorkerCheepsComputeClient derives from AndroidComputeClient. It manges a google
19*800a58d9SAndroid Build Coastguard Workercompute engine project that is setup for running Cheeps Virtual Devices.
20*800a58d9SAndroid Build Coastguard WorkerIt knows how to create a host instance from a Cheeps Stable Host Image, fetch
21*800a58d9SAndroid Build Coastguard WorkerAndroid build, and start Android within the host instance.
22*800a58d9SAndroid Build Coastguard Worker
23*800a58d9SAndroid Build Coastguard Worker** Class hierarchy **
24*800a58d9SAndroid Build Coastguard Worker
25*800a58d9SAndroid Build Coastguard Worker  base_cloud_client.BaseCloudApiClient
26*800a58d9SAndroid Build Coastguard Worker                ^
27*800a58d9SAndroid Build Coastguard Worker                |
28*800a58d9SAndroid Build Coastguard Worker       gcompute_client.ComputeClient
29*800a58d9SAndroid Build Coastguard Worker                ^
30*800a58d9SAndroid Build Coastguard Worker                |
31*800a58d9SAndroid Build Coastguard Worker       android_compute_client.AndroidComputeClient
32*800a58d9SAndroid Build Coastguard Worker                ^
33*800a58d9SAndroid Build Coastguard Worker                |
34*800a58d9SAndroid Build Coastguard Worker       cheeps_compute_client.CheepsComputeClient
35*800a58d9SAndroid Build Coastguard Worker"""
36*800a58d9SAndroid Build Coastguard Worker
37*800a58d9SAndroid Build Coastguard Workerimport logging
38*800a58d9SAndroid Build Coastguard Worker
39*800a58d9SAndroid Build Coastguard Workerfrom acloud import errors
40*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal import constants
41*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import android_compute_client
42*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import gcompute_client
43*800a58d9SAndroid Build Coastguard Worker
44*800a58d9SAndroid Build Coastguard Worker
45*800a58d9SAndroid Build Coastguard Workerlogger = logging.getLogger(__name__)
46*800a58d9SAndroid Build Coastguard Worker
47*800a58d9SAndroid Build Coastguard Worker
48*800a58d9SAndroid Build Coastguard Workerclass CheepsComputeClient(android_compute_client.AndroidComputeClient):
49*800a58d9SAndroid Build Coastguard Worker    """Client that manages Cheeps based Android Virtual Device.
50*800a58d9SAndroid Build Coastguard Worker
51*800a58d9SAndroid Build Coastguard Worker    Cheeps is a VM that run Chrome OS which runs on GCE.
52*800a58d9SAndroid Build Coastguard Worker    """
53*800a58d9SAndroid Build Coastguard Worker    # This is the timeout for betty to start.
54*800a58d9SAndroid Build Coastguard Worker    BOOT_TIMEOUT_SECS = 15*60
55*800a58d9SAndroid Build Coastguard Worker    # This is printed by betty.sh.
56*800a58d9SAndroid Build Coastguard Worker    BOOT_COMPLETED_MSG = "VM successfully started"
57*800a58d9SAndroid Build Coastguard Worker    # systemd prints this if betty.sh returns nonzero status code.
58*800a58d9SAndroid Build Coastguard Worker    BOOT_FAILED_MSG = "betty.service: Failed with result 'exit-code'"
59*800a58d9SAndroid Build Coastguard Worker
60*800a58d9SAndroid Build Coastguard Worker    def CheckBootFailure(self, serial_out, instance):
61*800a58d9SAndroid Build Coastguard Worker        """Overrides superclass. Determines if there's a boot failure."""
62*800a58d9SAndroid Build Coastguard Worker        if self.BOOT_FAILED_MSG in serial_out:
63*800a58d9SAndroid Build Coastguard Worker            raise errors.DeviceBootError("Betty failed to start")
64*800a58d9SAndroid Build Coastguard Worker
65*800a58d9SAndroid Build Coastguard Worker    # pylint: disable=too-many-locals,arguments-differ
66*800a58d9SAndroid Build Coastguard Worker    def CreateInstance(self, instance, image_name, image_project, avd_spec):
67*800a58d9SAndroid Build Coastguard Worker        """ Creates a cheeps instance in GCE.
68*800a58d9SAndroid Build Coastguard Worker
69*800a58d9SAndroid Build Coastguard Worker        Args:
70*800a58d9SAndroid Build Coastguard Worker            instance: name of the VM
71*800a58d9SAndroid Build Coastguard Worker            image_name: the GCE image to use
72*800a58d9SAndroid Build Coastguard Worker            image_project: project the GCE image is in
73*800a58d9SAndroid Build Coastguard Worker            avd_spec: An AVDSpec instance.
74*800a58d9SAndroid Build Coastguard Worker        """
75*800a58d9SAndroid Build Coastguard Worker        metadata = self._metadata.copy()
76*800a58d9SAndroid Build Coastguard Worker        metadata[constants.INS_KEY_AVD_TYPE] = constants.TYPE_CHEEPS
77*800a58d9SAndroid Build Coastguard Worker        # Update metadata by avd_spec
78*800a58d9SAndroid Build Coastguard Worker        if avd_spec:
79*800a58d9SAndroid Build Coastguard Worker            metadata["cvd_01_x_res"] = avd_spec.hw_property[constants.HW_X_RES]
80*800a58d9SAndroid Build Coastguard Worker            metadata["cvd_01_y_res"] = avd_spec.hw_property[constants.HW_Y_RES]
81*800a58d9SAndroid Build Coastguard Worker            metadata["cvd_01_dpi"] = avd_spec.hw_property[constants.HW_ALIAS_DPI]
82*800a58d9SAndroid Build Coastguard Worker            metadata[constants.INS_KEY_DISPLAY] = ("%sx%s (%s)" % (
83*800a58d9SAndroid Build Coastguard Worker                avd_spec.hw_property[constants.HW_X_RES],
84*800a58d9SAndroid Build Coastguard Worker                avd_spec.hw_property[constants.HW_Y_RES],
85*800a58d9SAndroid Build Coastguard Worker                avd_spec.hw_property[constants.HW_ALIAS_DPI]))
86*800a58d9SAndroid Build Coastguard Worker
87*800a58d9SAndroid Build Coastguard Worker            if avd_spec.username:
88*800a58d9SAndroid Build Coastguard Worker                metadata["user"] = avd_spec.username
89*800a58d9SAndroid Build Coastguard Worker                metadata["password"] = avd_spec.password
90*800a58d9SAndroid Build Coastguard Worker
91*800a58d9SAndroid Build Coastguard Worker            metadata["android_build_id"] = avd_spec.remote_image[constants.BUILD_ID]
92*800a58d9SAndroid Build Coastguard Worker            metadata["android_build_target"] = avd_spec.remote_image[constants.BUILD_TARGET]
93*800a58d9SAndroid Build Coastguard Worker            metadata["betty_image"] = avd_spec.cheeps_betty_image
94*800a58d9SAndroid Build Coastguard Worker            metadata["cheeps_features"] = ','.join(avd_spec.cheeps_features)
95*800a58d9SAndroid Build Coastguard Worker
96*800a58d9SAndroid Build Coastguard Worker            if avd_spec.connect_hostname:
97*800a58d9SAndroid Build Coastguard Worker                self._gce_hostname = gcompute_client.GetGCEHostName(
98*800a58d9SAndroid Build Coastguard Worker                    self._project, instance, self._zone)
99*800a58d9SAndroid Build Coastguard Worker
100*800a58d9SAndroid Build Coastguard Worker        gcompute_client.ComputeClient.CreateInstance(
101*800a58d9SAndroid Build Coastguard Worker            self,
102*800a58d9SAndroid Build Coastguard Worker            instance=instance,
103*800a58d9SAndroid Build Coastguard Worker            image_name=image_name,
104*800a58d9SAndroid Build Coastguard Worker            image_project=image_project,
105*800a58d9SAndroid Build Coastguard Worker            disk_type='pd-balanced',
106*800a58d9SAndroid Build Coastguard Worker            metadata=metadata,
107*800a58d9SAndroid Build Coastguard Worker            machine_type=self._machine_type,
108*800a58d9SAndroid Build Coastguard Worker            network=self._network,
109*800a58d9SAndroid Build Coastguard Worker            zone=self._zone)
110