nova源码分析--API(1)

本文详细剖析了Pike版本的Nova项目中API服务的实现。从nova/cmd/api.py开始,介绍了CONF变量的来源,如何启动WSGIService,以及如何通过加载osapi_compute来处理不同版本的API请求。接着,探讨了ServiceLauncher如何启动服务进程,完成Nova API服务的启动流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pike版的nova/cmd/api.py代码如下

import sys

from oslo_log import log as logging
from oslo_reports import guru_meditation_report as gmr

import nova.conf
from nova import config
from nova import exception
from nova import objects
from nova import service
from nova import utils
from nova import version

CONF = nova.conf.CONF


def main():
    config.parse_args(sys.argv)
    logging.setup(CONF, "nova")
    utils.monkey_patch()
    objects.register_all()
    if 'osapi_compute' in CONF.enabled_apis:
        # NOTE(mriedem): This is needed for caching the nova-compute service
        # version.
        objects.Service.enable_min_version_cache()
    log = logging.getLogger(__name__)

    gmr.TextGuruMeditation.setup_autorun(version)

    launcher = service.process_launcher()
    started = 0
    for api in CONF.enabled_apis:
        should_use_ssl = api in CONF.enabled_ssl_apis
        try:
            server = service.WSGIService(api, use_ssl=should_use_ssl)
            launcher.launch_service(server, workers=server.workers or 1)
            started += 1
        except exception.PasteAppNotFound as ex:
            log.warning("%s. ``enabled_apis`` includes bad values. "
                        "Fix to remove this warning.", ex)

    if started == 0:
        log.error('No APIs were started. '
                  'Check the enabled_apis config option.')
        sys.exit(1)

    launcher.wait()

变量CONF在文件nova/conf/__init__.py中定义,其中有一行service.register_opts(CONF),在文件nova/conf/service.py中有以下代码

cfg.ListOpt('enabled_apis',
                item_type=cfg.types.String(choices=['osapi_compute',
                                                    'metadata']),
                default=['osapi_compute', 'metadata'],
                help="List of APIs to be enabled by default."),
    cfg.ListOpt('enabled_ssl_apis',
                default=[],
                help="""
List of APIs with enabled SSL.

Nova provides SSL support for the API servers. enabled_ssl_apis option
allows configuring the SSL support.
"""),

定义了enabled_apis数组中的一项为osapi_compute,nova/cmd/api.py中的核心代码为

            server = service.WSGIService(api, use_ssl=should_use_ssl)
            launcher.launch_service(server, workers=server.workers or 1)

WSGIService的定义在nova/service.py文件中,代码如下

class WSGIService(service.Service):
    """Provides ability to launch API from a 'paste' configuration
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值