• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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.powerwash import powerwash
29from acloud.public import config
30from acloud.public import report
31from acloud.reconnect import reconnect
32
33
34logger = logging.getLogger(__name__)
35
36
37def RestartFromInstance(cfg, instance, instance_id, powerwash_data):
38    """Restart AVD from remote CF instance.
39
40    Args:
41        cfg: AcloudConfig object.
42        instance: list.Instance() object.
43        instance_id: Integer of the instance id.
44        powerwash_data: Boolean, True to powerwash AVD data.
45
46    Returns:
47        A Report instance.
48    """
49    ssh = Ssh(ip=IP(ip=instance.ip),
50              user=constants.GCE_USER,
51              ssh_private_key_path=cfg.ssh_private_key_path,
52              extra_args_ssh_tunnel=cfg.extra_args_ssh_tunnel)
53    logger.info("Start to restart AVD id (%s) from the instance: %s.",
54                instance_id, instance.name)
55    if powerwash_data:
56        powerwash.PowerwashDevice(ssh, instance_id)
57    else:
58        RestartDevice(ssh, instance_id)
59    reconnect.ReconnectInstance(cfg.ssh_private_key_path,
60                                instance,
61                                report.Report(command="reconnect"),
62                                cfg.extra_args_ssh_tunnel)
63    return report.Report(command="restart")
64
65
66@utils.TimeExecute(function_description="Waiting for AVD to restart")
67def RestartDevice(ssh, instance_id):
68    """Restart AVD with the instance id.
69
70    Args:
71        ssh: Ssh object.
72        instance_id: Integer of the instance id.
73    """
74    ssh_command = "./bin/restart_cvd --instance_num=%d" % (instance_id)
75    try:
76        ssh.Run(ssh_command)
77    except (subprocess.CalledProcessError, errors.DeviceConnectionError) as e:
78        logger.debug(str(e))
79        utils.PrintColorString(str(e), utils.TextColors.FAIL)
80
81
82def Run(args):
83    """Run restart.
84
85    After restart command executed, tool will return one Report instance.
86
87    Args:
88        args: Namespace object from argparse.parse_args.
89
90    Returns:
91        A Report instance.
92    """
93    cfg = config.GetAcloudConfig(args)
94    if args.instance_name:
95        instance = list_instances.GetInstancesFromInstanceNames(
96            cfg, [args.instance_name])
97        return RestartFromInstance(
98            cfg, instance[0], args.instance_id, args.powerwash)
99    return RestartFromInstance(cfg,
100                               list_instances.ChooseOneRemoteInstance(cfg),
101                               args.instance_id,
102                               args.powerwash)
103