如何从我的Python脚本执行fabfile?

0 投票
1 回答
1183 浏览
提问于 2025-04-18 08:05

我有一个fabfile,里面的内容是:

[root@ip-10-10-20-82 bakery]# cat fabfile.py
from fabric.api import run
def deploy():
        run('wget -P /tmp https://s3.amazonaws.com/LinuxBakery/httpd-2.2.26-1.1.amzn1.x86_64.rpm')
        run('sudo yum localinstall /tmp/httpd-2.2.26-1.1.amzn1.x86_64.rpm')

当我从命令行运行这个fabfile时,可以成功执行,命令是:

fab -f fabfile.py -u ec2-user -i id_rsa -H 10.10.15.242 deploy

但当我在我的Python脚本中运行它时,就不行了。以下是我在脚本中运行的方式:

import subprocess
subprocess.call(['fab', '-f', '/home/myhome/scripts/bakery/fabfile.py', '-u ec2-user', '-i', 'id_rsa', '-H', bakery_internalip, 'deploy'])

我遇到的错误是:

Fatal error: Fabfile didn't contain any commands!
Aborting.

我不明白为什么会出现这个错误。

1 个回答

0

修改一下fabfile.py文件,添加以下几个更改。

@task
def deploy(): 
    ... rest of the code

if __name__ == "__main__":
    local('fab -f /path/fabfile.py deploy')

然后在Python中运行它。

撰写回答