novaclient代码目录说明:
novaclient/openstack:openstack公共代码目录
novaclient/openstack/common
novaclient/tests:单元测试用例
novaclient/v1_1:novaapi v1_1版本功能core功能实现接口
novaclient/v1_1/contrib:插件功能实现接口
novaclient/v3:v3版本功能实现接口
novaclient/auth_plugin.py
novaclient/base.py:基本工具用来构建API操作managers
novaclient/client.py:nova client接口,处理rest请求和响应
novaclient/crypto.py:加密password
novaclient/exceptions.py:异常抛错类
novaclient/extension.py:
novaclient/service_catalog.py
novaclient/shell.py:命令行接口
novaclient/utils.py:工具类
novaclient代码分析:
首先查看nova命令所在目录/usr/local/bin/nova
#!/usr/bin/python
# PBR Generated from u'console_scripts'
import sys
from novaclient.shell import main
if __name__ == "__main__":
sys.exit(main())
来看novaclient命令调用novaclient/shell.py的main方法,来实现命令接口。
def main():
try:
argv = [strutils.safe_decode(a) for a in sys.argv[1:]]
#调用OpenStackComputeShell类的main方法
OpenStackComputeShell().main(argv)
except Exception as e:
logger.debug(e, exc_info=1)
details = {'name': strutils.safe_encode(e.__class__.__name__),
'msg': strutils.safe_encode(six.text_type(e))}
print("ERROR (%(name)s): %(msg)s" % details,
file=sys.stderr)
sys.exit(1)
except KeyboardInterrupt as e:
print("Shutting down novaclient", file=sys.stderr)
sys.exit(1)
novaclient的核心就是调用OpenStackComputeShell()类的main方法:
def main(self, argv):
# Parse args once to find version and debug settings
#加载基本的命令参数
parser = self.get_base_parser()
#把输入的参数argv放入到options对象中,后面参数的调用通过类的属性
(options, args) = parser.parse_known_args(argv)
#打开debug方式
self.setup_debugging(options.debug)
# Discover available auth plugins
novaclient.auth_plugin.discover_auth_systems()
# build available subcommands based on version
#此处api版本os_compute_api_version默认为v1_1
#根据api的版本,搜索出extensions扩展接口的信息
self.extensions = self._discover_extensions(
options.os_compute_api_version)
self._run_extension_hooks('__pre_parse_args__')
# 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'
#获取subcommand命令参数parser对象,默认所有版本为v1_1
subcommand_parser = self.get_subcommand_parser(
options.os_compute_api_version)
#定义parser命令参数对象
self.parser = subcommand_parser
#打印help信息
if options.help or not argv:
subcommand_parser.print_help()
return 0
args = subcommand_parser.parse_args(argv)
self._run_extension_hooks('__post_parse_args__', args)
# Short-circuit and deal with help right away.
if args.func == self.do_help:
self.do_help(args)
return 0
elif args.func == self.do_bash_completion:
self.do_bash_completion(args)
return 0
#根据命令解析参数对相应的变量进行赋值
os_username = args.os_username
os_user_id = args.os_user_id
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
if os_auth_system and os_auth_system != "keystone":
auth_plugin = novaclient.auth_plugin.load_plugin(os_auth_system)
else:
auth_plugin = None
if not endpoint_type:
endpoint_type = DEFAULT_NOVA_ENDPOINT_TYPE
if not service_type:
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]
service_type = utils.get_service_type(args.func) or service_type
# If we have an auth token but no management_url, we must auth anyway.
# Expired tokens are handled by client.py:_cs_request
must_auth = not (cliutils.isunauthenticated(args.func)
or (auth_token and management_url))
#FIXME(usrleon): Here should be restrict for project id same as
# for os_username or os_password but for compatibility it is not.
if must_auth:
if auth_plugin:
auth_plugin.parse_opts(args)
if not auth_plugin or not auth_plugin.opts:
if not os_username and not os_user_id:
raise exc.CommandError(_("You must provide a username "
"or user id via --os-username, --os-user-id, "
"env[OS_USERNAME] or env[OS_USER_ID]"))
if not os_tenant_name and not os_tenant_id:
raise exc.CommandError(_("You must provide a tenant name "
"or tenant id via --os-tenant-name, "
"--os-tenant-id, env[OS_TENANT_NAME] "
"or env[OS_TENANT_ID]"))
if not os_auth_url:
if os_auth_system and os_auth_system != 'keystone':
os_auth_url = auth_plugin.get_auth_url()
if not os_auth_url:
raise exc.CommandError(_("You must provide an auth url "
"via either --os-auth-url or env[OS_AUTH_URL] "
"or specify an auth_system which defines a "
"default url with --os-auth-system "
"or env[OS_AUTH_SYSTEM]"))
if (options.os_compute_api_version and
options.os_compute_api_version != '1.0'):
if not os_tenant_name and not os_tenant_id:
raise exc.CommandError(_("You must provide a tenant name "
"or tenant id via --os-tenant-name, "
"--os-tenant-id, env[OS_TENANT_NAME] "
"or env[OS_TENANT_ID]"))
if not os_auth_url:
raise exc.CommandError(_("You must provide an auth url "
"via either --os-auth-url or env[OS_AUTH_URL]"))
completion_cache = client.CompletionCache(os_username, os_auth_url)
#client类初始化
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,
auth_url=os_auth_url, insecure=insecure,
region_name=os_region_name, endpoint_type=endpoint_type,
extensions=self.extensions, service_type=service_type,
service_name=service_name, auth_system=os_auth_system,
auth_plugin=auth_plugin, auth_token=auth_token,
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,
completion_cache=completion_cache)
# Now check for the password/token of which pieces of the
# identifying keyring key can come from the underlying client
if must_auth:
helper = SecretsHelper(args, self.cs.client)
if (auth_plugin and auth_plugin.opts and
"os_password" not in auth_plugin.opts):
use_pw = False
else:
use_pw = True
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
if tenant_id and auth_token and management_url:
self.cs.client.tenant_id = tenant_id
self.cs.client.auth_token = auth_token
self.cs.client.management_url = management_url
self.cs.client.password_func = lambda: helper.password
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
self.cs.client.keyring_saver = helper
try:
# 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.
if not cliutils.isunauthenticated(args.func):
self.cs.authenticate()
except exc.Unauthorized:
raise exc.CommandError(_("Invalid OpenStack Nova credentials."))
except exc.AuthorizationFailure:
raise exc.CommandError(_("Unable to authorize user"))
if os_compute_api_version == "3" and service_type != 'image':
# 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'
# 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
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)
args.func(self.cs, args)
if args.timings:
self._dump_timings(self.cs.get_timings())
其中get_base_parser方法主要是初始化基本的命令参数
def get_base_parser(self):
#创建NovaClientArgumentParser类实例,也就是argparse.ArgumentParser类的实例
parser = NovaClientArgumentParser(
prog='nova',
description=__doc__.strip(),
epilog='See "nova help COMMAND" '
'for help on a specific command.',
add_help=False,
formatter_class=OpenStackHelpFormatter,
)
# Global arguments
#调用add_argument方法来添加具体的参数
parser.add_argument('-h', '--help',
action='store_true',
help=argparse.SUPPRESS,
)
parser.add_argument('--version',
action='version',
version=novaclient.__version__)
parser.add_argument('--debug',
default=False,
action='store_true',
help=_("Print debugging output"))
parser.add_argument('--os-cache',
default=strutils.bool_from_string(
utils.env('OS_CACHE', default=False), True),
action='store_true',
help=_("Use the auth token cache. Defaults to False if "
"env[OS_CACHE] is not set."))
#此处省略其余部分参数信息
# The auth-system-plugins might require some extra options
novaclient.auth_plugin.load_auth_system_opts(parser)
return parser
其中_discover_extensions方法主要实现以下的功能:
def _discover_extensions(self, version):
extensions = []
#itertools.chain生成迭代器函数
for name, module in itertools.chain(
#根据python path也就是sys.path目录来搜索,返回一个name, module迭代
self._discover_via_python_path(),
#根据contrib path目录来搜索,返回一个name, module迭代
self._discover_via_contrib_path(version),
#通过entry_points搜索,返回一个name, module迭代
self._discover_via_entry_points()):
extension = novaclient.extension.Extension(name, module)
extensions.append(extension)
return extensions
def _discover_via_python_path(self):
#iter_modules迭代返回sys.path目录下所有模块
for (module_loader, name, _ispkg) in pkgutil.iter_modules():
if name.endswith('_python_novaclient_ext'):
if not hasattr(module_loader, 'load_module'):
# Python 2.6 compat: actually get an ImpImporter obj
module_loader = module_loader.find_module(name)
module = module_loader.load_module(name)
if hasattr(module, 'extension_name'):
name = module.extension_name
yield name, module
def _discover_via_contrib_path(self, version):
#通过__file__返回文件所在目录
module_path = os.path.dirname(os.path.abspath(__file__))
#版本号v1.1替换为v1_1
version_str = "v%s" % version.replace('.', '_')
#生成contrib目录绝对路径
ext_path = os.path.join(module_path, version_str, 'contrib')
#路径后添加*.py字符,指定所有py结尾文件
ext_glob = os.path.join(ext_path, "*.py")
#扫描contrib目录所有*.py结尾的文件
for ext_path in glob.iglob(ext_glob):
name = os.path.basename(ext_path)[:-3]
if name == "__init__":
continue
#imp.load_source方法import指定的模块文件
module = imp.load_source(name, ext_path)
yield name, module
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
get_subcommand_parser方法,获取命令参数:
def get_subcommand_parser(self, version):
#获取基本的命令参数parser对象
parser = self.get_base_parser()
#添加子命令列表
self.subcommands = {}
subparsers = parser.add_subparsers(metavar='<subcommand>')
#加载版本默认v1_1的命令接口
try:
actions_module = {
'1.1': shell_v1_1,
'2': shell_v1_1,
'3': shell_v3,
}[version]
except KeyError:
#如果没有相应版本,默认选择v1_1版本
actions_module = shell_v1_1
#查找action方法
self._find_actions(subparsers, actions_module)
self._find_actions(subparsers, self)
#查找加载extension扩展方法
for extension in self.extensions:
self._find_actions(subparsers, extension.module)
self._add_bash_completion_subparser(subparsers)
return parser
分析一下_find_actions(self, subparsers, actions_module)方法,是如何获取相应action方法的
def _find_actions(self, subparsers, actions_module):
#返回actions_module中所有以do_开头的方法名
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
# I prefer to be hyphen-separated instead of underscores.
#替换命令中'_'为'-'
command = attr[3:].replace('_', '-')
#获取方法的回调入口
callback = getattr(actions_module, attr)
desc = callback.__doc__ or ''
action_help = desc.strip()
#获取方法的参数列表
arguments = getattr(callback, 'arguments', [])
subparser = subparsers.add_parser(command,
help=action_help,
description=desc,
add_help=False,
formatter_class=OpenStackHelpFormatter
)
subparser.add_argument('-h', '--help',
action='help',
help=argparse.SUPPRESS,
)
self.subcommands[command] = subparser
#添加方法的参数到subparser对象中
for (args, kwargs) in arguments:
subparser.add_argument(*args, **kwargs)
subparser.set_defaults(func=callback)
初始化Client实例方法:
#client类初始化
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,
auth_url=os_auth_url, insecure=insecure,
region_name=os_region_name, endpoint_type=endpoint_type,
extensions=self.extensions, service_type=service_type,
service_name=service_name, auth_system=os_auth_system,
auth_plugin=auth_plugin, auth_token=auth_token,
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,
completion_cache=completion_cache)
调用了novaclient.client.py中的def Client(version, *args, **kwargs)函数来加载novaclient
def get_client_class(version):
version_map = {
'1.1': 'novaclient.v1_1.client.Client',
'2': 'novaclient.v1_1.client.Client',
'3': 'novaclient.v3.client.Client',
}
try:
#根据版本号加载对应Client
client_path = version_map[str(version)]
except (KeyError, ValueError):
msg = _("Invalid client version '%(version)s'. must be one of: "
"%(keys)s") % {'version': version,
'keys': ', '.join(version_map.keys())}
raise exceptions.UnsupportedVersion(msg)
#返回client对象
return utils.import_class(client_path)
def Client(version, *args, **kwargs):
#调用get_client_class函数,获取指定版本的novaclient
client_class = get_client_class(version)
return client_class(*args, **kwargs)