1# Copyright 2022 - 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. 14 15"""This module implements the classes and functions needed for the common 16creation flow.""" 17 18import time 19 20from acloud.internal import constants 21from acloud.internal.lib import ssh 22from acloud.public import config 23 24 25# Report keys 26_VERSION = "version" 27 28 29class RemoteHostClient: 30 """A client that manages an instance on a remote host. 31 32 Attributes: 33 ip_addr: A string, the IP address of the host. 34 execution_time: A dictionary that records the execution time. The 35 possible keys are defined as TIME_* in constants.py. 36 stage: An integer. The possible values are defined as STAGE_* in 37 constants.py. 38 """ 39 40 def __init__(self, ip_addr): 41 """Initialize the attribtues.""" 42 self._ip_addr = ip_addr 43 self._execution_time = {} 44 self._stage = constants.STAGE_INIT 45 46 def RecordTime(self, key, start_time): 47 """Record the interval between the start time and the current time. 48 49 Args: 50 key: A string, the stage name. 51 start_time: A float, the timestamp when the stage starts. 52 53 Returns: 54 A float, the current time. 55 """ 56 current = time.time() 57 self._execution_time[key] = current - start_time 58 return current 59 60 def SetStage(self, stage): 61 """Set device creation progress.""" 62 self._stage = stage 63 64 # The following methods are called by common_operations.py. 65 def GetInstanceIP(self, _instance_name): 66 """Return the IP address of the host.""" 67 return ssh.IP(ip=self._ip_addr) 68 69 @staticmethod 70 def WaitForBoot(_instance_name, _boot_timeout_secs): 71 """Should not be called in the common creation flow.""" 72 raise NotImplementedError("The common creation flow should call " 73 "GetFailures instead of this method.") 74 75 @staticmethod 76 def GetSerialPortOutput(): 77 """Remote hosts do not support serial log.""" 78 return "" 79 80 @property 81 def execution_time(self): 82 """Return execution_time.""" 83 return self._execution_time 84 85 @property 86 def stage(self): 87 """Return stage.""" 88 return self._stage 89 90 @property 91 def dict_report(self): 92 """Return the key-value pairs to be written to the report.""" 93 return {_VERSION: config.GetVersion()} 94