def load_models(self): """ Setup the SQLAlchemy database models for all moksha applications. This method first looks to see if your application has a ``sqlalchemy.url`` set in it's configuration file, and will create a SQLAlchemy engine with it. If it does not exist, Moksha will create an engine for your application based on the ``app_db`` configuration, which defaults to ``sqlite:///$APPNAME.db``. It will then bind the engine to your model's :class:`sqlalchemy.MetaData`, and initialize all of your tables, if they don't already exist. """ for name, app in moksha.common.utils._apps.items(): sa_url = app.get('config', {}).get('sqlalchemy.url', None) app_db = self.config.get('app_db', 'sqlite:///%s.db') if sa_url: if app['config']['__file__'] == get_moksha_config_path(): # Moksha's apps don't specify their own SA url self.engines[name] = create_engine(app_db % name) else: # App has specified its own engine url self.engines[name] = create_engine(sa_url) # If a `model` module exists in the application, call it's # `init_model` method,and bind the engine to it's `metadata`. if app.get('model'): if not sa_url: self.engines[name] = create_engine(app_db % name) log.debug('Creating database engine for %s' % app['name']) app['model'].init_model(self.engines[name]) app['model'].metadata.create_all(bind=self.engines[name])
def find_hub_extensions(config): """ Return a tuple of hub extensions found in the config file. """ possible_bases = { 'amqp_broker': AMQPHubExtension, 'stomp_broker': StompHubExtension, 'zmq_enabled': ZMQHubExtension, } broker_vals = [config.get(k, None) for k in possible_bases.keys()] # If we're running outside of middleware and hub, load config if not any(broker_vals): config_path = get_moksha_config_path() if not config_path: raise ValueError(NO_CONFIG_MESSAGE) cfg = appconfig('config:' + config_path) config.update(cfg) broker_vals = [config.get(k, None) for k in possible_bases.keys()] # If there are no brokers defined.. that's a problem. if not any(broker_vals): raise ValueError("No messaging methods defined.") if len(filter(None, broker_vals)) > 1: log.warning("Running with multiple brokers. " "This mode is experimental and may or may not work") extensions = tuple( [b for k, b in possible_bases.items() if config.get(k, None)]) return extensions
def find_hub_extensions(config): """ Return a tuple of hub extensions found in the config file. """ possible_bases = { 'amqp_broker': AMQPHubExtension, 'stomp_broker': StompHubExtension, 'zmq_enabled': ZMQHubExtension, } broker_vals = [config.get(k, None) for k in possible_bases.keys()] # If we're running outside of middleware and hub, load config if not any(broker_vals): config_path = get_moksha_config_path() if not config_path: raise ValueError(NO_CONFIG_MESSAGE) cfg = appconfig('config:' + config_path) config.update(cfg) broker_vals = [config.get(k, None) for k in possible_bases.keys()] # If there are no brokers defined.. that's a problem. if not any(broker_vals): raise ValueError("No messaging methods defined.") if len(filter(None, broker_vals)) > 1: log.warning("Running with multiple brokers. " "This mode is experimental and may or may not work") extensions = tuple([ b for k, b in possible_bases.items() if config.get(k, None) ]) return extensions
def main(options=None, consumers=None, producers=None, framework=True): """ The main MokshaHub method """ # If we're running as a framework, then we're strictly calling other # people's code. So, as the outermost piece of software in the stack, we're # responsible for setting up logging. # If we're not running as a framework, but as a library, then someone else # is calling us. Therefore, we'll let them set up the logging themselves. if framework: setup_logger('-v' in sys.argv or '--verbose' in sys.argv) config = {} if not options: if sys.argv[-1].endswith('.ini'): config_path = os.path.abspath(sys.argv[-1]) else: config_path = get_moksha_config_path() if not config_path: print NO_CONFIG_MESSAGE return cfg = appconfig('config:' + config_path) config.update(cfg) else: config.update(options) hub = CentralMokshaHub(config, consumers=consumers, producers=producers) global _hub _hub = hub def handle_signal(signum, stackframe): from moksha.hub.reactor import reactor if signum in [signal.SIGHUP, signal.SIGINT]: hub.stop() try: reactor.stop() except ReactorNotRunning: pass signal.signal(signal.SIGHUP, handle_signal) signal.signal(signal.SIGINT, handle_signal) log.info("Running the MokshaHub reactor") from moksha.hub.reactor import reactor reactor.run(installSignalHandlers=False) log.info("MokshaHub reactor stopped")
def load_configs(self): """ Load the configuration files for all applications. Here we iterate over all applications, loading their configuration files and merging their [DEFAULT] configuration into ours. This requires that applications do not have conflicting configuration variable names. To mitigate this, applications should use some basic variable namespacing, such as `myapp.myvariable = myvalue`. We first make sure to load up Moksha's configuration, for the cases where it is being run as WSGI middleware in a different environment. """ apps = [] loaded_configs = [] conf_d = '/etc/moksha/conf.d/%s/' moksha_config_path = get_moksha_config_path() if moksha_config_path: moksha_config_path = os.path.dirname(moksha_config_path) apps = [{'path': moksha_config_path}] apps += moksha.common.utils._apps.values() for app in apps: for configfile in ('production.ini', 'development.ini'): for path in (app['path'], conf_d % app.get('project_name')): confpath = os.path.join(path, configfile) if os.path.exists(confpath): conf = appconfig('config:' + confpath) if app.get('name'): moksha.common.utils._apps[ app['name']]['config'] = conf if confpath in loaded_configs: continue log.info('Loading configuration: %s' % confpath) # This is leftover from the days of using paste.deploy.appconfig. Is anything # using this? # for entry in conf.global_conf: # if entry.startswith('_'): # continue # if entry in config: # log.warning('Conflicting variable: %s' % entry) # continue # else: # config[entry] = conf.global_conf[entry] # log.debug('Set `%s` in global config' % entry) loaded_configs.append(confpath) break
def load_configs(self): """ Load the configuration files for all applications. Here we iterate over all applications, loading their configuration files and merging their [DEFAULT] configuration into ours. This requires that applications do not have conflicting configuration variable names. To mitigate this, applications should use some basic variable namespacing, such as `myapp.myvariable = myvalue`. We first make sure to load up Moksha's configuration, for the cases where it is being run as WSGI middleware in a different environment. """ apps = [] loaded_configs = [] conf_d = '/etc/moksha/conf.d/%s/' moksha_config_path = get_moksha_config_path() if moksha_config_path: moksha_config_path = os.path.dirname(moksha_config_path) apps = [{'path': moksha_config_path}] apps += moksha.common.utils._apps.values() for app in apps: for configfile in ('production.ini', 'development.ini'): for path in (app['path'], conf_d % app.get('project_name')): confpath = os.path.join(path, configfile) if os.path.exists(confpath): conf = appconfig('config:' + confpath) if app.get('name'): moksha.common.utils._apps[app['name']]['config'] = conf if confpath in loaded_configs: continue log.info('Loading configuration: %s' % confpath) # This is leftover from the days of using paste.deploy.appconfig. Is anything # using this? # for entry in conf.global_conf: # if entry.startswith('_'): # continue # if entry in config: # log.warning('Conflicting variable: %s' % entry) # continue # else: # config[entry] = conf.global_conf[entry] # log.debug('Set `%s` in global config' % entry) loaded_configs.append(confpath) break
def main(options=None, consumers=None, producers=None): """ The main MokshaHub method """ setup_logger('-v' in sys.argv or '--verbose' in sys.argv) config = {} if not options: if sys.argv[-1].endswith('.ini'): config_path = os.path.abspath(sys.argv[-1]) else: config_path = get_moksha_config_path() if not config_path: print NO_CONFIG_MESSAGE return cfg = appconfig('config:' + config_path) config.update(cfg) else: config.update(options) hub = CentralMokshaHub(config, consumers=consumers, producers=producers) global _hub _hub = hub def handle_signal(signum, stackframe): from moksha.hub.reactor import reactor if signum in [signal.SIGHUP, signal.SIGINT]: hub.stop() try: reactor.stop() except ReactorNotRunning: pass signal.signal(signal.SIGHUP, handle_signal) signal.signal(signal.SIGINT, handle_signal) log.info("Running the MokshaHub reactor") from moksha.hub.reactor import reactor reactor.run(installSignalHandlers=False) log.info("MokshaHub reactor stopped")