Пример #1
0
def get_change_master_command(source, options):
    """Get the CHANGE MASTER command for export or copy of databases

    This method creates the replication commands based on the options chosen.
    This includes the stop and start slave commands as well as the change
    master command as follows.

    To create the CHANGE MASTER command for connecting to the existing server
    as the master, set rpl_mode = 'master'.

    To create the CHANGE MASTER command for using the existing server as the
    master, set rpl_mode = 'master'.

    You can also get both CHANGE MASTER commands by setting rpl_mode = 'both'.
    In this case, the second change master command (rpl_mode = 'slave') will
    be commented out.

    The method also checks the rpl_file option. If a file name is provided, it
    is checked to see if file exists or the user does not have access, an error
    is thrown. If no file is provided, the method writes the commands to
    stdout.

    The user may also comment the replication commands by specifying the
    comment_rpl option (True = comment).

    The method calls the negotiate_rpl_connection method of the replication
    module to create the CHANGE MASTER command. Additional error checking is
    performed in that method as follows. See the negotiate_rpl_connection
    method documentation for complete specifics.

      - binary log must be ON for a master
      - the rpl_user must exist

    source[in]         Server instance
    options[in]        option dictionary

    Returns tuple - CHANGE MASTER command[s], output file for writing commands.
    """
    if options is None:
        options = {}
    rpl_file = None
    rpl_cmds = []

    rpl_filename = options.get("rpl_file", "")
    rpl_mode = options.get("rpl_mode", "master")
    quiet = options.get("quiet", False)

    # Check for rpl_filename and create file.
    if rpl_filename:
        rpl_file = rpl_filename
        try:
            rf = open(rpl_filename, "w")
        except:
            raise UtilError("File inaccessible or bad path: "
                            "{0}".format(rpl_filename))
        rf.write("# Replication Commands:\n")
        rf.close()

    strict = rpl_mode == 'both' or options.get("strict", False)
    # Get change master as if this server was a master
    if rpl_mode in ["master", "both"]:

        if not quiet:
            rpl_cmds.append("# Connecting to the current server as master")

        change_master = negotiate_rpl_connection(source, True, strict, options)

        rpl_cmds.extend(change_master)

    # Get change master using this slave's master information
    if rpl_mode in ["slave", "both"]:

        if not quiet:
            rpl_cmds.append("# Connecting to the current server's master")

        change_master = negotiate_rpl_connection(source, False, strict,
                                                 options)

        rpl_cmds.extend(change_master)

    return rpl_cmds, rpl_file
Пример #2
0
def get_change_master_command(source, options):
    """Get the CHANGE MASTER command for export or copy of databases

    This method creates the replication commands based on the options chosen.
    This includes the stop and start slave commands as well as the change
    master command as follows.

    To create the CHANGE MASTER command for connecting to the existing server
    as the master, set rpl_mode = 'master'.

    To create the CHANGE MASTER command for using the existing server as the
    master, set rpl_mode = 'master'.

    You can also get both CHANGE MASTER commands by setting rpl_mode = 'both'.
    In this case, the second change master command (rpl_mode = 'slave') will
    be commented out.

    The method also checks the rpl_file option. If a file name is provided, it
    is checked to see if file exists or the user does not have access, an error
    is thrown. If no file is provided, the method writes the commands to
    stdout.

    The user may also comment the replication commands by specifying the
    comment_rpl option (True = comment).

    The method calls the negotiate_rpl_connection method of the replication
    module to create the CHANGE MASTER command. Additional error checking is
    performed in that method as follows. See the negotiate_rpl_connection
    method documentation for complete specifics.

      - binary log must be ON for a master
      - the rpl_user must exist

    source[in]         Server instance
    options[in]        option dictionary

    Returns tuple - CHANGE MASTER command[s], output file for writing commands.
    """
    if options is None:
        options = {}
    rpl_file = None
    rpl_cmds = []

    rpl_filename = options.get("rpl_file", "")
    rpl_mode = options.get("rpl_mode", "master")
    quiet = options.get("quiet", False)

    # Check for rpl_filename and create file.
    if rpl_filename:
        rpl_file = rpl_filename
        try:
            rf = open(rpl_filename, "w")
        except:
            raise UtilError("File inaccessible or bad path: "
                            "{0}".format(rpl_filename))
        rf.write("# Replication Commands:\n")
        rf.close()

    strict = rpl_mode == 'both' or options.get("strict", False)
    # Get change master as if this server was a master
    if rpl_mode in ["master", "both"]:

        if not quiet:
            rpl_cmds.append("# Connecting to the current server as master")

        change_master = negotiate_rpl_connection(source, True, strict, options)

        rpl_cmds.extend(change_master)

    # Get change master using this slave's master information
    if rpl_mode in ["slave", "both"]:

        if not quiet:
            rpl_cmds.append("# Connecting to the current server's master")

        change_master = negotiate_rpl_connection(source, False, strict,
                                                 options)

        rpl_cmds.extend(change_master)

    return rpl_cmds, rpl_file