| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
""" |
| 17 |
Command-line interface to the OpenStack Images API. |
| 18 |
""" |
| 19 |
|
| 20 |
import argparse |
| 21 |
import copy |
| 22 |
import getpass |
| 23 |
import hashlib |
| 24 |
import json |
| 25 |
import logging |
| 26 |
import os |
| 27 |
import sys |
| 28 |
import traceback |
| 29 |
|
| 30 |
from oslo_utils import encodeutils |
| 31 |
from oslo_utils import importutils |
| 32 |
import urllib.parse |
| 33 |
|
| 34 |
import glanceclient |
| 35 |
from glanceclient._i18n import _ |
| 36 |
from glanceclient.common import utils |
| 37 |
from glanceclient import exc |
| 38 |
|
| 39 |
from keystoneauth1 import discover |
| 40 |
from keystoneauth1 import exceptions as ks_exc |
| 41 |
from keystoneauth1.identity import v2 as v2_auth |
| 42 |
from keystoneauth1.identity import v3 as v3_auth |
| 43 |
from keystoneauth1 import loading |
| 44 |
|
| 45 |
osprofiler_profiler = importutils.try_import("osprofiler.profiler") |
| 46 |
|
| 47 |
SUPPORTED_VERSIONS = [1, 2] |
| 48 |
|
| 49 |
|
| 50 |
class OpenStackImagesShell(object): |
| 51 |
|
| 52 |
def _append_global_identity_args(self, parser, argv): |
| 53 |
|
| 54 |
parser.set_defaults(os_auth_url=utils.env('OS_AUTH_URL')) |
| 55 |
|
| 56 |
parser.set_defaults(os_project_name=utils.env( |
| 57 |
'OS_PROJECT_NAME', 'OS_TENANT_NAME')) |
| 58 |
parser.set_defaults(os_project_id=utils.env( |
| 59 |
'OS_PROJECT_ID', 'OS_TENANT_ID')) |
| 60 |
|
| 61 |
parser.add_argument('--os_tenant_id', |
| 62 |
help=argparse.SUPPRESS) |
| 63 |
|
| 64 |
parser.add_argument('--os_tenant_name', |
| 65 |
help=argparse.SUPPRESS) |
| 66 |
|
| 67 |
parser.add_argument('--os-region-name', |
| 68 |
default=utils.env('OS_REGION_NAME'), |
| 69 |
help='Defaults to env[OS_REGION_NAME].') |
| 70 |
|
| 71 |
parser.add_argument('--os_region_name', |
| 72 |
help=argparse.SUPPRESS) |
| 73 |
|
| 74 |
parser.add_argument('--os-auth-token', |
| 75 |
default=utils.env('OS_AUTH_TOKEN'), |
| 76 |
help='Defaults to env[OS_AUTH_TOKEN].') |
| 77 |
|
| 78 |
parser.add_argument('--os_auth_token', |
| 79 |
help=argparse.SUPPRESS) |
| 80 |
|
| 81 |
parser.add_argument('--os-service-type', |
| 82 |
default=utils.env('OS_SERVICE_TYPE'), |
| 83 |
help='Defaults to env[OS_SERVICE_TYPE].') |
| 84 |
|
| 85 |
parser.add_argument('--os_service_type', |
| 86 |
help=argparse.SUPPRESS) |
| 87 |
|
| 88 |
parser.add_argument('--os-endpoint-type', |
| 89 |
default=utils.env('OS_ENDPOINT_TYPE'), |
| 90 |
help='Defaults to env[OS_ENDPOINT_TYPE].') |
| 91 |
|
| 92 |
parser.add_argument('--os_endpoint_type', |
| 93 |
help=argparse.SUPPRESS) |
| 94 |
|
| 95 |
loading.register_session_argparse_arguments(parser) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
default_auth_plugin = 'password' |
| 101 |
token_opts = ['os-token', 'os-auth-token', 'os_auth-token'] |
| 102 |
if argv and any(i in token_opts for i in argv): |
| 103 |
default_auth_plugin = 'token' |
| 104 |
loading.register_auth_argparse_arguments( |
| 105 |
parser, argv, default=default_auth_plugin) |
| 106 |
|
| 107 |
def get_base_parser(self, argv): |
| 108 |
parser = argparse.ArgumentParser( |
| 109 |
prog='glance', |
| 110 |
description=__doc__.strip(), |
| 111 |
epilog='See "glance help COMMAND" ' |
| 112 |
'for help on a specific command.', |
| 113 |
add_help=False, |
| 114 |
formatter_class=HelpFormatter, |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
parser.add_argument('-h', '--help', |
| 119 |
action='store_true', |
| 120 |
help=argparse.SUPPRESS, |
| 121 |
) |
| 122 |
|
| 123 |
parser.add_argument('--version', |
| 124 |
action='version', |
| 125 |
version=glanceclient.__version__) |
| 126 |
|
| 127 |
parser.add_argument('-d', '--debug', |
| 128 |
default=bool(utils.env('GLANCECLIENT_DEBUG')), |
| 129 |
action='store_true', |
| 130 |
help='Defaults to env[GLANCECLIENT_DEBUG].') |
| 131 |
|
| 132 |
parser.add_argument('-v', '--verbose', |
| 133 |
default=False, action="store_true", |
| 134 |
help="Print more verbose output.") |
| 135 |
|
| 136 |
parser.add_argument('--get-schema', |
| 137 |
default=False, action="store_true", |
| 138 |
dest='get_schema', |
| 139 |
help='Ignores cached copy and forces retrieval ' |
| 140 |
'of schema that generates portions of the ' |
| 141 |
'help text. Ignored with API version 1.') |
| 142 |
|
| 143 |
parser.add_argument('-f', '--force', |
| 144 |
dest='force', |
| 145 |
default=False, action='store_true', |
| 146 |
help='Prevent select actions from requesting ' |
| 147 |
'user confirmation.') |
| 148 |
|
| 149 |
parser.add_argument('--os-image-url', |
| 150 |
default=utils.env('OS_IMAGE_URL'), |
| 151 |
help=('Defaults to env[OS_IMAGE_URL]. ' |
| 152 |
'If the provided image url contains ' |
| 153 |
'a version number and ' |
| 154 |
'`--os-image-api-version` is omitted ' |
| 155 |
'the version of the URL will be picked as ' |
| 156 |
'the image api version to use.')) |
| 157 |
|
| 158 |
parser.add_argument('--os_image_url', |
| 159 |
help=argparse.SUPPRESS) |
| 160 |
|
| 161 |
parser.add_argument('--os-image-api-version', |
| 162 |
default=utils.env('OS_IMAGE_API_VERSION', |
| 163 |
default=None), |
| 164 |
help='Defaults to env[OS_IMAGE_API_VERSION] or 2.') |
| 165 |
|
| 166 |
parser.add_argument('--os_image_api_version', |
| 167 |
help=argparse.SUPPRESS) |
| 168 |
|
| 169 |
parser.set_defaults(func=self.do_help) |
| 170 |
|
| 171 |
if osprofiler_profiler: |
| 172 |
parser.add_argument('--profile', |
| 173 |
metavar='HMAC_KEY', |
| 174 |
default=utils.env('OS_PROFILE'), |
| 175 |
help='HMAC key to use for encrypting context ' |
| 176 |
'data for performance profiling of operation. ' |
| 177 |
'This key should be the value of HMAC key ' |
| 178 |
'configured in osprofiler middleware in ' |
| 179 |
'glance, it is specified in glance ' |
| 180 |
'configuration file at ' |
| 181 |
'/etc/glance/glance-api.conf and ' |
| 182 |
'/etc/glance/glance-registry.conf. Without ' |
| 183 |
'key the profiling will not be triggered even ' |
| 184 |
'if osprofiler is enabled on server side. ' |
| 185 |
'Defaults to env[OS_PROFILE].') |
| 186 |
|
| 187 |
self._append_global_identity_args(parser, argv) |
| 188 |
|
| 189 |
return parser |
| 190 |
|
| 191 |
def get_subcommand_parser(self, version, argv=None): |
| 192 |
parser = self.get_base_parser(argv) |
| 193 |
|
| 194 |
self.subcommands = {} |
| 195 |
subparsers = parser.add_subparsers(metavar='<subcommand>') |
| 196 |
submodule = importutils.import_versioned_module('glanceclient', |
| 197 |
version, 'shell') |
| 198 |
|
| 199 |
self._find_actions(subparsers, submodule) |
| 200 |
self._find_actions(subparsers, self) |
| 201 |
|
| 202 |
self._add_bash_completion_subparser(subparsers) |
| 203 |
|
| 204 |
return parser |
| 205 |
|
| 206 |
def _find_actions(self, subparsers, actions_module): |
| 207 |
for attr in (a for a in dir(actions_module) if a.startswith('do_')): |
| 208 |
|
| 209 |
|
| 210 |
command = attr[3:].replace('_', '-') |
| 211 |
callback = getattr(actions_module, attr) |
| 212 |
desc = callback.__doc__ or '' |
| 213 |
help = desc.strip().split('\n')[0] |
| 214 |
arguments = getattr(callback, 'arguments', []) |
| 215 |
|
| 216 |
subparser = subparsers.add_parser(command, |
| 217 |
help=help, |
| 218 |
description=desc, |
| 219 |
add_help=False, |
| 220 |
formatter_class=HelpFormatter |
| 221 |
) |
| 222 |
subparser.add_argument('-h', '--help', |
| 223 |
action='help', |
| 224 |
help=argparse.SUPPRESS, |
| 225 |
) |
| 226 |
self.subcommands[command] = subparser |
| 227 |
required_args = subparser.add_argument_group('Required arguments') |
| 228 |
for (args, kwargs) in arguments: |
| 229 |
if kwargs.get('required', False): |
| 230 |
required_args.add_argument(*args, **kwargs) |
| 231 |
else: |
| 232 |
subparser.add_argument(*args, **kwargs) |
| 233 |
subparser.set_defaults(func=callback) |
| 234 |
|
| 235 |
def _add_bash_completion_subparser(self, subparsers): |
| 236 |
subparser = subparsers.add_parser('bash_completion', |
| 237 |
add_help=False, |
| 238 |
formatter_class=HelpFormatter) |
| 239 |
self.subcommands['bash_completion'] = subparser |
| 240 |
subparser.set_defaults(func=self.do_bash_completion) |
| 241 |
|
| 242 |
def _get_image_url(self, args): |
| 243 |
"""Translate the available url-related options into a single string. |
| 244 |
|
| 245 |
Return the endpoint that should be used to talk to Glance if a |
| 246 |
clear decision can be made. Otherwise, return None. |
| 247 |
""" |
| 248 |
if args.os_image_url: |
| 249 |
return args.os_image_url |
| 250 |
else: |
| 251 |
return None |
| 252 |
|
| 253 |
def _discover_auth_versions(self, session, auth_url): |
| 254 |
|
| 255 |
|
| 256 |
v2_auth_url = None |
| 257 |
v3_auth_url = None |
| 258 |
try: |
| 259 |
ks_discover = discover.Discover(session=session, url=auth_url) |
| 260 |
v2_auth_url = ks_discover.url_for('2.0') |
| 261 |
v3_auth_url = ks_discover.url_for('3.0') |
| 262 |
except ks_exc.ClientException as e: |
| 263 |
|
| 264 |
|
| 265 |
url_parts = urllib.parse.urlparse(auth_url) |
| 266 |
(scheme, netloc, path, params, query, fragment) = url_parts |
| 267 |
path = path.lower() |
| 268 |
if path.startswith('/v3'): |
| 269 |
v3_auth_url = auth_url |
| 270 |
elif path.startswith('/v2'): |
| 271 |
v2_auth_url = auth_url |
| 272 |
else: |
| 273 |
|
| 274 |
msg = ('Unable to determine the Keystone version ' |
| 275 |
'to authenticate with using the given ' |
| 276 |
'auth_url. Identity service may not support API ' |
| 277 |
'version discovery. Please provide a versioned ' |
| 278 |
'auth_url instead. error=%s') % (e) |
| 279 |
raise exc.CommandError(msg) |
| 280 |
|
| 281 |
return (v2_auth_url, v3_auth_url) |
| 282 |
|
| 283 |
def _get_keystone_auth_plugin(self, ks_session, **kwargs): |
| 284 |
|
| 285 |
auth_url = kwargs.pop('auth_url', None) |
| 286 |
(v2_auth_url, v3_auth_url) = self._discover_auth_versions( |
| 287 |
session=ks_session, |
| 288 |
auth_url=auth_url) |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
user_id = kwargs.pop('user_id', None) |
| 294 |
username = kwargs.pop('username', None) |
| 295 |
password = kwargs.pop('password', None) |
| 296 |
user_domain_name = kwargs.pop('user_domain_name', None) |
| 297 |
user_domain_id = kwargs.pop('user_domain_id', None) |
| 298 |
|
| 299 |
project_id = (kwargs.pop('project_id', None) or |
| 300 |
kwargs.pop('tenant_id', None)) |
| 301 |
project_name = (kwargs.pop('project_name', None) or |
| 302 |
kwargs.pop('tenant_name', None)) |
| 303 |
project_domain_id = kwargs.pop('project_domain_id', None) |
| 304 |
project_domain_name = kwargs.pop('project_domain_name', None) |
| 305 |
auth = None |
| 306 |
|
| 307 |
use_domain = (user_domain_id or |
| 308 |
user_domain_name or |
| 309 |
project_domain_id or |
| 310 |
project_domain_name) |
| 311 |
use_v3 = v3_auth_url and (use_domain or (not v2_auth_url)) |
| 312 |
use_v2 = v2_auth_url and not use_domain |
| 313 |
|
| 314 |
if use_v3: |
| 315 |
auth = v3_auth.Password( |
| 316 |
v3_auth_url, |
| 317 |
user_id=user_id, |
| 318 |
username=username, |
| 319 |
password=password, |
| 320 |
user_domain_id=user_domain_id, |
| 321 |
user_domain_name=user_domain_name, |
| 322 |
project_id=project_id, |
| 323 |
project_name=project_name, |
| 324 |
project_domain_id=project_domain_id, |
| 325 |
project_domain_name=project_domain_name) |
| 326 |
elif use_v2: |
| 327 |
auth = v2_auth.Password( |
| 328 |
v2_auth_url, |
| 329 |
username, |
| 330 |
password, |
| 331 |
tenant_id=project_id, |
| 332 |
tenant_name=project_name) |
| 333 |
else: |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
exc.CommandError("Credential and auth_url mismatch. The given " |
| 339 |
"auth_url is using Keystone V2 endpoint, which " |
| 340 |
"may not able to handle Keystone V3 credentials. " |
| 341 |
"Please provide a correct Keystone V3 auth_url.") |
| 342 |
|
| 343 |
return auth |
| 344 |
|
| 345 |
def _get_kwargs_to_create_auth_plugin(self, args): |
| 346 |
if not args.os_username: |
| 347 |
raise exc.CommandError( |
| 348 |
_("You must provide a username via" |
| 349 |
" either --os-username or " |
| 350 |
"env[OS_USERNAME]")) |
| 351 |
|
| 352 |
if not args.os_password: |
| 353 |
|
| 354 |
if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty(): |
| 355 |
|
| 356 |
try: |
| 357 |
args.os_password = getpass.getpass('OS Password: ') |
| 358 |
except EOFError: |
| 359 |
pass |
| 360 |
|
| 361 |
|
| 362 |
if not args.os_password: |
| 363 |
raise exc.CommandError( |
| 364 |
_("You must provide a password via " |
| 365 |
"either --os-password, " |
| 366 |
"env[OS_PASSWORD], " |
| 367 |
"or prompted response")) |
| 368 |
|
| 369 |
|
| 370 |
os_project_name = getattr( |
| 371 |
args, 'os_project_name', getattr(args, 'os_tenant_name', None)) |
| 372 |
os_project_id = getattr( |
| 373 |
args, 'os_project_id', getattr(args, 'os_tenant_id', None)) |
| 374 |
if not any([os_project_name, os_project_id]): |
| 375 |
|
| 376 |
|
| 377 |
raise exc.CommandError( |
| 378 |
_("You must provide a project_id or project_name (" |
| 379 |
"with project_domain_name or project_domain_id) " |
| 380 |
"via " |
| 381 |
" --os-project-id (env[OS_PROJECT_ID])" |
| 382 |
" --os-project-name (env[OS_PROJECT_NAME])," |
| 383 |
" --os-project-domain-id " |
| 384 |
"(env[OS_PROJECT_DOMAIN_ID])" |
| 385 |
" --os-project-domain-name " |
| 386 |
"(env[OS_PROJECT_DOMAIN_NAME])")) |
| 387 |
|
| 388 |
if not args.os_auth_url: |
| 389 |
raise exc.CommandError( |
| 390 |
_("You must provide an auth url via" |
| 391 |
" either --os-auth-url or " |
| 392 |
"via env[OS_AUTH_URL]")) |
| 393 |
|
| 394 |
kwargs = { |
| 395 |
'auth_url': args.os_auth_url, |
| 396 |
'username': args.os_username, |
| 397 |
'user_id': args.os_user_id, |
| 398 |
'user_domain_id': args.os_user_domain_id, |
| 399 |
'user_domain_name': args.os_user_domain_name, |
| 400 |
'password': args.os_password, |
| 401 |
'tenant_name': args.os_tenant_name, |
| 402 |
'tenant_id': args.os_tenant_id, |
| 403 |
'project_name': args.os_project_name, |
| 404 |
'project_id': args.os_project_id, |
| 405 |
'project_domain_name': args.os_project_domain_name, |
| 406 |
'project_domain_id': args.os_project_domain_id, |
| 407 |
} |
| 408 |
return kwargs |
| 409 |
|
| 410 |
def _get_versioned_client(self, api_version, args): |
| 411 |
endpoint = self._get_image_url(args) |
| 412 |
auth_token = args.os_auth_token |
| 413 |
|
| 414 |
if endpoint and auth_token: |
| 415 |
kwargs = { |
| 416 |
'token': auth_token, |
| 417 |
'insecure': args.insecure, |
| 418 |
'timeout': args.timeout, |
| 419 |
'cacert': args.os_cacert, |
| 420 |
'cert': args.os_cert, |
| 421 |
'key': args.os_key, |
| 422 |
} |
| 423 |
else: |
| 424 |
ks_session = loading.load_session_from_argparse_arguments(args) |
| 425 |
auth_plugin_kwargs = self._get_kwargs_to_create_auth_plugin(args) |
| 426 |
ks_session.auth = self._get_keystone_auth_plugin( |
| 427 |
ks_session=ks_session, **auth_plugin_kwargs) |
| 428 |
kwargs = {'session': ks_session} |
| 429 |
|
| 430 |
if endpoint is None: |
| 431 |
endpoint_type = args.os_endpoint_type or 'public' |
| 432 |
service_type = args.os_service_type or 'image' |
| 433 |
endpoint = ks_session.get_endpoint( |
| 434 |
service_type=service_type, |
| 435 |
interface=endpoint_type, |
| 436 |
region_name=args.os_region_name) |
| 437 |
|
| 438 |
return glanceclient.Client(api_version, endpoint, **kwargs) |
| 439 |
|
| 440 |
def _cache_schemas(self, options, client, home_dir='~/.glanceclient'): |
| 441 |
homedir = os.path.expanduser(home_dir) |
| 442 |
path_prefix = homedir |
| 443 |
if options.os_auth_url: |
| 444 |
hash_host = hashlib.sha1(options.os_auth_url.encode('utf-8')) |
| 445 |
path_prefix = os.path.join(path_prefix, hash_host.hexdigest()) |
| 446 |
if not os.path.exists(path_prefix): |
| 447 |
try: |
| 448 |
os.makedirs(path_prefix) |
| 449 |
except OSError as e: |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
msg = '%s' % e |
| 455 |
print(encodeutils.safe_decode(msg), file=sys.stderr) |
| 456 |
resources = ['image', 'metadefs/namespace', 'metadefs/resource_type'] |
| 457 |
schema_file_paths = [os.path.join(path_prefix, x + '_schema.json') |
| 458 |
for x in ['image', 'namespace', 'resource_type']] |
| 459 |
|
| 460 |
failed_download_schema = 0 |
| 461 |
for resource, schema_file_path in zip(resources, schema_file_paths): |
| 462 |
if (not os.path.exists(schema_file_path)) or options.get_schema: |
| 463 |
try: |
| 464 |
schema = client.schemas.get(resource) |
| 465 |
with open(schema_file_path, 'w') as f: |
| 466 |
f.write(json.dumps(schema.raw())) |
| 467 |
except exc.Unauthorized: |
| 468 |
raise exc.CommandError( |
| 469 |
"Invalid OpenStack Identity credentials.") |
| 470 |
except Exception: |
| 471 |
|
| 472 |
|
| 473 |
failed_download_schema += 1 |
| 474 |
pass |
| 475 |
|
| 476 |
return failed_download_schema >= len(resources) |
| 477 |
|
| 478 |
def main(self, argv): |
| 479 |
|
| 480 |
def _get_subparser(api_version): |
| 481 |
try: |
| 482 |
return self.get_subcommand_parser(api_version, argv) |
| 483 |
except ImportError as e: |
| 484 |
if not str(e): |
| 485 |
|
| 486 |
|
| 487 |
raise ImportError('Unable to import module. Re-run ' |
| 488 |
'with --debug for more info.') |
| 489 |
raise |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
base_argv = copy.deepcopy(argv) |
| 496 |
parser = self.get_base_parser(argv) |
| 497 |
(options, args) = parser.parse_known_args(base_argv) |
| 498 |
|
| 499 |
try: |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
endpoint = self._get_image_url(options) |
| 506 |
endpoint, url_version = utils.strip_version(endpoint) |
| 507 |
except ValueError: |
| 508 |
|
| 509 |
url_version = None |
| 510 |
|
| 511 |
|
| 512 |
try: |
| 513 |
api_version = int(options.os_image_api_version or url_version or 2) |
| 514 |
if api_version not in SUPPORTED_VERSIONS: |
| 515 |
raise ValueError |
| 516 |
except ValueError: |
| 517 |
msg = ("Invalid API version parameter. " |
| 518 |
"Supported values are %s" % SUPPORTED_VERSIONS) |
| 519 |
utils.exit(msg=msg) |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if options.help or not argv: |
| 524 |
parser = _get_subparser(api_version) |
| 525 |
self.do_help(options, parser=parser) |
| 526 |
return 0 |
| 527 |
|
| 528 |
|
| 529 |
sub_parser = _get_subparser(api_version) |
| 530 |
args = sub_parser.parse_args(argv) |
| 531 |
|
| 532 |
if args.func == self.do_help: |
| 533 |
self.do_help(args, parser=sub_parser) |
| 534 |
return 0 |
| 535 |
elif args.func == self.do_bash_completion: |
| 536 |
self.do_bash_completion(args) |
| 537 |
return 0 |
| 538 |
|
| 539 |
if not options.os_image_api_version and api_version == 2: |
| 540 |
switch_version = True |
| 541 |
client = self._get_versioned_client('2', args) |
| 542 |
|
| 543 |
resp, body = client.http_client.get('/versions') |
| 544 |
|
| 545 |
for version in body['versions']: |
| 546 |
if version['id'].startswith('v2'): |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
switch_version = self._cache_schemas(options, client) |
| 551 |
break |
| 552 |
|
| 553 |
if switch_version: |
| 554 |
print('WARNING: The client is falling back to v1 because' |
| 555 |
' the accessing to v2 failed. This behavior will' |
| 556 |
' be removed in future versions', file=sys.stderr) |
| 557 |
api_version = 1 |
| 558 |
|
| 559 |
sub_parser = _get_subparser(api_version) |
| 560 |
|
| 561 |
|
| 562 |
args = sub_parser.parse_args(argv) |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
if not args.os_password and options.os_password: |
| 569 |
args.os_password = options.os_password |
| 570 |
|
| 571 |
if args.debug: |
| 572 |
|
| 573 |
|
| 574 |
logging.basicConfig(level=logging.DEBUG) |
| 575 |
|
| 576 |
logging.getLogger('iso8601').setLevel(logging.WARNING) |
| 577 |
LOG = logging.getLogger('glanceclient') |
| 578 |
LOG.addHandler(logging.StreamHandler()) |
| 579 |
LOG.setLevel(logging.DEBUG if args.debug else logging.INFO) |
| 580 |
|
| 581 |
profile = osprofiler_profiler and options.profile |
| 582 |
if profile: |
| 583 |
osprofiler_profiler.init(options.profile) |
| 584 |
|
| 585 |
client = self._get_versioned_client(api_version, args) |
| 586 |
|
| 587 |
try: |
| 588 |
args.func(client, args) |
| 589 |
except exc.Unauthorized: |
| 590 |
raise exc.CommandError("Invalid OpenStack Identity credentials.") |
| 591 |
finally: |
| 592 |
if profile: |
| 593 |
trace_id = osprofiler_profiler.get().get_base_id() |
| 594 |
print("Profiling trace ID: %s" % trace_id) |
| 595 |
print("To display trace use next command:\n" |
| 596 |
"osprofiler trace show --html %s " % trace_id) |
| 597 |
|
| 598 |
@utils.arg('command', metavar='<subcommand>', nargs='?', |
| 599 |
help='Display help for <subcommand>.') |
| 600 |
def do_help(self, args, parser): |
| 601 |
"""Display help about this program or one of its subcommands.""" |
| 602 |
command = getattr(args, 'command', '') |
| 603 |
|
| 604 |
if command: |
| 605 |
if args.command in self.subcommands: |
| 606 |
self.subcommands[args.command].print_help() |
| 607 |
else: |
| 608 |
raise exc.CommandError("'%s' is not a valid subcommand" % |
| 609 |
args.command) |
| 610 |
else: |
| 611 |
parser.print_help() |
| 612 |
|
| 613 |
if not args.os_image_api_version or args.os_image_api_version == '2': |
| 614 |
|
| 615 |
|
| 616 |
try: |
| 617 |
if command is None: |
| 618 |
print("\nRun `glance --os-image-api-version 1 help`" |
| 619 |
" for v1 help") |
| 620 |
else: |
| 621 |
self.get_subcommand_parser(1) |
| 622 |
if command in self.subcommands: |
| 623 |
command = ' ' + command |
| 624 |
print(("\nRun `glance --os-image-api-version 1 help%s`" |
| 625 |
" for v1 help") % (command or '')) |
| 626 |
except ImportError: |
| 627 |
pass |
| 628 |
|
| 629 |
def do_bash_completion(self, _args): |
| 630 |
"""Prints arguments for bash_completion. |
| 631 |
|
| 632 |
Prints all of the commands and options to stdout so that the |
| 633 |
glance.bash_completion script doesn't have to hard code them. |
| 634 |
""" |
| 635 |
commands = set() |
| 636 |
options = set() |
| 637 |
for sc_str, sc in self.subcommands.items(): |
| 638 |
commands.add(sc_str) |
| 639 |
for option in sc._optionals._option_string_actions.keys(): |
| 640 |
options.add(option) |
| 641 |
|
| 642 |
commands.remove('bash_completion') |
| 643 |
commands.remove('bash-completion') |
| 644 |
print(' '.join(commands | options)) |
| 645 |
|
| 646 |
|
| 647 |
class HelpFormatter(argparse.HelpFormatter): |
| 648 |
def start_section(self, heading): |
| 649 |
|
| 650 |
heading = '%s%s' % (heading[0].upper(), heading[1:]) |
| 651 |
super(HelpFormatter, self).start_section(heading) |
| 652 |
|
| 653 |
|
| 654 |
def main(): |
| 655 |
try: |
| 656 |
argv = [encodeutils.safe_decode(a) for a in sys.argv[1:]] |
| 657 |
OpenStackImagesShell().main(argv) |
| 658 |
except KeyboardInterrupt: |
| 659 |
utils.exit('... terminating glance client', exit_code=130) |
| 660 |
except Exception as e: |
| 661 |
if utils.debug_enabled(argv) is True: |
| 662 |
traceback.print_exc() |
| 663 |
utils.exit(str(e)) |