# Copyright 2018 - 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. r"""Instance class. Define the instance class used to hold details about an AVD instance. The instance class will hold details about AVD instances (remote/local) used to enable users to understand what instances they've created. This will be leveraged for the list, delete, and reconnect commands. The details include: - instance name (for remote instances) - creation date/instance duration - instance image details (branch/target/build id) - and more! """ import collections import datetime import json import logging import os import re import subprocess import tempfile # pylint: disable=import-error,too-many-lines import dateutil.parser import dateutil.tz from acloud.create import local_image_local_instance from acloud.internal import constants from acloud.internal.lib import cvd_runtime_config from acloud.internal.lib import utils from acloud.internal.lib.adb_tools import AdbTools from acloud.internal.lib.local_instance_lock import LocalInstanceLock from acloud.internal.lib.gcompute_client import GetInstanceIP from acloud.internal.lib.gcompute_client import GetGCEHostName logger = logging.getLogger(__name__) _ACLOUD_CVD_TEMP = os.path.join(tempfile.gettempdir(), "acloud_cvd_temp") _CFG_KEY_INSTANCES = "instances" _CVD_RUNTIME_FOLDER_NAME = "cuttlefish_runtime" _CVD_BIN = "cvd" _CVD_BIN_FOLDER = "host_bins/bin" _CVD_STATUS_BIN = "cvd_status" _CVD_STOP_ERROR_KEYWORDS = "cvd_internal_stop E" # Default timeout 30 secs for cvd commands. _CVD_TIMEOUT = 30 # Keywords read from runtime config. _ADB_HOST_PORT = "adb_host_port" # Keywords read from the output of "cvd status". _DISPLAYS = "displays" _WEBRTC_PORT = "webrtc_port" _ADB_SERIAL = "adb_serial" _INSTANCE_ASSEMBLY_DIR = "cuttlefish_assembly" _LOCAL_INSTANCE_NAME_FORMAT = "local-instance-%(id)d" _LOCAL_INSTANCE_NAME_PATTERN = re.compile(r"^local-instance-(?P\d+)$") _ACLOUDWEB_INSTANCE_START_STRING = "cf-" _MSG_UNABLE_TO_CALCULATE = "Unable to calculate" _NO_ANDROID_ENV = "android source not available" _RE_GROUP_ADB = "local_adb_port" _RE_GROUP_VNC = "local_vnc_port" _RE_SSH_TUNNEL_PATTERN = (r"((.*\s*-L\s)(?P<%s>\d+):127.0.0.1:%s)" r"((.*\s*-L\s)(?P<%s>\d+):127.0.0.1:%s)" r"(.+(%s|%s))") _RE_TIMEZONE = re.compile(r"^(?P