<s> import inspect <EOL> import os <EOL> import random <EOL> from oslo_concurrency import processutils <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_messaging import server as rpc_server <EOL> from oslo_service import loopingcall <EOL> from oslo_service import service as common_service <EOL> from oslo_utils import excutils <EOL> from oslo_utils import importutils <EOL> from neutron . _i18n import _ , _LE , _LI <EOL> from neutron . common import config <EOL> from neutron . common import rpc as n_rpc <EOL> from neutron import context <EOL> from neutron . db import api as session <EOL> from neutron import manager <EOL> from neutron import worker <EOL> from neutron import wsgi <EOL> service_opts = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:1> , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:1> , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:5> , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( service_opts ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class WsgiService ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app_name ) : <EOL> self . app_name = app_name <EOL> self . wsgi_app = None <EOL> def start ( self ) : <EOL> self . wsgi_app = _run_wsgi ( self . app_name ) <EOL> def wait ( self ) : <EOL> self . wsgi_app . wait ( ) <EOL> class NeutronApiService ( WsgiService ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def create ( cls , app_name = '<STR_LIT>' ) : <EOL> config . setup_logging ( ) <EOL> service = cls ( app_name ) <EOL> return service <EOL> def serve_wsgi ( cls ) : <EOL> try : <EOL> service = cls . create ( ) <EOL> service . start ( ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return service <EOL> def start_plugin_workers ( ) : <EOL> launchers = [ ] <EOL> for plugin in manager . NeutronManager . get_unique_service_plugins ( ) : <EOL> for plugin_worker in getattr ( plugin , '<STR_LIT>' , tuple ) ( ) : <EOL> launcher = common_service . ProcessLauncher ( cfg . CONF ) <EOL> launcher . launch_service ( plugin_worker ) <EOL> launchers . append ( launcher ) <EOL> return launchers <EOL> class RpcWorker ( worker . NeutronWorker ) : <EOL> """<STR_LIT>""" <EOL> start_listeners_method = '<STR_LIT>' <EOL> def __init__ ( self , plugins ) : <EOL> self . _plugins = plugins <EOL> self . _servers = [ ] <EOL> def start ( self ) : <EOL> super ( RpcWorker , self ) . start ( ) <EOL> for plugin in self . _plugins : <EOL> if hasattr ( plugin , self . start_listeners_method ) : <EOL> try : <EOL> servers = getattr ( plugin , self . start_listeners_method ) ( ) <EOL> except NotImplementedError : <EOL> continue <EOL> self . _servers . extend ( servers ) <EOL> def wait ( self ) : <EOL> try : <EOL> self . _wait ( ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> raise <EOL> def _wait ( self ) : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> for server in self . _servers : <EOL> if isinstance ( server , rpc_server . MessageHandlingServer ) : <EOL> LOG . debug ( '<STR_LIT>' , server ) <EOL> server . wait ( ) <EOL> else : <EOL> LOG . debug ( '<STR_LIT>' , server ) <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> def stop ( self ) : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> for server in self . _servers : <EOL> if isinstance ( server , rpc_server . MessageHandlingServer ) : <EOL> LOG . debug ( '<STR_LIT>' , server ) <EOL> server . stop ( ) <EOL> @ staticmethod <EOL> def reset ( ) : <EOL> config . reset_service ( ) <EOL> class RpcReportsWorker ( RpcWorker ) : <EOL> start_listeners_method = '<STR_LIT>' <EOL> def serve_rpc ( ) : <EOL> plugin = manager . NeutronManager . get_plugin ( ) <EOL> service_plugins = ( <EOL> manager . NeutronManager . get_service_plugins ( ) . values ( ) ) <EOL> if cfg . CONF . rpc_workers < <NUM_LIT:1> : <EOL> cfg . CONF . set_override ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if not plugin . rpc_workers_supported ( ) : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> if <NUM_LIT:0> < cfg . CONF . rpc_workers : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . CONF . rpc_workers ) <EOL> raise NotImplementedError ( ) <EOL> try : <EOL> rpc = RpcWorker ( service_plugins ) <EOL> LOG . debug ( '<STR_LIT>' , cfg . CONF . rpc_workers ) <EOL> session . dispose ( ) <EOL> launcher = common_service . ProcessLauncher ( cfg . CONF , wait_interval = <NUM_LIT:1.0> ) <EOL> launcher . launch_service ( rpc , workers = cfg . CONF . rpc_workers ) <EOL> if ( cfg . CONF . rpc_state_report_workers > <NUM_LIT:0> and <EOL> plugin . rpc_state_report_workers_supported ( ) ) : <EOL> rpc_state_rep = RpcReportsWorker ( [ plugin ] ) <EOL> LOG . debug ( '<STR_LIT>' , <EOL> cfg . CONF . rpc_state_report_workers ) <EOL> launcher . launch_service ( <EOL> rpc_state_rep , workers = cfg . CONF . rpc_state_report_workers ) <EOL> return launcher <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def _get_api_workers ( ) : <EOL> workers = cfg . CONF . api_workers <EOL> if not workers : <EOL> workers = processutils . get_worker_count ( ) <EOL> return workers <EOL> def _run_wsgi ( app_name ) : <EOL> app = config . load_paste_app ( app_name ) <EOL> if not app : <EOL> LOG . error ( _LE ( '<STR_LIT>' ) ) <EOL> return <EOL> return run_wsgi_app ( app ) <EOL> def run_wsgi_app ( app ) : <EOL> server = wsgi . Server ( "<STR_LIT>" ) <EOL> server . start ( app , cfg . CONF . bind_port , cfg . CONF . bind_host , <EOL> workers = _get_api_workers ( ) ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> { '<STR_LIT:host>' : cfg . CONF . bind_host , '<STR_LIT:port>' : cfg . CONF . bind_port } ) <EOL> return server <EOL> class Service ( n_rpc . Service ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , binary , topic , manager , report_interval = None , <EOL> periodic_interval = None , periodic_fuzzy_delay = None , <EOL> * args , ** kwargs ) : <EOL> self . binary = binary <EOL> self . manager_class_name = manager <EOL> manager_class = importutils . import_class ( self . manager_class_name ) <EOL> self . manager = manager_class ( host = host , * args , ** kwargs ) <EOL> self . report_interval = report_interval <EOL> self . periodic_interval = periodic_interval <EOL> self . periodic_fuzzy_delay = periodic_fuzzy_delay <EOL> self . saved_args , self . saved_kwargs = args , kwargs <EOL> self . timers = [ ] <EOL> super ( Service , self ) . __init__ ( host , topic , manager = self . manager ) <EOL> def start ( self ) : <EOL> self . manager . init_host ( ) <EOL> super ( Service , self ) . start ( ) <EOL> if self . report_interval : <EOL> pulse = loopingcall . FixedIntervalLoopingCall ( self . report_state ) <EOL> pulse . start ( interval = self . report_interval , <EOL> initial_delay = self . report_interval ) <EOL> self . timers . append ( pulse ) <EOL> if self . periodic_interval : <EOL> if self . periodic_fuzzy_delay : <EOL> initial_delay = random . randint ( <NUM_LIT:0> , self . periodic_fuzzy_delay ) <EOL> else : <EOL> initial_delay = None <EOL> periodic = loopingcall . FixedIntervalLoopingCall ( <EOL> self . periodic_tasks ) <EOL> periodic . start ( interval = self . periodic_interval , <EOL> initial_delay = initial_delay ) <EOL> self . timers . append ( periodic ) <EOL> self . manager . after_start ( ) <EOL> def __getattr__ ( self , key ) : <EOL> manager = self . __dict__ . get ( '<STR_LIT>' , None ) <EOL> return getattr ( manager , key ) <EOL> @ classmethod <EOL> def create ( cls , host = None , binary = None , topic = None , manager = None , <EOL> report_interval = None , periodic_interval = None , <EOL> periodic_fuzzy_delay = None ) : <EOL> """<STR_LIT>""" <EOL> if not host : <EOL> host = CONF . host <EOL> if not binary : <EOL> binary = os . path . basename ( inspect . stack ( ) [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> if not topic : <EOL> topic = binary . rpartition ( '<STR_LIT>' ) [ <NUM_LIT:2> ] <EOL> topic = topic . replace ( "<STR_LIT:->" , "<STR_LIT:_>" ) <EOL> if not manager : <EOL> manager = CONF . get ( '<STR_LIT>' % topic , None ) <EOL> if report_interval is None : <EOL> report_interval = CONF . report_interval <EOL> if periodic_interval is None : <EOL> periodic_interval = CONF . periodic_interval <EOL> if periodic_fuzzy_delay is None : <EOL> periodic_fuzzy_delay = CONF . periodic_fuzzy_delay <EOL> service_obj = cls ( host , binary , topic , manager , <EOL> report_interval = report_interval , <EOL> periodic_interval = periodic_interval , <EOL> periodic_fuzzy_delay = periodic_fuzzy_delay ) <EOL> return service_obj <EOL> def kill ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stop ( ) <EOL> def stop ( self ) : <EOL> super ( Service , self ) . stop ( ) <EOL> for x in self . timers : <EOL> try : <EOL> x . stop ( ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> self . timers = [ ] <EOL> def wait ( self ) : <EOL> super ( Service , self ) . wait ( ) <EOL> for x in self . timers : <EOL> try : <EOL> x . wait ( ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> def reset ( self ) : <EOL> config . reset_service ( ) <EOL> def periodic_tasks ( self , raise_on_error = False ) : <EOL> """<STR_LIT>""" <EOL> ctxt = context . get_admin_context ( ) <EOL> self . manager . periodic_tasks ( ctxt , raise_on_error = raise_on_error ) <EOL> def report_state ( self ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
