aboutsummaryrefslogtreecommitdiff
path: root/internal/lib/cheeps_compute_client.py
blob: 5ef344a8e02cabca97692aee0746740ef2946ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2019 - The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A client that manages Cheeps Virtual Device on compute engine.

** CheepsComputeClient **

CheepsComputeClient derives from AndroidComputeClient. It manges a google
compute engine project that is setup for running Cheeps Virtual Devices.
It knows how to create a host instance from a Cheeps Stable Host Image, fetch
Android build, and start Android within the host instance.

** Class hierarchy **

  base_cloud_client.BaseCloudApiClient
                ^
                |
       gcompute_client.ComputeClient
                ^
                |
       android_compute_client.AndroidComputeClient
                ^
                |
       cheeps_compute_client.CheepsComputeClient
"""

import getpass
import logging

from acloud import errors
from acloud.internal.lib import android_compute_client
from acloud.internal.lib import gcompute_client

logger = logging.getLogger(__name__)

class CheepsComputeClient(android_compute_client.AndroidComputeClient):
    """Client that manages Cheeps based Android Virtual Device.

    Cheeps is a VM that run Chrome OS which runs on GCE.
    """
    # This is the timeout for betty to start.
    BOOT_TIMEOUT_SECS = 10*60
    # This is printed by betty.sh.
    BOOT_COMPLETED_MSG = "VM successfully started"
    # systemd prints this if betty.sh returns nonzero status code.
    BOOT_FAILED_MSG = "betty.service: Failed with result 'exit-code'"

    def CheckBootFailure(self, serial_out, instance):
        """Overrides superclass. Determines if there's a boot failure."""
        if self.BOOT_FAILED_MSG in serial_out:
            raise errors.DeviceBootError("Betty failed to start")

    # pylint: disable=too-many-locals,arguments-differ
    def CreateInstance(self, instance, image_name, image_project,
                       build_id=None):
        """ Creates a cheeps instance in GCE.

        Args:
            instance: name of the VM
            image_name: the GCE image to use
            image_project: project the GCE image is in
            build_id: (optional) the Android build id to use. To specify a
                different betty image you should use a different image_name
        """
        metadata = self._metadata.copy()
        if build_id:
            metadata['android_build_id'] = build_id

        # Add per-instance ssh key
        if self._ssh_public_key_path:
            rsa = self._LoadSshPublicKey(self._ssh_public_key_path)
            logger.info("ssh_public_key_path is specified in config: %s, "
                        "will add the key to the instance.",
                        self._ssh_public_key_path)
            metadata["sshKeys"] = "%s:%s" % (getpass.getuser(), rsa)
        else:
            logger.warning("ssh_public_key_path is not specified in config, "
                           "only project-wide key will be effective.")

        gcompute_client.ComputeClient.CreateInstance(
            self,
            instance=instance,
            image_name=image_name,
            image_project=image_project,
            disk_args=None,
            metadata=metadata,
            machine_type=self._machine_type,
            network=self._network,
            zone=self._zone,
        )