Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the nova code of openstack components

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to parse the nova code of openstack components, I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Feel free to take notes when viewing the nova code. The nova.wsgi file defines the base class of the wsgi functional component.

Parsing of nova.wsgi.Application Module

This class is the base class for wsgi app:

Class Application (object): WSGI application base class encapsulation. The subclass needs to implement the _ _ call__ method. "@ classmethod def factory (cls, global_config, * * local_config):" any local configuration called in the paste.deploy configuration file (that is, the value of [app:APPNAME] in the paste configuration file) will be passed to the `_ _ init__` method as kwargs. Suppose the configuration is as follows: [app:wadl] latest_version= 1.3 paste.app_factory = nova.api.fancy_api:Wadl.factory so that you can implement paste to call the Wadl class in a way similar to the following: import nova.api.fancy_api fancy_api.Wadl (latest_version='1.3'), of course. You can reimplement the `factory` method in a subclass, but it should not be necessary to pass it using kwarg. "" return cls (* * local_config) def _ _ call__ (self, environ, start_response): "" subclasses can implement _ _ call__: @ webob.dec.wsgify (RequestClass=Request) def _ _ call__ (self) like the following Req): # any of the following objects can be used as responses: # Option 1: simple string res = 'message\ n' # Option 2: well-formatted HTTP exception page res = exc.HTTPForbidden (explanation='Nice try') # Option 3: a webob Response object (content such as header needs to be processed.) Res = Response () res.app_iter = open ('somefile') # Option 4: any wsgi application that runs next res = self.application # Option 5: you can get a Response object for the wsgi application to handle things like header res = req.get_response (self.application) # or you can just return response... Return res #... Or set req.response and return None. Req.response = res See the end of http://pythonpaste.org/webob/modules/dec.html for more info. " Parsing of raise NotImplementedError (_ ('You must implement _ _ call__')) nova.wsgi.Router module

This class is the base class of the nova router system:

Class Router (object): "this class is WSGI middleware that maps incoming requests to WSGI applications." Def _ _ init__ (self, mapper): create a router for the given routes.Mapper. Each route in `mapper` must specify a 'controller'. Specify an 'action' and make the controller an object so that the request can be routed to an action-specific method. Examples: mapper = routes.Mapper () sc = ServerController () # an explicit route to controller+action mapper.connect (None,'/ svrlist', controller=sc, action='list') # Actions is an implicitly defined mapper.resource ('server',' servers', controller=sc) # pointing to any WSGI app. You can specify the {path_info:.*} parameter so that the target application can pass only this part of the URL. Mapper.connect (None,'/ v1.0 / {path_info:.*}', controller=BlogApp ()) "" self.map = mapperself._router = routes.middleware.RoutesMiddleware (self._dispatch, self.map) @ webob.dec.wsgify (RequestClass=Request) def _ _ call__ (self, req): "" routes the incoming request to the self.map-based controller. If it does not match, 404 is returned. "" return self._router@staticmethod @ webob.dec.wsgify (RequestClass=Request) def _ dispatch (req): "" dispatches the request to the appropriate controller. Matching the incoming request to the route and putting the information into the req.environ is called by the self._router. "" match = req.environ ['wsgiorg.routing_args'] [1] if not match:return webob.exc.HTTPNotFound () app = match [' controller'] return appnova.wsgi.Middleware module parsing class Middleware (Application): "WSGI middleware base class. # these classes need to initialize an application that will be called in the next step. By default, middleware only calls its encapsulated application, or it can overload the _ _ call__ method to customize the processing behavior. "" @ classmethod def factory (cls, global_config, * * local_config): "" any local configuration called in the paste.deploy configuration file (that is, the value of [app:APPNAME] in the paste configuration file) will be passed to the `_ _ init__` method as kwargs. Suppose the configuration is as follows: [filter:analytics] redis_host= 127.0.0.1 paste.filter_factory = nova.api.analytics:Analytics.factory can implement the behavior such as python calling `Analytics`, as follows: import nova.api.analytics analytics.Analytics (app_from_paste, redis_host='127.0.0.1') of course You can re-implement the `factory` method "def _ factory (app): return cls (app, * * local_config) return _ factorydef _ _ init__ (self, application): self.application = applicationdef process_request (self, req):"is called in each request. # if it does not return any value, proceed to the next application in the stack. # if it returns a response, then the response will be returned and execution will stop here. "" return None def process_response (self, response): "" some preprocessing can be done before returning response. "" Return response @ webob.dec.wsgify (RequestClass=Request) def _ _ call__ (self, req): response = self.process_request (req) # call this method to handle the api request if response:return response # return if there is a response, generally response represents an exception response = req.get_response (self.application) # execute wsgi application to get response return self.process_response (response) # call this method to preprocess the response. After reading the above, have you mastered how to parse the nova code of openstack components? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report