<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os . path <EOL> import socket <EOL> import ssl <EOL> import sys <EOL> import eventlet . wsgi <EOL> import greenlet <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_service . service import ServiceBase <EOL> from oslo_utils import excutils <EOL> from paste import deploy <EOL> import routes . middleware <EOL> import six <EOL> import webob . dec <EOL> import webob . exc <EOL> from ec2api import exception <EOL> from ec2api . i18n import _ <EOL> wsgi_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = "<STR_LIT>" , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( <EOL> '<STR_LIT>' , <EOL> default = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:1000> , <EOL> help = "<STR_LIT>" ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( wsgi_opts ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class Server ( ServiceBase ) : <EOL> """<STR_LIT>""" <EOL> default_pool_size = CONF . wsgi_default_pool_size <EOL> def __init__ ( self , name , app , host = '<STR_LIT>' , port = <NUM_LIT:0> , pool_size = None , <EOL> protocol = eventlet . wsgi . HttpProtocol , backlog = <NUM_LIT> , <EOL> use_ssl = False , max_url_len = None ) : <EOL> """<STR_LIT>""" <EOL> eventlet . wsgi . MAX_HEADER_LINE = CONF . max_header_line <EOL> self . name = name <EOL> self . app = app <EOL> self . _server = None <EOL> self . _protocol = protocol <EOL> self . pool_size = pool_size or self . default_pool_size <EOL> self . _pool = eventlet . GreenPool ( self . pool_size ) <EOL> self . _logger = logging . getLogger ( "<STR_LIT>" ) <EOL> self . _use_ssl = use_ssl <EOL> self . _max_url_len = max_url_len <EOL> if backlog < <NUM_LIT:1> : <EOL> raise exception . InvalidInput ( <EOL> reason = '<STR_LIT>' ) <EOL> bind_addr = ( host , port ) <EOL> try : <EOL> info = socket . getaddrinfo ( bind_addr [ <NUM_LIT:0> ] , <EOL> bind_addr [ <NUM_LIT:1> ] , <EOL> socket . AF_UNSPEC , <EOL> socket . SOCK_STREAM ) [ <NUM_LIT:0> ] <EOL> family = info [ <NUM_LIT:0> ] <EOL> bind_addr = info [ - <NUM_LIT:1> ] <EOL> except Exception : <EOL> family = socket . AF_INET <EOL> try : <EOL> self . _socket = eventlet . listen ( bind_addr , family , backlog = backlog ) <EOL> except EnvironmentError : <EOL> LOG . error ( _ ( "<STR_LIT>" ) , <EOL> { '<STR_LIT:host>' : host , '<STR_LIT:port>' : port } ) <EOL> raise <EOL> ( self . host , self . port ) = self . _socket . getsockname ( ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> LOG . info ( _ ( "<STR_LIT>" ) , <EOL> { '<STR_LIT:name>' : self . name , '<STR_LIT:host>' : self . host , '<STR_LIT:port>' : self . port } ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> dup_socket = self . _socket . dup ( ) <EOL> dup_socket . setsockopt ( socket . SOL_SOCKET , <EOL> socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> dup_socket . setsockopt ( socket . SOL_SOCKET , <EOL> socket . SO_KEEPALIVE , <NUM_LIT:1> ) <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> dup_socket . setsockopt ( socket . IPPROTO_TCP , <EOL> socket . TCP_KEEPIDLE , <EOL> CONF . tcp_keepidle ) <EOL> if self . _use_ssl : <EOL> try : <EOL> cert_file = CONF . ssl_cert_file <EOL> key_file = CONF . ssl_key_file <EOL> if cert_file and not os . path . exists ( cert_file ) : <EOL> raise RuntimeError ( <EOL> _ ( "<STR_LIT>" ) % cert_file ) <EOL> if key_file and not os . path . exists ( key_file ) : <EOL> raise RuntimeError ( <EOL> _ ( "<STR_LIT>" ) % key_file ) <EOL> if self . _use_ssl and ( not cert_file or not key_file ) : <EOL> raise RuntimeError ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> ssl_kwargs = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : cert_file , <EOL> '<STR_LIT>' : key_file , <EOL> '<STR_LIT>' : ssl . CERT_NONE , <EOL> } <EOL> dup_socket = eventlet . wrap_ssl ( dup_socket , <EOL> ** ssl_kwargs ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . error ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT:name>' : self . name , '<STR_LIT:host>' : self . host , <EOL> '<STR_LIT:port>' : self . port } ) <EOL> wsgi_kwargs = { <EOL> '<STR_LIT>' : eventlet . wsgi . server , <EOL> '<STR_LIT>' : dup_socket , <EOL> '<STR_LIT>' : self . app , <EOL> '<STR_LIT>' : self . _protocol , <EOL> '<STR_LIT>' : self . _pool , <EOL> '<STR_LIT>' : self . _logger , <EOL> '<STR_LIT>' : CONF . wsgi_log_format , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> if self . _max_url_len : <EOL> wsgi_kwargs [ '<STR_LIT>' ] = self . _max_url_len <EOL> self . _server = eventlet . spawn ( ** wsgi_kwargs ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _pool . resize ( self . pool_size ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> LOG . info ( _ ( "<STR_LIT>" ) ) <EOL> if self . _server is not None : <EOL> self . _pool . resize ( <NUM_LIT:0> ) <EOL> self . _server . kill ( ) <EOL> def wait ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . _server is not None : <EOL> self . _server . wait ( ) <EOL> except greenlet . GreenletExit : <EOL> LOG . info ( _ ( "<STR_LIT>" ) ) <EOL> class Request ( webob . Request ) : <EOL> pass <EOL> class Application ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def factory ( cls , global_config , ** local_config ) : <EOL> """<STR_LIT>""" <EOL> return cls ( ** local_config ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> r"""<STR_LIT>""" <EOL> raise NotImplementedError ( _ ( '<STR_LIT>' ) ) <EOL> class Middleware ( Application ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def factory ( cls , global_config , ** local_config ) : <EOL> """<STR_LIT>""" <EOL> def _factory ( app ) : <EOL> return cls ( app , ** local_config ) <EOL> return _factory <EOL> def __init__ ( self , application ) : <EOL> self . application = application <EOL> def process_request ( self , req ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def process_response ( self , response ) : <EOL> """<STR_LIT>""" <EOL> return response <EOL> @ webob . dec . wsgify ( RequestClass = Request ) <EOL> def __call__ ( self , req ) : <EOL> response = self . process_request ( req ) <EOL> if response : <EOL> return response <EOL> response = req . get_response ( self . application ) <EOL> return self . process_response ( response ) <EOL> class Debug ( Middleware ) : <EOL> """<STR_LIT>""" <EOL> @ webob . dec . wsgify ( RequestClass = Request ) <EOL> def __call__ ( self , req ) : <EOL> print ( ( '<STR_LIT:*>' * <NUM_LIT> ) + '<STR_LIT>' ) <EOL> for key , value in req . environ . items ( ) : <EOL> print ( key , '<STR_LIT:=>' , value ) <EOL> print ( ) <EOL> resp = req . get_response ( self . application ) <EOL> print ( ( '<STR_LIT:*>' * <NUM_LIT> ) + '<STR_LIT>' ) <EOL> for ( key , value ) in six . iteritems ( resp . headers ) : <EOL> print ( key , '<STR_LIT:=>' , value ) <EOL> print ( ) <EOL> resp . app_iter = self . print_generator ( resp . app_iter ) <EOL> return resp <EOL> @ staticmethod <EOL> def print_generator ( app_iter ) : <EOL> """<STR_LIT>""" <EOL> print ( ( '<STR_LIT:*>' * <NUM_LIT> ) + '<STR_LIT>' ) <EOL> for part in app_iter : <EOL> sys . stdout . write ( part ) <EOL> sys . stdout . flush ( ) <EOL> yield part <EOL> print ( ) <EOL> class Router ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mapper ) : <EOL> """<STR_LIT>""" <EOL> self . map = mapper <EOL> self . _router = routes . middleware . RoutesMiddleware ( self . _dispatch , <EOL> self . map ) <EOL> @ webob . dec . wsgify ( RequestClass = Request ) <EOL> def __call__ ( self , req ) : <EOL> """<STR_LIT>""" <EOL> return self . _router <EOL> @ staticmethod <EOL> @ webob . dec . wsgify ( RequestClass = Request ) <EOL> def _dispatch ( req ) : <EOL> """<STR_LIT>""" <EOL> match = req . environ [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> if not match : <EOL> return webob . exc . HTTPNotFound ( ) <EOL> app = match [ '<STR_LIT>' ] <EOL> return app <EOL> class Loader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config_path = None ) : <EOL> """<STR_LIT>""" <EOL> self . config_path = None <EOL> config_path = config_path or CONF . api_paste_config <EOL> if not os . path . isabs ( config_path ) : <EOL> self . config_path = CONF . find_file ( config_path ) <EOL> elif os . path . exists ( config_path ) : <EOL> self . config_path = config_path <EOL> if not self . config_path : <EOL> raise exception . EC2APIConfigNotFound ( path = config_path ) <EOL> def load_app ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) % <EOL> { '<STR_LIT:name>' : name , '<STR_LIT:path>' : self . config_path } ) <EOL> return deploy . loadapp ( "<STR_LIT>" % self . config_path , name = name ) <EOL> except LookupError as err : <EOL> LOG . error ( err ) <EOL> raise exception . EC2APIPasteAppNotFound ( name = name , <EOL> path = self . config_path ) </s>
