glance.common.wsgi module

Utility methods for working with WSGI servers

class glance.common.wsgi.APIMapper(controller_scan=<function controller_scan>, directory=None, always_scan=False, register=True, explicit=True)[source]

Bases: Mapper

Handle route matching when url is ‘’ because routes.Mapper returns an error in this case.

routematch(url=None, environ=None)[source]

Match a URL against against one of the routes contained.

Will return None if no valid match is found, otherwise a result dict and a route object is returned.

resultdict, route_obj = m.match('/joe/sixpack')
class glance.common.wsgi.BaseServer(threads=1000, initialize_glance_store=False)[source]

Bases: object

Server class to manage multiple WSGI sockets and applications.

This class requires initialize_glance_store set to True if glance store needs to be initialized.

configure(old_conf=None, has_changed=None)[source]

Apply configuration settings

Parameters:
  • old_conf – Cached old configuration settings (if any)

  • changed (has) – callable to determine if a parameter has changed

configure_socket(old_conf=None, has_changed=None)[source]

Ensure a socket exists and is appropriately configured.

This function is called on start up, and can also be called in the event of a configuration reload.

When called for the first time a new socket is created. If reloading and either bind_host or bind port have been changed the existing socket must be closed and a new socket opened (laws of physics).

In all other cases (bind_host/bind_port have not changed) the existing socket is reused.

Parameters:
  • old_conf – Cached old configuration settings (if any)

  • changed (has) – callable to determine if a parameter has changed

create_pool()[source]
hup(*args)[source]

Reloads configuration files with zero down time

abstract kill_children(*args)[source]
reload()[source]
abstract run_child()[source]
run_server()[source]

Run a WSGI server.

static set_signal_handler(signal_name, handler)[source]
start(application, default_port)[source]

Run a WSGI server with the given application.

Parameters:
  • application – The application to be run in the WSGI server

  • default_port – Port to bind to if none is specified in conf

start_wsgi()[source]
wait()[source]

Wait until all servers have completed running.

abstract wait_on_children()[source]
class glance.common.wsgi.Debug(application)[source]

Bases: Middleware

Helper class that can be inserted into any WSGI application chain to get information about the request and response.

static print_generator(app_iter)[source]

Iterator that prints the contents of a wrapper string iterator when iterated.

class glance.common.wsgi.JSONRequestDeserializer[source]

Bases: object

default(request)[source]
from_json(datastring)[source]
has_body(request)[source]

Returns whether a Webob.Request object will possess an entity body.

Parameters:

request – Webob.Request object

httpverb_may_have_body = frozenset({'PATCH', 'POST', 'PUT'})
classmethod is_valid_encoding(request)[source]
classmethod is_valid_method(request)[source]
valid_transfer_encoding = frozenset({'chunked', 'compress', 'deflate', 'gzip', 'identity'})
class glance.common.wsgi.JSONResponseSerializer[source]

Bases: object

default(response, result)[source]
to_json(data)[source]
class glance.common.wsgi.Middleware(application)[source]

Bases: object

Base WSGI middleware wrapper. These classes require an application to be initialized that will be called next. By default the middleware will simply call its wrapped app, or you can override __call__ to customize its behavior.

classmethod factory(global_conf, **local_conf)[source]
process_request(req)[source]

Called on each request.

If this returns None, the next application down the stack will be executed. If it returns a response then that response will be returned and execution will stop here.

process_response(response)[source]

Do whatever you’d like to the response.

class glance.common.wsgi.PosixServer(*args, **kwargs)[source]

Bases: BaseServer

kill_children(*args)[source]

Kills the entire process group.

reload()[source]

Reload and re-apply configuration settings

Existing child processes are sent a SIGHUP signal and will exit after completing existing requests. New child processes, which will have the updated configuration, are spawned. This allows preventing interruption to the service.

run_child()[source]
wait_on_children()[source]
class glance.common.wsgi.RejectMethodController[source]

Bases: object

reject(req, allowed_methods, *args, **kwargs)[source]
class glance.common.wsgi.Request(environ, *args, **kwargs)[source]

Bases: Request

Add some OpenStack API-specific logic to the base webob.Request.

best_match_content_type()[source]

Determine the requested response content-type.

best_match_language()[source]

Determines best available locale from the Accept-Language header.

Returns:

the best language match or None if the ‘Accept-Language’ header was not available in the request.

property body_file

Input stream of the request (wsgi.input). Setting this property resets the content_length and seekable flag (unlike setting req.body_file_raw).

get_content_type(allowed_content_types)[source]

Determine content type of the request body.

get_range_from_request(image_size)[source]

Return the Range in a request.

class glance.common.wsgi.Resource(controller, deserializer=None, serializer=None)[source]

Bases: object

WSGI app that handles (de)serialization and controller dispatch.

Reads routing information supplied by RoutesMiddleware and calls the requested action method upon its deserializer, controller, and serializer. Those three objects may implement any of the basic controller action methods (create, update, show, index, delete) along with any that may be specified in the api router. A ‘default’ method may also be implemented to be used in place of any non-implemented actions. Deserializer methods must accept a request argument and return a dictionary. Controller methods must accept a request argument. Additionally, they must also accept keyword arguments that represent the keys returned by the Deserializer. They may raise a webob.exc exception or return a dict, which will be serialized by requested content type.

dispatch(obj, action, *args, **kwargs)[source]

Find action-specific method on self and call it.

get_action_args(request_environment)[source]

Parse dictionary created by routes library.

class glance.common.wsgi.Router(mapper)[source]

Bases: object

WSGI middleware that maps incoming requests to WSGI apps.

classmethod factory(global_conf, **local_conf)[source]
glance.common.wsgi.Server

alias of PosixServer

class glance.common.wsgi.Win32ProcessLauncher[source]

Bases: object

add_process(cmd)[source]
wait()[source]
class glance.common.wsgi.Win32Server(*args, **kwargs)[source]

Bases: BaseServer

configure_socket(old_conf=None, has_changed=None)[source]

Ensure a socket exists and is appropriately configured.

This function is called on start up, and can also be called in the event of a configuration reload.

When called for the first time a new socket is created. If reloading and either bind_host or bind port have been changed the existing socket must be closed and a new socket opened (laws of physics).

In all other cases (bind_host/bind_port have not changed) the existing socket is reused.

Parameters:
  • old_conf – Cached old configuration settings (if any)

  • changed (has) – callable to determine if a parameter has changed

kill_children(*args)[source]
run_child()[source]
wait_on_children()[source]
glance.common.wsgi.get_asynchronous_eventlet_pool(size=1000)[source]

Return eventlet pool to caller.

Also store pools created in global list, to wait on it after getting signal for graceful shutdown.

Parameters:

size – eventlet pool size

Returns:

eventlet pool

glance.common.wsgi.get_bind_addr(default_port=None)[source]

Return the host and port to bind to.

glance.common.wsgi.get_num_workers()[source]

Return the configured number of workers.

glance.common.wsgi.get_socket(default_port)[source]

Bind socket to bind ip:port in conf

note: Mostly comes from Swift with a few small changes…

Parameters:

default_port – port to bind to if none is specified in conf

Returns:

a socket object as returned from socket.listen

glance.common.wsgi.initialize_glance_store()[source]

Initialize glance store.

glance.common.wsgi.initialize_multi_store()[source]

Initialize glance multi store backends.

glance.common.wsgi.register_cli_opts()[source]
glance.common.wsgi.set_eventlet_hub()[source]
glance.common.wsgi.translate_exception(req, e)[source]

Translates all translatable elements of the given exception.