3

失败的代码在基于python:3.6-stretchdebian 的 Docker 容器中运行。它发生在 Django 将文件从一个 Docker 卷移动到另一个卷时。

当我在 MacOS 10 上测试时,它可以正常工作。在这里,Docker 容器使用 docker-compose 启动,并在本地机器上使用常规 Docker 卷。

部署到 Azure(AKS - Azure 上的 Kubernetes),移动文件成功,但复制统计信息失败,并出现以下错误:

  File "/usr/local/lib/python3.6/site-packages/django/core/files/move.py", line 70, in file_move_safe
    copystat(old_file_name, new_file_name)
  File "/usr/local/lib/python3.6/shutil.py", line 225, in copystat
    _copyxattr(src, dst, follow_symlinks=follow)
  File "/usr/local/lib/python3.6/shutil.py", line 157, in _copyxattr
    names = os.listxattr(src, follow_symlinks=follow_symlinks)
OSError: [Errno 38] Function not implemented: '/some/path/file.pdf'

ReadWriteManyAzure 上的卷是具有访问模式的持久卷声明。

现在,copystat记录为:

copystat() 永远不会返回失败。

https://docs.python.org/3/library/shutil.html

我的问题是:

  • 这是一个“错误”,因为文档说它应该“永远不会返回失败”吗?
  • 我可以节省地尝试/排除这个错误,因为有问题的文件被移动了(它只会在稍后失败,同时尝试复制统计信息)
  • 我可以更改解决此问题的 Azure 设置吗?(可能不是)

这里在 Azure 本身的机器上进行了一些小测试:

root:/media/documents# ls -al
insgesamt 267
drwxrwxrwx 2 1000 1000      0 Jul 31 15:29 .
drwxrwxrwx 2 1000 1000      0 Jul 31 15:29 ..
-rwxrwxrwx 1 1000 1000 136479 Jul 31 16:48 orig.pdf
-rwxrwxrwx 1 1000 1000 136479 Jul 31 15:29 testfile
root:/media/documents# lsattr 
--S-----c-jI------- ./orig.pdf
--S-----c-jI------- ./testfile
root:/media/documents# python
Python 3.6.6 (default, Jul 17 2018, 11:12:33) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copystat('orig.pdf', 'testfile')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/shutil.py", line 225, in copystat
    _copyxattr(src, dst, follow_symlinks=follow)
  File "/usr/local/lib/python3.6/shutil.py", line 157, in _copyxattr
    names = os.listxattr(src, follow_symlinks=follow_symlinks)
OSError: [Errno 38] Function not implemented: 'orig.pdf'
>>> shutil.copystat('orig.pdf', 'testfile', follow_symlinks=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/shutil.py", line 225, in copystat
    _copyxattr(src, dst, follow_symlinks=follow)
  File "/usr/local/lib/python3.6/shutil.py", line 157, in _copyxattr
    names = os.listxattr(src, follow_symlinks=follow_symlinks)
OSError: [Errno 38] Function not implemented: 'orig.pdf'
>>> 
4

1 回答 1

2

以下解决方案是一个修补程序。它必须应用于任何直接或间接调用的方法copystat(或任何产生 ignorable 的 shutil 方法errno.ENOSYS)。

if hasattr(os, 'listxattr'):
    LOGGER.warning('patching listxattr to avoid ERROR 38 (errno.ENOSYS)')
    # avoid "ERROR 38 function not implemented on Azure"
    with mock.patch('os.listxattr', return_value=[]):
        file_field.save(name=name, content=GeneratedFile(fresh, content_type=content_type), save=True)
else:
    file_field.save(name=name, content=GeneratedFile(fresh, content_type=content_type), save=True)

file_field.save是调用相关shutil代码的 Django 方法。这是我的代码中出现错误之前的最后一个位置。

于 2018-08-01T14:07:20.640 回答