Fabric是一个Python(2.5-2.7)库和命令行工具,用于简化SSH在应用程序部署或系统管理任务中的使用。它使您可以通过命令行执行任意Python函数。
通过pip install fabric
创建fabfile.py在根目录中安装结构:
#myproject/fabfile.py fromfabric.apiimport * @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('pythonmanage.pymigrate --noinput') run('pythonmanage.pycollectstatic --noinput') run('touch reload.txt')
要执行文件,只需使用以下fab命令:
$ fab dev run # for release server, `fab release run`
注意:您无法为github配置ssh密钥,而只能在fabfile运行时手动键入login和password,与密钥相同。