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)))
def get_admin_db_url(self, db_name): engine = osdb.get_db_engine() if not engine: return None if cfg.exists('database_admin_url'): admin_url = cfg.get("database_admin_url") if engine == "postgres": admin_url = osdb.set_url_db(admin_url, 'postgres') else: if engine == 'postgres': if getuser() != "postgres": logger.error("Command must be run as 'postgres' user: "******"sudo -u postgres opensips-cli ...") return None """ For PG, do the initial setup using 'postgres' as role + DB """ admin_url = "postgres://postgres@localhost/postgres" else: admin_url = "{}://root@localhost".format(engine) if osdb.get_url_pswd(admin_url) is None: pswd = getpass("Password for admin {} user ({}): ".format( osdb.get_url_driver(admin_url, capitalize=True), osdb.get_url_user(admin_url))) logger.debug("read password: '******'", pswd) admin_url = osdb.set_url_password(admin_url, pswd) logger.debug("admin DB URL: '{}'".format(admin_url)) return admin_url
def ensure_user(self, db_url, db_name, admin_db): """ Ensures that the user/password in @db_url can connect to @db_name. It assumes @db_name has been created beforehand. If the user doesn't exist or has insufficient permissions, this will be fixed using the @admin_db connection. """ db_url = osdb.set_url_db(db_url, db_name) try: db = self.get_db(db_url, db_name, check_access=True) logger.info("connected to DB, '%s' user is already created", osdb.get_url_user(db_url)) except osdbAccessDeniedError: logger.info("creating access user for {} ...".format(db_name)) if not admin_db.ensure_user(db_url): logger.error("failed to create user on {} DB".format(db_name)) return -1 db = self.get_db(db_url, db_name, cfg_url_param='database_url') if db is None: return -1 db.destroy() return 0
def create_tables(self, db_name, db_url, admin_db, tables=[], create_std=True): """ create database tables """ db_url = osdb.set_url_db(db_url, db_name) # 2) prepare new object store database instance # use it to connect to the created database db = self.get_db(db_url, db_name) if db is None: return -1 if not db.exists(): logger.warning("database '{}' does not exist!".format(db_name)) return -1 schema_path = self.get_schema_path(db.dialect) if schema_path is None: return -1 if create_std: 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 table_files = {'standard': standard_file_path} else: table_files = {} # check to see what tables we shall deploy if tables: pass elif cfg.exists("database_modules"): # we know exactly what modules we want to instsall tables_line = cfg.get("database_modules").strip().lower() if tables_line == "all": logger.debug("Creating all tables") tables = [ f.replace('-create.sql', '') \ for f in os.listdir(schema_path) \ if os.path.isfile(os.path.join(schema_path, f)) and \ f.endswith('-create.sql') ] else: logger.debug("Creating custom tables") tables = tables_line.split(" ") else: logger.debug("Creating standard tables") tables = STANDARD_DB_MODULES # check for corresponding SQL schemas files in system path logger.debug("checking 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 SQL file for module {}: {}".format( table, table_file_path)) else: table_files[table] = table_file_path username = osdb.get_url_user(db_url) admin_db.connect(db_name) # create tables from SQL schemas for module, table_file in table_files.items(): logger.info("Running {}...".format(os.path.basename(table_file))) try: db.create_module(table_file) if db.dialect == "postgres": self.pg_grant_table_access(table_file, username, admin_db) except osdbModuleAlreadyExistsError: logger.error("{} table(s) are already created!".format(module)) except osdbError as ex: logger.error("cannot import: {}".format(ex)) # terminate active database connection db.destroy() return 0