From 6e3643bf6afce107e9ce58d5b78dce4d0be093f0 Mon Sep 17 00:00:00 2001 From: Matt Jia Date: Aug 15 2017 01:51:38 +0000 Subject: refactor: in order to read configuration and policies outside of Flask --- diff --git a/greenwave/app_factory.py b/greenwave/app_factory.py index 5519e7c..b43c639 100644 --- a/greenwave/app_factory.py +++ b/greenwave/app_factory.py @@ -1,50 +1,21 @@ # SPDX-License-Identifier: GPL-2.0+ -import os -import glob -import yaml from flask import Flask from greenwave.logger import init_logging from greenwave.api_v1 import api -from greenwave.utils import json_error +from greenwave.utils import json_error, load_config from requests import ConnectionError, Timeout from werkzeug.exceptions import default_exceptions -def load_config(app): - # Load default config, then override that with a config file - if os.getenv('DEV') == 'true': - default_config_obj = 'greenwave.config.DevelopmentConfig' - default_config_file = os.getcwd() + '/conf/settings.py' - elif os.getenv('TEST') == 'true': - default_config_obj = 'greenwave.config.TestingConfig' - default_config_file = os.getcwd() + '/conf/settings.py' - else: - default_config_obj = 'greenwave.config.ProductionConfig' - default_config_file = '/etc/greenwave/settings.py' - app.config.from_object(default_config_obj) - config_file = os.environ.get('GREENWAVE_CONFIG', default_config_file) - app.config.from_pyfile(config_file) - if os.environ.get('SECRET_KEY'): - app.config['SECRET_KEY'] = os.environ['SECRET_KEY'] - - # applicaiton factory http://flask.pocoo.org/docs/0.12/patterns/appfactories/ def create_app(config_obj=None): app = Flask(__name__) - if config_obj: - app.config.from_object(config_obj) - else: - load_config(app) + app.config.update(load_config(config_obj)) if app.config['PRODUCTION'] and app.secret_key == 'replace-me-with-something-random': raise Warning("You need to change the app.secret_key value for production") - #load policies - policy_pathnames = glob.glob(os.path.join(app.config['POLICIES_DIR'], '*.yaml')) - app.config['policies'] = [] - for policy_pathname in policy_pathnames: - app.config['policies'].extend(yaml.safe_load_all(open(policy_pathname, 'r'))) # register error handlers for code in default_exceptions.iterkeys(): app.register_error_handler(code, json_error) diff --git a/greenwave/utils.py b/greenwave/utils.py index 4853abf..6ef54b7 100644 --- a/greenwave/utils.py +++ b/greenwave/utils.py @@ -1,6 +1,10 @@ # SPDX-License-Identifier: GPL-2.0+ +import os +import glob +import yaml from flask import jsonify, current_app +from flask.config import Config from werkzeug.exceptions import HTTPException @@ -22,3 +26,47 @@ def json_error(error): response = jsonify(message=str(error.message)) response.status_code = 500 return response + + +def load_config(config_obj=None): + """ + Load Greenwave configuration. If the config_obj is given, it will load the + configuration from there. Otherwise, it will load the configuration based on + how the environment is configured. + :param str config_obj: An config object. For example, greenwave.config.DevelopmentConfig. + :return: A dict of Greenwave configuration. + """ + config = Config(__name__) + if config_obj: + config.from_object(config_obj) + else: + # Load default config, then override that with a config file + default_config_file = None + if os.getenv('DEV') == 'true': + default_config_obj = 'greenwave.config.DevelopmentConfig' + elif os.getenv('TEST') == 'true': + default_config_obj = 'greenwave.config.TestingConfig' + else: + default_config_obj = 'greenwave.config.ProductionConfig' + default_config_file = '/etc/greenwave/settings.py' + config.from_object(default_config_obj) + config_file = os.environ.get('GREENWAVE_CONFIG', default_config_file) + if config_file: + config.from_pyfile(config_file) + config['policies'] = load_policies(config['POLICIES_DIR']) + return config + + +def load_policies(policies_dir): + """ + Load Greenwave policies from the given policies directory. + + :param str policies_dir: A path points to the policies directory. + :return: A list of policies. + + """ + policy_pathnames = glob.glob(os.path.join(policies_dir, '*.yaml')) + policies = [] + for policy_pathname in policy_pathnames: + policies.extend(yaml.safe_load_all(open(policy_pathname, 'r'))) + return policies