1*800a58d9SAndroid Build Coastguard Worker# Copyright 2020 - 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"""Powerwash entry point. 15*800a58d9SAndroid Build Coastguard Worker 16*800a58d9SAndroid Build Coastguard WorkerThis command will powerwash the 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 Worker 22*800a58d9SAndroid Build Coastguard Workerfrom acloud import errors 23*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal import constants 24*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib import utils 25*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib.ssh import Ssh 26*800a58d9SAndroid Build Coastguard Workerfrom acloud.internal.lib.ssh import IP 27*800a58d9SAndroid Build Coastguard Workerfrom acloud.list import list as list_instances 28*800a58d9SAndroid Build Coastguard Workerfrom acloud.public import config 29*800a58d9SAndroid Build Coastguard Workerfrom acloud.public import report 30*800a58d9SAndroid Build Coastguard Worker 31*800a58d9SAndroid Build Coastguard Worker 32*800a58d9SAndroid Build Coastguard Workerlogger = logging.getLogger(__name__) 33*800a58d9SAndroid Build Coastguard Worker 34*800a58d9SAndroid Build Coastguard Worker 35*800a58d9SAndroid Build Coastguard Workerdef PowerwashFromInstance(cfg, instance, instance_id): 36*800a58d9SAndroid Build Coastguard Worker """Powerwash AVD from remote CF instance. 37*800a58d9SAndroid Build Coastguard Worker 38*800a58d9SAndroid Build Coastguard Worker Args: 39*800a58d9SAndroid Build Coastguard Worker cfg: AcloudConfig object. 40*800a58d9SAndroid Build Coastguard Worker instance: list.Instance() object. 41*800a58d9SAndroid Build Coastguard Worker instance_id: Integer of the instance id. 42*800a58d9SAndroid Build Coastguard Worker 43*800a58d9SAndroid Build Coastguard Worker Returns: 44*800a58d9SAndroid Build Coastguard Worker A Report instance. 45*800a58d9SAndroid Build Coastguard Worker """ 46*800a58d9SAndroid Build Coastguard Worker ssh = Ssh(ip=IP(ip=instance.ip), 47*800a58d9SAndroid Build Coastguard Worker user=constants.GCE_USER, 48*800a58d9SAndroid Build Coastguard Worker ssh_private_key_path=cfg.ssh_private_key_path, 49*800a58d9SAndroid Build Coastguard Worker extra_args_ssh_tunnel=cfg.extra_args_ssh_tunnel) 50*800a58d9SAndroid Build Coastguard Worker logger.info("Start to powerwash AVD id (%s) from the instance: %s.", 51*800a58d9SAndroid Build Coastguard Worker instance_id, instance.name) 52*800a58d9SAndroid Build Coastguard Worker PowerwashDevice(ssh, instance_id) 53*800a58d9SAndroid Build Coastguard Worker return report.Report(command="powerwash") 54*800a58d9SAndroid Build Coastguard Worker 55*800a58d9SAndroid Build Coastguard Worker 56*800a58d9SAndroid Build Coastguard Worker@utils.TimeExecute(function_description="Waiting for AVD to powerwash") 57*800a58d9SAndroid Build Coastguard Workerdef PowerwashDevice(ssh, instance_id): 58*800a58d9SAndroid Build Coastguard Worker """Powerwash AVD with the instance id. 59*800a58d9SAndroid Build Coastguard Worker 60*800a58d9SAndroid Build Coastguard Worker Args: 61*800a58d9SAndroid Build Coastguard Worker ssh: Ssh object. 62*800a58d9SAndroid Build Coastguard Worker instance_id: Integer of the instance id. 63*800a58d9SAndroid Build Coastguard Worker """ 64*800a58d9SAndroid Build Coastguard Worker ssh_command = "./bin/powerwash_cvd --instance_num=%d" % (instance_id) 65*800a58d9SAndroid Build Coastguard Worker try: 66*800a58d9SAndroid Build Coastguard Worker ssh.Run(ssh_command) 67*800a58d9SAndroid Build Coastguard Worker except (subprocess.CalledProcessError, errors.DeviceConnectionError) as e: 68*800a58d9SAndroid Build Coastguard Worker logger.debug(str(e)) 69*800a58d9SAndroid Build Coastguard Worker utils.PrintColorString(str(e), utils.TextColors.FAIL) 70*800a58d9SAndroid Build Coastguard Worker 71*800a58d9SAndroid Build Coastguard Worker 72*800a58d9SAndroid Build Coastguard Workerdef Run(args): 73*800a58d9SAndroid Build Coastguard Worker """Run powerwash. 74*800a58d9SAndroid Build Coastguard Worker 75*800a58d9SAndroid Build Coastguard Worker After powerwash command executed, tool will return one Report instance. 76*800a58d9SAndroid Build Coastguard Worker 77*800a58d9SAndroid Build Coastguard Worker Args: 78*800a58d9SAndroid Build Coastguard Worker args: Namespace object from argparse.parse_args. 79*800a58d9SAndroid Build Coastguard Worker 80*800a58d9SAndroid Build Coastguard Worker Returns: 81*800a58d9SAndroid Build Coastguard Worker A Report instance. 82*800a58d9SAndroid Build Coastguard Worker """ 83*800a58d9SAndroid Build Coastguard Worker cfg = config.GetAcloudConfig(args) 84*800a58d9SAndroid Build Coastguard Worker if args.instance_name: 85*800a58d9SAndroid Build Coastguard Worker instance = list_instances.GetInstancesFromInstanceNames( 86*800a58d9SAndroid Build Coastguard Worker cfg, [args.instance_name]) 87*800a58d9SAndroid Build Coastguard Worker return PowerwashFromInstance(cfg, instance[0], args.instance_id) 88*800a58d9SAndroid Build Coastguard Worker return PowerwashFromInstance(cfg, 89*800a58d9SAndroid Build Coastguard Worker list_instances.ChooseOneRemoteInstance(cfg), 90*800a58d9SAndroid Build Coastguard Worker args.instance_id) 91