2013-07-08 16:48:10 -05:00
|
|
|
# Copyright 2012-2013 OpenStack Foundation
|
2015-03-18 10:25:31 -05:00
|
|
|
# Copyright 2015 Dean Troyer
|
2012-04-19 22:41:44 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# 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
|
2012-04-19 22:41:44 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2012-04-19 22:41:44 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# 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.
|
2012-04-19 22:41:44 -05:00
|
|
|
#
|
|
|
|
|
2013-01-31 13:31:41 -06:00
|
|
|
"""Command-line interface to the OpenStack APIs"""
|
2012-04-19 22:41:44 -05:00
|
|
|
|
arguments are not locale decoded into Unicode
When the openstackclient in Python2 passes command line arguments to a
subcommand it fails to pass the arguments as text
(e.g. Unicode). Instead it passes the arguments as binary data encoded
using the current locales encoding.
An easy way to see this is trying to pass a username with a non-ASCII
character.
% openstack user delete ñew
No user with a name or ID of 'ñew' exists.
What occurs internally is when the user data is retrieved it's it
properly represented in a Unicode object. However the username pased
from the command line is still a str object encoded in the locales
encoding (typically UTF-8). A string comparison is attempted between
the encoded data from the command line and the Unicode text found in
the user representation. This seldom ends well, either the comparison
fails to match or a codec error is raised.
There is a hard and fast rule, all text data must be stored in Unicode
objects and the conversion from binary encoded text to Unicode must
occur as close to the I/O boundary as possible. Python3 enforces this
behavior automatically but in Python2 it is the programmers job to do
so.
In the past there have been attempts to fix problems deep inside
internal code by attempting to decode from UTF-8. There are two
problems with this approach. First, internal code has no way to
accurately know what encoding was used to encode the binary data. This
is way it needs to be decoded as close to the I/O source as possible
because that is the best place to know the actual encoding. Guessing
UTF-8 is at best a heuristic. Second, there must be a canonical
representation for data "inside" the program, you don't want dozens of
individual modules, classes, methods, etc. performing conversions,
instead they should be able to make the assumption in what format text
is represented in, the format for text data must be Unicode. This is
another reason to decode as close to the I/O as possible.
In Python3 the argv strings are decoded from the locales encoding by
the interpreter. By the time any Python3 code sees the argv strings
they will be Unicode. However in Python2 there must be explicit code
added to decode the argv strings into Unicode.
The conversion of sys.argv into Unicode only occurs when argv is not
passed to OpenStackShell.run(). If a caller of OpenStackShell.run()
supplies their own arg it is their responsiblity to assure they are
passing actual text objects. Consider this a requirement of the API.
Note: This patch does not contain a unittest to exercise the behavior
because it is difficult to construct a test that depends on command
invocation from a shell. The general structure of the unit tests is to
pass fake argv into OpenStackShell.run() as if it came from a
shell. Because the new code only operates when argv is not passed and
defaults to sys.argv it conflicts with the unittest design.
Change-Id: I779d260744728eae8455ff9dedb6e5c09c165559
Closes-Bug: 1603494
Signed-off-by: John Dennis <jdennis@redhat.com>
2016-07-15 14:46:29 -04:00
|
|
|
import locale
|
2012-04-19 22:41:44 -05:00
|
|
|
import sys
|
|
|
|
|
2016-06-23 17:51:54 -05:00
|
|
|
from osc_lib.api import auth
|
2019-05-16 08:24:58 -05:00
|
|
|
from osc_lib.command import commandmanager
|
2016-06-23 17:51:54 -05:00
|
|
|
from osc_lib import shell
|
|
|
|
import six
|
2012-04-25 16:48:19 -05:00
|
|
|
|
2013-08-02 11:57:17 -05:00
|
|
|
import openstackclient
|
2012-05-02 17:02:08 -04:00
|
|
|
from openstackclient.common import clientmanager
|
2012-04-19 22:41:44 -05:00
|
|
|
|
|
|
|
|
2013-04-18 17:49:42 -05:00
|
|
|
DEFAULT_DOMAIN = 'default'
|
2013-01-31 19:30:25 -06:00
|
|
|
|
2012-04-25 16:48:19 -05:00
|
|
|
|
2016-06-23 17:51:54 -05:00
|
|
|
class OpenStackShell(shell.OpenStackShell):
|
2012-04-25 16:48:19 -05:00
|
|
|
|
|
|
|
def __init__(self):
|
2015-08-18 10:09:46 -06:00
|
|
|
|
2012-04-25 16:48:19 -05:00
|
|
|
super(OpenStackShell, self).__init__(
|
|
|
|
description=__doc__.strip(),
|
2013-08-02 11:57:17 -05:00
|
|
|
version=openstackclient.__version__,
|
2015-03-17 23:44:53 +01:00
|
|
|
command_manager=commandmanager.CommandManager('openstack.cli'),
|
|
|
|
deferred_help=True)
|
2012-04-25 16:48:19 -05:00
|
|
|
|
2014-08-27 23:25:44 -05:00
|
|
|
self.api_version = {}
|
|
|
|
|
2013-10-07 12:23:00 -05:00
|
|
|
# Assume TLS host certificate verification is enabled
|
|
|
|
self.verify = True
|
|
|
|
|
2012-04-25 16:48:19 -05:00
|
|
|
def build_option_parser(self, description, version):
|
|
|
|
parser = super(OpenStackShell, self).build_option_parser(
|
|
|
|
description,
|
2013-01-31 13:31:41 -06:00
|
|
|
version)
|
2016-06-23 17:51:54 -05:00
|
|
|
parser = clientmanager.build_plugin_option_parser(parser)
|
|
|
|
parser = auth.build_auth_plugins_option_parser(parser)
|
|
|
|
return parser
|
2014-10-13 11:13:48 -05:00
|
|
|
|
2016-06-23 17:51:54 -05:00
|
|
|
def _final_defaults(self):
|
|
|
|
super(OpenStackShell, self)._final_defaults()
|
2015-12-10 15:48:52 +03:00
|
|
|
|
2019-08-21 12:20:41 -05:00
|
|
|
# Set the default plugin to admin_token if endpoint and token are given
|
|
|
|
if (self.options.endpoint and self.options.token):
|
|
|
|
# Use token authentication
|
|
|
|
self._auth_type = 'admin_token'
|
2016-08-24 15:26:39 -05:00
|
|
|
else:
|
|
|
|
self._auth_type = 'password'
|
2012-04-19 22:41:44 -05:00
|
|
|
|
2016-06-23 17:51:54 -05:00
|
|
|
def _load_plugins(self):
|
|
|
|
"""Load plugins via stevedore
|
2012-05-10 15:20:40 -04:00
|
|
|
|
2016-06-23 17:51:54 -05:00
|
|
|
osc-lib has no opinion on what plugins should be loaded
|
2012-05-10 15:20:40 -04:00
|
|
|
"""
|
2013-11-20 18:02:09 -06:00
|
|
|
# Loop through extensions to get API versions
|
2014-10-13 11:13:48 -05:00
|
|
|
for mod in clientmanager.PLUGIN_MODULES:
|
2015-05-08 13:14:15 -06:00
|
|
|
default_version = getattr(mod, 'DEFAULT_API_VERSION', None)
|
2017-02-06 18:49:40 +08:00
|
|
|
# Only replace the first instance of "os", some service names will
|
|
|
|
# have "os" in their name, like: "antiddos"
|
|
|
|
option = mod.API_VERSION_OPTION.replace('os_', '', 1)
|
2015-11-04 11:20:43 -07:00
|
|
|
version_opt = str(self.cloud.config.get(option, default_version))
|
2014-08-27 23:25:44 -05:00
|
|
|
if version_opt:
|
|
|
|
api = mod.API_NAME
|
|
|
|
self.api_version[api] = version_opt
|
2015-09-04 16:22:33 -05:00
|
|
|
|
|
|
|
# Add a plugin interface to let the module validate the version
|
|
|
|
# requested by the user
|
|
|
|
skip_old_check = False
|
|
|
|
mod_check_api_version = getattr(mod, 'check_api_version', None)
|
|
|
|
if mod_check_api_version:
|
|
|
|
# this throws an exception if invalid
|
|
|
|
skip_old_check = mod_check_api_version(version_opt)
|
|
|
|
|
|
|
|
mod_versions = getattr(mod, 'API_VERSIONS', None)
|
|
|
|
if not skip_old_check and mod_versions:
|
|
|
|
if version_opt not in mod_versions:
|
2016-10-06 16:02:09 +03:00
|
|
|
sorted_versions = sorted(
|
|
|
|
mod.API_VERSIONS.keys(),
|
|
|
|
key=lambda s: list(map(int, s.split('.'))))
|
2015-09-04 16:22:33 -05:00
|
|
|
self.log.warning(
|
2016-10-06 16:02:09 +03:00
|
|
|
"%s version %s is not in supported versions: %s"
|
|
|
|
% (api, version_opt, ', '.join(sorted_versions)))
|
2015-09-04 16:22:33 -05:00
|
|
|
|
2014-11-14 17:27:58 -06:00
|
|
|
# Command groups deal only with major versions
|
|
|
|
version = '.v' + version_opt.replace('.', '_').split('_')[0]
|
2014-08-27 23:25:44 -05:00
|
|
|
cmd_group = 'openstack.' + api.replace('-', '_') + version
|
|
|
|
self.command_manager.add_command_group(cmd_group)
|
|
|
|
self.log.debug(
|
|
|
|
'%(name)s API version %(version)s, cmd group %(group)s',
|
|
|
|
{'name': api, 'version': version_opt, 'group': cmd_group}
|
|
|
|
)
|
2013-01-31 19:30:25 -06:00
|
|
|
|
2016-06-23 17:51:54 -05:00
|
|
|
def _load_commands(self):
|
|
|
|
"""Load commands via cliff/stevedore
|
|
|
|
|
|
|
|
osc-lib has no opinion on what commands should be loaded
|
|
|
|
"""
|
2013-07-09 17:10:33 -05:00
|
|
|
# Commands that span multiple APIs
|
|
|
|
self.command_manager.add_command_group(
|
|
|
|
'openstack.common')
|
|
|
|
|
2013-03-08 15:39:48 -06:00
|
|
|
# This is the naive extension implementation referred to in
|
|
|
|
# blueprint 'client-extensions'
|
|
|
|
# Extension modules can register their commands in an
|
|
|
|
# 'openstack.extension' entry point group:
|
|
|
|
# entry_points={
|
|
|
|
# 'openstack.extension': [
|
|
|
|
# 'list_repo=qaz.github.repo:ListRepo',
|
|
|
|
# 'show_repo=qaz.github.repo:ShowRepo',
|
|
|
|
# ],
|
|
|
|
# }
|
|
|
|
self.command_manager.add_command_group(
|
|
|
|
'openstack.extension')
|
|
|
|
|
2016-06-23 17:51:54 -05:00
|
|
|
def initialize_app(self, argv):
|
|
|
|
super(OpenStackShell, self).initialize_app(argv)
|
2013-01-31 19:30:25 -06:00
|
|
|
|
2019-08-22 23:55:01 -05:00
|
|
|
# Re-create the client_manager with our subclass
|
2014-10-20 18:53:10 -05:00
|
|
|
self.client_manager = clientmanager.ClientManager(
|
2015-03-02 17:05:35 -06:00
|
|
|
cli_options=self.cloud,
|
2014-10-20 18:53:10 -05:00
|
|
|
api_version=self.api_version,
|
2017-02-01 16:40:04 -06:00
|
|
|
pw_func=shell.prompt_for_password,
|
2014-10-20 18:53:10 -05:00
|
|
|
)
|
|
|
|
|
2012-04-25 16:48:19 -05:00
|
|
|
|
arguments are not locale decoded into Unicode
When the openstackclient in Python2 passes command line arguments to a
subcommand it fails to pass the arguments as text
(e.g. Unicode). Instead it passes the arguments as binary data encoded
using the current locales encoding.
An easy way to see this is trying to pass a username with a non-ASCII
character.
% openstack user delete ñew
No user with a name or ID of 'ñew' exists.
What occurs internally is when the user data is retrieved it's it
properly represented in a Unicode object. However the username pased
from the command line is still a str object encoded in the locales
encoding (typically UTF-8). A string comparison is attempted between
the encoded data from the command line and the Unicode text found in
the user representation. This seldom ends well, either the comparison
fails to match or a codec error is raised.
There is a hard and fast rule, all text data must be stored in Unicode
objects and the conversion from binary encoded text to Unicode must
occur as close to the I/O boundary as possible. Python3 enforces this
behavior automatically but in Python2 it is the programmers job to do
so.
In the past there have been attempts to fix problems deep inside
internal code by attempting to decode from UTF-8. There are two
problems with this approach. First, internal code has no way to
accurately know what encoding was used to encode the binary data. This
is way it needs to be decoded as close to the I/O source as possible
because that is the best place to know the actual encoding. Guessing
UTF-8 is at best a heuristic. Second, there must be a canonical
representation for data "inside" the program, you don't want dozens of
individual modules, classes, methods, etc. performing conversions,
instead they should be able to make the assumption in what format text
is represented in, the format for text data must be Unicode. This is
another reason to decode as close to the I/O as possible.
In Python3 the argv strings are decoded from the locales encoding by
the interpreter. By the time any Python3 code sees the argv strings
they will be Unicode. However in Python2 there must be explicit code
added to decode the argv strings into Unicode.
The conversion of sys.argv into Unicode only occurs when argv is not
passed to OpenStackShell.run(). If a caller of OpenStackShell.run()
supplies their own arg it is their responsiblity to assure they are
passing actual text objects. Consider this a requirement of the API.
Note: This patch does not contain a unittest to exercise the behavior
because it is difficult to construct a test that depends on command
invocation from a shell. The general structure of the unit tests is to
pass fake argv into OpenStackShell.run() as if it came from a
shell. Because the new code only operates when argv is not passed and
defaults to sys.argv it conflicts with the unittest design.
Change-Id: I779d260744728eae8455ff9dedb6e5c09c165559
Closes-Bug: 1603494
Signed-off-by: John Dennis <jdennis@redhat.com>
2016-07-15 14:46:29 -04:00
|
|
|
def main(argv=None):
|
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv[1:]
|
|
|
|
if six.PY2:
|
|
|
|
# Emulate Py3, decode argv into Unicode based on locale so that
|
|
|
|
# commands always see arguments as text instead of binary data
|
|
|
|
encoding = locale.getpreferredencoding()
|
|
|
|
if encoding:
|
|
|
|
argv = map(lambda arg: arg.decode(encoding), argv)
|
|
|
|
|
2013-11-01 13:54:44 -06:00
|
|
|
return OpenStackShell().run(argv)
|
2012-04-19 22:41:44 -05:00
|
|
|
|
2019-06-14 11:51:52 +01:00
|
|
|
|
2012-04-19 22:41:44 -05:00
|
|
|
if __name__ == "__main__":
|
arguments are not locale decoded into Unicode
When the openstackclient in Python2 passes command line arguments to a
subcommand it fails to pass the arguments as text
(e.g. Unicode). Instead it passes the arguments as binary data encoded
using the current locales encoding.
An easy way to see this is trying to pass a username with a non-ASCII
character.
% openstack user delete ñew
No user with a name or ID of 'ñew' exists.
What occurs internally is when the user data is retrieved it's it
properly represented in a Unicode object. However the username pased
from the command line is still a str object encoded in the locales
encoding (typically UTF-8). A string comparison is attempted between
the encoded data from the command line and the Unicode text found in
the user representation. This seldom ends well, either the comparison
fails to match or a codec error is raised.
There is a hard and fast rule, all text data must be stored in Unicode
objects and the conversion from binary encoded text to Unicode must
occur as close to the I/O boundary as possible. Python3 enforces this
behavior automatically but in Python2 it is the programmers job to do
so.
In the past there have been attempts to fix problems deep inside
internal code by attempting to decode from UTF-8. There are two
problems with this approach. First, internal code has no way to
accurately know what encoding was used to encode the binary data. This
is way it needs to be decoded as close to the I/O source as possible
because that is the best place to know the actual encoding. Guessing
UTF-8 is at best a heuristic. Second, there must be a canonical
representation for data "inside" the program, you don't want dozens of
individual modules, classes, methods, etc. performing conversions,
instead they should be able to make the assumption in what format text
is represented in, the format for text data must be Unicode. This is
another reason to decode as close to the I/O as possible.
In Python3 the argv strings are decoded from the locales encoding by
the interpreter. By the time any Python3 code sees the argv strings
they will be Unicode. However in Python2 there must be explicit code
added to decode the argv strings into Unicode.
The conversion of sys.argv into Unicode only occurs when argv is not
passed to OpenStackShell.run(). If a caller of OpenStackShell.run()
supplies their own arg it is their responsiblity to assure they are
passing actual text objects. Consider this a requirement of the API.
Note: This patch does not contain a unittest to exercise the behavior
because it is difficult to construct a test that depends on command
invocation from a shell. The general structure of the unit tests is to
pass fake argv into OpenStackShell.run() as if it came from a
shell. Because the new code only operates when argv is not passed and
defaults to sys.argv it conflicts with the unittest design.
Change-Id: I779d260744728eae8455ff9dedb6e5c09c165559
Closes-Bug: 1603494
Signed-off-by: John Dennis <jdennis@redhat.com>
2016-07-15 14:46:29 -04:00
|
|
|
sys.exit(main())
|