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