Fabric文档:http://docs.fabfile.org/en/1.3.4/index.html
Fabric是一个Python Library,可以把大量频繁使用的ssh操作写到一个脚本中,通常用于部署或者系统维护等任务,我现在部署Django应用时都会用它,节省了大量时间。
首先当然要安装Fabric,通过pip安装最方便:
pip install fabric
之后编写Fabric脚本fabfile.py
,根据自己的部署流程需要来写即可:
from fabric.api import *
env.hosts = ['123.456.78.90:1234']
env.user = "root"
def update_django_project():
""" Updates the remote django project. """
with cd('/home/****/www/****/******'):
run('git pull origin master')
with prefix('source ../bin/activate'):
run('pip install -r requirements.txt')
run('python manage.py syncdb') # run('python manage.py schemamigration **** --auto')
run('python manage.py migrate ****')
run('python manage.py collectstatic --noinput')
def restart_webserver():
""" Restarts remote nginx and uwsgi """
sudo("killall -s HUP /home/****/www/****/bin/uwsgi")
sudo("nginx -s stop") sudo("/etc/rc.d/nginx start ")
def deploy(): """ Deploy Django Project. """
update_django_project()
restart_webserver()
先把最新代码push到master:
git push origin master
再执行Fabric脚本部署:
fab -f fabfile.py deploy