def main(): """ The main MokshaHub method """ setup_logger('-v' in sys.argv or '--verbose' in sys.argv) config_path = get_moksha_config_path() if not config_path: print """ Cannot find Moksha configuration! Place a development.ini or production.ini in /etc/moksha or in the current directory. """ return cfg = appconfig('config:' + config_path) config.update(cfg) hub = CentralMokshaHub() 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") reactor.run(installSignalHandlers=False) log.info("MokshaHub reactor stopped")
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._apps.items(): sa_url = app.get('config', {}).get('sqlalchemy.url', None) app_db = 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 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.utils._apps.items(): sa_url = app.get('config', {}).get('sqlalchemy.url', None) app_db = 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 start(self): """ Start all of the Moksha components """ from moksha.lib.helpers import get_moksha_config_path orbited = ['orbited'] if os.path.exists('/etc/moksha/orbited.cfg'): orbited += ['-c', '/etc/moksha/orbited.cfg'] self._exec(*orbited) self._exec('paster', 'serve', get_moksha_config_path()) self._exec('moksha-hub', '-v')
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}] main_app_config_path = os.path.dirname(get_main_app_config_path()) apps += moksha._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._apps[app['name']]['config'] = conf if app['path'] == main_app_config_path or \ confpath in loaded_configs: continue log.info('Loading configuration: %s' % confpath) 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}] main_app_config_path = os.path.dirname(get_main_app_config_path()) apps += moksha.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.utils._apps[app['name']]['config'] = conf if app['path'] == main_app_config_path or \ confpath in loaded_configs: continue log.info('Loading configuration: %s' % confpath) 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