neutron-server启动过程分析
1. /etc/init.d/neutron-server
DAEMON=/usr/bin/neutron-server
DAEMON_ARGS="--log-file=$LOGFILE"
DAEMON_DIR=/var/run
...
case $1 in
start)
test "$ENABLED" = "true" || exit 0
log_daemon_msg "Starting neutron server" "neutron-server"
start-stop-daemon -Sbmv --pidfile $PIDFILE --chdir $DAEMON_DIR --exec $DAEMON -- $DAEMON_ARGS
log_end_msg $?
;;
...
esac
2. /usr/bin/neutron-server
import sys
from neutron.server import main if __name__ == "__main__":
sys.exit(main())
3. neutron.server.main
ef main():
# the configuration will be read into the cfg.CONF global data structure
config.init(sys.argv[1:])
if not cfg.CONF.config_file:
sys.exit(_("ERROR: Unable to find configuration file via the default"
" search paths (~/.neutron/, ~/, /etc/neutron/, /etc/) and"
" the '--config-file' option!"))
try:
pool = eventlet.GreenPool() # 以协程方式启动Restful API
neutron_api = service.serve_wsgi(service.NeutronApiService)
api_thread = pool.spawn(neutron_api.wait) # 启动RPC API
try:
neutron_rpc = service.serve_rpc()
except NotImplementedError:
LOG.info(_("RPC was already started in parent process by plugin."))
else:
rpc_thread = pool.spawn(neutron_rpc.wait) # api and rpc should die together. When one dies, kill the other.
rpc_thread.link(lambda gt: api_thread.kill())
api_thread.link(lambda gt: rpc_thread.kill()) pool.waitall()
except KeyboardInterrupt:
pass
except RuntimeError as e:
sys.exit(_("ERROR: %s") % e)
4. 先看neutron.service.serve_rpc()
neutron.service.serve_rpc()最重要的工作就是启动各个插件的RpcWorker
plugin = manager.NeutronManager.get_plugin() try:
rpc = RpcWorker(plugin) if cfg.CONF.rpc_workers < 1:
rpc.start()
return rpc
else:
launcher = common_service.ProcessLauncher(wait_interval=1.0)
launcher.launch_service(rpc, workers=cfg.CONF.rpc_workers)
return launcher
而RpcWorker最重要的工作是调用plugin的start_rpc_listeners来监听消息队列:
def start(self):
# We may have just forked from parent process. A quick disposal of the
# existing sql connections avoids producing errors later when they are
# discovered to be broken.
session.get_engine().pool.dispose()
self._servers = self._plugin.start_rpc_listeners()
5. 再来看Rest API部分
service.serve_wsgi(service.NeutronApiService)
def serve_wsgi(cls): try:
service = cls.create()
service.start()
except Exception:
with excutils.save_and_reraise_exception():
LOG.exception(_('Unrecoverable error: please check log '
'for details.')) return service
service.start()
即为self.wsgi_app = _run_wsgi(self.app_name)
,而该函数最重要的工作是从api-paste.ini中加载app并启动
def _run_wsgi(app_name):
app = config.load_paste_app(app_name)
if not app:
LOG.error(_('No known API applications configured.'))
return
server = wsgi.Server("Neutron")
server.start(app, cfg.CONF.bind_port, cfg.CONF.bind_host,
workers=cfg.CONF.api_workers)
# Dump all option values here after all options are parsed
cfg.CONF.log_opt_values(LOG, std_logging.DEBUG)
LOG.info(_("Neutron service started, listening on %(host)s:%(port)s"),
{'host': cfg.CONF.bind_host,
'port': cfg.CONF.bind_port})
return server
6. api-paste.ini
[composite:neutron]
use = egg:Paste#urlmap
/: neutronversions
/v2.0: neutronapi_v2_0 [composite:neutronapi_v2_0]
use = call:neutron.auth:pipeline_factory
noauth = request_id catch_errors extensions neutronapiapp_v2_0
keystone = request_id catch_errors authtoken keystonecontext extensions neutronapiapp_v2_0 [filter:request_id]
paste.filter_factory = neutron.openstack.common.middleware.request_id:RequestIdMiddleware.factory [filter:catch_errors]
paste.filter_factory = neutron.openstack.common.middleware.catch_errors:CatchErrorsMiddleware.factory [filter:keystonecontext]
paste.filter_factory = neutron.auth:NeutronKeystoneContext.factory [filter:authtoken]
paste.filter_factory = keystoneclient.middleware.auth_token:filter_factory [filter:extensions]
paste.filter_factory = neutron.api.extensions:plugin_aware_extension_middleware_factory [app:neutronversions]
paste.app_factory = neutron.api.versions:Versions.factory [app:neutronapiapp_v2_0]
paste.app_factory = neutron.api.v2.router:APIRouter.factory
实例化neutron/api/v2/router.py中的APIRouter
- class APIRouter(wsgi.Router):
- # 一个工厂类方法
- @classmethod
- def factory(cls, global_config, **local_config):
- return cls(**local_config)
- # 真正调用的实例化方法
- def __init__(self, **local_config):
- ...
- #获取NeutornManage的core_plugin,这个定义在/etc/neutron/neutron.conf,比如我的是
- #core_plugin = neutron.plugins.openvswitch.ovs_neutron_plugin.OVSNeutronPluginV2
- plugin = manager.NeutronManager.get_plugin()
- #扫描特定路径下的extensions
- ext_mgr = extensions.PluginAwareExtensionManager.get_instance()
- ...
- #定义的局部方法
- def _map_resource(collection, resource, params, parent=None):
- ...
- controller = base.create_resource(
- collection, resource, plugin, params, allow_bulk=allow_bulk,
- parent=parent, allow_pagination=allow_pagination,
- allow_sorting=allow_sorting)
- ...
- # 将这些resource加进router中
- return mapper.collection(collection, resource, **mapper_kwargs)
- # 遍历 {'network': 'networks', 'subnet': 'subnets','port': 'ports'}
- # 添加controller
- for resource in RESOURCES:
- _map_resource(RESOURCES[resource], resource,
- attributes.RESOURCE_ATTRIBUTE_MAP.get(
- RESOURCES[resource], dict()))
- for resource in SUB_RESOURCES:
- ...
- #其实操作和上面一个差不多,
由这个可以看出,添加的controller类型主要分为三类:(其实只要你在neutron目录下grep一下,看哪里调用了create_resource
方法即可)
- OVSNeutronPluginV2
- extensions/*.py
- plugins/*.py
针对前两途径加载resource的类,下面慢慢进行描述。至于第三种,则是在各个不同的plugin内部额外实现的,不是必须的。
顺便简单的提一下,在neutron/api/extensions.py
下的get_instance
方法,这里其实也是和nova一样,是遍历目录下的py文件,来增加extension的
- ...
- @classmethod
- def get_instance(cls):
- if cls._instance is None:
- cls._instance = cls(get_extensions_path(),
- ... NeutronManager.get_service_plugins())
Resource:OVSNeutronPluginV2的实现
看了代码的你肯定知道,OVSNeutronPluginV2
这个类,作为core_plugin
继承了好多的的类
- class OVSNeutronPluginV2(db_base_plugin_v2.NeutronDbPluginV2,
- external_net_db.External_net_db_mixin,
- extraroute_db.ExtraRoute_db_mixin,
- l3_gwmode_db.L3_NAT_db_mixin,
- sg_db_rpc.SecurityGroupServerRpcMixin,
- l3_agentschedulers_db.L3AgentSchedulerDbMixin,
- agentschedulers_db.DhcpAgentSchedulerDbMixin,
- portbindings_db.PortBindingMixin,
- extradhcpopt_db.ExtraDhcpOptMixin,
- addr_pair_db.AllowedAddressPairsMixin):
OVSNeutronPluginV2
基本上没有什么自己的method,全靠它的"爹们"了。
随便抓两个来看下,比如NeutronDbPluginV2
,他的method有get_port
,create_network
之类的,还有L3_NAT_db_mixin
的create_router
等。反正与db的操作,OVSNeutronPluginV2
是不会管的,都在它的父类那边处理。
再看看OVSNeutronPluginV2
继承的这些父类们:
- #NeutronDbPluginV2继承自NeutronPluginBaseV2
- class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
- CommonDbMixin):
- class NeutronPluginBaseV2(...) :
- @abstractmethod
- def create_subnet(self, context, subnet):
- @abstractmethod
- def update_subnet(self, context, id, subnet):
- @abstractmethod
- def get_subnet(self, context, id, fields=None):
- @abstractmethod
- def get_subnets(self, context, filters=None, fields=None,
其类图如下:(仅展示部分)
基本上可以说有一个接口类(如图中的NeutronPluginBaseV2
),定义了抽象方法,然后一个具体的db类来实现(如NeutronDbPluginV2
,这里是采用SQLAlchemy
来完成db模型的)
plugin_aware_extension_middleware_factory
在请求进入APIRouter之前,会先经过RequestIdMiddleware(请求header中添加 openstack.request_id)、CatchErrorsMiddleware(错误处理)、keystone权限验证以及 plugin_aware_extension_middleware_factory等几个filter的处理,前三个filter比较直 观,plugin_aware_extension_middleware_factory初始化了Extension目录下的Resource:
class ExtensionMiddleware(wsgi.Middleware):
"""Extensions middleware for WSGI.""" def __init__(self, application,
ext_mgr=None):
self.ext_mgr = (ext_mgr
or ExtensionManager(get_extensions_path()))
mapper = routes.Mapper() # extended resources ext_mgr.get_resources()其实在内部会调用每个extensions目录下的extension类的get_resources方法
for resource in self.ext_mgr.get_resources():
path_prefix = resource.path_prefix
if resource.parent:
path_prefix = (resource.path_prefix +
"/%s/{%s_id}" %
(resource.parent["collection_name"],
resource.parent["member_name"])) LOG.debug(_('Extended resource: %s'),
resource.collection)
for action, method in resource.collection_actions.iteritems():
conditions = dict(method=[method])
path = "/%s/%s" % (resource.collection, action)
with mapper.submapper(controller=resource.controller,
action=action,
path_prefix=path_prefix,
conditions=conditions) as submap:
submap.connect(path)
submap.connect("%s.:(format)" % path) mapper.resource(resource.collection, resource.collection,
controller=resource.controller,
member=resource.member_actions,
parent_resource=resource.parent,
path_prefix=path_prefix) # extended actions
action_controllers = self._action_ext_controllers(application,
self.ext_mgr, mapper)
for action in self.ext_mgr.get_actions():
LOG.debug(_('Extended action: %s'), action.action_name)
controller = action_controllers[action.collection]
controller.add_action(action.action_name, action.handler) # extended requests
req_controllers = self._request_ext_controllers(application,
self.ext_mgr, mapper)
for request_ext in self.ext_mgr.get_request_extensions():
LOG.debug(_('Extended request: %s'), request_ext.key)
controller = req_controllers[request_ext.key]
controller.add_handler(request_ext.handler) self._router = routes.middleware.RoutesMiddleware(self._dispatch,
mapper)
super(ExtensionMiddleware, self).__init__(application)
比如在extensions下的securitygroup
.py中的get_resources
方法,看这个代码就知道其中可以处理security_group
和security_group_rule
两类请求了:
@classmethod
def get_resources(cls):
"""Returns Ext Resources."""
my_plurals = [(key, key[:-1]) for key in RESOURCE_ATTRIBUTE_MAP.keys()]
attr.PLURALS.update(dict(my_plurals))
exts = []
plugin = manager.NeutronManager.get_plugin()
for resource_name in ['security_group', 'security_group_rule']:
collection_name = resource_name.replace('_', '-') + "s"
params = RESOURCE_ATTRIBUTE_MAP.get(resource_name + "s", dict())
quota.QUOTAS.register_resource_by_name(resource_name)
controller = base.create_resource(collection_name,
resource_name,
plugin, params, allow_bulk=True,
allow_pagination=True,
allow_sorting=True) ex = extensions.ResourceExtension(collection_name,
controller,
attr_map=params)
exts.append(ex) return exts
如此,Neutron-Server
就已经基本上启动了,无外乎就是加载配置,router各种resource,然后就等待请求了。其中router哪些resource完全是由配置文件来决定的。 当然,在启动的过程中也会初始化db,这也就是为何在安装neutron
的时候无需像nova
,glance
等要执行db sync
的原因了。
参考资料
- Neutron网络简介 http://blog.ustack.com/blog/neutron_intro/
- http://www.lnmpy.com/neutron-server-how-to-start/
Neutron分析(2)——neutron-server启动过程分析的更多相关文章
-
Eureka Server启动过程分析
1.首先,SpringCloud充分利用了SpringBoot的自动装配特点 eureka-server的jar包,发现在META-INF下面的配置文件spring.factories,里面记录了Sp ...
-
Neutron分析(3)—— neutron-l3-agent
一.Layer-3 Networking Extension neutron l3作为一种API扩展,向租户提供了路由和NAT功能. l3扩展包含两种资源: router:在不同内部子网中转发数据包: ...
-
openstack Neutron分析(3)—— neutron-dhcp-agent源码分析
1.neutron dhcp3个主要部件分别为什么?2.dhcp模块包含哪些内容?3.Dnsmasq配置文件是如何创建和更新的?4.DHCP agent的信息存放在neutron数据库的哪个表中? 扩 ...
-
ASP.Net Core MVC6 RC2 启动过程分析[偏源码分析]
入口程序 如果做过Web之外开发的人,应该记得这个是标准的Console或者Winform的入口.为什么会这样呢? .NET Web Development and Tools Blog ASP.NE ...
-
图解Android - Zygote, System Server 启动分析
Init 是所有Linux程序的起点,而Zygote于Android,正如它的英文意思,是所有java程序的'孵化池'(玩过星际虫族的兄弟都晓得的).用ps 输出可以看到 >adb shell ...
-
Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6629298 在前面一篇文章浅谈Android系 ...
-
Disconf源码分析之启动过程分析下(2)
接上文,下面是第二次扫描的XML配置. <bean id="disconfMgrBean2" class="com.baidu.disconf.client.Dis ...
-
netty源码分析之一:server的启动
nio server启动的第一步,都是要创建一个serverSocketChannel,我截取一段启动代码,一步步分析: public void afterPropertiesSet() throws ...
-
(3.4)mysql基础深入——mysql.server启动脚本源码阅读与分析
(3.4)mysql基础深入——mysql.server启动脚本源码阅读与分析 my.server主要分为3大部分 [1]变量初始化部分 [2]函数声明部分 [3]具体执行部分 #!/bin/sh # ...
随机推荐
-
WKInterfaceImage 无法更新图片的问题
最近涉及到AppleWatch的相关项目,但有个奇怪问题无法解决,而且无法理解: 根据不同的用户操作,需要修改播放器的专辑图片. 不知道跟我的项目需求是不是有关系:我需要轮询共享空间,以拿取同步数据, ...
-
C#以管理员身份运行程序
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...
-
GitHub 操作流程示例
最新文章:Virson's Blog 参考文章: 博客园-Web前端开发,博客园-喻头快跑,GotGitHub 首先.通过github网站新建一个仓库,得到仓库地址 https://github.co ...
-
Excel中的隐藏函数
excel有一些隐藏函数,这些函数在帮助文件中找不到介绍,常用的有以下三个: 1.DATEDIF() 功能:计算两个日期的差值 语法:DATEDIF(start_date,end_date,unit) ...
-
js与flash结合使用
最近,做个了一个falsh和js 通信的小东西. flash负责接收参数和返回结果.js负责处理信息,接收返回结果,将结果返回到服务器端. 听着很复杂,做起来页面还是很简单的.用的技术还是不少的,fl ...
-
XE8 hash
c++builder xe8 hash calc md5.sha256.sha384.sha512 file and string sha256.sha384.sha512 must call l ...
-
TFTPD32, 3CDaemon, FlashFxp
TFTPD32, 3CDaemon, FlashFxp ——各种网络传输下载工具简介—— 一.将3CDaemon.exe作为TFTP服务端,开发板作为TFTP客户端 1.如上图所示,设置好3CDaem ...
-
Java:网络编程
一.因特网地址 InetAddress类:实现主机名和因特网地址之间的转换. InetAddress address=InetAddress.getByName(String);返回一个InetAdd ...
-
python脚本后台启动
nohup python -u test.py > out.log 2>&1 & nohup python -u 文件路径 > 打印日志 2>&1 &a ...
-
浅谈awk命令
简介 awk是一个强大的文本分析工具,相对于grep.sed命令,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,然后将每行切片,再对切开的部分进行处理. awk有 ...