Fabric自动部署太方便了

时间:2023-03-09 08:27:33
Fabric自动部署太方便了

之前不知道有Fabric工具,每次发布程序到服务器上的时候,基本流程:本地打包程序 -> Ftp上传 -> 停服务器Apache -> 覆盖文件 -> 启动Apache, 非常繁琐。

前几天无意发现了Fabric工具,研究了一下,现在我通过该工具发布程序只要一条指令即可:

fab pack deploy

具体如何实现的,请看实例,你只需要在你的程序跟目录下添加fabfile.py文件,内容参考如下:

#!/usr/bin/env python

from fabric.api import *

# Configure user, private key, etc. for SFTP deployment
env.user = 'root'
env.hosts = ['SERVER IP'] def pack():
local('npm run build', capture=False)
local('python setup.py sdist --formats=gztar', capture=False) def deploy():
dist = local('python setup.py --fullname', capture=True).strip() # clean up
sudo('rm -rf /tmp/towerres /tmp/towerres.tar.gz') put('dist/%s.tar.gz' % dist, '/tmp/towerres.tar.gz') # stop apache
run('apachectl stop') # remove priouse folder
run('rm /home/maplye/httpd/towerresource/towerres -rf')
run('rm /home/maplye/httpd/towerresource/migrations -rf') # copy
run('mkdir /tmp/towerres')
with cd('/tmp/towerres'):
run('tar zxf /tmp/towerres.tar.gz') run('cp -rf /tmp/towerres/%s/towerres /home/maplye/httpd/towerresource/' % dist)
run('cp -rf /tmp/towerres/%s/migrations /home/maplye/httpd/towerresource/' % dist) # run
with cd('/home/maplye/httpd/towerresource'):
run('chown -R apache:apache towerres')
run('python manage.py db upgrade') # start apache
run('apachectl start') # clean up
sudo('rm -rf /tmp/towerres /tmp/towerres.tar.gz')

  具体参照官网: http://www.fabfile.org/

  中文文档: http://fabric-docs-cn.readthedocs.org/zh_CN/latest/