1#   Copyright 2012-2013 OpenStack Foundation
2#
3#   Licensed under the Apache License, Version 2.0 (the "License"); you may
4#   not use this file except in compliance with the License. You may obtain
5#   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, WITHOUT
11#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12#   License for the specific language governing permissions and limitations
13#   under the License.
14#
15
16"""Compute v2 Console action implementations"""
17
18from osc_lib.cli import parseractions
19from osc_lib.command import command
20from osc_lib import utils
21
22from openstackclient.i18n import _
23
24
25class ShowConsoleLog(command.Command):
26    _description = _("Show server's console output")
27
28    def get_parser(self, prog_name):
29        parser = super(ShowConsoleLog, self).get_parser(prog_name)
30        parser.add_argument(
31            'server',
32            metavar='<server>',
33            help=_("Server to show console log (name or ID)")
34        )
35        parser.add_argument(
36            '--lines',
37            metavar='<num-lines>',
38            type=int,
39            default=None,
40            action=parseractions.NonNegativeAction,
41            help=_("Number of lines to display from the end of the log "
42                   "(default=all)")
43        )
44        return parser
45
46    def take_action(self, parsed_args):
47        compute_client = self.app.client_manager.compute
48
49        server = utils.find_resource(
50            compute_client.servers,
51            parsed_args.server,
52        )
53        length = parsed_args.lines
54        if length:
55            # NOTE(dtroyer): get_console_output() appears to shortchange the
56            #                output by one line
57            length += 1
58
59        data = server.get_console_output(length=length)
60
61        if data and data[-1] != '\n':
62            data += '\n'
63        self.app.stdout.write(data)
64
65
66class ShowConsoleURL(command.ShowOne):
67    _description = _("Show server's remote console URL")
68
69    def get_parser(self, prog_name):
70        parser = super(ShowConsoleURL, self).get_parser(prog_name)
71        parser.add_argument(
72            'server',
73            metavar='<server>',
74            help=_("Server to show URL (name or ID)")
75        )
76        type_group = parser.add_mutually_exclusive_group()
77        type_group.add_argument(
78            '--novnc',
79            dest='url_type',
80            action='store_const',
81            const='novnc',
82            default='novnc',
83            help=_("Show noVNC console URL (default)")
84        )
85        type_group.add_argument(
86            '--xvpvnc',
87            dest='url_type',
88            action='store_const',
89            const='xvpvnc',
90            help=_("Show xvpvnc console URL")
91        )
92        type_group.add_argument(
93            '--spice',
94            dest='url_type',
95            action='store_const',
96            const='spice-html5',
97            help=_("Show SPICE console URL")
98        )
99        type_group.add_argument(
100            '--rdp',
101            dest='url_type',
102            action='store_const',
103            const='rdp-html5',
104            help=_("Show RDP console URL"),
105        )
106        type_group.add_argument(
107            '--serial',
108            dest='url_type',
109            action='store_const',
110            const='serial',
111            help=_("Show serial console URL"),
112        )
113        type_group.add_argument(
114            '--mks',
115            dest='url_type',
116            action='store_const',
117            const='webmks',
118            help=_("Show WebMKS console URL"),
119        )
120        return parser
121
122    def take_action(self, parsed_args):
123        compute_client = self.app.client_manager.compute
124        server = utils.find_resource(
125            compute_client.servers,
126            parsed_args.server,
127        )
128
129        data = server.get_console_url(parsed_args.url_type)
130        if not data:
131            return ({}, {})
132
133        info = {}
134        # NOTE(Rui Chen): Return 'remote_console' in compute microversion API
135        #                 2.6 and later, return 'console' in compute
136        #                 microversion API from 2.0 to 2.5, do compatibility
137        #                 handle for different microversion API.
138        console_data = data.get('remote_console', data.get('console'))
139        info.update(console_data)
140        return zip(*sorted(info.items()))
141