Fabric 就是一个自动化部署工具。需要在客户端安装(需要安装python2.7版本,因为Fabric使用此版本开发的。)
文档:http://docs.fabfile.org/en/1.13/tutorial.html#hello-fab
pip install fabric
一种工具,它允许您通过命令行执行任意Python函数。
一个子程序库,通过SSH轻松执行shell命令和Pythonic。
在 fabfile.py 中写一个函数:
def hello():
print("Hello world!")
然后再控制台运行:
$ fab hello
Hello world!
Done.
瞬间觉得吊炸天啊。
如果你想带参数:
def hello(name):
print('fab hello! %s' % name)
输出:
$ fab hello:kd
fab hello! kd
Done.
Fabric通过操作调用的程序的返回值来进行检查,如果它们没有完全退出,则中止执行。
from __future__ import with_statement
from fabric.api import local, settings, abort
from fabric.contrib.console import confirm
def test():
# warn_only=True将abort转换为警告
with settings(warn_only=True):
result = local('./manage.py test my_app', capture=True)
# 如果执行失败并且confirm时选择的是n
if result.failed and not confirm("Tests failed. Continue anyway?"):
# abort()用于手动中止执行
abort("Aborting at user request.")
from __future__ import with_statement
from fabric.api import local, settings, abort, run, cd
from fabric.contrib.console import confirm
def deploy():
code_dir = '/srv/django/myproject'
with cd(code_dir):
run("git pull")
run("touch app.wsgi")
# cd() 是切换远程服务器的目录,lcd() 是切换本地目录(Fabric所在的主机)
# run() 是运行远程指令,local() 是在本地运行命令行(Fabric所在的主机)