1# Copyright 2019 - The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14r"""CheepsRemoteImageRemoteInstance class. 15 16Create class that is responsible for creating a cheeps remote instance AVD with 17a remote image. 18""" 19import logging 20 21from acloud.create import base_avd_create 22from acloud.internal import constants 23from acloud.internal.lib import auth 24from acloud.internal.lib import cheeps_compute_client 25from acloud.internal.lib import utils 26from acloud.public.actions import base_device_factory 27from acloud.public.actions import common_operations 28 29 30logger = logging.getLogger(__name__) 31 32 33class CheepsRemoteImageRemoteInstance(base_avd_create.BaseAVDCreate): 34 """Create class for a Cheeps remote image remote instance AVD.""" 35 36 @utils.TimeExecute(function_description="Total time: ", 37 print_before_call=False, print_status=False) 38 def _CreateAVD(self, avd_spec, no_prompts): 39 """Create the AVD. 40 41 Args: 42 avd_spec: AVDSpec object that tells us what we're going to create. 43 no_prompts: Boolean, True to skip all prompts. 44 45 Returns: 46 A Report instance. 47 """ 48 logger.info( 49 "Creating a cheeps device in project %s, build_id: %s", 50 avd_spec.cfg.project, avd_spec.remote_image[constants.BUILD_ID]) 51 52 device_factory = CheepsDeviceFactory(avd_spec.cfg, avd_spec) 53 54 report = common_operations.CreateDevices( 55 command="create_cheeps", 56 cfg=avd_spec.cfg, 57 device_factory=device_factory, 58 num=avd_spec.num, 59 report_internal_ip=avd_spec.report_internal_ip, 60 autoconnect=avd_spec.autoconnect, 61 avd_type=constants.TYPE_CHEEPS, 62 client_adb_port=avd_spec.client_adb_port, 63 boot_timeout_secs=avd_spec.boot_timeout_secs) 64 65 # Launch vnc client if we're auto-connecting. 66 if avd_spec.connect_vnc: 67 utils.LaunchVNCFromReport(report, avd_spec, no_prompts) 68 69 return report 70 71 72class CheepsDeviceFactory(base_device_factory.BaseDeviceFactory): 73 """A class that can produce a cheeps device. 74 75 Attributes: 76 _cfg: An AcloudConfig instance. 77 78 """ 79 LOG_FILES = [] 80 81 def __init__(self, cfg, avd_spec=None): 82 """Initialize. 83 84 Args: 85 cfg: An AcloudConfig instance. 86 avd_spec: An AVDSpec instance. 87 """ 88 self.credentials = auth.CreateCredentials(cfg) 89 90 compute_client = cheeps_compute_client.CheepsComputeClient( 91 cfg, self.credentials) 92 super(CheepsDeviceFactory, self).__init__(compute_client) 93 94 self._cfg = cfg 95 self._avd_spec = avd_spec 96 97 def GetBuildInfoDict(self): 98 """Get build info dictionary. 99 100 Returns: 101 A build info dictionary. 102 """ 103 return {"build_id": self._avd_spec.remote_image[constants.BUILD_ID]} 104 105 def CreateInstance(self): 106 """Creates single configured cheeps device. 107 108 Returns: 109 String, the name of created instance. 110 """ 111 instance = self._compute_client.GenerateInstanceName( 112 build_id=self._avd_spec.remote_image[constants.BUILD_ID], 113 build_target=self._avd_spec.remote_image[constants.BUILD_TARGET]) 114 115 # Cheeps image specified through args (if any) overrides that in the 116 # Acloud config file. 117 image_name = (self._avd_spec.stable_cheeps_host_image_name or 118 self._cfg.stable_cheeps_host_image_name) 119 image_project = (self._avd_spec.stable_cheeps_host_image_project or 120 self._cfg.stable_cheeps_host_image_project) 121 if not (image_name and image_project): 122 raise ValueError( 123 "Both Cheeps image name and project should be set, either in " 124 "Acloud config or via command line args.") 125 126 self._compute_client.CreateInstance( 127 instance=instance, 128 image_name=image_name, 129 image_project=image_project, 130 avd_spec=self._avd_spec) 131 return instance 132