Created
March 18, 2014 12:41
-
-
Save zhwei/9619285 to your computer and use it in GitHub Desktop.
Django项目中使用的fabfile, 其实也是通用的
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from fabric.colors import green, red | |
from fabric.contrib.console import confirm | |
from fabric.api import run, env, cd, put, sudo, abort | |
# configs | |
env.user = 'username' | |
env.hosts = ['IP:PORT',] | |
## project home path | |
PROJECT_HOME = '/path/to/project' | |
## project name in supervisor | |
PROJECT_NAME_IN_SUPERVISOR = 'project_name_in_supervisor' | |
## default deploy branch | |
DEPLOY_BARNCH = 'deploy_branch' | |
def put_sshkey(): | |
"""push ssh key to server | |
""" | |
with cd('/tmp'): | |
put('id_rsa.pub.master', 'id_rsa.pub.master') | |
run('cat id_rsa.pub.master >> ~/.ssh/authorized_keys') | |
# git | |
def git_pull(branch): | |
ret = run('git pull origin %s' % branch) | |
if ret.failed and not confirm('Pull from origin %s Failed, Continue anyway ?') % branch: | |
run('git status') | |
abort(red('Aborting at pull from origin')) | |
print(green('Pull from branch %s successfully') % branch) | |
# service control | |
def restart_project(project_name): | |
#if not confirm(green('Do you want to restart project ?')): | |
sudo('supervisorctl restart %s' % project_name) | |
print(green('Restart Supervisor project [%s] Successfully !!') % project_name) | |
def restart_nginx(action="reload"): | |
sudo('nginx -s %s' % action) | |
print(green('[%s] Nginx Successfully !!') % action) | |
# python | |
def install_require(package=None): | |
if package: | |
sudo('pip install %s' % package) | |
else: | |
sudo('pip install -r requirements.txt') | |
print(green('Install Complete !')) | |
# project custom | |
def deploy(do='app'): | |
with cd(PROJECT_HOME): | |
if 'app' == do: | |
git_pull(DEPLOY_BARNCH) | |
install_require() | |
elif 'db' == do: | |
if not confirm(red("Are you sure to recreate your database ? this can not be undo !!")): | |
abort('Do Nothing ... ') | |
run('rm db/mysite.db') | |
run('python manage.py syncdb') | |
restart_project(PROJECT_NAME_IN_SUPERVISOR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment