2011-08-03 17:41:33 -04:00
|
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
2013-03-13 18:09:17 -04:00
|
|
|
# Copyright 2011 OpenStack Foundation
|
2011-08-03 17:41:33 -04:00
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# 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 OpenStack Nova API.
|
|
|
|
"""
|
|
|
|
|
2013-02-20 09:46:57 +01:00
|
|
|
from __future__ import print_function
|
2011-08-03 17:41:33 -04:00
|
|
|
import argparse
|
2013-01-30 12:53:35 -08:00
|
|
|
import getpass
|
2011-12-15 19:39:33 +00:00
|
|
|
import glob
|
|
|
|
import imp
|
2012-01-06 16:37:28 -06:00
|
|
|
import itertools
|
2013-01-30 12:53:35 -08:00
|
|
|
import logging
|
2011-08-03 17:41:33 -04:00
|
|
|
import os
|
2012-11-07 22:58:32 +00:00
|
|
|
import pkg_resources
|
2012-01-06 16:37:28 -06:00
|
|
|
import pkgutil
|
2011-08-03 17:41:33 -04:00
|
|
|
import sys
|
2013-01-30 12:53:35 -08:00
|
|
|
|
|
|
|
HAS_KEYRING = False
|
2013-03-18 12:02:25 -04:00
|
|
|
all_errors = ValueError
|
2013-01-30 12:53:35 -08:00
|
|
|
try:
|
|
|
|
import keyring
|
|
|
|
HAS_KEYRING = True
|
2013-02-11 15:55:33 -05:00
|
|
|
try:
|
|
|
|
if isinstance(keyring.get_keyring(), keyring.backend.GnomeKeyring):
|
2013-02-21 11:41:02 -05:00
|
|
|
import gnomekeyring
|
2013-03-18 12:02:25 -04:00
|
|
|
all_errors = (ValueError,
|
|
|
|
gnomekeyring.IOError,
|
|
|
|
gnomekeyring.NoKeyringDaemonError)
|
2013-02-11 15:55:33 -05:00
|
|
|
except Exception:
|
2013-03-18 12:02:25 -04:00
|
|
|
pass
|
2013-01-30 12:53:35 -08:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-08-24 18:01:30 +00:00
|
|
|
import novaclient
|
2013-03-06 16:41:46 +01:00
|
|
|
import novaclient.auth_plugin
|
2011-12-29 15:37:05 -05:00
|
|
|
from novaclient import client
|
2011-08-06 00:41:48 -07:00
|
|
|
from novaclient import exceptions as exc
|
2011-12-21 19:25:19 +00:00
|
|
|
import novaclient.extension
|
2013-03-04 18:11:54 +01:00
|
|
|
from novaclient.openstack.common import strutils
|
2011-08-03 17:41:33 -04:00
|
|
|
from novaclient import utils
|
|
|
|
from novaclient.v1_1 import shell as shell_v1_1
|
|
|
|
|
2012-04-12 14:16:31 -05:00
|
|
|
DEFAULT_OS_COMPUTE_API_VERSION = "1.1"
|
2012-02-02 10:32:30 -06:00
|
|
|
DEFAULT_NOVA_ENDPOINT_TYPE = 'publicURL'
|
2012-02-24 02:30:48 +00:00
|
|
|
DEFAULT_NOVA_SERVICE_TYPE = 'compute'
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-01-20 16:28:09 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2012-01-23 13:10:03 -05:00
|
|
|
|
2013-01-11 21:44:56 -08:00
|
|
|
def positive_non_zero_float(text):
|
|
|
|
if text is None:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
value = float(text)
|
|
|
|
except ValueError:
|
|
|
|
msg = "%s must be a float" % text
|
|
|
|
raise argparse.ArgumentTypeError(msg)
|
|
|
|
if value <= 0:
|
|
|
|
msg = "%s must be greater than 0" % text
|
|
|
|
raise argparse.ArgumentTypeError(msg)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2013-01-30 12:53:35 -08:00
|
|
|
class SecretsHelper(object):
|
|
|
|
def __init__(self, args, client):
|
|
|
|
self.args = args
|
|
|
|
self.client = client
|
|
|
|
self.key = None
|
|
|
|
|
|
|
|
def _validate_string(self, text):
|
|
|
|
if text is None or len(text) == 0:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _make_key(self):
|
|
|
|
if self.key is not None:
|
|
|
|
return self.key
|
|
|
|
keys = [
|
|
|
|
self.client.auth_url,
|
|
|
|
self.client.projectid,
|
|
|
|
self.client.user,
|
|
|
|
self.client.region_name,
|
|
|
|
self.client.endpoint_type,
|
|
|
|
self.client.service_type,
|
|
|
|
self.client.service_name,
|
|
|
|
self.client.volume_service_name,
|
|
|
|
]
|
|
|
|
for (index, key) in enumerate(keys):
|
|
|
|
if key is None:
|
|
|
|
keys[index] = '?'
|
|
|
|
else:
|
|
|
|
keys[index] = str(keys[index])
|
|
|
|
self.key = "/".join(keys)
|
|
|
|
return self.key
|
|
|
|
|
|
|
|
def _prompt_password(self, verify=True):
|
|
|
|
pw = None
|
|
|
|
if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty():
|
|
|
|
# Check for Ctl-D
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
pw1 = getpass.getpass('OS Password: ')
|
|
|
|
if verify:
|
|
|
|
pw2 = getpass.getpass('Please verify: ')
|
|
|
|
else:
|
|
|
|
pw2 = pw1
|
|
|
|
if pw1 == pw2 and self._validate_string(pw1):
|
|
|
|
pw = pw1
|
|
|
|
break
|
|
|
|
except EOFError:
|
|
|
|
pass
|
|
|
|
return pw
|
|
|
|
|
2013-01-31 11:51:29 -08:00
|
|
|
def save(self, auth_token, management_url, tenant_id):
|
2013-01-30 12:53:35 -08:00
|
|
|
if not HAS_KEYRING or not self.args.os_cache:
|
|
|
|
return
|
|
|
|
if (auth_token == self.auth_token and
|
|
|
|
management_url == self.management_url):
|
|
|
|
# Nothing changed....
|
|
|
|
return
|
2013-01-31 11:51:29 -08:00
|
|
|
if not all([management_url, auth_token, tenant_id]):
|
2013-01-30 12:53:35 -08:00
|
|
|
raise ValueError("Unable to save empty management url/auth token")
|
2013-01-31 11:51:29 -08:00
|
|
|
value = "|".join([str(auth_token),
|
|
|
|
str(management_url),
|
|
|
|
str(tenant_id)])
|
2013-01-30 12:53:35 -08:00
|
|
|
keyring.set_password("novaclient_auth", self._make_key(), value)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def password(self):
|
|
|
|
if self._validate_string(self.args.os_password):
|
|
|
|
return self.args.os_password
|
|
|
|
if self._validate_string(self.args.apikey):
|
|
|
|
return self.args.apikey
|
2013-02-21 11:15:50 -05:00
|
|
|
verify_pass = utils.bool_from_str(utils.env("OS_VERIFY_PASSWORD"))
|
2013-01-30 12:53:35 -08:00
|
|
|
return self._prompt_password(verify_pass)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def management_url(self):
|
2013-04-02 12:16:37 +02:00
|
|
|
if not HAS_KEYRING or not self.args.os_cache:
|
2013-01-30 12:53:35 -08:00
|
|
|
return None
|
|
|
|
management_url = None
|
|
|
|
try:
|
|
|
|
block = keyring.get_password('novaclient_auth', self._make_key())
|
|
|
|
if block:
|
2013-01-31 11:51:29 -08:00
|
|
|
_token, management_url, _tenant_id = block.split('|', 2)
|
2013-03-18 12:02:25 -04:00
|
|
|
except all_errors:
|
2013-01-30 12:53:35 -08:00
|
|
|
pass
|
|
|
|
return management_url
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auth_token(self):
|
|
|
|
# Now is where it gets complicated since we
|
|
|
|
# want to look into the keyring module, if it
|
|
|
|
# exists and see if anything was provided in that
|
|
|
|
# file that we can use.
|
2013-04-02 12:16:37 +02:00
|
|
|
if not HAS_KEYRING or not self.args.os_cache:
|
2013-01-30 12:53:35 -08:00
|
|
|
return None
|
|
|
|
token = None
|
|
|
|
try:
|
|
|
|
block = keyring.get_password('novaclient_auth', self._make_key())
|
|
|
|
if block:
|
2013-01-31 11:51:29 -08:00
|
|
|
token, _management_url, _tenant_id = block.split('|', 2)
|
2013-03-18 12:02:25 -04:00
|
|
|
except all_errors:
|
2013-01-30 12:53:35 -08:00
|
|
|
pass
|
|
|
|
return token
|
|
|
|
|
2013-01-31 11:51:29 -08:00
|
|
|
@property
|
|
|
|
def tenant_id(self):
|
2013-04-02 12:16:37 +02:00
|
|
|
if not HAS_KEYRING or not self.args.os_cache:
|
2013-01-31 11:51:29 -08:00
|
|
|
return None
|
|
|
|
tenant_id = None
|
|
|
|
try:
|
|
|
|
block = keyring.get_password('novaclient_auth', self._make_key())
|
|
|
|
if block:
|
|
|
|
_token, _management_url, tenant_id = block.split('|', 2)
|
2013-03-18 12:02:25 -04:00
|
|
|
except all_errors:
|
2013-01-31 11:51:29 -08:00
|
|
|
pass
|
|
|
|
return tenant_id
|
|
|
|
|
2013-01-30 12:53:35 -08:00
|
|
|
|
2011-12-20 11:49:45 +08:00
|
|
|
class NovaClientArgumentParser(argparse.ArgumentParser):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(NovaClientArgumentParser, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def error(self, message):
|
|
|
|
"""error(message: string)
|
|
|
|
|
|
|
|
Prints a usage message incorporating the message to stderr and
|
|
|
|
exits.
|
|
|
|
"""
|
|
|
|
self.print_usage(sys.stderr)
|
|
|
|
#FIXME(lzyeval): if changes occur in argparse.ArgParser._check_value
|
|
|
|
choose_from = ' (choose from'
|
2012-03-26 12:07:21 -05:00
|
|
|
progparts = self.prog.partition(' ')
|
|
|
|
self.exit(2, "error: %(errmsg)s\nTry '%(mainp)s help %(subp)s'"
|
|
|
|
" for more information.\n" %
|
|
|
|
{'errmsg': message.split(choose_from)[0],
|
|
|
|
'mainp': progparts[0],
|
|
|
|
'subp': progparts[2]})
|
2011-12-20 11:49:45 +08:00
|
|
|
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
class OpenStackComputeShell(object):
|
|
|
|
|
|
|
|
def get_base_parser(self):
|
2011-12-20 11:49:45 +08:00
|
|
|
parser = NovaClientArgumentParser(
|
2011-08-03 17:41:33 -04:00
|
|
|
prog='nova',
|
|
|
|
description=__doc__.strip(),
|
2013-02-01 12:13:19 -08:00
|
|
|
epilog='See "nova help COMMAND" '
|
2011-08-03 17:41:33 -04:00
|
|
|
'for help on a specific command.',
|
|
|
|
add_help=False,
|
|
|
|
formatter_class=OpenStackHelpFormatter,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Global arguments
|
|
|
|
parser.add_argument('-h', '--help',
|
2012-04-05 13:24:50 +01:00
|
|
|
action='store_true',
|
2011-08-03 17:41:33 -04:00
|
|
|
help=argparse.SUPPRESS,
|
|
|
|
)
|
|
|
|
|
2012-08-24 18:01:30 +00:00
|
|
|
parser.add_argument('--version',
|
|
|
|
action='version',
|
|
|
|
version=novaclient.__version__)
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
parser.add_argument('--debug',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
2012-01-20 16:28:09 -05:00
|
|
|
help="Print debugging output")
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--no-cache',
|
2012-12-12 15:41:31 -08:00
|
|
|
default=not utils.bool_from_str(
|
|
|
|
utils.env('OS_NO_CACHE', default='true')),
|
2012-12-07 11:47:49 -05:00
|
|
|
action='store_false',
|
|
|
|
dest='os_cache',
|
|
|
|
help=argparse.SUPPRESS)
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--no_cache',
|
2012-12-07 11:47:49 -05:00
|
|
|
action='store_false',
|
|
|
|
dest='os_cache',
|
2012-08-22 13:01:17 -05:00
|
|
|
help=argparse.SUPPRESS)
|
2012-06-18 17:37:45 -03:00
|
|
|
|
2012-12-07 11:47:49 -05:00
|
|
|
parser.add_argument('--os-cache',
|
|
|
|
default=utils.env('OS_CACHE', default=False),
|
|
|
|
action='store_true',
|
|
|
|
help="Use the auth token cache.")
|
|
|
|
|
2012-06-15 15:12:23 -03:00
|
|
|
parser.add_argument('--timings',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help="Print call timing info")
|
|
|
|
|
2013-01-11 21:44:56 -08:00
|
|
|
parser.add_argument('--timeout',
|
|
|
|
default=600,
|
|
|
|
metavar='<seconds>',
|
|
|
|
type=positive_non_zero_float,
|
|
|
|
help="Set HTTP call timeout (in seconds)")
|
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os-username',
|
|
|
|
metavar='<auth-user-name>',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('OS_USERNAME', 'NOVA_USERNAME'),
|
2011-12-21 09:59:01 -08:00
|
|
|
help='Defaults to env[OS_USERNAME].')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os_username',
|
|
|
|
help=argparse.SUPPRESS)
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os-password',
|
|
|
|
metavar='<auth-password>',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('OS_PASSWORD', 'NOVA_PASSWORD'),
|
2011-12-16 00:10:47 -08:00
|
|
|
help='Defaults to env[OS_PASSWORD].')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os_password',
|
|
|
|
help=argparse.SUPPRESS)
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os-tenant-name',
|
|
|
|
metavar='<auth-tenant-name>',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('OS_TENANT_NAME', 'NOVA_PROJECT_ID'),
|
2011-12-16 00:10:47 -08:00
|
|
|
help='Defaults to env[OS_TENANT_NAME].')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os_tenant_name',
|
|
|
|
help=argparse.SUPPRESS)
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os-auth-url',
|
|
|
|
metavar='<auth-url>',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('OS_AUTH_URL', 'NOVA_URL'),
|
2011-12-16 00:10:47 -08:00
|
|
|
help='Defaults to env[OS_AUTH_URL].')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os_auth_url',
|
|
|
|
help=argparse.SUPPRESS)
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os-region-name',
|
|
|
|
metavar='<region-name>',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('OS_REGION_NAME', 'NOVA_REGION_NAME'),
|
2012-02-02 10:32:30 -06:00
|
|
|
help='Defaults to env[OS_REGION_NAME].')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os_region_name',
|
|
|
|
help=argparse.SUPPRESS)
|
2011-09-02 11:44:25 -07:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os-auth-system',
|
|
|
|
metavar='<auth-system>',
|
2012-08-02 16:41:47 +02:00
|
|
|
default=utils.env('OS_AUTH_SYSTEM'),
|
|
|
|
help='Defaults to env[OS_AUTH_SYSTEM].')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os_auth_system',
|
|
|
|
help=argparse.SUPPRESS)
|
2012-08-02 16:41:47 +02:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--service-type',
|
|
|
|
metavar='<service-type>',
|
2012-02-24 02:30:48 +00:00
|
|
|
help='Defaults to compute for most actions')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--service_type',
|
|
|
|
help=argparse.SUPPRESS)
|
2012-02-24 02:30:48 +00:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--service-name',
|
|
|
|
metavar='<service-name>',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('NOVA_SERVICE_NAME'),
|
2012-01-31 18:08:22 -06:00
|
|
|
help='Defaults to env[NOVA_SERVICE_NAME]')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--service_name',
|
|
|
|
help=argparse.SUPPRESS)
|
2012-01-31 18:08:22 -06:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--volume-service-name',
|
|
|
|
metavar='<volume-service-name>',
|
2012-04-12 15:41:35 -05:00
|
|
|
default=utils.env('NOVA_VOLUME_SERVICE_NAME'),
|
|
|
|
help='Defaults to env[NOVA_VOLUME_SERVICE_NAME]')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--volume_service_name',
|
|
|
|
help=argparse.SUPPRESS)
|
2012-04-12 15:41:35 -05:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--endpoint-type',
|
|
|
|
metavar='<endpoint-type>',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('NOVA_ENDPOINT_TYPE',
|
2012-02-02 10:32:30 -06:00
|
|
|
default=DEFAULT_NOVA_ENDPOINT_TYPE),
|
|
|
|
help='Defaults to env[NOVA_ENDPOINT_TYPE] or '
|
|
|
|
+ DEFAULT_NOVA_ENDPOINT_TYPE + '.')
|
2012-08-22 13:01:17 -05:00
|
|
|
# NOTE(dtroyer): We can't add --endpoint_type here due to argparse
|
|
|
|
# thinking usage-list --end is ambiguous; but it
|
|
|
|
# works fine with only --endpoint-type present
|
|
|
|
# Go figure. I'm leaving this here for doc purposes.
|
|
|
|
#parser.add_argument('--endpoint_type',
|
|
|
|
# help=argparse.SUPPRESS)
|
|
|
|
|
|
|
|
parser.add_argument('--os-compute-api-version',
|
|
|
|
metavar='<compute-api-ver>',
|
2012-04-12 14:16:31 -05:00
|
|
|
default=utils.env('OS_COMPUTE_API_VERSION',
|
2012-08-22 13:01:17 -05:00
|
|
|
default=DEFAULT_OS_COMPUTE_API_VERSION),
|
2012-04-12 14:16:31 -05:00
|
|
|
help='Accepts 1.1, defaults to env[OS_COMPUTE_API_VERSION].')
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--os_compute_api_version',
|
|
|
|
help=argparse.SUPPRESS)
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-12-18 14:05:29 -06:00
|
|
|
parser.add_argument('--os-cacert',
|
|
|
|
metavar='<ca-certificate>',
|
|
|
|
default=utils.env('OS_CACERT', default=None),
|
|
|
|
help='Specify a CA bundle file to use in '
|
|
|
|
'verifying a TLS (https) server certificate. '
|
|
|
|
'Defaults to env[OS_CACERT]')
|
|
|
|
|
2011-11-03 02:13:36 -05:00
|
|
|
parser.add_argument('--insecure',
|
2012-03-20 23:15:41 +00:00
|
|
|
default=utils.env('NOVACLIENT_INSECURE', default=False),
|
2011-11-03 02:13:36 -05:00
|
|
|
action='store_true',
|
2012-07-20 10:20:32 +02:00
|
|
|
help="Explicitly allow novaclient to perform \"insecure\" "
|
|
|
|
"SSL (https) requests. The server's certificate will "
|
|
|
|
"not be verified against any certificate authorities. "
|
|
|
|
"This option should be used with caution.")
|
2011-11-03 02:13:36 -05:00
|
|
|
|
2012-03-13 22:30:52 -05:00
|
|
|
# FIXME(dtroyer): The args below are here for diablo compatibility,
|
|
|
|
# remove them in folsum cycle
|
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
# alias for --os-username, left in for backwards compatibility
|
2012-03-13 22:30:52 -05:00
|
|
|
parser.add_argument('--username',
|
2012-08-22 13:01:17 -05:00
|
|
|
help=argparse.SUPPRESS)
|
2012-03-13 22:30:52 -05:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
# alias for --os-region-name, left in for backwards compatibility
|
2012-03-13 22:30:52 -05:00
|
|
|
parser.add_argument('--region_name',
|
2012-08-22 13:01:17 -05:00
|
|
|
help=argparse.SUPPRESS)
|
2012-03-13 22:30:52 -05:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
# alias for --os-password, left in for backwards compatibility
|
2012-03-13 22:30:52 -05:00
|
|
|
parser.add_argument('--apikey', '--password', dest='apikey',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('NOVA_API_KEY'),
|
2012-08-22 13:01:17 -05:00
|
|
|
help=argparse.SUPPRESS)
|
2012-02-02 10:32:30 -06:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
# alias for --os-tenant-name, left in for backward compatibility
|
2012-03-13 22:30:52 -05:00
|
|
|
parser.add_argument('--projectid', '--tenant_name', dest='projectid',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('NOVA_PROJECT_ID'),
|
2012-08-22 13:01:17 -05:00
|
|
|
help=argparse.SUPPRESS)
|
2012-02-02 10:32:30 -06:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
# alias for --os-auth-url, left in for backward compatibility
|
2012-03-13 22:30:52 -05:00
|
|
|
parser.add_argument('--url', '--auth_url', dest='url',
|
2012-02-15 11:22:05 +00:00
|
|
|
default=utils.env('NOVA_URL'),
|
2012-08-22 13:01:17 -05:00
|
|
|
help=argparse.SUPPRESS)
|
2012-02-02 10:32:30 -06:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--bypass-url',
|
|
|
|
metavar='<bypass-url>',
|
|
|
|
dest='bypass_url',
|
2012-06-15 15:12:23 -03:00
|
|
|
help="Use this API endpoint instead of the Service Catalog")
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--bypass_url',
|
|
|
|
help=argparse.SUPPRESS)
|
2012-06-15 15:12:23 -03:00
|
|
|
|
2013-03-06 16:41:46 +01:00
|
|
|
# The auth-system-plugins might require some extra options
|
|
|
|
novaclient.auth_plugin.load_auth_system_opts(parser)
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
return parser
|
|
|
|
|
2011-12-21 19:25:19 +00:00
|
|
|
def get_subcommand_parser(self, version):
|
2011-08-03 17:41:33 -04:00
|
|
|
parser = self.get_base_parser()
|
|
|
|
|
|
|
|
self.subcommands = {}
|
|
|
|
subparsers = parser.add_subparsers(metavar='<subcommand>')
|
|
|
|
|
2011-08-04 10:55:08 -04:00
|
|
|
try:
|
|
|
|
actions_module = {
|
|
|
|
'1.1': shell_v1_1,
|
2011-12-09 14:26:06 -05:00
|
|
|
'2': shell_v1_1,
|
2011-08-04 10:55:08 -04:00
|
|
|
}[version]
|
|
|
|
except KeyError:
|
2011-12-15 23:10:59 +00:00
|
|
|
actions_module = shell_v1_1
|
2011-08-03 17:41:33 -04:00
|
|
|
|
|
|
|
self._find_actions(subparsers, actions_module)
|
|
|
|
self._find_actions(subparsers, self)
|
|
|
|
|
2011-12-21 19:25:19 +00:00
|
|
|
for extension in self.extensions:
|
|
|
|
self._find_actions(subparsers, extension.module)
|
2011-12-15 19:39:33 +00:00
|
|
|
|
|
|
|
self._add_bash_completion_subparser(subparsers)
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
return parser
|
|
|
|
|
2011-12-15 19:39:33 +00:00
|
|
|
def _discover_extensions(self, version):
|
2012-01-06 16:37:28 -06:00
|
|
|
extensions = []
|
|
|
|
for name, module in itertools.chain(
|
2012-06-26 11:34:31 -05:00
|
|
|
self._discover_via_python_path(),
|
2012-11-07 22:58:32 +00:00
|
|
|
self._discover_via_contrib_path(version),
|
|
|
|
self._discover_via_entry_points()):
|
2012-01-06 16:37:28 -06:00
|
|
|
|
|
|
|
extension = novaclient.extension.Extension(name, module)
|
|
|
|
extensions.append(extension)
|
|
|
|
|
|
|
|
return extensions
|
|
|
|
|
2012-06-26 11:34:31 -05:00
|
|
|
def _discover_via_python_path(self):
|
|
|
|
for (module_loader, name, _ispkg) in pkgutil.iter_modules():
|
Allow extensions to provide a name when discovered on the python path.
Using novaclient with some extensions via python code, you might have an
invocation like this:
extensions = shell.OpenStackComputeShell()._discover_extensions("1.1")
novaclient = Client("1.1", user, apikey, project, authurl, extensions=extensions,
endpoint_type=shell.DEFAULT_NOVA_ENDPOINT_TYPE,
service_type=shell.DEFAULT_NOVA_SERVICE_TYPE)
If you have an extension like 'myextension.py' in the v1_1/contrib directory, you'll
end up with a very sensible attribute on the resulting novaclient object, i.e.
novaclient.myextension
If you have a package distributed in the package myextension_python_novaclient_ext,
then it'll automatically be picked up as an extension (awesome!) but the name is not
as intuitive.
novaclient.myextension_python_novaclient_ext
This patch simply changes this to allow the Extension to provide a name for itself.
The possibility of collisions exists, but is not really any more significant than
before (where you might have different versions of the same package installed in the
system or heck, even a bizarrely named 'myextension_python_novaclient_ext.py' in the
contrib/ directory).
Fixes bug 1058366
Change-Id: Ie68463ffd7a939744e035b20fd50a7dc8da605de
2013-02-13 13:47:45 -05:00
|
|
|
if name.endswith('_python_novaclient_ext'):
|
2012-01-10 21:33:07 +00:00
|
|
|
if not hasattr(module_loader, 'load_module'):
|
|
|
|
# Python 2.6 compat: actually get an ImpImporter obj
|
|
|
|
module_loader = module_loader.find_module(name)
|
|
|
|
|
2012-01-06 16:37:28 -06:00
|
|
|
module = module_loader.load_module(name)
|
Allow extensions to provide a name when discovered on the python path.
Using novaclient with some extensions via python code, you might have an
invocation like this:
extensions = shell.OpenStackComputeShell()._discover_extensions("1.1")
novaclient = Client("1.1", user, apikey, project, authurl, extensions=extensions,
endpoint_type=shell.DEFAULT_NOVA_ENDPOINT_TYPE,
service_type=shell.DEFAULT_NOVA_SERVICE_TYPE)
If you have an extension like 'myextension.py' in the v1_1/contrib directory, you'll
end up with a very sensible attribute on the resulting novaclient object, i.e.
novaclient.myextension
If you have a package distributed in the package myextension_python_novaclient_ext,
then it'll automatically be picked up as an extension (awesome!) but the name is not
as intuitive.
novaclient.myextension_python_novaclient_ext
This patch simply changes this to allow the Extension to provide a name for itself.
The possibility of collisions exists, but is not really any more significant than
before (where you might have different versions of the same package installed in the
system or heck, even a bizarrely named 'myextension_python_novaclient_ext.py' in the
contrib/ directory).
Fixes bug 1058366
Change-Id: Ie68463ffd7a939744e035b20fd50a7dc8da605de
2013-02-13 13:47:45 -05:00
|
|
|
if hasattr(module, 'extension_name'):
|
|
|
|
name = module.extension_name
|
|
|
|
|
2012-01-06 16:37:28 -06:00
|
|
|
yield name, module
|
|
|
|
|
|
|
|
def _discover_via_contrib_path(self, version):
|
2011-12-15 19:39:33 +00:00
|
|
|
module_path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
version_str = "v%s" % version.replace('.', '_')
|
|
|
|
ext_path = os.path.join(module_path, version_str, 'contrib')
|
|
|
|
ext_glob = os.path.join(ext_path, "*.py")
|
|
|
|
|
|
|
|
for ext_path in glob.iglob(ext_glob):
|
|
|
|
name = os.path.basename(ext_path)[:-3]
|
2011-12-15 22:38:05 +00:00
|
|
|
|
|
|
|
if name == "__init__":
|
|
|
|
continue
|
|
|
|
|
2011-12-21 19:25:19 +00:00
|
|
|
module = imp.load_source(name, ext_path)
|
2012-01-06 16:37:28 -06:00
|
|
|
yield name, module
|
2011-12-15 19:39:33 +00:00
|
|
|
|
2012-11-07 22:58:32 +00:00
|
|
|
def _discover_via_entry_points(self):
|
|
|
|
for ep in pkg_resources.iter_entry_points('novaclient.extension'):
|
|
|
|
name = ep.name
|
|
|
|
module = ep.load()
|
|
|
|
|
|
|
|
yield name, module
|
|
|
|
|
2011-12-15 19:39:33 +00:00
|
|
|
def _add_bash_completion_subparser(self, subparsers):
|
|
|
|
subparser = subparsers.add_parser('bash_completion',
|
|
|
|
add_help=False,
|
|
|
|
formatter_class=OpenStackHelpFormatter
|
|
|
|
)
|
|
|
|
self.subcommands['bash_completion'] = subparser
|
|
|
|
subparser.set_defaults(func=self.do_bash_completion)
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
def _find_actions(self, subparsers, actions_module):
|
|
|
|
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
|
|
|
|
# I prefer to be hypen-separated instead of underscores.
|
|
|
|
command = attr[3:].replace('_', '-')
|
|
|
|
callback = getattr(actions_module, attr)
|
|
|
|
desc = callback.__doc__ or ''
|
2012-06-26 11:34:31 -05:00
|
|
|
action_help = desc.strip().split('\n')[0]
|
2011-08-03 17:41:33 -04:00
|
|
|
arguments = getattr(callback, 'arguments', [])
|
|
|
|
|
|
|
|
subparser = subparsers.add_parser(command,
|
2012-06-26 11:34:31 -05:00
|
|
|
help=action_help,
|
2011-08-03 17:41:33 -04:00
|
|
|
description=desc,
|
|
|
|
add_help=False,
|
|
|
|
formatter_class=OpenStackHelpFormatter
|
|
|
|
)
|
|
|
|
subparser.add_argument('-h', '--help',
|
|
|
|
action='help',
|
|
|
|
help=argparse.SUPPRESS,
|
|
|
|
)
|
|
|
|
self.subcommands[command] = subparser
|
|
|
|
for (args, kwargs) in arguments:
|
|
|
|
subparser.add_argument(*args, **kwargs)
|
|
|
|
subparser.set_defaults(func=callback)
|
|
|
|
|
2012-01-20 16:28:09 -05:00
|
|
|
def setup_debugging(self, debug):
|
|
|
|
if not debug:
|
|
|
|
return
|
|
|
|
|
|
|
|
streamformat = "%(levelname)s (%(module)s:%(lineno)d) %(message)s"
|
2013-03-13 16:47:16 +01:00
|
|
|
# Set up the root logger to debug so that the submodules can
|
|
|
|
# print debug messages
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
|
|
format=streamformat)
|
2012-01-20 16:28:09 -05:00
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
def main(self, argv):
|
2013-03-06 16:41:46 +01:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
# Parse args once to find version and debug settings
|
2011-08-03 17:41:33 -04:00
|
|
|
parser = self.get_base_parser()
|
|
|
|
(options, args) = parser.parse_known_args(argv)
|
2012-01-20 16:28:09 -05:00
|
|
|
self.setup_debugging(options.debug)
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2013-03-06 16:41:46 +01:00
|
|
|
# Discover available auth plugins
|
|
|
|
novaclient.auth_plugin.discover_auth_systems()
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
# build available subcommands based on version
|
2012-04-12 14:16:31 -05:00
|
|
|
self.extensions = self._discover_extensions(
|
|
|
|
options.os_compute_api_version)
|
2011-12-21 19:25:19 +00:00
|
|
|
self._run_extension_hooks('__pre_parse_args__')
|
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
# NOTE(dtroyer): Hackery to handle --endpoint_type due to argparse
|
|
|
|
# thinking usage-list --end is ambiguous; but it
|
|
|
|
# works fine with only --endpoint-type present
|
|
|
|
# Go figure.
|
|
|
|
if '--endpoint_type' in argv:
|
|
|
|
spot = argv.index('--endpoint_type')
|
|
|
|
argv[spot] = '--endpoint-type'
|
|
|
|
|
2012-04-12 14:16:31 -05:00
|
|
|
subcommand_parser = self.get_subcommand_parser(
|
|
|
|
options.os_compute_api_version)
|
2011-08-03 17:41:33 -04:00
|
|
|
self.parser = subcommand_parser
|
|
|
|
|
2012-11-08 21:03:13 +01:00
|
|
|
if options.help or not argv:
|
2012-04-05 13:24:50 +01:00
|
|
|
subcommand_parser.print_help()
|
|
|
|
return 0
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
args = subcommand_parser.parse_args(argv)
|
2011-12-21 19:25:19 +00:00
|
|
|
self._run_extension_hooks('__post_parse_args__', args)
|
2011-08-03 17:41:33 -04:00
|
|
|
|
|
|
|
# Short-circuit and deal with help right away.
|
|
|
|
if args.func == self.do_help:
|
|
|
|
self.do_help(args)
|
|
|
|
return 0
|
2011-12-15 19:39:33 +00:00
|
|
|
elif args.func == self.do_bash_completion:
|
|
|
|
self.do_bash_completion(args)
|
|
|
|
return 0
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2013-01-30 12:53:35 -08:00
|
|
|
(os_username, os_tenant_name, os_auth_url,
|
2012-08-02 16:41:47 +02:00
|
|
|
os_region_name, os_auth_system, endpoint_type, insecure,
|
2012-04-12 15:41:35 -05:00
|
|
|
service_type, service_name, volume_service_name,
|
2013-01-30 12:53:35 -08:00
|
|
|
username, projectid, url, region_name,
|
2013-01-11 21:44:56 -08:00
|
|
|
bypass_url, os_cache, cacert, timeout) = (
|
2013-01-30 12:53:35 -08:00
|
|
|
args.os_username,
|
2012-03-13 22:30:52 -05:00
|
|
|
args.os_tenant_name, args.os_auth_url,
|
2012-08-02 16:41:47 +02:00
|
|
|
args.os_region_name, args.os_auth_system,
|
|
|
|
args.endpoint_type, args.insecure, args.service_type,
|
|
|
|
args.service_name, args.volume_service_name,
|
2013-01-30 12:53:35 -08:00
|
|
|
args.username, args.projectid,
|
2012-06-15 15:12:23 -03:00
|
|
|
args.url, args.region_name,
|
2012-12-18 14:05:29 -06:00
|
|
|
args.bypass_url, args.os_cache,
|
2013-01-11 21:44:56 -08:00
|
|
|
args.os_cacert, args.timeout)
|
2011-11-08 10:27:41 -08:00
|
|
|
|
2013-03-06 16:41:46 +01:00
|
|
|
if os_auth_system and os_auth_system != "keystone":
|
|
|
|
auth_plugin = novaclient.auth_plugin.load_plugin(os_auth_system)
|
|
|
|
else:
|
|
|
|
auth_plugin = None
|
|
|
|
|
2013-01-30 12:53:35 -08:00
|
|
|
# Fetched and set later as needed
|
|
|
|
os_password = None
|
|
|
|
|
2012-01-31 18:08:22 -06:00
|
|
|
if not endpoint_type:
|
2012-02-02 10:32:30 -06:00
|
|
|
endpoint_type = DEFAULT_NOVA_ENDPOINT_TYPE
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-02-24 02:30:48 +00:00
|
|
|
if not service_type:
|
|
|
|
service_type = DEFAULT_NOVA_SERVICE_TYPE
|
|
|
|
service_type = utils.get_service_type(args.func) or service_type
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
#FIXME(usrleon): Here should be restrict for project id same as
|
2012-03-13 22:30:52 -05:00
|
|
|
# for os_username or os_password but for compatibility it is not.
|
2011-11-17 05:12:46 -06:00
|
|
|
if not utils.isunauthenticated(args.func):
|
2013-03-06 16:41:46 +01:00
|
|
|
if auth_plugin:
|
|
|
|
auth_plugin.parse_opts(args)
|
|
|
|
|
|
|
|
if not auth_plugin or not auth_plugin.opts:
|
|
|
|
if not os_username:
|
|
|
|
if not username:
|
|
|
|
raise exc.CommandError("You must provide a username "
|
|
|
|
"via either --os-username or env[OS_USERNAME]")
|
|
|
|
else:
|
|
|
|
os_username = username
|
2011-11-17 05:12:46 -06:00
|
|
|
|
2012-03-13 22:30:52 -05:00
|
|
|
if not os_tenant_name:
|
2012-02-02 10:32:30 -06:00
|
|
|
if not projectid:
|
|
|
|
raise exc.CommandError("You must provide a tenant name "
|
2012-08-22 13:01:17 -05:00
|
|
|
"via either --os-tenant-name or "
|
2012-03-13 22:30:52 -05:00
|
|
|
"env[OS_TENANT_NAME]")
|
2012-02-02 10:32:30 -06:00
|
|
|
else:
|
2012-03-13 22:30:52 -05:00
|
|
|
os_tenant_name = projectid
|
2011-11-17 05:12:46 -06:00
|
|
|
|
2012-03-13 22:30:52 -05:00
|
|
|
if not os_auth_url:
|
2012-02-02 10:32:30 -06:00
|
|
|
if not url:
|
2012-08-02 16:41:47 +02:00
|
|
|
if os_auth_system and os_auth_system != 'keystone':
|
2013-03-06 16:41:46 +01:00
|
|
|
os_auth_url = auth_plugin.get_auth_url()
|
2012-02-02 10:32:30 -06:00
|
|
|
else:
|
2012-03-13 22:30:52 -05:00
|
|
|
os_auth_url = url
|
|
|
|
|
2012-08-02 16:41:47 +02:00
|
|
|
if not os_auth_url:
|
|
|
|
raise exc.CommandError("You must provide an auth url "
|
2012-08-22 13:01:17 -05:00
|
|
|
"via either --os-auth-url or env[OS_AUTH_URL] "
|
2012-08-02 16:41:47 +02:00
|
|
|
"or specify an auth_system which defines a "
|
2012-08-22 13:01:17 -05:00
|
|
|
"default url with --os-auth-system "
|
2013-03-06 15:40:37 +01:00
|
|
|
"or env[OS_AUTH_SYSTEM]")
|
2012-08-02 16:41:47 +02:00
|
|
|
|
2012-03-13 22:30:52 -05:00
|
|
|
if not os_region_name and region_name:
|
|
|
|
os_region_name = region_name
|
2011-11-17 05:12:46 -06:00
|
|
|
|
2012-04-12 14:16:31 -05:00
|
|
|
if (options.os_compute_api_version and
|
|
|
|
options.os_compute_api_version != '1.0'):
|
2012-03-13 22:30:52 -05:00
|
|
|
if not os_tenant_name:
|
2012-02-02 10:32:30 -06:00
|
|
|
raise exc.CommandError("You must provide a tenant name "
|
2012-08-22 13:01:17 -05:00
|
|
|
"via either --os-tenant-name or env[OS_TENANT_NAME]")
|
2011-11-17 05:12:46 -06:00
|
|
|
|
2012-03-13 22:30:52 -05:00
|
|
|
if not os_auth_url:
|
2012-02-02 10:32:30 -06:00
|
|
|
raise exc.CommandError("You must provide an auth url "
|
2012-08-22 13:01:17 -05:00
|
|
|
"via either --os-auth-url or env[OS_AUTH_URL]")
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-04-12 14:16:31 -05:00
|
|
|
self.cs = client.Client(options.os_compute_api_version, os_username,
|
|
|
|
os_password, os_tenant_name, os_auth_url, insecure,
|
|
|
|
region_name=os_region_name, endpoint_type=endpoint_type,
|
|
|
|
extensions=self.extensions, service_type=service_type,
|
2012-08-02 16:41:47 +02:00
|
|
|
service_name=service_name, auth_system=os_auth_system,
|
2013-03-06 16:41:46 +01:00
|
|
|
auth_plugin=auth_plugin,
|
2012-06-15 15:12:23 -03:00
|
|
|
volume_service_name=volume_service_name,
|
2012-06-18 17:37:45 -03:00
|
|
|
timings=args.timings, bypass_url=bypass_url,
|
2012-12-18 14:05:29 -06:00
|
|
|
os_cache=os_cache, http_log_debug=options.debug,
|
2013-01-11 21:44:56 -08:00
|
|
|
cacert=cacert, timeout=timeout)
|
2011-08-03 18:38:29 -04:00
|
|
|
|
2013-01-30 12:53:35 -08:00
|
|
|
# Now check for the password/token of which pieces of the
|
|
|
|
# identifying keyring key can come from the underlying client
|
|
|
|
if not utils.isunauthenticated(args.func):
|
|
|
|
helper = SecretsHelper(args, self.cs.client)
|
2013-03-06 16:41:46 +01:00
|
|
|
if (auth_plugin and auth_plugin.opts and
|
|
|
|
"os_password" not in auth_plugin.opts):
|
|
|
|
use_pw = False
|
|
|
|
else:
|
|
|
|
use_pw = True
|
|
|
|
|
2013-01-31 11:51:29 -08:00
|
|
|
tenant_id, auth_token, management_url = (helper.tenant_id,
|
|
|
|
helper.auth_token,
|
|
|
|
helper.management_url)
|
|
|
|
if tenant_id and auth_token and management_url:
|
|
|
|
self.cs.client.tenant_id = tenant_id
|
2013-01-30 12:53:35 -08:00
|
|
|
self.cs.client.auth_token = auth_token
|
|
|
|
self.cs.client.management_url = management_url
|
|
|
|
# Try to auth with the given info, if it fails
|
|
|
|
# go into password mode...
|
|
|
|
try:
|
|
|
|
self.cs.authenticate()
|
|
|
|
use_pw = False
|
|
|
|
except (exc.Unauthorized, exc.AuthorizationFailure):
|
|
|
|
# Likely it expired or just didn't work...
|
|
|
|
self.cs.client.auth_token = None
|
|
|
|
self.cs.client.management_url = None
|
|
|
|
if use_pw:
|
|
|
|
# Auth using token must have failed or not happened
|
|
|
|
# at all, so now switch to password mode and save
|
|
|
|
# the token when its gotten... using our keyring
|
|
|
|
# saver
|
|
|
|
os_password = helper.password
|
|
|
|
if not os_password:
|
|
|
|
raise exc.CommandError(
|
|
|
|
'Expecting a password provided via either '
|
|
|
|
'--os-password, env[OS_PASSWORD], or '
|
|
|
|
'prompted response')
|
|
|
|
self.cs.client.password = os_password
|
|
|
|
self.cs.client.keyring_saver = helper
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
try:
|
2011-11-17 05:12:46 -06:00
|
|
|
if not utils.isunauthenticated(args.func):
|
|
|
|
self.cs.authenticate()
|
2011-08-06 00:41:48 -07:00
|
|
|
except exc.Unauthorized:
|
|
|
|
raise exc.CommandError("Invalid OpenStack Nova credentials.")
|
2011-08-10 13:25:17 -04:00
|
|
|
except exc.AuthorizationFailure:
|
|
|
|
raise exc.CommandError("Unable to authorize user")
|
2011-08-03 17:41:33 -04:00
|
|
|
|
|
|
|
args.func(self.cs, args)
|
|
|
|
|
2012-06-15 15:12:23 -03:00
|
|
|
if args.timings:
|
|
|
|
self._dump_timings(self.cs.get_timings())
|
|
|
|
|
|
|
|
def _dump_timings(self, timings):
|
|
|
|
class Tyme(object):
|
|
|
|
def __init__(self, url, seconds):
|
|
|
|
self.url = url
|
|
|
|
self.seconds = seconds
|
|
|
|
results = [Tyme(url, end - start) for url, start, end in timings]
|
|
|
|
total = 0.0
|
|
|
|
for tyme in results:
|
|
|
|
total += tyme.seconds
|
|
|
|
results.append(Tyme("Total", total))
|
|
|
|
utils.print_list(results, ["url", "seconds"], sortby_index=None)
|
|
|
|
|
2011-12-21 19:25:19 +00:00
|
|
|
def _run_extension_hooks(self, hook_type, *args, **kwargs):
|
|
|
|
"""Run hooks for all registered extensions."""
|
|
|
|
for extension in self.extensions:
|
|
|
|
extension.run_hooks(hook_type, *args, **kwargs)
|
|
|
|
|
2012-06-26 11:34:31 -05:00
|
|
|
def do_bash_completion(self, _args):
|
2011-12-15 19:39:33 +00:00
|
|
|
"""
|
|
|
|
Prints all of the commands and options to stdout so that the
|
|
|
|
nova.bash_completion script doesn't have to hard code them.
|
|
|
|
"""
|
|
|
|
commands = set()
|
|
|
|
options = set()
|
|
|
|
for sc_str, sc in self.subcommands.items():
|
|
|
|
commands.add(sc_str)
|
|
|
|
for option in sc._optionals._option_string_actions.keys():
|
|
|
|
options.add(option)
|
|
|
|
|
|
|
|
commands.remove('bash-completion')
|
|
|
|
commands.remove('bash_completion')
|
2013-02-20 09:46:57 +01:00
|
|
|
print(' '.join(commands | options))
|
2011-12-15 19:39:33 +00:00
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
@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 args.command:
|
|
|
|
if args.command in self.subcommands:
|
|
|
|
self.subcommands[args.command].print_help()
|
|
|
|
else:
|
2011-08-06 00:41:48 -07:00
|
|
|
raise exc.CommandError("'%s' is not a valid subcommand" %
|
|
|
|
args.command)
|
2011-08-03 17:41:33 -04:00
|
|
|
else:
|
|
|
|
self.parser.print_help()
|
|
|
|
|
|
|
|
|
|
|
|
# I'm picky about my shell help.
|
|
|
|
class OpenStackHelpFormatter(argparse.HelpFormatter):
|
|
|
|
def start_section(self, heading):
|
|
|
|
# Title-case the headings
|
|
|
|
heading = '%s%s' % (heading[0].upper(), heading[1:])
|
|
|
|
super(OpenStackHelpFormatter, self).start_section(heading)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
2013-03-04 18:11:54 +01:00
|
|
|
OpenStackComputeShell().main(map(strutils.safe_decode, sys.argv[1:]))
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2013-02-20 09:46:57 +01:00
|
|
|
except Exception as e:
|
2012-01-20 16:28:09 -05:00
|
|
|
logger.debug(e, exc_info=1)
|
2013-03-04 18:11:54 +01:00
|
|
|
print("ERROR: %s" % strutils.safe_encode(unicode(e)), file=sys.stderr)
|
2011-08-03 17:41:33 -04:00
|
|
|
sys.exit(1)
|
2012-01-23 13:10:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|