diff options
author | 2018-02-07 22:06:31 +0100 | |
---|---|---|
committer | 2018-02-07 22:06:31 +0100 | |
commit | 9e2b4956598d08676c49315550b56a0ae19b680a (patch) | |
tree | e85a821206d3efd3e729257acd3e40c76ab4dcb4 | |
parent | Updated the banner logo (diff) |
update gunicorn_config (#2)
* update gunicorn_config
this changes the initialisation step of the database to use the environment variables
-rw-r--r-- | gunicorn_config.py | 60 |
1 files changed, 31 insertions, 29 deletions
diff --git a/gunicorn_config.py b/gunicorn_config.py index 2b5c52ea..34d9fa55 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -1,41 +1,43 @@ def when_ready(server): """ server hook that only runs when the gunicorn master process loads """ + import os import traceback import rethinkdb as r try: server.log.info("rethinkdb initialising") - construct = [ - {'DB': 'test', 'TABLE': 'test', 'INDEXES': ['test']}, - ] - for struct in construct: - DB = struct['DB'] - TABLE = struct['TABLE'] - INDEXES = struct['INDEXES'] - - conn = r.connect(host='pdrdb', port=28016, db=DB) - - # Check if database exists, if not create it - db_exists = r.db_list().contains(DB).run(conn) - if not db_exists: - server.log.info('adding database {0}'.format(DB)) - r.db_create(DB).run(conn) - - # Check if table exist, if not create it - table_exists = r.db(DB).table_list().contains(TABLE).run(conn) - if not table_exists: - server.log.info('adding table {0}'.format(TABLE)) - r.db(DB).table_create(TABLE).run(conn) - - # Check if index exists if not add it - rtable = r.db(DB).table(TABLE) - current_indexes = rtable.index_list().run(conn) - for index in INDEXES: - if index not in current_indexes: - server.log.info('adding index {0}'.format(index)) - rtable.index_create(index).run(conn) + DB_HOST = os.environ.get("RETHINKDB_HOST") + DB_PORT = os.environ.get("RETHINKDB_PORT") + DB_DATABASE = os.environ.get("RETHINKDB_DATABASE") + DB_TABLE = os.environ.get("RETHINKDB_TABLE") + indexes = ['test'] + + conn = r.connect(host=DB_HOST, port=DB_PORT, db=DB_DATABASE) + + # Check if database exists, if not create it + db_exists = r.db_list().contains(DB_DATABASE).run(conn) + + if not db_exists: + server.log.info('adding database {0}'.format(DB_DATABASE)) + r.db_create(DB_DATABASE).run(conn) + + # Check if table exist, if not create it + table_exists = r.db(DB_DATABASE).table_list().contains(DB_TABLE).run(conn) + + if not table_exists: + server.log.info('adding table {0}'.format(DB_TABLE)) + r.db(DB_DATABASE).table_create(DB_TABLE).run(conn) + + # Check if index exists if not add it + rtable = r.db(DB_DATABASE).table(DB_TABLE) + current_indexes = rtable.index_list().run(conn) + + for index in indexes: + if index not in current_indexes: + server.log.info('adding index {0}'.format(index)) + rtable.index_create(index).run(conn) server.log.info("rethinkdb ready") |