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():
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():
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'
)