Skip to content

Commit

Permalink
changes to database connection code to support "authsource" parameter…
Browse files Browse the repository at this point in the history
… in db file
  • Loading branch information
computron committed May 10, 2019
1 parent 760af82 commit 02eb9a4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
5 changes: 3 additions & 2 deletions atomate/feff/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
class FeffCalcDb(CalcDb):

def __init__(self, host="localhost", port=27017, database="feff", collection="tasks", user=None,
password=None):
super(FeffCalcDb, self).__init__(host, port, database, collection, user, password)
password=None, **kwargs):
super(FeffCalcDb, self).__init__(host, port, database, collection,
user, password, **kwargs)

def build_indexes(self, indexes=None, background=True):
_indexes = indexes if indexes else ["structure.formula"]
Expand Down
5 changes: 3 additions & 2 deletions atomate/lammps/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
class LammpsCalcDb(CalcDb):

def __init__(self, host="localhost", port=27017, database="lammps", collection="tasks",
user=None, password=None):
super(LammpsCalcDb, self).__init__(host, port, database, collection, user, password)
user=None, password=None, **kwargs):
super(LammpsCalcDb, self).__init__(host, port, database, collection,
user, password, **kwargs)

def build_indexes(self, indexes=None, background=True):
indexes = indexes or []
Expand Down
5 changes: 3 additions & 2 deletions atomate/qchem/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ def __init__(self,
database="qchem",
collection="tasks",
user=None,
password=None):
password=None,
**kwargs):
super(QChemCalcDb, self).__init__(host, port, database, collection,
user, password)
user, password, **kwargs)

def build_indexes(self, indexes=None, background=True):
"""
Expand Down
13 changes: 10 additions & 3 deletions atomate/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@

class CalcDb(six.with_metaclass(ABCMeta)):

def __init__(self, host, port, database, collection, user, password):
def __init__(self, host, port, database, collection, user, password, **kwargs):
self.host = host
self.db_name = database
self.user = user
self.password = password
self.port = int(port)

try:
self.connection = MongoClient(self.host, self.port)
self.connection = MongoClient(host=self.host, port=self.port,
username=self.user,
password=self.password, **kwargs)
self.db = self.connection[self.db_name]
except:
logger.error("Mongodb connection failed")
Expand Down Expand Up @@ -122,5 +125,9 @@ def from_db_file(cls, db_file, admin=True):
user = creds.get("readonly_user")
password = creds.get("readonly_password")

kwargs = {}
if "authsource" in creds:
kwargs["authsource"] = creds["authsource"]

return cls(creds["host"], int(creds["port"]), creds["database"], creds["collection"],
user, password)
user, password, **kwargs)
14 changes: 11 additions & 3 deletions atomate/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,23 @@ def get_uri(dir_name):

def get_database(config_file=None, settings=None, admin=False, **kwargs):
d = loadfn(config_file) if settings is None else settings
conn = MongoClient(host=d["host"], port=d["port"], **kwargs)
db = conn[d["database"]]

try:
user = d["admin_user"] if admin else d["readonly_user"]
passwd = d["admin_password"] if admin else d["readonly_password"]
db.authenticate(user, passwd)
except (KeyError, TypeError, ValueError):
logger.warning("No {admin,readonly}_user/password found in config. file, "
"accessing DB without authentication")
user = None
passwd = None

if "authsource" in d and "authsource" not in kwargs:
kwargs["authsource"] = d["authsource"]

conn = MongoClient(host=d["host"], port=d["port"], username=user,
password=passwd, **kwargs)
db = conn[d["database"]]

return db


Expand Down
5 changes: 3 additions & 2 deletions atomate/vasp/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class VaspCalcDb(CalcDb):
"""

def __init__(self, host="localhost", port=27017, database="vasp", collection="tasks", user=None,
password=None):
super(VaspCalcDb, self).__init__(host, port, database, collection, user, password)
password=None, **kwargs):
super(VaspCalcDb, self).__init__(host, port, database, collection, user,
password, **kwargs)

def build_indexes(self, indexes=None, background=True):
"""
Expand Down

0 comments on commit 02eb9a4

Please sign in to comment.