我看到大多数Fabric API都与功能一起使用.
文件示例(sample.py):
from fabric.api import * print "Hello" def deploy(): with settings(hosts_string="Remote", user = "ubuntu", key_filename="/home/ubuntu/key.pem"): put('/home/localuser/sample.sh', '/home/ubuntu/') run('bash /home/ubuntu/sample.sh')
我运行命令来执行
fab deploy
是否可以在Main方法中运行Fabric.因此,当我将其作为python脚本运行时,将执行结构.
python ./sample.py
谢谢!
要直接回答您的问题,您可以将此代码段添加到您的文件中:
from fabric.api import * print "Hello" def deploy(): with settings(host_string="Remote", user = "ubuntu", key_filename="/home/ubuntu/key.pem"): put('/home/localuser/sample.sh', '/home/ubuntu/') run('bash /home/ubuntu/sample.sh') if __name__ == '__main__': deploy()
现在,当你跑步时python ./sample.py
,它会做同样的事情fab deploy
但是,fab
除了您的简单示例之外,您还可以执行更多操作.有关命令灵活性的更多信息,请参阅fab文档fab
.