Created
April 25, 2017 15:25
-
-
Save ResolveWang/836117f8103255e4aeeba886ac3e5034 to your computer and use it in GitHub Desktop.
fabfile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# fabfile.py这个名字固定不能改变 | |
from datetime import datetime | |
from fabric.api import env, local, cd, run, put, settings, lcd | |
from fabric.context_managers import prefix | |
def production(): | |
env.hosts = ["production@123.123.123.123:22"] | |
env.key_filename = "/path/to/key_file" | |
# env.password = "123456" # password 和 keyfile 两者只需要一个就可以 | |
def staging(): | |
""" 设置 staging 环境 """ | |
env.hosts = ["staging@111.111.111.111:22"] | |
env.password = "123456" # 如果不写密码,会在 fab 执行时有交互提示输入密码 | |
def prepare(): | |
# local 用于执行本地命令 | |
local("git pull") | |
# 会有交互输入 commit message | |
local("pip freeze > requirements.txt") | |
local("git add -p && git commit") | |
local("git push") | |
def update(): | |
# 远程调用先要准备环境,再进行调用,比如 fab myremote deploy,这里myremote就是环境 | |
""" 本地提交代码,准备部署 """ | |
with cd("/path/to/usercenter"), prefix("workon usercenter"): | |
run("git pull") # run 用于服务器上执行命令 | |
run("pip install -r requirements.txt") | |
run("python manage.py db migrate") | |
run("supervisorctl restart usercenter") | |
def pack(): | |
' 定义一个pack任务 ' | |
# 本地调用直接是 fab pack,也可以把本地调用和远程写到一个函数中一起执行 | |
# 打一个tar包: | |
# tar_files = ['*.py', 'static/*', 'templates/*', 'favicon.ico'] | |
# local('rm -f amei.tar.gz') | |
# 不能指定被打包的具体路径,否则路径就被包含在了包中 | |
with lcd('/Users/resolvewang/Downloads'): | |
local('ls') | |
local('rm -f amei.tar.gz') | |
local('tar -czvf amei.tar.gz amei') | |
def remote_test(): | |
run('ls') | |
run('pwd') | |
remote_dist_dir = '/home/ubuntu/website/src/myblog' | |
with cd(remote_dist_dir): | |
r = run('ls|grep amei') | |
print('=' * 10) | |
print(r) | |
print('*' * 10) | |
def remote_deploy(): | |
' 定义一个部署任务 ' | |
# 远程服务器的临时文件: | |
# remote_tmp_tar = '/tmp/example.tar.gz' | |
# tag = datetime.now().strftime('%y.%m.%d_%H.%M.%S') | |
# remote_dist_dir = '/home/ubuntu/website/src/myblog/amei' | |
remote_dist_dir = '/home/ubuntu/website/src/myblog' | |
# run('rm -f %s' % remote_tmp_tar) | |
# 上传tar文件至远程服务器: | |
# amei.tar.gz默认是当前路径 | |
# 解压: | |
# remote_dist_link = '/srv/www.example.com' | |
# run('rm -rf %s' % remote_dist_dir) | |
# run('mkdir -p %s' % remote_dist_dir) | |
with cd(remote_dist_dir): | |
put('amei.tar.gz', 'a.tar.gz') | |
run('tar -zxvf %s' % 'a.tar.gz') | |
# run('mv %s amei' % 'amei_20170423') | |
run('rm -f %s' % 'a.tar.gz') | |
# 设定新目录的www-data权限: | |
run('chmod -R 755 %s' % remote_dist_dir) | |
# 删除旧的软链接: | |
# run('rm -f %s' % remote_dist_link) | |
# 创建新的软链接指向新部署的目录: | |
# run('ln -s %s %s' % (remote_dist_dir, remote_dist_link)) | |
# run('chown -R www-data:www-data %s' % remote_dist_link) | |
# 重启fastcgi: | |
# fcgi = '/etc/init.d/py-fastcgi' | |
# with settings(warn_only=True): | |
# run('%s stop' % fcgi) | |
# run('%s start' % fcgi) | |
def myremote(): | |
env.hosts = ["ubuntu@123.206.21.165:2048"] | |
# env.hosts = ['wpm@223.129.0.190:22'] | |
env.key_filename = '~/.ssh/id_rsa.pub' | |
# env.password = "123456" # 如果不写密码,会在 fab 执行时有交互提示输入密码 | |
def deploy(): | |
# prepare() | |
# update() | |
pack() | |
remote_deploy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment