Created
November 24, 2015 10:11
-
-
Save nik-hil/d395e3d06e20b6d31084 to your computer and use it in GitHub Desktop.
Fabric Script to deploy code on django+uwsgi+nginx on aws server
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
from __future__ import with_statement | |
from fabvenv import virtualenv | |
from fabric.api import * | |
from fabric.contrib.console import confirm | |
# http://stackoverflow.com/questions/1180411/activate-a-virtualenv-via-fabric-as-deploy-user | |
# http://stackoverflow.com/questions/30964502/python-fabric-nested-roles | |
server = { | |
'prod':{ | |
'hosts' : ['list of ip address'], | |
'db':['list of ip address'], | |
}, | |
'test':{ | |
'hosts' : ['list of ip address'], | |
} | |
} | |
# http://docs.fabfile.org/en/1.4.1/usage/execution.html#failure-handling | |
env.warn_only = True | |
env.user = '<server username to login>' | |
# public key | |
env.key_filename = 'path to aws public key' | |
env.src = 'path to code in virtual environment' | |
bin_dir = 'path to virtual environment. One level above bin folder' | |
env.project = 'path to virtual environment.' | |
# # aws login done | |
# def test(): | |
# with settings(warn_only=True): | |
# result = local('./manage.py test my_app', capture=True) | |
# if result.failed and not confirm("Tests failed. Continue anyway?"): | |
# abort("Aborting at user request.") | |
# def commit(): | |
# local("git add -p && git commit") | |
# | |
# def push(): | |
# local("git push") | |
@roles('hosts') | |
def pull(): | |
with cd(env.src): | |
run('whoami') | |
run("git pull") | |
@roles('db') | |
def migrate(): | |
with cd(env.src): | |
run('pwd') | |
run("python manage.py makemigrations <project name>") | |
run("python manage.py migrate") | |
@roles('hosts') | |
def restart(): | |
with virtualenv(env.project): | |
with cd(env.src): | |
run('pwd') | |
run('echo $PATH') | |
run("uwsgi --reload <path>.pid ") | |
run("supervisorctl restart celeryd") | |
@roles('hosts') | |
def restartngix(): | |
sudo("service nginx restart ") | |
@roles('hosts') | |
def start(conf): | |
# add cleanup part killing all process | |
with virtualenv(env.project): | |
with cd(env.src): | |
run('pwd') | |
run("uwsgi --ini uwsgi.ini") | |
run("supervisord -c %s.conf"%conf) | |
# run("supervisorctl start celeryd") | |
@roles('hosts') | |
def collectstatic(): | |
with virtualenv(env.project): | |
with cd(env.src): | |
run("python manage.py collectstatic") | |
@roles('hosts') | |
def uname(): | |
run('uname -a') | |
def deploy(conf): | |
if not conf: | |
raise Exception("conf is empty.") | |
execute(uname) | |
# execute(pull) | |
# execute(collectstatic) | |
# execute(migrate) | |
# execute(start(conf)) | |
execute(restart) | |
# execute(restartngix) | |
def test(): | |
env.roledefs = server['test'] | |
deploy(conf='supervisor_test') | |
def prod(): | |
env.roledefs = server['prod'] | |
deploy(conf="supervisor") | |
# run as $fab test or $fab prod | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment