keystone在httpd的入口执行文件为/usr/bin/keystone-wsgi-public
查看文件/usr/bin/keystone-wsgi-public,根据代码,看到主要是这几行代码对keystone的服务进行了初始化,keystone服务通过wsgiref.simple_server与httpd进行交互,初始化函数为initialize_public_application
from keystone.server.wsgi import initialize_public_application if __name__ == "__main__":
......
import wsgiref.simple_server as wss ...... server = wss.make_server(args.host, args.port, initialize_public_application()) .......
server.serve_forever()
查看initialize_public_application
from keystone.server.flask import core as flask_core
def initialize_public_application():
return flask_core.initialize_application(name='public', config_files=flask_core._get_config_files())
initialize_admin_application = initialize_public_application
从这里可以看到在Rocky版本的keystone中,admin和public已经合并在了一起,不再区分admin和public,flask_core.initialize_application接受三个参数,原型如下
def initialize_application(name, post_log_configured_function=lambda: None,
config_files=None):
possible_topdir = os.path.normpath(os.path.join(
os.path.abspath(__file__),
os.pardir,
os.pardir,
os.pardir,
os.pardir)) dev_conf = os.path.join(possible_topdir,
'etc',
'keystone.conf')
if not config_files:
config_files = None
if os.path.exists(dev_conf):
config_files = [dev_conf] keystone.server.configure(config_files=config_files) if CONF.debug:
CONF.log_opt_values(log.getLogger(CONF.prog), log.DEBUG) post_log_configured_function() def loadapp():
app = application.application_factory(name)
return app _unused, app = keystone.server.setup_backends(
startup_application_fn=loadapp) profiler.setup(name) return setup_app_middleware(app)
name 这个不需要讲,但是name必须是public或者admin,在后面的初始化过程中有限制
post_log_configured_function 接收一个函数,会在initialize_application这个函数中调用,但是具体是什么作用,还没有领悟出来,但是这个函数应该是可以做一些类似于初始化等等作用的
config_files 在开发模式下可以自定义keystone.conf,如果自己定义了keyston.conf这个文件,那么就会完全的替代/etc/keystone/keystone.conf中定义的一些配置参数,关于配置参数,后续会介绍 接着往下看这个函数
keystone.server.configure这个函数对keystone进行了一些配置
def configure(version=None, config_files=None,
pre_setup_logging_fn=lambda: None):
keystone.conf.configure()
sql.initialize()
keystone.conf.set_config_defaults() CONF(project='keystone', version=version,
default_config_files=config_files) pre_setup_logging_fn()
keystone.conf.setup_logging() if CONF.insecure_debug:
LOG.warning(
'insecure_debug is enabled so responses may include sensitive '
'information.')
keystone.conf.configure()这个函数添加了几个参数,
standard-threads 可以自定义多少个线程
pydev-debug-host以及pydev-debug-port是对远程debug的机器和port进行了定义,并在conf中注册了它们三个参数
def configure(conf=None):
if conf is None:
conf = CONF conf.register_cli_opt(
cfg.BoolOpt('standard-threads', default=False,
help='Do not monkey-patch threading system modules.'))
conf.register_cli_opt(
cfg.StrOpt('pydev-debug-host',
help='Host to connect to for remote debugger.'))
conf.register_cli_opt(
cfg.PortOpt('pydev-debug-port',
help='Port to connect to for remote debugger.')) for module in conf_modules:
module.register_opts(conf) # register any non-default auth methods here (used by extensions, etc)
auth.setup_authentication() # add oslo.cache related config options
cache.configure(conf)
auth.setup_authentication(),配置了几种认证方式,password,token等等,跟上述参数一样,先定义,后注册到conf中
cache.configure(conf),对cache进行了定义和注册,主要涉及到各种cache的定义等等 回到 keystone.server.configure
sql.initialize() 这个函数主要是对数据库进行了初始化,导入配置文件中的参数
keystone.conf.set_config_defaults() 采用默认值对keystone的设置进行初始化
CONF(project='keystone', version=version, default_config_files=config_files) 这块比较难理解,先说第三个参数,第三个参数采用指定的config_files对默认参数进行复写 CONF的原型是 keystone.conf.CONF, 它采用了从文件中直接导入的方式,构造了一个单例的configure配置类 CONF = ConfigOpts() CONF()调用了ConfigOpts.__call__()这个方法
pre_setup_logging_fn()这个和前文的post_log_configured_function是相似的,都是完成一些自定义的动作
keystone.conf.setup_logging()这个参数是对log进行配置 回到initialize_application
keystone.server.setup_backend(startup_application_fn=loadapp)
def setup_backends(load_extra_backends_fn=lambda: {},
startup_application_fn=lambda: None):
drivers = backends.load_backends()
drivers.update(load_extra_backends_fn())
res = startup_application_fn()
return drivers, res
def loadapp():
app = application.application_factory(name)
return app
这两段代码中,setup_backends首先导入一些driver,导入方法主要是用stevedore的DriverManager类对一些方法进行导入,并将这些driver注册到provider_api中,供后续调用
load_app采用了flask.application的工厂模式方法构造
def application_factory(name='public'):
if name not in ('admin', 'public'):
raise RuntimeError('Application name (for base_url lookup) must be '
'either `admin` or `public`.') app = flask.Flask(name)
app.after_request(_add_vary_x_auth_token_header) app.config.update(PROPAGATE_EXCEPTIONS=True) # TODO(morgan): Convert Subsystems over to Flask-Native, for now, we simply
dispatch_map = collections.OrderedDict() hc_app = healthcheck.Healthcheck.app_factory(
{}, oslo_config_project='keystone')
dispatch_map['/healthcheck'] = hc_app
_routers = []
sub_routers = []
mapper = routes.Mapper()
for api_routers in ALL_API_ROUTERS:
moved_found = [pfx for
pfx in getattr(api_routers, '_path_prefixes', [])
if pfx in _MOVED_API_PREFIXES]
if moved_found:
raise RuntimeError('An API Router is trying to register path '
'prefix(s) `%(pfx)s` that is handled by the '
'native Flask app. Keystone cannot '
'start.' %
{'pfx': ', '.join([p for p in moved_found])}) routers_instance = api_routers.Routers()
_routers.append(routers_instance)
routers_instance.append_v3_routers(mapper, sub_routers) keystone.api.discovery.register_version('v3')
for api in keystone.api.__apis__:
for api_bp in api.APIs:
api_bp.instantiate_and_register_to_app(app)
sub_routers.append(_ComposibleRouterStub(_routers))
legacy_dispatcher = keystone_wsgi.ComposingRouter(mapper, sub_routers) for pfx in itertools.chain(*[rtr.Routers._path_prefixes for
rtr in ALL_API_ROUTERS]):
dispatch_map['/v3/%s' % pfx] = legacy_dispatcher app.wsgi_app = KeystoneDispatcherMiddleware(
app.wsgi_app,
dispatch_map)
return app
这块限定了name只能是public or admin,这个函数主要是为keystone的请求添加路由的mapper以及中间件的调用,先由这个函数对请求的API的路径进行解析, 最后KeystoneDispatcherMiddleware完成对路径API的访问,同样是调用了__call__()方法
profiler.setup(name)是对信息的采集,对性能统计有作用
setup_app_middleware()同样是通过stevedore.DriverManager进行中间件的导入 到此基本上整个keystone便启动了~