Exemplo n.º 1
0
def shutdown_mysql(instance):
    """ Send a mysqladmin shutdown to an instance

    Args:
    instance - a hostaddr object
    """
    username, password = get_mysql_user_for_role('admin')
    cmd = ''.join((MYSQLADMIN, ' -u ', username, ' -p', password, ' -h ',
                   instance.hostname, ' -P ', str(instance.port), ' shutdown'))
    log.info(cmd)
    host_utils.shell_exec(cmd)
Exemplo n.º 2
0
def shutdown_mysql(instance):
    """ Send a mysqladmin shutdown to an instance

    Args:
    instance - a hostaddr object
    """
    username, password = get_mysql_user_for_role('admin')
    cmd = ''.join((MYSQLADMIN,
                   ' -u ', username,
                   ' -p', password,
                   ' -h ', instance.hostname,
                   ' -P ', str(instance.port),
                   ' shutdown'))
    log.info(cmd)
    host_utils.shell_exec(cmd)
Exemplo n.º 3
0
def get_remote_backup(hostaddr, date=None):
    """ Find the most recent xbstream file on the desired instance

    Args:
    hostaddr - A hostaddr object for the desired instance
    date - desire date of restore file

    Returns:
    filename - The path to the most recent xbstream file
    size - The size of the backup file
    """
    path = os.path.join(TARGET_DIR, str(hostaddr.port))
    if date:
        date_param = '{date}*'.format(date=date)
    else:
        date_param = ''
    cmd = ("ssh {ops} {auth}@{host} 'ls -tl {path}/*{date_param}.xbstream |"
           " head -n1'").format(ops=SSH_OPTIONS,
                                auth=SSH_AUTH,
                                host=hostaddr.hostname,
                                path=path,
                                date_param=date_param)
    log.info(cmd)

    # We really only care here if there is output. Basically we will
    # ignore return code and stderr if we get stdout
    (out, err, _) = host_utils.shell_exec(cmd)

    if not out:
        msg = 'No backup found for {host} in {path}.\nError: {err}'
        raise Exception(msg.format(host=hostaddr.hostname,
                                   path=path,
                                   err=err))

    entries = out.split()
    size = entries[4]
    filename = entries[8]

    # Probably unlikely that we'd have a remote-server backup that's too small,
    # but we check anyway.
    if size < MINIMUM_VALID_BACKUP_SIZE_BYTES:
        msg = ('Found backup {backup_file} for host {host} '
               'but it is too small... '
               '({size} bytes < {min_size} bytes) '
               'Ignoring it.').format(backup_file=filename,
                                      host=hostaddr.hostname,
                                      size=size,
                                      min_size=MINIMUM_VALID_BACKUP_SIZE_BYTES)
        raise Exception(msg)

    msg = ('Found a backup {filename} with a '
           'size of {size}').format(size=size,
                                    filename=filename)
    log.debug(msg)
    return filename, size
Exemplo n.º 4
0
def get_installed_mysqld_version():
    """ Get the version of mysqld installed on localhost

    Returns the numeric MySQL version

    Example: 5.6.22-72.0
    """
    (std_out, std_err, return_code) = host_utils.shell_exec(MYSQL_VERSION_COMMAND)
    if return_code or not std_out:
        raise Exception("Could not determine installed mysql version: " "{std_err}")
    return re.search(".+Ver ([0-9.-]+)", std_out).groups()[0]
Exemplo n.º 5
0
def get_installed_mysqld_version():
    """ Get the version of mysqld installed on localhost

    Returns the numeric MySQL version

    Example: 5.6.22-72.0
    """
    (std_out, std_err, return_code) = host_utils.shell_exec(MYSQL_VERSION_COMMAND)
    if return_code or not std_out:
        raise Exception('Could not determine installed mysql version: '
                        '{std_err}')
    return re.search('.+Ver ([0-9.-]+)', std_out).groups()[0]
Exemplo n.º 6
0
def get_remote_backup(hostaddr, date=None):
    """ Find the most recent xbstream file on the desired instance

    Args:
    hostaddr - A hostaddr object for the desired instance
    date - desire date of restore file

    Returns:
    filename - The path to the most recent xbstream file
    size - The size of the backup file
    """
    path = os.path.join(TARGET_DIR, str(hostaddr.port))
    if date:
        date_param = '{date}*'.format(date=date)
    else:
        date_param = ''
    cmd = ("ssh {ops} {auth}@{host} 'ls -tl {path}/*{date_param}.xbstream |"
           " head -n1'").format(ops=SSH_OPTIONS,
                                auth=SSH_AUTH,
                                host=hostaddr.hostname,
                                path=path,
                                date_param=date_param)
    log.info(cmd)

    # We really only care here if there is output. Basically we will
    # ignore return code and stderr if we get stdout
    (out, err, _) = host_utils.shell_exec(cmd)

    if not out:
        msg = 'No backup found for {host} in {path}.\nError: {err}'
        raise Exception(msg.format(host=hostaddr.hostname, path=path, err=err))

    entries = out.split()
    size = entries[4]
    filename = entries[8]

    # Probably unlikely that we'd have a remote-server backup that's too small,
    # but we check anyway.
    if size < MINIMUM_VALID_BACKUP_SIZE_BYTES:
        msg = ('Found backup {backup_file} for host {host} '
               'but it is too small... '
               '({size} bytes < {min_size} bytes) '
               'Ignoring it.').format(backup_file=filename,
                                      host=hostaddr.hostname,
                                      size=size,
                                      min_size=MINIMUM_VALID_BACKUP_SIZE_BYTES)
        raise Exception(msg)

    msg = ('Found a backup {filename} with a '
           'size of {size}').format(size=size, filename=filename)
    log.debug(msg)
    return filename, size
Exemplo n.º 7
0
def shutdown_mysql(instance):
    """ Send a mysqladmin shutdown to an instance

    Args:
    instance - a hostaddr object
    """
    username, password = get_mysql_user_for_role("admin")
    cmd = "".join(
        (
            MYSQLADMIN,
            " -u ",
            username,
            " -p",
            password,
            " -h ",
            instance.hostname,
            " -P ",
            str(instance.port),
            " shutdown",
        )
    )
    log.info(cmd)
    host_utils.shell_exec(cmd)