test_direct_access.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:kuberdock-platform 作者: cloudlinux 项目源码 文件源码
def _compare_files(local_path, remote_path, ssh):
    """
    Compare hashes of two files, one on local and another one on remote server
    :param local_path: path to file on the local server
    :param remote_path: path to file on the remote server
    :param ssh: SSHClient instance to communicate with remote server
    :returns: True/False on success/fail
    :rtype: bool
    """
    logger.debug(u'{}Comparing files. host: {} and container: {}{}'.format(
        Style.DIM, local_path, remote_path, Style.RESET_ALL))

    # Sometimes ssh_exec exits with a 0 code, but no stdout can be read,
    # so we moved the file hash call inside a function.
    def _remote_md5sum(ssh, path):
        remote_cmd = 'md5sum {}'.format(path)
        ret_code, out, err = ssh_exec(ssh=ssh, cmd=remote_cmd, get_pty=True)
        _remote_file_hash = out.strip().split()[0].strip()
        return _remote_file_hash

    # Get hash of the remote file.
    remote_file_hash = retry(_remote_md5sum, ssh=ssh, path=remote_path,
                             tries=3, interval=1)
    # Get hash of the local file
    try:
        with open(local_path) as f:
            local_file_hash = hashlib.md5(f.read()).hexdigest()
    except (OSError, IOError) as e:
        raise FileTransferValidationFailed(str(e))

    # Compare hashes
    if local_file_hash != remote_file_hash:
        message = 'Hashes not equal. Host: {} != container: {}'.format(
            local_file_hash, remote_file_hash)
        logger.debug(u'{}host: {} container: {}{}'.format(
            Fore.RED, local_path, remote_path, Style.RESET_ALL))
        raise FileTransferValidationFailed(message)
    logger.debug(u'{}Validation: OK{}'.format(Fore.GREEN, Style.RESET_ALL))
    return True
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号