1#
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the 'License');
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an 'AS IS' BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import logging
18
19from host_controller.acloud import acloud_client
20from host_controller.command_processor import base_command_processor
21
22
23class CommandAcloud(base_command_processor.BaseCommandProcessor):
24    '''Command processor for acloud command.
25
26    Attributes:
27        arg_parser: ConsoleArgumentParser object, argument parser.
28        console: cmd.Cmd console object.
29        command: string, command name which this processor will handle.
30        command_detail: string, detailed explanation for the command.
31    '''
32
33    command = 'acloud'
34    command_detail = 'Create acloud instances.'
35
36    def Run(self, arg_line):
37        '''Creates an acloud instance and connects to it via adb.
38
39        Args:
40            arg_line: string, line of command arguments
41        '''
42        args = self.arg_parser.ParseLine(arg_line)
43
44        if args.provider == "ab":
45            if args.build_id.lower() == "latest":
46                build_id = self.console._build_provider["ab"].GetLatestBuildId(
47                    args.branch,
48                    args.target)
49        else:
50            # TODO(yuexima): support more provider types.
51            logging.error("Provider %s not supported yet." % args.provider)
52            return
53
54        ac = acloud_client.ACloudClient()
55        ac.PrepareConfig(args.config_path)
56        ac.CreateInstance(args.build_id)
57        ac.ConnectInstanceToAdb(ah.GetInstanceIP())
58
59    def SetUp(self):
60        """Initializes the parser for acloud command."""
61        self.arg_parser.add_argument(
62            "--build_id",
63            help="Build ID to use.")
64        self.arg_parser.add_argument(
65            "--provider",
66            default="ab",
67            choices=("local_fs", "gcs", "pab", "ab"),
68            help="Build provider type")
69        self.arg_parser.add_argument(
70            "--branch",  # not required for local_fs
71            help="Branch to grab the artifact from.")
72        self.arg_parser.add_argument(
73            "--target",  # not required for local_fs
74            help="Target product to grab the artifact from.")
75        self.arg_parser.add_argument(
76            "--config_path",
77            required=True,
78            help="Acloud config path.")
79