1*800a58d9SAndroid Build Coastguard Worker# Copyright 2021 - The Android Open Source Project 2*800a58d9SAndroid Build Coastguard Worker# 3*800a58d9SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*800a58d9SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*800a58d9SAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*800a58d9SAndroid Build Coastguard Worker# 7*800a58d9SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*800a58d9SAndroid Build Coastguard Worker# 9*800a58d9SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*800a58d9SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*800a58d9SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*800a58d9SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*800a58d9SAndroid Build Coastguard Worker# limitations under the License. 14*800a58d9SAndroid Build Coastguard Workerr"""Restart entry point. 15*800a58d9SAndroid Build Coastguard Worker 16*800a58d9SAndroid Build Coastguard WorkerThis command will restart the CF AVD from a remote instance. 17*800a58d9SAndroid Build Coastguard Worker""" 18*800a58d9SAndroid Build Coastguard Worker 19*800a58d9SAndroid Build Coastguard Workerimport logging 20*800a58d9SAndroid Build Coastguard Workerimport subprocess 21*800a58d9SAndroid Build Coastguard Workerimport sys 22*800a58d9SAndroid Build Coastguard Worker 23*800a58d9SAndroid Build Coastguard Workerfrom acloud import errors 24*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal import constants 25*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import utils 26*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib.ssh import Ssh 27*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib.ssh import IP 28*800a58d9SAndroid Build Coastguard Workerfrom acloud.list import list as list_instances 29*800a58d9SAndroid Build Coastguard Workerfrom acloud.powerwash import powerwash 30*800a58d9SAndroid Build Coastguard Workerfrom acloud.public import config 31*800a58d9SAndroid Build Coastguard Workerfrom acloud.public import report 32*800a58d9SAndroid Build Coastguard Workerfrom acloud.reconnect import reconnect 33*800a58d9SAndroid Build Coastguard Worker 34*800a58d9SAndroid Build Coastguard Worker 35*800a58d9SAndroid Build Coastguard Workerlogger = logging.getLogger(__name__) 36*800a58d9SAndroid Build Coastguard Worker_NOT_SUPPORT_MSG = ("Currently the restart function doesn't support local " 37*800a58d9SAndroid Build Coastguard Worker "instances. Please try to create one new instance.") 38*800a58d9SAndroid Build Coastguard Worker 39*800a58d9SAndroid Build Coastguard Worker 40*800a58d9SAndroid Build Coastguard Workerdef RestartFromInstance(cfg, instance, instance_id, powerwash_data): 41*800a58d9SAndroid Build Coastguard Worker """Restart AVD from remote CF instance. 42*800a58d9SAndroid Build Coastguard Worker 43*800a58d9SAndroid Build Coastguard Worker Args: 44*800a58d9SAndroid Build Coastguard Worker cfg: AcloudConfig object. 45*800a58d9SAndroid Build Coastguard Worker instance: list.Instance() object. 46*800a58d9SAndroid Build Coastguard Worker instance_id: Integer of the instance id. 47*800a58d9SAndroid Build Coastguard Worker powerwash_data: Boolean, True to powerwash AVD data. 48*800a58d9SAndroid Build Coastguard Worker 49*800a58d9SAndroid Build Coastguard Worker Returns: 50*800a58d9SAndroid Build Coastguard Worker A Report instance. 51*800a58d9SAndroid Build Coastguard Worker """ 52*800a58d9SAndroid Build Coastguard Worker ssh = Ssh(ip=IP(ip=instance.ip), 53*800a58d9SAndroid Build Coastguard Worker user=constants.GCE_USER, 54*800a58d9SAndroid Build Coastguard Worker ssh_private_key_path=cfg.ssh_private_key_path, 55*800a58d9SAndroid Build Coastguard Worker extra_args_ssh_tunnel=cfg.extra_args_ssh_tunnel) 56*800a58d9SAndroid Build Coastguard Worker logger.info("Start to restart AVD id (%s) from the instance: %s.", 57*800a58d9SAndroid Build Coastguard Worker instance_id, instance.name) 58*800a58d9SAndroid Build Coastguard Worker if powerwash_data: 59*800a58d9SAndroid Build Coastguard Worker powerwash.PowerwashDevice(ssh, instance_id) 60*800a58d9SAndroid Build Coastguard Worker else: 61*800a58d9SAndroid Build Coastguard Worker RestartDevice(ssh, instance_id) 62*800a58d9SAndroid Build Coastguard Worker reconnect.ReconnectInstance(cfg.ssh_private_key_path, 63*800a58d9SAndroid Build Coastguard Worker instance, 64*800a58d9SAndroid Build Coastguard Worker report.Report(command="reconnect"), 65*800a58d9SAndroid Build Coastguard Worker cfg.extra_args_ssh_tunnel) 66*800a58d9SAndroid Build Coastguard Worker return report.Report(command="restart") 67*800a58d9SAndroid Build Coastguard Worker 68*800a58d9SAndroid Build Coastguard Worker 69*800a58d9SAndroid Build Coastguard Worker@utils.TimeExecute(function_description="Waiting for AVD to restart") 70*800a58d9SAndroid Build Coastguard Workerdef RestartDevice(ssh, instance_id): 71*800a58d9SAndroid Build Coastguard Worker """Restart AVD with the instance id. 72*800a58d9SAndroid Build Coastguard Worker 73*800a58d9SAndroid Build Coastguard Worker Args: 74*800a58d9SAndroid Build Coastguard Worker ssh: Ssh object. 75*800a58d9SAndroid Build Coastguard Worker instance_id: Integer of the instance id. 76*800a58d9SAndroid Build Coastguard Worker """ 77*800a58d9SAndroid Build Coastguard Worker ssh_command = "./bin/restart_cvd --instance_num=%d" % (instance_id) 78*800a58d9SAndroid Build Coastguard Worker try: 79*800a58d9SAndroid Build Coastguard Worker ssh.Run(ssh_command) 80*800a58d9SAndroid Build Coastguard Worker except (subprocess.CalledProcessError, errors.DeviceConnectionError) as e: 81*800a58d9SAndroid Build Coastguard Worker logger.debug(str(e)) 82*800a58d9SAndroid Build Coastguard Worker utils.PrintColorString(str(e), utils.TextColors.FAIL) 83*800a58d9SAndroid Build Coastguard Worker 84*800a58d9SAndroid Build Coastguard Worker 85*800a58d9SAndroid Build Coastguard Workerdef Run(args): 86*800a58d9SAndroid Build Coastguard Worker """Run restart. 87*800a58d9SAndroid Build Coastguard Worker 88*800a58d9SAndroid Build Coastguard Worker After restart command executed, tool will return one Report instance. 89*800a58d9SAndroid Build Coastguard Worker 90*800a58d9SAndroid Build Coastguard Worker Args: 91*800a58d9SAndroid Build Coastguard Worker args: Namespace object from argparse.parse_args. 92*800a58d9SAndroid Build Coastguard Worker 93*800a58d9SAndroid Build Coastguard Worker Returns: 94*800a58d9SAndroid Build Coastguard Worker A Report instance. 95*800a58d9SAndroid Build Coastguard Worker """ 96*800a58d9SAndroid Build Coastguard Worker cfg = config.GetAcloudConfig(args) 97*800a58d9SAndroid Build Coastguard Worker if args.instance_name: 98*800a58d9SAndroid Build Coastguard Worker instance = list_instances.GetInstancesFromInstanceNames( 99*800a58d9SAndroid Build Coastguard Worker cfg, [args.instance_name]) 100*800a58d9SAndroid Build Coastguard Worker return RestartFromInstance( 101*800a58d9SAndroid Build Coastguard Worker cfg, instance[0], args.instance_id, args.powerwash) 102*800a58d9SAndroid Build Coastguard Worker if (not list_instances.GetCFRemoteInstances(cfg) 103*800a58d9SAndroid Build Coastguard Worker and list_instances.GetLocalInstances()): 104*800a58d9SAndroid Build Coastguard Worker utils.PrintColorString(_NOT_SUPPORT_MSG, utils.TextColors.FAIL) 105*800a58d9SAndroid Build Coastguard Worker sys.exit() 106*800a58d9SAndroid Build Coastguard Worker return RestartFromInstance(cfg, 107*800a58d9SAndroid Build Coastguard Worker list_instances.ChooseOneRemoteInstance(cfg), 108*800a58d9SAndroid Build Coastguard Worker args.instance_id, 109*800a58d9SAndroid Build Coastguard Worker args.powerwash) 110