1# Copyright 2021 - 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"""Restart entry point. 15 16This command will restart the CF AVD from a remote instance. 17""" 18 19import logging 20import subprocess 21import sys 22 23from acloud import errors 24from acloud.internal import constants 25from acloud.internal.lib import utils 26from acloud.internal.lib.ssh import Ssh 27from acloud.internal.lib.ssh import IP 28from acloud.list import list as list_instances 29from acloud.powerwash import powerwash 30from acloud.public import config 31from acloud.public import report 32from acloud.reconnect import reconnect 33 34 35logger = logging.getLogger(__name__) 36_NOT_SUPPORT_MSG = ("Currently the restart function doesn't support local " 37 "instances. Please try to create one new instance.") 38 39 40def RestartFromInstance(cfg, instance, instance_id, powerwash_data): 41 """Restart AVD from remote CF instance. 42 43 Args: 44 cfg: AcloudConfig object. 45 instance: list.Instance() object. 46 instance_id: Integer of the instance id. 47 powerwash_data: Boolean, True to powerwash AVD data. 48 49 Returns: 50 A Report instance. 51 """ 52 ssh = Ssh(ip=IP(ip=instance.ip), 53 user=constants.GCE_USER, 54 ssh_private_key_path=cfg.ssh_private_key_path, 55 extra_args_ssh_tunnel=cfg.extra_args_ssh_tunnel) 56 logger.info("Start to restart AVD id (%s) from the instance: %s.", 57 instance_id, instance.name) 58 if powerwash_data: 59 powerwash.PowerwashDevice(ssh, instance_id) 60 else: 61 RestartDevice(ssh, instance_id) 62 reconnect.ReconnectInstance(cfg.ssh_private_key_path, 63 instance, 64 report.Report(command="reconnect"), 65 cfg.extra_args_ssh_tunnel) 66 return report.Report(command="restart") 67 68 69@utils.TimeExecute(function_description="Waiting for AVD to restart") 70def RestartDevice(ssh, instance_id): 71 """Restart AVD with the instance id. 72 73 Args: 74 ssh: Ssh object. 75 instance_id: Integer of the instance id. 76 """ 77 ssh_command = "./bin/restart_cvd --instance_num=%d" % (instance_id) 78 try: 79 ssh.Run(ssh_command) 80 except (subprocess.CalledProcessError, errors.DeviceConnectionError) as e: 81 logger.debug(str(e)) 82 utils.PrintColorString(str(e), utils.TextColors.FAIL) 83 84 85def Run(args): 86 """Run restart. 87 88 After restart command executed, tool will return one Report instance. 89 90 Args: 91 args: Namespace object from argparse.parse_args. 92 93 Returns: 94 A Report instance. 95 """ 96 cfg = config.GetAcloudConfig(args) 97 if args.instance_name: 98 instance = list_instances.GetInstancesFromInstanceNames( 99 cfg, [args.instance_name]) 100 return RestartFromInstance( 101 cfg, instance[0], args.instance_id, args.powerwash) 102 if (not list_instances.GetCFRemoteInstances(cfg) 103 and list_instances.GetLocalInstances()): 104 utils.PrintColorString(_NOT_SUPPORT_MSG, utils.TextColors.FAIL) 105 sys.exit() 106 return RestartFromInstance(cfg, 107 list_instances.ChooseOneRemoteInstance(cfg), 108 args.instance_id, 109 args.powerwash) 110