Commit daca4db6 authored by Lukáš Lalinský's avatar Lukáš Lalinský

Read config from env variables

parent df2a708d
Pipeline #20164 passed with stage
in 1 minute and 18 seconds
......@@ -10,6 +10,22 @@ from sqlalchemy.engine.url import URL
logger = logging.getLogger(__name__)
def str_to_bool(x):
return x.lower() in ('1', 'on', 'true')
def read_env_item(obj, key, name, convert=None):
value is None
if name in os.environ:
value = os.environ[name]
if name + '_FILE' in os.environ:
value = open(os.environ[name + '_FILE']).read().strip()
if value is not None:
if convert is not None:
value = convert(value)
setattr(obj, key, value)
class DatabaseConfig(object):
def __init__(self):
......@@ -65,6 +81,12 @@ class DatabaseConfig(object):
if parser.has_option(section, 'password'):
self.password = parser.get(section, 'password')
def read_env(self):
read_env_item(self, 'name', 'ACOUSTID_POSTGRES_DB')
read_env_item(self, 'host', 'ACOUSTID_POSTGRES_HOST')
read_env_item(self, 'port', 'ACOUSTID_POSTGRES_PORT', convert=int)
read_env_item(self, 'user', 'ACOUSTID_POSTGRES_USER')
read_env_item(self, 'password', 'ACOUSTID_POSTGRES_PASSWORD')
class IndexConfig(object):
......@@ -78,6 +100,10 @@ class IndexConfig(object):
if parser.has_option(section, 'port'):
self.port = parser.getint(section, 'port')
def read_env(self):
read_env_item(self, 'host', 'ACOUSTID_INDEX_HOST')
read_env_item(self, 'port', 'ACOUSTID_INDEX_PORT', convert=int)
class RedisConfig(object):
......@@ -91,6 +117,10 @@ class RedisConfig(object):
if parser.has_option(section, 'port'):
self.port = parser.getint(section, 'port')
def read_env(self):
read_env_item(self, 'host', 'ACOUSTID_REDIS_HOST')
read_env_item(self, 'port', 'ACOUSTID_REDIS_PORT', convert=int)
class LoggingConfig(object):
......@@ -111,6 +141,9 @@ class LoggingConfig(object):
if parser.has_option(section, 'syslog_facility'):
self.syslog_facility = parser.get(section, 'syslog_facility')
def read_env(self):
pass # XXX
class WebSiteConfig(object):
......@@ -138,6 +171,14 @@ class WebSiteConfig(object):
if parser.has_option(section, 'maintenance'):
self.maintenance = parser.getboolean(section, 'maintenance')
def read_env(self):
read_env_item(self, 'debug', 'ACOUSTID_DEBUG', convert=str_to_bool)
read_env_item(self, 'maintenance', 'ACOUSTID_MAINTENANCE', convert=str_to_bool)
read_env_item(self, 'mb_oauth_client_id', 'ACOUSTID_MB_OAUTH_CLIENT_ID')
read_env_item(self, 'mb_oauth_client_secret', 'ACOUSTID_MB_OAUTH_CLIENT_SECRET')
read_env_item(self, 'google_oauth_client_id', 'ACOUSTID_GOOGLE_OAUTH_CLIENT_ID')
read_env_item(self, 'google_oauth_client_secret', 'ACOUSTID_GOOGLE_OAUTH_CLIENT_SECRET')
class ReplicationConfig(object):
......@@ -151,6 +192,9 @@ class ReplicationConfig(object):
if parser.has_option(section, 'import_acoustid_musicbrainz'):
self.import_acoustid_musicbrainz = parser.get(section, 'import_acoustid_musicbrainz')
def read_env(self):
pass # XXX
class ClusterConfig(object):
......@@ -167,6 +211,11 @@ class ClusterConfig(object):
if parser.has_option(section, 'secret'):
self.secret = parser.get(section, 'secret')
def read_env(self):
read_env_item(self, 'role', 'ACOUSTID_CLUSTER_ROLE')
read_env_item(self, 'base_master_url', 'ACOUSTID_CLUSTER_BASE_MASTER_URL')
read_env_item(self, 'secret', 'ACOUSTID_CLUSTER_SECRET')
class RateLimiterConfig(object):
......@@ -181,27 +230,41 @@ class RateLimiterConfig(object):
elif name.startswith('application.'):
self.applications[int(name.split('.', 1)[1])] = parser.getfloat(section, name)
def read_env(self):
pass # XXX
class Config(object):
def __init__(self, path):
logger.info("Loading configuration file %s", path)
self.database = DatabaseConfig()
self.logging = LoggingConfig()
self.website = WebSiteConfig()
self.index = IndexConfig()
self.redis = RedisConfig()
self.replication = ReplicationConfig()
self.cluster = ClusterConfig()
self.rate_limiter = RateLimiterConfig()
def read(self, path):
parser = ConfigParser.RawConfigParser()
parser.read(path)
self.database = DatabaseConfig()
self.database.read(parser, 'database')
self.logging = LoggingConfig()
self.logging.read(parser, 'logging')
self.website = WebSiteConfig()
self.website.read(parser, 'website')
self.index = IndexConfig()
self.index.read(parser, 'index')
self.redis = RedisConfig()
self.redis.read(parser, 'redis')
self.replication = ReplicationConfig()
self.replication.read(parser, 'replication')
self.cluster = ClusterConfig()
self.cluster.read(parser, 'cluster')
self.rate_limiter = RateLimiterConfig()
self.rate_limiter.read(parser, 'rate_limiter')
def read_env(self):
self.database.read_env()
self.logging.read_env()
self.website.read_env()
self.index.read_env()
self.redis.read_env()
self.replication.read_env()
self.cluster.read_env()
self.rate_limiter.read_env()
......@@ -17,7 +17,10 @@ logger = logging.getLogger(__name__)
class Script(object):
def __init__(self, config_path, tests=False):
self.config = Config(config_path)
self.config = Config()
if config_path:
self.config.read(config_path)
self.config.read_env()
if tests:
self.engine = sqlalchemy.create_engine(self.config.database.create_url(),
poolclass=sqlalchemy.pool.AssertionPool)
......
......@@ -8,7 +8,6 @@ from werkzeug.routing import Map, Rule, Submount
from werkzeug.wrappers import Request, Response
from jinja2 import Environment, FileSystemLoader
import sqlalchemy
from acoustid.config import Config
from acoustid import api, handlers
from acoustid.script import Script
import api.v1
......
......@@ -13,7 +13,7 @@ from acoustid.web.views.metadata import metadata_page
from acoustid.web.views.stats import stats_page
from acoustid.web.views.admin import admin_page
config_filename = os.environ['ACOUSTID_CONFIG']
config_filename = os.environ.get('ACOUSTID_CONFIG')
script = Script(config_filename)
script.setup_logging()
......
......@@ -13,7 +13,9 @@ target_metadata = acoustid.tables.metadata
import acoustid.config
acoustid_config_filename = os.environ.get('ACOUSTID_CONFIG',
os.path.join(os.path.dirname(__file__), '..', 'acoustid.conf'))
acoustid_config = acoustid.config.Config(acoustid_config_filename)
acoustid_config = acoustid.config.Config()
acoustid_config.read(acoustid_config_filename)
acoustid_config.read_env()
def include_object(obj, name, type, reflected, compare_to):
if type == "table" and obj.schema == "musicbrainz":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment