예제 #1
0
def _show_user_grants(source, user_source, base_user, verbosity):
    """Show grants for a specific user.
    """
    try:
        if not user_source:
            user_source = User(source, base_user, verbosity)
        print "# Dumping grants for user " + base_user
        user_source.print_grants()
    except UtilError:
        print "# Cannot show grants for user %s." % base_user + "Please check user and host for valid names."
예제 #2
0
def _show_user_grants(source, user_source, base_user, verbosity):
    """Show grants for a specific user.
    """
    try:
        if not user_source:
            user_source = User(source, base_user, verbosity)
        print("# Dumping grants for user " + base_user)
        user_source.print_grants()
    except UtilError:
        print("# Cannot show grants for user %s." % base_user + \
              "Please check user and host for valid names.")
예제 #3
0
def _show_user_grants(source, user_source, base_user, verbosity):
    """Show grants for a specific user.
    """
    from mysql.utilities.common.user import User

    try:
        if not user_source:
            user_source = User(source, base_user, verbosity)
        print "# Dumping grants for user " + base_user
        user_source.print_grants()
    except UtilError, e:
        print "# Cannot show grants for user %s." % base_user + \
              "Please check user and host for valid names."
예제 #4
0
def clone_user(src_val, dest_val, base_user, new_user_list, options):
    """Clone a user to one or more new user accounts

    This method will create one or more new user accounts copying the
    grant statements from a given user. If source and destination are the
    same, the copy will occur on a single server otherwise, the caller may
    specify a destination server to where the user accounts will be copied.

    NOTES:
    The user is responsible for making sure the databases and objects
    referenced in the cloned GRANT statements exist prior to running this
    utility.

    src_val[in]        a dictionary containing connection information for the
                       source including:
                       (user, password, host, port, socket)
    dest_val[in]       a dictionary containing connection information for the
                       destination including:
                       (user, password, host, port, socket)
    base_user[in]      the user account on the source machine to be used as
                       the template for the new users
    user_list[in]      a list of new user accounts in the form:
                       (username:password@host)
    options[in]        optional parameters dictionary including:
                         dump_sql - if True, print grants for base user
                                    (no new users are created)
                         force    - drop new users if they exist
                         verbosity - print add'l information during operation
                         quiet   - do not print information during operation
                                   Note: Error messages are printed regardless
                         global_privs - include global privileges (i.e. user@%)

    Returns bool True = success, raises UtilError if error
    """

    from mysql.utilities.common.server import connect_servers
    from mysql.utilities.common.user import User

    dump_sql = options.get("dump", False)
    overwrite = options.get("overwrite", False)
    verbosity = options.get("verbosity", False)
    quiet = options.get("quiet", False)
    global_privs = options.get("global_privs", False)

    # Don't require destination for dumping base user grants
    conn_options = {
        'quiet'     : quiet,
        'version'   : "5.1.0",
    }
    if dump_sql:
        servers = connect_servers(src_val, None, conn_options)
    else:
        servers = connect_servers(src_val, dest_val, conn_options)

    source = servers[0]
    destination = servers[1]
    if destination is None:
        destination = servers[0]

    # Create an instance of the user class for source.
    user_source = User(source, base_user, verbosity)

    # Create an instance of the user class for destination.
    user_dest = User(destination, base_user, verbosity)

    # Check to ensure base user exists.
    if not user_source.exists(base_user):
        raise UtilError("Base user does not exist!")

    # Process dump operation
    if dump_sql and not quiet:
        print "Dumping grants for user " + base_user
        user_source.print_grants()
        return True

    # Check to ensure new users don't exist.
    if overwrite is None:
        for new_user in new_user_list:
            if user_dest.exists(new_user):
                raise UtilError("User %s already exists. Use --force "
                      "to drop and recreate user." % new_user)

    if not quiet:
        print "# Cloning %d users..." % (len(new_user_list))

    # Perform the clone here. Loop through new users and clone.
    for new_user in new_user_list:
        if not quiet:
            print "# Cloning %s to user %s " % (base_user, new_user)
        # Check to see if user exists.
        if user_dest.exists(new_user):
            user_dest.drop(new_user)
        # Clone user.
        try:
            user_source.clone(new_user, destination, global_privs)
        except UtilError, e:
            raise