Пример #1
0
 def get_db(self,
            db_url,
            db_name,
            cfg_url_param="database_admin_url",
            check_access=False):
     """
     helper function: check database url and its dialect
     """
     try:
         return osdb(db_url, db_name)
     except osdbAccessDeniedError:
         if check_access:
             raise
         logger.error(
             "failed to connect to DB as %s, please provide or " +
             "fix the '%s'", osdb.get_url_user(db_url), cfg_url_param)
     except osdbArgumentError:
         logger.error("Bad URL, it should resemble: {}".format(
             "backend://*****:*****@hostname" if not \
                 db_url.startswith('sqlite:') else "sqlite:///path/to/db"))
     except osdbConnectError:
         logger.error("Failed to connect to database!")
     except osdbNoSuchModuleError:
         logger.error("This database backend is not supported!  " \
                     "Supported: {}".format(', '.join(SUPPORTED_BACKENDS)))
Пример #2
0
    def do_drop(self, params=None):

        db_url = cfg.read_param("database_url",
                                "Please provide us the URL of the database")
        if db_url is None:
            print()
            logger.error("no URL specified: aborting!")
            return -1

        if params and len(params) > 0:
            db_name = params[0]
        else:
            db_name = cfg.read_param("database_name",
                                     "Please provide the database to drop",
                                     DEFAULT_DB_NAME)

        db = osdb(db_url, db_name)

        # check to see if the database has already been created
        if db.exists():
            if cfg.read_param(
                    "database_force_drop",
                    "Do you really want to drop the '{}' database".format(
                        db_name), False, True):
                db.drop()
            else:
                logger.info("database '{}' not dropped!".format(db_name))
        else:
            logger.warning("database '{}' does not exist!".format(db_name))
Пример #3
0
    def user_db_connect(self):
        engine = osdb.get_db_engine()

        db_url = cfg.read_param(["database_user_url", "database_url"],
                                "Please provide us the URL of the database")
        if db_url is None:
            print()
            logger.error("no URL specified: aborting!")
            return None

        db_url = osdb.set_url_driver(db_url, engine)
        db_name = cfg.read_param(["database_user_name", "database_name"],
                                 "Please provide the database to add user to",
                                 DEFAULT_DB_NAME)

        try:
            db = osdb(db_url, db_name)
        except osdbError:
            logger.error("failed to connect to database %s", db_name)
            return None

        if not db.connect():
            return None

        return db
Пример #4
0
 def getdb(self, db_url, db_name):
     try:
         return osdb(db_url, db_name)
     except osdbArgumentError:
         logger.error("Bad URL, it should resemble: {}".format(
             "backend://*****:*****@hostname" if not \
                 db_url.startswith('sqlite:') else "sqlite:///path/to/db"))
     except osdbConnectError:
         logger.error("Failed to connect to database!")
     except osdbNoSuchModuleError:
         logger.error("This database backend is not supported!  " \
                     "Supported: {}".format(', '.join(SUPPORTED_BACKENDS)))
Пример #5
0
    def do_add(self, params):

        if len(params) < 1:
            logger.error("No module to add added")
            return -1
        module = params[0]

        db_url = cfg.read_param("database_url",
                                "Please provide us the URL of the database")
        if db_url is None:
            print()
            logger.error("no URL specified: aborting!")
            return -1

        if len(params) < 2:
            db_name = cfg.read_param(
                "database_name",
                "Please provide the database to add the module to",
                DEFAULT_DB_NAME)
        else:
            db_name = params[1]

        db = osdb(db_url, db_name)

        if not db.exists():
            logger.warning("database '{}' does not exist!".format(db_name))
            return -1

        db_schema = db.dialect
        schema_path = self.get_schema_path(db_schema)
        if schema_path is None:
            return -1

        module_file_path = os.path.join(schema_path,
                                        "{}-create.sql".format(module))
        if not os.path.isfile(module_file_path):
            logger.warning(
                "cannot find OpenSIPS DB file: '{}'!".format(module_file_path))
            return -1

        db.use()
        try:
            db.create_module(module_file_path)
        except osdbError as ex:
            logger.error("cannot import: {}".format(ex))
            return -1

        db.destroy()
        logger.info("Module {} has been successfully added!".format(module))
        return 0
Пример #6
0
    def user_db_connect(self):
        db_url = cfg.read_param(["database_user_url", "database_url"],
                                "Please provide us the URL of the database")
        if db_url is None:
            print()
            logger.error("no URL specified: aborting!")
            return None

        db_name = cfg.read_param(["database_user_name", "database_name"],
                                 "Please provide the database to add user to",
                                 DEFAULT_DB_NAME)

        db = osdb(db_url, db_name)
        db.connect()
        return db
Пример #7
0
    def user_db_connect(self):
        engine = osdb.get_db_engine()

        db_url = cfg.read_param(["database_user_url", "database_url"],
                                "Please provide us the URL of the database")
        if db_url is None:
            print()
            logger.error("no URL specified: aborting!")
            return None, None

        db_url = osdb.set_url_driver(db_url, engine)
        db_name = cfg.read_param(["database_user_name", "database_name"],
                                 "Please provide the database to add user to",
                                 DEFAULT_DB_NAME)

        try:
            db = osdb(db_url, db_name)
        except osdbError:
            logger.error("failed to connect to database %s", db_name)
            return None, None

        if not db.connect():
            return None, None

        res = db.find('version', 'table_version', {'table_name': USER_TABLE})
        if not res:
            osips_ver = '3.2+'
        else:
            # RFC 8760 support was introduced in OpenSIPS 3.2 (table ver 8+)
            tb_ver = res.first()[0]
            if tb_ver >= 8:
                osips_ver = '3.2+'
            else:
                osips_ver = '3.1'

        return db, osips_ver
Пример #8
0
    def do_create(self, params=None):

        db_url = cfg.read_param("database_url",
                                "Please provide us the URL of the database")
        if db_url is None:
            print()
            logger.error("no URL specified: aborting!")
            return -1

        if params and len(params) > 0:
            db_name = params[0]
        else:
            db_name = cfg.read_param("database_name",
                                     "Please provide the database to create",
                                     DEFAULT_DB_NAME)

        db = osdb(db_url, db_name)

        # check to see if the database has already been created
        if db.exists():
            logger.warn("database '{}' already exists!".format(db_name))
            return -2
        db_schema = db_url.split(":")[0]
        schema_path = self.get_schema_path(db_schema)
        if schema_path is None:
            return -1

        standard_file_path = os.path.join(schema_path, "standard-create.sql")
        if not os.path.isfile(standard_file_path):
            logger.error("cannot find stardard OpenSIPS DB file: '{}'!".format(
                standard_file_path))
            return -1
        tables_files = [standard_file_path]

        # all good now - check to see what tables we shall deploy
        if cfg.exists("database_modules"):
            tables = cfg.get("database_modules").split(" ")
        else:
            tables = STANDARD_DB_MODULES

        logger.debug("deploying tables {}".format(" ".join(tables)))
        for table in tables:
            if table == "standard":
                # already checked for it
                continue
            table_file_path = os.path.join(schema_path,
                                           "{}-create.sql".format(table))
            if not os.path.isfile(table_file_path):
                logger.warn("cannot find file to create {}: {}".format(
                    table, table_file_path))
            else:
                tables_files.append(table_file_path)

        db.create()
        db.use()

        for table_file in tables_files:
            try:
                db.create_module(table_file)
            except osdbError as ex:
                logger.error("cannot import: {}".format(ex))

        db.destroy()
        logger.info("The database has been successfully created.")
        return 0