def create_db(self, db_name, admin_url, db=None): # 1) create an object store database instance # -> use it to create the database itself if not db: db = self.get_db(admin_url, db_name) if not db: return -1 destroy = True else: destroy = False # check to see if the database has already been created if db.exists(db_name): logger.warn("database '%s' already exists!", db_name) return -2 # create the db instance if not db.create(db_name): return -1 if destroy: 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
def _do_create(self, db, db_name=None, do_all_tables=False): if db_name is None: db_name = db.db_name # check to see if the database has already been created if db.exists(db_name): logger.error("database '{}' already exists!".format(db_name)) return -2 db_schema = db.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.read_param(None, "Create [a]ll tables or just the [c]urrently configured ones?", default="a").lower() == "a": print("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: print("Creating the currently configured set of tables ...") 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_name) db.use(db_name) for table_file in tables_files: print("Running {}...".format(os.path.basename(table_file))) try: db.create_module(table_file) except osdbError as ex: logger.error("cannot import: {}".format(ex)) print("The '{}' database has been successfully created!".format(db_name))
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
def create_tables(self, db_name=None, do_all_tables=False, db_url=None): """ create database tables """ if db_url is None: db_url = cfg.read_param( "database_url", "Please provide the URL connecting to the database") if db_url is None: logger.error("no URL specified: aborting!") return -1 # 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 # connect to the database db.connect(db_name) # check to see if the database has already been created #if db.exists(db_name): # logger.error("database '{}' already exists!".format(db_name)) # return -2 db_schema = db.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] # check to see what tables we shall deploy if cfg.read_param( None, "Create [a]ll tables or just the [c]urrently configured ones?", default="a").lower() == "a": print("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: print("Creating the currently configured set of tables ...") if cfg.exists("database_modules"): tables = cfg.get("database_modules").split(" ") else: tables = STANDARD_DB_MODULES # check for corresponding SQL schemas files in system path 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) # create tables from SQL schemas for table_file in tables_files: print("Running {}...".format(os.path.basename(table_file))) try: db.create_module(table_file) except osdbError as ex: logger.error("cannot import: {}".format(ex)) logger.info("database tables have been successfully created.") # terminate active database connection db.destroy() return 0
def _do_create_db(self, params=None, db_url=None): """ create database and roles assignment """ if db_url is None: db_url = cfg.read_param( "template_url", "Please provide the URL to connect to as template") if db_url is None: print() logger.error("no URL specified: aborting!") return -1 if len(params) >= 1: db_name = ''.join(params[0]) else: db_name = cfg.read_param("database_name", "Please provide the database to create", DEFAULT_DB_NAME) logger.debug("db_name: '%s'", db_name) # 1) create an object store database instance # -> use it to create the database itself db = self.get_db(db_url, db_name) if db is None: return -1 # check to see if the database has already been created if db.exists(db_name): logger.warn("database '{}' already exists!".format(db_name)) return -2 # create the db instance if not db.create(db_name): return -1 # create the role and assing correct access rights if db.dialect == "postgres": if len(params) == 2: role_name = ''.join(params[1]) else: role_name = None if role_name is None: role_name = cfg.read_param( "role_name", "Please provide the associated role name to access the database", DEFAULT_ROLE_NAME) logger.debug("role_name: '%s'", role_name) if db.exists_role(role_name) is False: # call function with parameter list params = [role_name] logger.debug("params: '%s'", params) if self.do_create_role(params) is False: return -3 # assign the access rights db.grant_db_options(role_name) return 0