Add central configuration by python modules

Configuration module is loaded from environment variable
OGN_CONFIG_MODULE, defaults to "config/defaults.py".
pull/35/head
Fabian P. Schmidt 2016-01-29 02:34:12 +01:00
rodzic 539630ea91
commit 6aec9ee35d
4 zmienionych plików z 30 dodań i 10 usunięć

Wyświetl plik

@ -93,6 +93,14 @@ The following scripts run in the foreground and should be deamonized
celery -A ogn.collect worker -l info
```
To load a custom configuration, create a file `myconfig.py` (see [config/default.py](config/default.py))
and set the environment variable `OGN_CONFIG_MODULE` accordingly.
```
export OGN_CONFIG_MODULE="myconfig.py"
./manage.py gateway.run
```
### manage.py - CLI options
```
usage: manage.py [<namespace>.]<command> [<args>]

Wyświetl plik

@ -0,0 +1,4 @@
SQLALCHEMY_DATABASE_URI = 'sqlite:///beacons.db'
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

Wyświetl plik

@ -1,21 +1,19 @@
import os
import importlib
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from celery import Celery
from celery.signals import worker_init, worker_shutdown
app = Celery('ogn.collect',
broker='redis://localhost:6379/0',
backend='redis://localhost:6379/0',
include=["ogn.collect.database", "ogn.collect.logbook"])
DB_URI = 'sqlite:///beacons.db'
os.environ.setdefault('OGN_CONFIG_MODULE', 'config.default')
config = importlib.import_module(os.environ['OGN_CONFIG_MODULE'])
@worker_init.connect
def connect_db(signal, sender):
# Load settings like DB_URI...
engine = create_engine(DB_URI, echo=False)
engine = create_engine(config.SQLALCHEMY_DATABASE_URI, echo=False)
Session = sessionmaker(bind=engine)
sender.app.session = Session()
@ -26,5 +24,8 @@ def close_db(signal, sender):
sender.app.session.close()
if __name__ == '__main__':
app.start()
app = Celery('ogn.collect',
include=["ogn.collect.database",
"ogn.collect.logbook"])
app.config_from_envvar("OGN_CONFIG_MODULE")

Wyświetl plik

@ -1,6 +1,13 @@
import os
import importlib
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///beacons.db', echo=False)
os.environ.setdefault('OGN_CONFIG_MODULE', 'config.default')
config = importlib.import_module(os.environ['OGN_CONFIG_MODULE'])
engine = create_engine(config.SQLALCHEMY_DATABASE_URI, echo=False)
Session = sessionmaker(bind=engine)
session = Session()