X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_common%2Fdbmongo.py;fp=osm_common%2Fdbmongo.py;h=af63d5b172ac7ecd3a3469c7db2a122525c6164d;hb=136f29577fd83028369c2c4fc4c60f738e0d26d3;hp=9b5bc57e8c6bfac246e5caec44b9273acf1e4685;hpb=48b78e112662d927cad376d739e43fb94f108ff3;p=osm%2Fcommon.git diff --git a/osm_common/dbmongo.py b/osm_common/dbmongo.py index 9b5bc57..af63d5b 100644 --- a/osm_common/dbmongo.py +++ b/osm_common/dbmongo.py @@ -22,6 +22,7 @@ from osm_common.dbbase import DbException, DbBase from http import HTTPStatus from time import time, sleep from copy import deepcopy +from base64 import b64decode __author__ = "Alfonso Tierno " @@ -66,10 +67,11 @@ class DbMongo(DbBase): self.client = None self.db = None - def db_connect(self, config): + def db_connect(self, config, target_version=None): """ Connect to database :param config: Configuration of database + :param target_version: if provided it checks if database contains required version, raising exception otherwise. :return: None or raises DbException on error """ try: @@ -83,7 +85,19 @@ class DbMongo(DbBase): now = time() while True: try: - self.db.users.find_one({"username": "admin"}) + version_data = self.get_one("admin", {"_id": "version"}, fail_on_empty=False, fail_on_more=True) + # check database status is ok + if version_data and version_data.get("status") != 'ENABLED': + raise DbException("Wrong database status '{}'".format(version_data.get("status")), + http_code=HTTPStatus.INTERNAL_SERVER_ERROR) + # check version + db_version = None if not version_data else version_data.get("version") + if target_version and target_version != db_version: + raise DbException("Invalid database version {}. Expected {}".format(db_version, target_version)) + # get serial + if version_data and version_data.get("serial"): + self.set_secret_key(b64decode(version_data["serial"])) + self.logger.info("Connected to database {} version {}".format(config["name"], db_version)) return except errors.ConnectionFailure as e: if time() - now >= self.conn_initial_timout: