diff --git a/persistance/mnemodb.py b/persistance/mnemodb.py index 8579102..50786ed 100644 --- a/persistance/mnemodb.py +++ b/persistance/mnemodb.py @@ -32,11 +32,17 @@ class MnemoDB(object): - def __init__(self, database_name): + def __init__(self, mongo_host, mongo_port, database_name, mongo_user=None, mongo_password=None, mongo_auth_mechanism=None): logger.info('Connecting to mongodb, using "{0}" as database.'.format(database_name)) - conn = MongoClient(auto_start_request=False) - self.rg = ReportGenerator(database_name) - self.db = conn[database_name] + if mongo_user is not None: + conn = MongoClient(host=mongo_host, port=mongo_port, auto_start_request=False) + self.rg = ReportGenerator(mongo_host, mongo_port, database_name, mongo_user, mongo_password,mongo_auth_mechanism) + self.db = conn[database_name] + self.db.authenticate(mongo_user, mongo_password, mechanism=mongo_auth_mechanism) + else: + conn = MongoClient(host=mongo_host, port=mongo_port, auto_start_request=False) + self.rg = ReportGenerator(mongo_host, mongo_port, database_name) + self.db = conn[database_name] self.ensure_index() def ensure_index(self): diff --git a/persistance/preagg_reports.py b/persistance/preagg_reports.py index eea649f..7b8c2f3 100644 --- a/persistance/preagg_reports.py +++ b/persistance/preagg_reports.py @@ -27,10 +27,15 @@ class ReportGenerator: Generates pre-aggregated reports. """ - def __init__(self, database_name): + def __init__(self, mongo_host, mongo_port, database_name, mongo_user=None, mongo_password=None, mongo_auth_mechanism=None): logger.info('Connecting to mongodb, using "{0}" as database.'.format(database_name)) - conn = MongoClient(w=0) - self.db = conn[database_name] + if mongo_user is not None: + conn = MongoClient(host=mongo_host, port=mongo_port, w=0) + self.db = conn[database_name] + self.db.authenticate(mongo_user, mongo_password, mechanism=mongo_auth_mechanism) + else: + conn = MongoClient(host=mongo_host, port=mongo_port, w=0) + self.db = conn[database_name] def hpfeeds(self, entry): hour = entry['timestamp'].hour diff --git a/runner.py b/runner.py index e9389ed..36d7591 100644 --- a/runner.py +++ b/runner.py @@ -57,6 +57,21 @@ def parse_config(config_file): config['mongo_db'] = parser.get('mongodb', 'database') + if os.getenv("REMOTE_MONGO") == "true": + config['mongo_host'] = os.getenv("MONGO_HOST") + config['mongo_port'] = int(os.getenv("MONGO_PORT")) + + config['mongo_auth'] = False + if os.getenv("MONGO_AUTH") == "true": + config['mongo_auth'] = True + config['mongo_user'] = os.getenv("MONGO_USER") + config['mongo_password'] = os.getenv("MONGO_PASSWORD") + config['mongo_auth_mechanism'] = os.getenv("MONGO_AUTH_MECHANISM") + else: + config['mongo_auth'] = False + config['mongo_host'] = "127.0.0.1" + config['mongo_port'] = 27017 + config['hpf_feeds'] = parser.get('hpfriends', 'channels').split(',') config['hpf_ident'] = parser.get('hpfriends', 'ident') config['hpf_secret'] = parser.get('hpfriends', 'secret') @@ -113,7 +128,10 @@ def do_logging(file_log=None, loggly_token=None): greenlets = {} - db = mnemodb.MnemoDB(c['mongo_db']) + if c['mongo_auth']: + db = mnemodb.MnemoDB(c['mongo_host'], c['mongo_port'], c['mongo_db'], c['mongo_user'], c['mongo_password'], c['mongo_auth_mechanism']) + else: + db = mnemodb.MnemoDB(c['mongo_host'], c['mongo_port'], c['mongo_db']) webapi = None hpfriends_puller = None