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 openwrt: A boolean, whether the openwrt device is created. 39 dict_report: A dict containing the data to be written to the report. 40 """ 41 42 def __init__(self, ip_addr): 43 """Initialize the attribtues.""" 44 self._ip_addr = ip_addr 45 self._execution_time = {} 46 self._stage = constants.STAGE_INIT 47 self._openwrt = False 48 self._dict_report = {_VERSION: config.GetVersion()} 49 50 def RecordTime(self, key, start_time): 51 """Record the interval between the start time and the current time. 52 53 Args: 54 key: A string, the stage name. 55 start_time: A float, the timestamp when the stage starts. 56 57 Returns: 58 A float, the current time. 59 """ 60 current = time.time() 61 self._execution_time[key] = current - start_time 62 return current 63 64 def SetStage(self, stage): 65 """Set device creation progress.""" 66 self._stage = stage 67 68 def ExtendReportData(self, key, value): 69 """Add a key-value pair to the report.""" 70 self._dict_report[key] = value 71 72 # The following methods are called by common_operations.py. 73 def GetInstanceIP(self, _instance_name): 74 """Return the IP address of the host.""" 75 return ssh.IP(ip=self._ip_addr) 76 77 @staticmethod 78 def WaitForBoot(_instance_name, _boot_timeout_secs): 79 """Should not be called in the common creation flow.""" 80 raise NotImplementedError("The common creation flow should call " 81 "GetFailures instead of this method.") 82 83 @staticmethod 84 def GetSerialPortOutput(): 85 """Remote hosts do not support serial log.""" 86 return "" 87 88 @property 89 def execution_time(self): 90 """Return execution_time.""" 91 return self._execution_time 92 93 @property 94 def stage(self): 95 """Return stage.""" 96 return self._stage 97 98 @property 99 def openwrt(self): 100 """Return openwrt.""" 101 return self._openwrt 102 103 @openwrt.setter 104 def openwrt(self, value): 105 """Set openwrt.""" 106 self._openwrt = value 107 108 @property 109 def dict_report(self): 110 """Return the key-value pairs to be written to the report.""" 111 return self._dict_report 112