|
| 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 | +"""Console action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | +import sys |
| 20 | + |
| 21 | +from cliff import command |
| 22 | +from cliff import show |
| 23 | + |
| 24 | +from openstackclient.common import utils |
| 25 | + |
| 26 | + |
| 27 | +class ShowConsoleLog(command.Command): |
| 28 | + """Show console-log command""" |
| 29 | + |
| 30 | + api = 'compute' |
| 31 | + log = logging.getLogger(__name__ + '.ShowConsoleLog') |
| 32 | + |
| 33 | + def get_parser(self, prog_name): |
| 34 | + parser = super(ShowConsoleLog, self).get_parser(prog_name) |
| 35 | + parser.add_argument( |
| 36 | + 'server', |
| 37 | + metavar='<server>', |
| 38 | + help='Name or ID of server to display console log', |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + '--lines', |
| 42 | + metavar='<num-lines>', |
| 43 | + type=int, |
| 44 | + default=None, |
| 45 | + help='Number of lines to display from the end of the log ' |
| 46 | + '(default=all)', |
| 47 | + ) |
| 48 | + return parser |
| 49 | + |
| 50 | + def take_action(self, parsed_args): |
| 51 | + self.log.debug('take_action(%s)' % parsed_args) |
| 52 | + compute_client = self.app.client_manager.compute |
| 53 | + |
| 54 | + server = utils.find_resource( |
| 55 | + compute_client.servers, |
| 56 | + parsed_args.server, |
| 57 | + ) |
| 58 | + # NOTE(dtroyer): get_console_output() appears to shortchange the |
| 59 | + # output by one line |
| 60 | + data = server.get_console_output(length=parsed_args.lines + 1) |
| 61 | + sys.stdout.write(data) |
| 62 | + return |
| 63 | + |
| 64 | + |
| 65 | +class ShowConsoleURL(show.ShowOne): |
| 66 | + """Show console-url command""" |
| 67 | + |
| 68 | + api = 'compute' |
| 69 | + log = logging.getLogger(__name__ + '.ShowConsoleURL') |
| 70 | + |
| 71 | + def get_parser(self, prog_name): |
| 72 | + parser = super(ShowConsoleURL, self).get_parser(prog_name) |
| 73 | + parser.add_argument( |
| 74 | + 'server', |
| 75 | + metavar='<server>', |
| 76 | + help='Name or ID of server to display console log', |
| 77 | + ) |
| 78 | + type_group = parser.add_mutually_exclusive_group() |
| 79 | + type_group.add_argument( |
| 80 | + '--novnc', |
| 81 | + dest='url_type', |
| 82 | + action='store_const', |
| 83 | + const='novnc', |
| 84 | + default='novnc', |
| 85 | + help='Show noVNC console URL (default)', |
| 86 | + ) |
| 87 | + type_group.add_argument( |
| 88 | + '--xvpvnc', |
| 89 | + dest='url_type', |
| 90 | + action='store_const', |
| 91 | + const='xvpvnc', |
| 92 | + help='Show xpvnc console URL', |
| 93 | + ) |
| 94 | + type_group.add_argument( |
| 95 | + '--spice', |
| 96 | + dest='url_type', |
| 97 | + action='store_const', |
| 98 | + const='spice', |
| 99 | + help='Show SPICE console URL', |
| 100 | + ) |
| 101 | + return parser |
| 102 | + |
| 103 | + def take_action(self, parsed_args): |
| 104 | + self.log.debug('take_action(%s)' % parsed_args) |
| 105 | + compute_client = self.app.client_manager.compute |
| 106 | + server = utils.find_resource( |
| 107 | + compute_client.servers, |
| 108 | + parsed_args.server, |
| 109 | + ) |
| 110 | + |
| 111 | + print "type: %s" % parsed_args.url_type |
| 112 | + if parsed_args.url_type in ['novnc', 'xvpvnc']: |
| 113 | + data = server.get_vnc_console(parsed_args.url_type) |
| 114 | + if parsed_args.url_type in ['spice']: |
| 115 | + data = server.get_spice_console(parsed_args.url_type) |
| 116 | + |
| 117 | + if not data: |
| 118 | + return ({}, {}) |
| 119 | + print "data: %s" % data['console'] |
| 120 | + |
| 121 | + info = {} |
| 122 | + info.update(data['console']) |
| 123 | + return zip(*sorted(info.iteritems())) |
0 commit comments