fabric deploy "mkdir -p" 出现套接字错误 "no such file or directory",但命令在 ssh 中有效

fabric deploy "mkdir -p" gives socket error "no such file or directory", but command works in ssh

我正在使用 fabric3 将 django 项目部署到 ubuntu (16.04) 服务器。如果需要,该代码包括使用 mkdir -p 创建目录。一段代码:

from fabric.api import env, local, run
(...)
def _create_directory_structure_if_necessary(site_folder):
    for subfolder in ('database', 'static', 'virtualenv', 'source'):
        run(f'mkdir -p {site_folder}/{subfolder}')

当我 运行 fab deploy:host=ubuntu@my.site.com 时,出现以下错误:

[ubuntu@my.site.com] Executing task 'deploy'
[ubuntu@my.site.com] run: mkdir -p /home/ubuntu/sites/my.site.com/database

Fatal error: Low level socket error connecting to host my.site.com on port 22: No such file or directory (tried 1 time)

Underlying exception:
    No such file or directory

Aborting.

生成的目录是正确的,并且服务器上已经有这个文件结构和一个工作网站。如果我通过 ssh 进入服务器,mkdir -p /home/ubuntu/sites/my.site.com/database 可以正常工作。为什么该命令在 ssh window 中有效,但在 fabric 脚本中无效?

详细信息:使用 Amazon Web Services EC2 服务器,默认 ubuntu 用户。应用程序使用 django、nginx、gunicorn、Python3.6。我正在关注 Harry Percival 的书 Test-Driven Development with Python。从 Mac OS X.

部署

问题在于密钥文件的路径在目录名称中包含 space。我使用了引用的路径名来适应这个:

env.key_filename = '~/"Bad Foldername/aws/keyname.pem"'

将密钥移动到没有 space 的路径并删除多余的引号。

我遇到了同样的问题,但这是由于给定路径中不存在密钥文件造成的。

修复方法是在指定路径添加 env.key_filename 文件,它起作用了!