简单的远程部署 fabfile.py
Fabric 是一个 Python(2.5-2.7)库和命令行工具,用于简化 SSH 在应用程序部署或系统管理任务中的使用。它允许你通过命令行执行任意 Python 函数。
通过 pip install fabric
安装结构
在根目录中创建 fabfile.py
:
#myproject/fabfile.py
from fabric.api import *
@task
def dev():
# details of development server
env.user = # your ssh user
env.password = #your ssh password
env.hosts = # your ssh hosts (list instance, with comma-separated hosts)
env.key_filename = # pass to ssh key for github in your local keyfile
@task
def release():
# details of release server
env.user = # your ssh user
env.password = #your ssh password
env.hosts = # your ssh hosts (list instance, with comma-separated hosts)
env.key_filename = # pass to ssh key for github in your local keyfile
@task
def run():
with cd('path/to/your_project/'):
with prefix('source ../env/bin/activate'):
# activate venv, suppose it appear in one level higher
# pass commands one by one
run('git pull')
run('pip install -r requirements.txt')
run('python manage.py migrate --noinput')
run('python manage.py collectstatic --noinput')
run('touch reload.txt')
要执行该文件,只需使用 fab
命令:
$ fab dev run # for release server, `fab release run`
注意:你无法为 github 配置 ssh 密钥,只需手动键入登录名和密码,而 fabfile 运行时,与密钥相同。