def wsgi_app(): """ Start flask application runtime """ # Setup the app new_app = Flask('archelond') # Get configuration from default or via environment variable if os.environ.get('ARCHELOND_CONF'): new_app.config.from_envvar('ARCHELOND_CONF') else: new_app.config.from_object('archelond.config') # Setup database if new_app.config['DATABASE_TYPE'] == 'MemoryData': new_app.data = MemoryData(new_app.config) elif new_app.config['DATABASE_TYPE'] == 'ElasticData': new_app.data = ElasticData(new_app.config) else: raise Exception('No valid database type is set') # Set up logging configure_logging(new_app) # Load up user database try: new_app.config['users'] = HtpasswdFile(new_app.config['HTPASSWD_PATH']) except IOError: log.critical( 'No htpasswd file loaded, please set `ARCHELOND_HTPASSWD`' 'environment variable to a valid apache htpasswd file.' ) new_app.config['users'] = HtpasswdFile() return new_app
def wsgi_app(): """ Start flask application runtime """ # Disabling check since both types are implementations of same base class # pylint: disable=redefined-variable-type # Setup the app new_app = Flask('archelond') # Get configuration from default or via environment variable if os.environ.get('ARCHELOND_CONF'): new_app.config.from_envvar('ARCHELOND_CONF') else: new_app.config.from_object('archelond.config') # Setup database if new_app.config['DATABASE_TYPE'] == 'MemoryData': new_app.data = MemoryData(new_app.config) elif new_app.config['DATABASE_TYPE'] == 'ElasticData': new_app.data = ElasticData(new_app.config) else: raise Exception('No valid database type is set') # Set up logging configure_logging(new_app) return new_app
def test_syslog_devices(self): """ Test syslog address handling and handler """ # Pylint in Python 3.4 (not 3.3 or 2.7), is triggering # this for syslog_handler.address even though the asserts # show it is available at runtime. # pylint: disable=no-member import logging for log_device in ['/dev/log', '/var/run/syslog', '']: root_logger = logging.getLogger() # Nuke syslog handlers from init syslog_handlers = [] for handler in root_logger.handlers: if isinstance(handler, logging.handlers.SysLogHandler): syslog_handlers.append(handler) for handler in syslog_handlers: root_logger.removeHandler(handler) real_exists = os.path.exists(log_device) def mock_effect(*args): """Contextual choice of log device.""" if args[0] == log_device: # pylint: disable=cell-var-from-loop return True return False # Call so that it will think /dev/log exists with mock.patch('os.path') as os_exists: os_exists.exists.side_effect = mock_effect from archelond.log import configure_logging if not real_exists and log_device != '': with self.assertRaises(Exception): configure_logging(app) else: configure_logging(app) syslog_handler = None for handler in root_logger.handlers: if isinstance(handler, logging.handlers.SysLogHandler): syslog_handler = handler self.assertIsNotNone(syslog_handler) if log_device == '': self.assertEqual( syslog_handler.address, ('127.0.0.1', 514) ) else: self.assertEqual(syslog_handler.address, log_device)
def run_server(): """ If started from command line, rebuild object in debug mode and run directly """ host = os.environ.get('ARCHELOND_HOST', 'localhost') port = int(os.environ.get('ARCHELOND_PORT', '8580')) app.debug = True log.critical( 'Running in debug mode. Do not run this way in production' ) app.config['LOG_LEVEL'] = 'DEBUG' app.config['ASSETS_DEBUG'] = True configure_logging(app) app.run(host=host, port=port)
def test_no_log_level(self): """ Make sure we leave things alone if no log level is set. """ import logging root_logger = logging.getLogger() log_level = root_logger.level self.assertEqual(logging.NOTSET, log_level) from archelond.log import configure_logging log_level = configure_logging(app) self.assertEqual(logging.NOTSET, log_level)
def test_bad_log_level(self): """ Set a non-existent log level and make sure we raise properly """ import logging root_logger = logging.getLogger() log_level = root_logger.level self.assertEqual(logging.NOTSET, log_level) from archelond.log import configure_logging with self.assertRaisesRegexp(ValueError, 'Invalid log level.+'): log_level = configure_logging(app)
def test_config_log_level(self): """ Patch config and make sure we are setting to it """ import logging root_logger = logging.getLogger() log_level = root_logger.level self.assertEqual(logging.NOTSET, log_level) from archelond.log import configure_logging log_level = configure_logging(app) root_logger = logging.getLogger() self.assertEqual(root_logger.level, getattr(logging, TEST_LOG_LEVEL))