def get_redis_client(system_config, decode_response=True): assert "redis" in system_config, "Can't read redis config from system config" redis_config = system_config["redis"] redis_password = None if "password" in redis_config: redis_password = redis_config["password"] redis_client = RedisDatabase(redis_config["host"], redis_config["port"], redis_config["db"], redis_password, decode_response) return redis_client
from nmtwizard import config, task from nmtwizard.redis_database import RedisDatabase from nmtwizard.worker import Worker cfg = configparser.ConfigParser() cfg.read('settings.ini') logging.basicConfig(stream=sys.stdout, level=cfg.get('default', 'log_level')) logger = logging.getLogger() redis_password = None if cfg.has_option('redis', 'password'): redis_password = cfg.get('redis', 'password') redis = RedisDatabase(cfg.get('redis', 'host'), cfg.getint('redis', 'port'), cfg.get('redis', 'db'), redis_password) retry = 0 while retry < 10: try: # make sure notify events are set redis.config_set('notify-keyspace-events', 'Klgx') break except ConnectionError as e: retry += 1 logger.warn("cannot connect to redis DB - retrying (%d)" % retry) time.sleep(1) assert retry < 10, "Cannot connect to redis DB - aborting"
with open(default_file) as default_fh: default_config = default_fh.read() base_config = json.loads(default_config) assert 'storages' in base_config, "incomplete configuration - missing " \ "`storages` in %s" % default_file app.iniconfig = FlaskIni() with app.app_context(): app.iniconfig.read(config_file) app.logger.setLevel( logging.getLevelName( app.iniconfig.get('default', 'log_level', fallback='ERROR'))) redis_db = RedisDatabase(app.iniconfig.get('redis', 'host'), app.iniconfig.get('redis', 'port', fallback=6379), app.iniconfig.get('redis', 'db', fallback=0), app.iniconfig.get('redis', 'password', fallback=None)) redis_db_without_decode = RedisDatabase( app.iniconfig.get('redis', 'host'), app.iniconfig.get('redis', 'port', fallback=6379), app.iniconfig.get('redis', 'db', fallback=0), app.iniconfig.get('redis', 'password', fallback=None), False) assert app.iniconfig.get( 'default', 'taskfile_dir'), "missing taskfile_dir from settings.ini" taskfile_dir = app.iniconfig.get('default', 'taskfile_dir') assert os.path.isdir( taskfile_dir), "taskfile_dir (%s) must be a directory" % taskfile_dir retry = 0
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) config.add_log_handler(ch) common.add_log_handler(ch) cfg = configparser.ConfigParser() cfg.read('settings.ini') MODE = os.getenv('LAUNCHER_MODE', 'Production') redis_password = None if cfg.has_option(MODE, 'redis_password'): redis_password = cfg.get(MODE, 'redis_password') redis = RedisDatabase(cfg.get(MODE, 'redis_host'), cfg.getint(MODE, 'redis_port'), cfg.get(MODE, 'redis_db'), redis_password) services = config.load_services(cfg.get(MODE, 'config_dir')) def _get_service(service): """Wrapper to fail on invalid service.""" if service not in services: response = flask.jsonify(message="invalid service name: %s" % service) flask.abort(flask.make_response(response, 404)) return services[service] app = flask.Flask(__name__) @app.route("/list_services", methods=["GET"])
from nmtwizard import config, common import logging app = Flask(__name__) ch = logging.StreamHandler() ch.setLevel(logging.ERROR) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) config.add_log_handler(ch) common.add_log_handler(ch) config_file = os.getenv('LAUNCHER_CONFIG') assert config_file is not None and os.path.isfile( config_file), "invalid LAUNCHER_CONFIG" app.iniconfig = FlaskIni() with app.app_context(): app.iniconfig.read(config_file) redis = RedisDatabase(app.iniconfig.get('redis', 'host'), app.iniconfig.get('redis', 'port', fallback=6379), app.iniconfig.get('redis', 'db', fallback=0), app.iniconfig.get('redis', 'password', fallback=None)) services, base_config = config.load_services( app.iniconfig.get('default', 'config_dir')) from app import routes