def _get_user(self, username, hostname): LOG.debug("Get details of a given database user %s." % username) user = models.MySQLUser() user.name = username databases, marker = self.list_databases() out = None for database in databases: db2_db = models.MySQLDatabase() db2_db.deserialize(database) try: out, err = run_command(system.LIST_DB_USERS % {'dbname': db2_db.name}) except exception.ProcessExecutionError: LOG.debug( "Error while trying to get the users for database: %s." % db2_db.name) continue for item in out.split('\n'): user_access = item.split() if item != "" else None if (user_access is not None and user_access[0].lower() == username.lower() and user_access[1] == 'Y'): user.databases = db2_db.name break return user
def list_dbnames(self): dbNames = [] databases, marker = self.admin.list_databases() for database in databases: mydb = models.MySQLDatabase() mydb.deserialize(database) dbNames.append(mydb.name) return dbNames
def load_with_client(cls, client, limit, marker, include_marker): schemas, next_marker = client.list_databases( limit=limit, marker=marker, include_marker=include_marker) model_schemas = [] for schema in schemas: mysql_schema = guest_models.MySQLDatabase() mysql_schema.deserialize(schema) if mysql_schema.name in cfg.get_ignored_dbs(): continue model_schemas.append( Schema(mysql_schema.name, mysql_schema.collate, mysql_schema.character_set)) return model_schemas, next_marker
def list_databases(self, limit=None, marker=None, include_marker=False): """List databases the user created on this mysql instance.""" LOG.debug("---Listing Databases---") databases = [] with LocalSqlClient(get_engine()) as client: # If you have an external volume mounted at /var/lib/mysql # the lost+found directory will show up in mysql as a database # which will create errors if you try to do any database ops # on it. So we remove it here if it exists. q = sql_query.Query() q.columns = [ 'schema_name as name', 'default_character_set_name as charset', 'default_collation_name as collation', ] q.tables = ['information_schema.schemata'] q.where = [ "schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")" ] q.order = ['schema_name ASC'] if limit: q.limit = limit + 1 if marker: q.where.append( "schema_name %s '%s'" % (INCLUDE_MARKER_OPERATORS[include_marker], marker)) t = text(str(q)) database_names = client.execute(t) next_marker = None LOG.debug("database_names = %r." % database_names) for count, database in enumerate(database_names): if count >= limit: break LOG.debug("database = %s." % str(database)) mysql_db = models.MySQLDatabase() mysql_db.name = database[0] next_marker = mysql_db.name mysql_db.character_set = database[1] mysql_db.collate = database[2] databases.append(mysql_db.serialize()) LOG.debug("databases = " + str(databases)) if database_names.rowcount <= limit: next_marker = None return databases, next_marker
def list_databases(self, limit=None, marker=None, include_marker=False): LOG.debug("Listing all the DB2 databases.") databases = [] next_marker = None try: out, err = run_command(system.LIST_DB_COMMAND) dblist = out.split() result = iter(dblist) count = 0 if marker is not None: try: item = next(result) while item != marker: item = next(result) if item == marker: marker = None except StopIteration: pass try: item = next(result) while item: count = count + 1 if (limit and count <= limit) or limit is None: db2_db = models.MySQLDatabase() db2_db.name = item LOG.debug("database = %s ." % item) db2_db.character_set = None db2_db.collate = None next_marker = db2_db.name databases.append(db2_db.serialize()) item = next(result) else: next_marker = None break except StopIteration: next_marker = None LOG.debug("databases = %s." % str(databases)) except exception.ProcessExecutionError as pe: err_msg = encodeutils.exception_to_unicode(pe) LOG.exception( _("An error occurred listing databases: %s.") % err_msg) pass return databases, next_marker
def _associate_dbs(self, user): """Internal. Given a MySQLUser, populate its databases attribute.""" LOG.debug("Associating dbs to user %s at %s." % (user.name, user.host)) with self.local_sql_client(self.mysql_app.get_engine()) as client: q = sql_query.Query() q.columns = ["grantee", "table_schema"] q.tables = ["information_schema.SCHEMA_PRIVILEGES"] q.group = ["grantee", "table_schema"] q.where = ["privilege_type != 'USAGE'"] t = text(str(q)) db_result = client.execute(t) for db in db_result: LOG.debug("\t db: %s." % db) if db['grantee'] == "'%s'@'%s'" % (user.name, user.host): mysql_db = models.MySQLDatabase() mysql_db.name = db['table_schema'] user.databases.append(mysql_db.serialize())
def list_databases(self, limit=None, marker=None, include_marker=False): """List databases the user created on this mysql instance.""" LOG.debug("---Listing Databases---") ignored_database_names = "'%s'" % "', '".join(CONF.ignore_dbs) LOG.debug("The following database names are on ignore list and will " "be omitted from the listing: %s" % ignored_database_names) databases = [] with LocalSqlClient(get_engine()) as client: q = sql_query.Query() q.columns = [ 'schema_name as name', 'default_character_set_name as charset', 'default_collation_name as collation', ] q.tables = ['information_schema.schemata'] q.where = ["schema_name NOT IN (" + ignored_database_names + ")"] q.order = ['schema_name ASC'] if limit: q.limit = limit + 1 if marker: q.where.append( "schema_name %s '%s'" % (INCLUDE_MARKER_OPERATORS[include_marker], marker)) t = text(str(q)) database_names = client.execute(t) next_marker = None LOG.debug("database_names = %r." % database_names) for count, database in enumerate(database_names): if count >= limit: break LOG.debug("database = %s." % str(database)) mysql_db = models.MySQLDatabase() mysql_db.name = database[0] next_marker = mysql_db.name mysql_db.character_set = database[1] mysql_db.collate = database[2] databases.append(mysql_db.serialize()) LOG.debug("databases = " + str(databases)) if database_names.rowcount <= limit: next_marker = None return databases, next_marker
def list_users(self, limit=None, marker=None, include_marker=False): LOG.debug( "List all users for all the databases in a DB2 server instance.") users = [] user_map = {} next_marker = None count = 0 databases, marker = self.list_databases() for database in databases: db2_db = models.MySQLDatabase() db2_db.deserialize(database) out = None try: out, err = run_command(system.LIST_DB_USERS % {'dbname': db2_db.name}) except exception.ProcessExecutionError: LOG.debug( "There was an error while listing users for database: %s." % db2_db.name) continue userlist = [] for item in out.split('\n'): LOG.debug("item = %r" % item) user = item.split() if item != "" else None LOG.debug("user = %r" % (user)) if user is not None and user[0] not in IGNORE_USERS_LIST \ and user[1] == 'Y': userlist.append(user[0]) result = iter(userlist) if marker is not None: try: item = result.next() while item != marker: item = result.next() if item == marker: marker = None except StopIteration: pass try: item = result.next() db2db = models.MySQLDatabase() db2db.name = db2_db.name while item: ''' Check if the user has already been discovered. If so, add this database to the database list for this user. ''' if item in user_map: db2user = user_map.get(item) db2user.databases.append(db2db.serialize()) item = result.next() continue ''' If this user was not previously discovered, then add this to the user's list. ''' count = count + 1 if (limit and count <= limit) or limit is None: db2_user = models.MySQLUser() db2_user.name = item db2_user.databases.append(db2db.serialize()) users.append(db2_user.serialize()) user_map.update({item: db2_user}) item = result.next() else: next_marker = None break except StopIteration: next_marker = None if count == limit: break return users, next_marker