/usr/lib/python2.7/dist-packages/tuskarclient/shell.py is in python-tuskarclient 0.1.18-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | # Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Command-line interface to the Tuskar API.
"""
from __future__ import print_function
import argparse
import logging
import logging.handlers
import sys
import six
import tuskarclient
from tuskarclient import client
import tuskarclient.common.utils as utils
from tuskarclient.openstack.common.apiclient import exceptions as exc
logger = logging.getLogger(__name__)
class TuskarShell(object):
def __init__(self, raw_args,
argument_parser_class=argparse.ArgumentParser):
self.raw_args = raw_args
self.argument_parser_class = argument_parser_class
self.partial_args = None
self.parser = None
self.subparsers = None
self._prepare_parsers()
def _prepare_parsers(self):
nonversioned_parser = self._nonversioned_parser()
self.partial_args = (
nonversioned_parser.parse_known_args(self.raw_args)[0])
self.parser, self.subparsers = (
self._parser(self.partial_args.tuskar_api_version))
def run(self):
'''Run the CLI. Parse arguments and do the respective action.'''
# run self.do_help() if we have no raw_args at all or just -h/--help
if (not self.raw_args
or self.raw_args in (['-h'], ['--help'])):
self.do_help(self.partial_args)
return 0
args = self.parser.parse_args(self.raw_args)
self._setup_logging(args.debug)
# run self.do_help() if we have help subcommand or -h/--help option
if args.func == self.do_help or args.help:
self.do_help(args)
return 0
self._ensure_auth_info(args)
tuskar_client = client.get_client(
self.partial_args.tuskar_api_version, **args.__dict__)
args.func(tuskar_client, args)
def _ensure_auth_info(self, args):
'''Ensure that authentication information is provided. Two variants
of authentication are supported:
- provide username, password, tenant and auth url
- provide token and tuskar url (or auth url instead of tuskar url)
'''
if not args.os_auth_token:
if not args.os_username:
raise exc.CommandError("You must provide username via either "
"--os-username or env[OS_USERNAME]")
if not args.os_password:
raise exc.CommandError("You must provide password via either "
"--os-password or env[OS_PASSWORD]")
if not args.os_tenant_id and not args.os_tenant_name:
raise exc.CommandError("You must provide tenant via either "
"--os-tenant-name or --os-tenant-id or "
"env[OS_TENANT_NAME] or "
"env[OS_TENANT_ID]")
if not args.os_auth_url:
raise exc.CommandError("You must provide auth URL via either "
"--os-auth-url or env[OS_AUTH_URL]")
else:
if not args.tuskar_url and not args.os_auth_url:
raise exc.CommandError("You must provide either "
"--tuskar-url or --os-auth-url or "
"env[TUSKAR_URL] or env[OS_AUTH_URL]")
def _parser(self, version):
'''Create a top level argument parser.
:param version: version of Tuskar API (and corresponding CLI
commands) to use
:return: main parser and subparsers
:rtype: (Parser, Subparsers)
'''
parser = self._nonversioned_parser()
subparsers = parser.add_subparsers(metavar='<subcommand>')
versioned_shell = utils.import_versioned_module(version, 'shell')
versioned_shell.enhance_parser(parser, subparsers)
utils.define_commands_from_module(subparsers, self)
return parser, subparsers
def _nonversioned_parser(self):
'''Create a basic parser that doesn't contain version-specific
subcommands. This one is mainly useful for parsing which API
version should be used for the versioned full blown parser and
defining common version-agnostic options.
'''
parser = self.argument_parser_class(
prog='tuskar',
description='OpenStack Management CLI',
add_help=False,
)
parser.add_argument('-h', '--help',
action='store_true',
help="Print this help message and exit.",
)
parser.add_argument('--version',
action='version',
version=tuskarclient.__version__,
help="Shows the client version and exits.")
parser.add_argument('-d', '--debug',
default=bool(utils.env('TUSKARCLIENT_DEBUG')),
action='store_true',
help='Defaults to env[TUSKARCLIENT_DEBUG].')
parser.add_argument('--os-username',
default=utils.env('OS_USERNAME'),
help='Defaults to env[OS_USERNAME]',
)
parser.add_argument('--os_username',
help=argparse.SUPPRESS,
)
parser.add_argument('--os-password',
default=utils.env('OS_PASSWORD'),
help='Defaults to env[OS_PASSWORD]',
)
parser.add_argument('--os_password',
help=argparse.SUPPRESS,
)
parser.add_argument('--os-tenant-id',
default=utils.env('OS_TENANT_ID'),
help='Defaults to env[OS_TENANT_ID]',
)
parser.add_argument('--os_tenant_id',
help=argparse.SUPPRESS,
)
parser.add_argument('--os-tenant-name',
default=utils.env('OS_TENANT_NAME'),
help='Defaults to env[OS_TENANT_NAME]',
)
parser.add_argument('--os_tenant_name',
help=argparse.SUPPRESS,
)
parser.add_argument('--os-auth-url',
default=utils.env('OS_AUTH_URL'),
help='Defaults to env[OS_AUTH_URL]',
)
parser.add_argument('--os_auth_url',
help=argparse.SUPPRESS,
)
parser.add_argument('--os-auth-token',
default=utils.env('OS_AUTH_TOKEN',
default=None),
help='Defaults to env[OS_AUTH_TOKEN]')
parser.add_argument('--os_auth_token',
help=argparse.SUPPRESS)
parser.add_argument('--tuskar-url',
default=utils.env('TUSKAR_URL'),
help='Defaults to env[TUSKAR_URL]')
parser.add_argument('--tuskar_url',
help=argparse.SUPPRESS)
parser.add_argument('--tuskar-api-version',
default=utils.env('TUSKAR_API_VERSION',
default='2'),
help='Defaults to env[TUSKAR_API_VERSION] '
'or 2')
parser.add_argument('--tuskar_api_version',
help=argparse.SUPPRESS)
return parser
@utils.arg(
'command', metavar='<subcommand>', nargs='?',
help='Display help for <subcommand>')
def do_help(self, args):
"""Display help about this program or one of its subcommands."""
if getattr(args, 'command', None):
if args.command in self.subparsers.choices:
# print help for subcommand
self.subparsers.choices[args.command].print_help()
else:
raise exc.CommandError("'%s' is not a valid subcommand" %
args.command)
else:
# print general help
self.parser.print_help()
def _setup_logging(self, debug):
log_lvl = logging.DEBUG if debug else logging.WARNING
logging.basicConfig(
format="%(levelname)s (%(module)s) %(message)s",
level=log_lvl)
def main():
try:
TuskarShell(sys.argv[1:]).run()
except exc.CommandError as e:
print(six.text_type(e), file=sys.stderr)
except Exception as e:
logger.exception("Exiting due to an error:")
sys.exit(1)
if __name__ == '__main__':
main()
|