Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for connection to remote mongodb #23

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
14 changes: 10 additions & 4 deletions persistance/mnemodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 8 additions & 3 deletions persistance/preagg_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down