1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
from __future__ import unicode_literals from fabric.api import *
env.user = 'root'
env.password = 'youpassword'
env.hosts = ['www.example.com']
TAR_FILE_NAME = 'deploy.tar.gz'
def pack(): """ 定义一个pack任务, 打一个tar包 :return: """ tar_files = ['*.py', 'static/*', 'templates/*', 'vue_app/', '*/*.py', 'requirements.txt'] exclude_files = ['fabfile.py', 'deploy/*', '*.tar.gz', '.DS_Store', '*/.DS_Store', '*/.*.py', '__pycache__/*'] exclude_files = ['--exclude=\'%s\'' % t for t in exclude_files] local('rm -f %s' % TAR_FILE_NAME) local('tar -czvf %s %s %s' % (TAR_FILE_NAME, ' '.join(exclude_files), ' '.join(tar_files))) print('在当前目录创建一个打包文件: %s' % TAR_FILE_NAME)
def deploy(): """ 定义一个部署任务 :return: """ pack()
remote_tmp_tar = '/tmp/%s' % TAR_FILE_NAME run('rm -f %s' % remote_tmp_tar) put(TAR_FILE_NAME, remote_tmp_tar) remote_dist_base_dir = '/home/python/django_app' run('mkdir -p %s' % remote_dist_dir)
with cd(remote_dist_dir): print('解压文件到到目录: %s' % remote_dist_dir) run('tar -xzvf %s' % remote_tmp_tar) print('安装 requirements.txt 中的依赖包') run('pip3 install -r requirements.txt') remote_settings_file = '%s/django_app/settings.py' % remote_dist_dir settings_file = 'deploy/settings.py' % name print('上传 settings.py 文件 %s' % settings_file) put(settings_file, remote_settings_file)
nginx_file = 'deploy/django_app.conf' remote_nginx_file = '/etc/nginx/conf.d/django_app.conf' print('上传 nginx 配置文件 %s' % nginx_file) put(nginx_file, remote_nginx_file) supervisor_file = 'deploy/django_app.ini' remote_supervisor_file = '/etc/supervisord.d/django_app.ini' print('上传 supervisor 配置文件 %s' % supervisor_file) put(supervisor_file, remote_supervisor_file) run('nginx -s reload') run('nginx -t') local('rm -f %s' % TAR_FILE_NAME) run('supervisorctl reload')
|