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-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
|
|
|
|
2014-08-27 18:08:14 +03:00
|
|
|
from oslo.utils import encodeutils
|
|
|
|
from oslo.utils import strutils
|
2013-06-24 10:03:19 -05:00
|
|
|
import pkg_resources
|
|
|
|
import six
|
|
|
|
|
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
|
|
|
|
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
|
2014-01-08 12:07:30 +01:00
|
|
|
from novaclient.openstack.common import cliutils
|
2013-12-12 23:17:28 -05:00
|
|
|
from novaclient.openstack.common.gettextutils import _
|
2011-08-03 17:41:33 -04:00
|
|
|
from novaclient import utils
|
|
|
|
from novaclient.v1_1 import shell as shell_v1_1
|
2013-08-06 12:48:24 -05:00
|
|
|
from novaclient.v3 import shell as shell_v3
|
2011-08-03 17:41:33 -04:00
|
|
|
|
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'
|
2013-12-02 23:09:03 +10:30
|
|
|
# NOTE(cyeoh): Having the service type dependent on the API version
|
|
|
|
# is pretty ugly, but we have to do this because traditionally the
|
|
|
|
# catalog entry for compute points directly to the V2 API rather than
|
|
|
|
# the root, and then doing version discovery.
|
|
|
|
DEFAULT_NOVA_SERVICE_TYPE_MAP = {'1.1': 'compute',
|
|
|
|
'2': 'compute',
|
|
|
|
'3': 'computev3'}
|
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:
|
2013-12-12 23:17:28 -05:00
|
|
|
msg = _("%s must be a float") % text
|
2013-01-11 21:44:56 -08:00
|
|
|
raise argparse.ArgumentTypeError(msg)
|
|
|
|
if value <= 0:
|
2013-12-12 23:17:28 -05:00
|
|
|
msg = _("%s must be greater than 0") % text
|
2013-01-11 21:44:56 -08:00
|
|
|
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
|
2013-12-19 19:26:06 +00:00
|
|
|
self._password = None
|
2013-01-30 12:53:35 -08:00
|
|
|
|
|
|
|
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-12-12 23:17:28 -05: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):
|
2013-12-19 19:26:06 +00:00
|
|
|
# Cache password so we prompt user at most once
|
|
|
|
if self._password:
|
|
|
|
pass
|
|
|
|
elif self._validate_string(self.args.os_password):
|
|
|
|
self._password = self.args.os_password
|
|
|
|
else:
|
|
|
|
verify_pass = strutils.bool_from_string(
|
|
|
|
utils.env("OS_VERIFY_PASSWORD", default=False), True)
|
|
|
|
self._password = self._prompt_password(verify_pass)
|
|
|
|
if not self._password:
|
|
|
|
raise exc.CommandError(
|
|
|
|
'Expecting a password provided via either '
|
|
|
|
'--os-password, env[OS_PASSWORD], or '
|
|
|
|
'prompted response')
|
|
|
|
return self._password
|
2013-01-30 12:53:35 -08:00
|
|
|
|
|
|
|
@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)
|
2014-08-05 21:35:12 +02:00
|
|
|
# FIXME(lzyeval): if changes occur in argparse.ArgParser._check_value
|
2011-12-20 11:49:45 +08:00
|
|
|
choose_from = ' (choose from'
|
2012-03-26 12:07:21 -05:00
|
|
|
progparts = self.prog.partition(' ')
|
2013-12-12 23:17:28 -05:00
|
|
|
self.exit(2, _("error: %(errmsg)s\nTry '%(mainp)s help %(subp)s'"
|
|
|
|
" for more information.\n") %
|
2012-03-26 12:07:21 -05:00
|
|
|
{'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',
|
2013-12-12 23:17:28 -05:00
|
|
|
help=_("Print debugging output"))
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2012-12-07 11:47:49 -05:00
|
|
|
parser.add_argument('--os-cache',
|
2013-12-22 08:01:36 -05:00
|
|
|
default=strutils.bool_from_string(
|
|
|
|
utils.env('OS_CACHE', default=False), True),
|
2012-12-07 11:47:49 -05:00
|
|
|
action='store_true',
|
2013-12-12 23:17:28 -05:00
|
|
|
help=_("Use the auth token cache. Defaults to False if "
|
|
|
|
"env[OS_CACHE] is not set."))
|
2012-12-07 11:47:49 -05:00
|
|
|
|
2012-06-15 15:12:23 -03:00
|
|
|
parser.add_argument('--timings',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
2013-12-12 23:17:28 -05:00
|
|
|
help=_("Print call timing info"))
|
2012-06-15 15:12:23 -03:00
|
|
|
|
2013-01-11 21:44:56 -08:00
|
|
|
parser.add_argument('--timeout',
|
|
|
|
default=600,
|
|
|
|
metavar='<seconds>',
|
|
|
|
type=positive_non_zero_float,
|
2013-12-12 23:17:28 -05:00
|
|
|
help=_("Set HTTP call timeout (in seconds)"))
|
2013-01-11 21:44:56 -08:00
|
|
|
|
2013-12-19 19:26:06 +00:00
|
|
|
parser.add_argument('--os-auth-token',
|
|
|
|
default=utils.env('OS_AUTH_TOKEN'),
|
|
|
|
help='Defaults to env[OS_AUTH_TOKEN]')
|
|
|
|
|
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'),
|
2013-12-12 23:17:28 -05: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
|
|
|
|
2014-03-06 12:37:12 +00:00
|
|
|
parser.add_argument('--os-user-id',
|
|
|
|
metavar='<auth-user-id>',
|
|
|
|
default=utils.env('OS_USER_ID'),
|
|
|
|
help=_('Defaults to env[OS_USER_ID].'))
|
|
|
|
|
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'),
|
2013-12-12 23:17:28 -05: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'),
|
2013-12-12 23:17:28 -05: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
|
|
|
|
2013-06-27 22:57:10 +01:00
|
|
|
parser.add_argument('--os-tenant-id',
|
|
|
|
metavar='<auth-tenant-id>',
|
|
|
|
default=utils.env('OS_TENANT_ID'),
|
2013-12-12 23:17:28 -05:00
|
|
|
help=_('Defaults to env[OS_TENANT_ID].'))
|
2013-06-27 22:57:10 +01: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'),
|
2013-12-12 23:17:28 -05: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'),
|
2013-12-12 23:17:28 -05: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>',
|
2013-12-12 23:17:28 -05: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'),
|
2013-12-12 23:17:28 -05: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'),
|
2013-12-12 23:17:28 -05:00
|
|
|
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),
|
2013-12-12 23:17:28 -05:00
|
|
|
help=_('Defaults to env[NOVA_ENDPOINT_TYPE] or ')
|
2012-02-02 10:32:30 -06:00
|
|
|
+ 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.
|
2014-08-05 21:35:12 +02:00
|
|
|
# parser.add_argument('--endpoint_type',
|
|
|
|
# help=argparse.SUPPRESS)
|
2012-08-22 13:01:17 -05:00
|
|
|
|
|
|
|
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),
|
2013-12-12 23:17:28 -05:00
|
|
|
help=_('Accepts 1.1 or 3, '
|
|
|
|
'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',
|
2013-12-12 23:17:28 -05:00
|
|
|
help=_("Explicitly allow novaclient to perform \"insecure\" "
|
2012-07-20 10:20:32 +02:00
|
|
|
"SSL (https) requests. The server's certificate will "
|
|
|
|
"not be verified against any certificate authorities. "
|
2013-12-12 23:17:28 -05:00
|
|
|
"This option should be used with caution."))
|
2011-11-03 02:13:36 -05:00
|
|
|
|
2012-08-22 13:01:17 -05:00
|
|
|
parser.add_argument('--bypass-url',
|
|
|
|
metavar='<bypass-url>',
|
|
|
|
dest='bypass_url',
|
2014-06-03 15:33:15 -05:00
|
|
|
default=utils.env('NOVACLIENT_BYPASS_URL'),
|
|
|
|
help="Use this API endpoint instead of the Service Catalog. "
|
|
|
|
"Defaults to env[NOVACLIENT_BYPASS_URL]")
|
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,
|
2013-08-06 12:48:24 -05:00
|
|
|
'3': shell_v3,
|
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_')):
|
2013-11-22 12:09:47 +08:00
|
|
|
# I prefer to be hyphen-separated instead of underscores.
|
2011-08-03 17:41:33 -04:00
|
|
|
command = attr[3:].replace('_', '-')
|
|
|
|
callback = getattr(actions_module, attr)
|
|
|
|
desc = callback.__doc__ or ''
|
2013-07-29 14:36:31 +09:00
|
|
|
action_help = desc.strip()
|
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):
|
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-12-19 19:26:06 +00:00
|
|
|
os_username = args.os_username
|
2014-03-06 12:37:12 +00:00
|
|
|
os_user_id = args.os_user_id
|
2013-12-19 19:26:06 +00:00
|
|
|
os_password = None # Fetched and set later as needed
|
|
|
|
os_tenant_name = args.os_tenant_name
|
|
|
|
os_tenant_id = args.os_tenant_id
|
|
|
|
os_auth_url = args.os_auth_url
|
|
|
|
os_region_name = args.os_region_name
|
|
|
|
os_auth_system = args.os_auth_system
|
|
|
|
endpoint_type = args.endpoint_type
|
|
|
|
insecure = args.insecure
|
|
|
|
service_type = args.service_type
|
|
|
|
service_name = args.service_name
|
|
|
|
volume_service_name = args.volume_service_name
|
|
|
|
bypass_url = args.bypass_url
|
|
|
|
os_cache = args.os_cache
|
|
|
|
cacert = args.os_cacert
|
|
|
|
timeout = args.timeout
|
|
|
|
|
|
|
|
# We may have either, both or none of these.
|
|
|
|
# If we have both, we don't need USERNAME, PASSWORD etc.
|
|
|
|
# Fill in the blanks from the SecretsHelper if possible.
|
|
|
|
# Finally, authenticate unless we have both.
|
|
|
|
# Note if we don't auth we probably don't have a tenant ID so we can't
|
|
|
|
# cache the token.
|
|
|
|
auth_token = args.os_auth_token if args.os_auth_token else None
|
|
|
|
management_url = bypass_url if bypass_url else None
|
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
|
|
|
|
|
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:
|
2013-12-02 23:09:03 +10:30
|
|
|
os_compute_api_version = (options.os_compute_api_version or
|
|
|
|
DEFAULT_OS_COMPUTE_API_VERSION)
|
|
|
|
try:
|
|
|
|
service_type = DEFAULT_NOVA_SERVICE_TYPE_MAP[
|
|
|
|
os_compute_api_version]
|
|
|
|
except KeyError:
|
|
|
|
service_type = DEFAULT_NOVA_SERVICE_TYPE_MAP[
|
|
|
|
DEFAULT_OS_COMPUTE_API_VERSION]
|
2014-09-19 16:34:48 +03:00
|
|
|
service_type = cliutils.get_service_type(args.func) or service_type
|
2012-02-24 02:30:48 +00:00
|
|
|
|
2013-12-19 19:26:06 +00:00
|
|
|
# If we have an auth token but no management_url, we must auth anyway.
|
|
|
|
# Expired tokens are handled by client.py:_cs_request
|
2014-01-08 12:07:30 +01:00
|
|
|
must_auth = not (cliutils.isunauthenticated(args.func)
|
2013-12-19 19:26:06 +00:00
|
|
|
or (auth_token and management_url))
|
|
|
|
|
2014-08-05 21:35:12 +02: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.
|
2013-12-19 19:26:06 +00:00
|
|
|
if must_auth:
|
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:
|
2014-03-06 12:37:12 +00:00
|
|
|
if not os_username and not os_user_id:
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exc.CommandError(_("You must provide a username "
|
2014-03-06 12:37:12 +00:00
|
|
|
"or user id via --os-username, --os-user-id, "
|
|
|
|
"env[OS_USERNAME] or env[OS_USER_ID]"))
|
2011-11-17 05:12:46 -06:00
|
|
|
|
2013-06-27 22:57:10 +01:00
|
|
|
if not os_tenant_name and not os_tenant_id:
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exc.CommandError(_("You must provide a tenant name "
|
2013-06-27 22:57:10 +01:00
|
|
|
"or tenant id via --os-tenant-name, "
|
|
|
|
"--os-tenant-id, env[OS_TENANT_NAME] "
|
2013-12-12 23:17:28 -05:00
|
|
|
"or env[OS_TENANT_ID]"))
|
2011-11-17 05:12:46 -06:00
|
|
|
|
2012-03-13 22:30:52 -05:00
|
|
|
if not os_auth_url:
|
2013-06-26 13:56:57 -05:00
|
|
|
if os_auth_system and os_auth_system != 'keystone':
|
|
|
|
os_auth_url = auth_plugin.get_auth_url()
|
2012-03-13 22:30:52 -05:00
|
|
|
|
2012-08-02 16:41:47 +02:00
|
|
|
if not os_auth_url:
|
2013-12-12 23:17:28 -05: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] "
|
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-12-12 23:17:28 -05:00
|
|
|
"or env[OS_AUTH_SYSTEM]"))
|
2012-08-02 16:41:47 +02:00
|
|
|
|
2012-04-12 14:16:31 -05:00
|
|
|
if (options.os_compute_api_version and
|
|
|
|
options.os_compute_api_version != '1.0'):
|
2013-06-27 22:57:10 +01:00
|
|
|
if not os_tenant_name and not os_tenant_id:
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exc.CommandError(_("You must provide a tenant name "
|
2013-06-27 22:57:10 +01:00
|
|
|
"or tenant id via --os-tenant-name, "
|
|
|
|
"--os-tenant-id, env[OS_TENANT_NAME] "
|
2013-12-12 23:17:28 -05:00
|
|
|
"or env[OS_TENANT_ID]"))
|
2011-11-17 05:12:46 -06:00
|
|
|
|
2012-03-13 22:30:52 -05:00
|
|
|
if not os_auth_url:
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exc.CommandError(_("You must provide an auth url "
|
|
|
|
"via either --os-auth-url or env[OS_AUTH_URL]"))
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2014-06-19 18:34:17 -05:00
|
|
|
completion_cache = client.CompletionCache(os_username, os_auth_url)
|
|
|
|
|
2014-03-06 12:37:12 +00:00
|
|
|
self.cs = client.Client(options.os_compute_api_version,
|
|
|
|
os_username, os_password, os_tenant_name,
|
|
|
|
tenant_id=os_tenant_id, user_id=os_user_id,
|
2013-06-27 22:57:10 +01:00
|
|
|
auth_url=os_auth_url, insecure=insecure,
|
2012-04-12 14:16:31 -05:00
|
|
|
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-12-19 19:26:06 +00:00
|
|
|
auth_plugin=auth_plugin, auth_token=auth_token,
|
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,
|
2014-06-19 18:34:17 -05:00
|
|
|
cacert=cacert, timeout=timeout,
|
|
|
|
completion_cache=completion_cache)
|
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
|
2013-12-19 19:26:06 +00:00
|
|
|
if must_auth:
|
2013-01-30 12:53:35 -08:00
|
|
|
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-12-19 19:26:06 +00:00
|
|
|
tenant_id = helper.tenant_id
|
|
|
|
# Allow commandline to override cache
|
|
|
|
if not auth_token:
|
|
|
|
auth_token = helper.auth_token
|
|
|
|
if not management_url:
|
|
|
|
management_url = helper.management_url
|
2013-01-31 11:51:29 -08:00
|
|
|
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
|
2014-02-13 02:45:51 +09:00
|
|
|
self.cs.client.password_func = lambda: helper.password
|
2013-12-19 19:26:06 +00:00
|
|
|
elif use_pw:
|
|
|
|
# We're missing something, so auth with user/pass and save
|
|
|
|
# the result in our helper.
|
|
|
|
self.cs.client.password = helper.password
|
2013-01-30 12:53:35 -08:00
|
|
|
self.cs.client.keyring_saver = helper
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
try:
|
2013-12-19 19:26:06 +00:00
|
|
|
# This does a couple of bits which are useful even if we've
|
|
|
|
# got the token + service URL already. It exits fast in that case.
|
2014-01-08 12:07:30 +01:00
|
|
|
if not cliutils.isunauthenticated(args.func):
|
2011-11-17 05:12:46 -06:00
|
|
|
self.cs.authenticate()
|
2011-08-06 00:41:48 -07:00
|
|
|
except exc.Unauthorized:
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exc.CommandError(_("Invalid OpenStack Nova credentials."))
|
2011-08-10 13:25:17 -04:00
|
|
|
except exc.AuthorizationFailure:
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exc.CommandError(_("Unable to authorize user"))
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2014-08-07 04:18:14 +08:00
|
|
|
if options.os_compute_api_version == "3" and service_type != 'image':
|
2013-12-24 21:34:22 +10:30
|
|
|
# NOTE(cyeoh): create an image based client because the
|
|
|
|
# images api is no longer proxied by the V3 API and we
|
|
|
|
# sometimes need to be able to look up images information
|
|
|
|
# via glance when connected to the nova api.
|
|
|
|
image_service_type = 'image'
|
2014-02-08 03:45:47 +09:00
|
|
|
# NOTE(hdd): the password is needed again because creating a new
|
|
|
|
# Client without specifying bypass_url will force authentication.
|
|
|
|
# We can't reuse self.cs's bypass_url, because that's the URL for
|
|
|
|
# the nova service; we need to get glance's URL for this Client
|
|
|
|
if not os_password:
|
|
|
|
os_password = helper.password
|
2013-12-24 21:34:22 +10:30
|
|
|
self.cs.image_cs = client.Client(
|
|
|
|
options.os_compute_api_version, os_username,
|
|
|
|
os_password, os_tenant_name, tenant_id=os_tenant_id,
|
|
|
|
auth_url=os_auth_url, insecure=insecure,
|
|
|
|
region_name=os_region_name, endpoint_type=endpoint_type,
|
|
|
|
extensions=self.extensions, service_type=image_service_type,
|
|
|
|
service_name=service_name, auth_system=os_auth_system,
|
|
|
|
auth_plugin=auth_plugin,
|
|
|
|
volume_service_name=volume_service_name,
|
|
|
|
timings=args.timings, bypass_url=bypass_url,
|
|
|
|
os_cache=os_cache, http_log_debug=options.debug,
|
|
|
|
cacert=cacert, timeout=timeout)
|
|
|
|
|
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:
|
2013-12-12 23:17:28 -05:00
|
|
|
raise exc.CommandError(_("'%s' is not a valid subcommand") %
|
2011-08-06 00:41:48 -07:00
|
|
|
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):
|
2014-06-04 20:11:17 +02:00
|
|
|
def __init__(self, prog, indent_increment=2, max_help_position=32,
|
|
|
|
width=None):
|
|
|
|
super(OpenStackHelpFormatter, self).__init__(prog, indent_increment,
|
|
|
|
max_help_position, width)
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
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:
|
2014-08-27 18:08:14 +03:00
|
|
|
argv = [encodeutils.safe_decode(a) for a in sys.argv[1:]]
|
2014-03-20 13:29:10 -07:00
|
|
|
OpenStackComputeShell().main(argv)
|
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)
|
2014-08-27 18:08:14 +03:00
|
|
|
details = {'name': encodeutils.safe_encode(e.__class__.__name__),
|
|
|
|
'msg': encodeutils.safe_encode(six.text_type(e))}
|
2014-03-18 15:55:58 -04:00
|
|
|
print("ERROR (%(name)s): %(msg)s" % details,
|
2013-06-24 10:03:19 -05:00
|
|
|
file=sys.stderr)
|
2011-08-03 17:41:33 -04:00
|
|
|
sys.exit(1)
|
2013-11-26 23:00:01 -08:00
|
|
|
except KeyboardInterrupt as e:
|
2014-09-25 11:31:26 +05:30
|
|
|
print("... terminating nova client", file=sys.stderr)
|
|
|
|
sys.exit(130)
|
2012-01-23 13:10:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|