Last active
December 15, 2015 08:08
-
-
Save mikedory/5228140 to your computer and use it in GitHub Desktop.
A handy gist for deploying Django code to a remote server via Git/Fabric and then managing the app via Supervisor. Heavily inspired by the excellent docs at http://docs.fabfile.org/en/1.6/. Switch out the "appname" bits and everything in local_settings.py to suit your app's settings, naturally.
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
# import the fabric requirements | |
from __future__ import with_statement | |
from fabric.api import * | |
from fabric.contrib.console import confirm | |
# import local_settings.py | |
from local_settings import * | |
# run Django's test framework | |
def test(): | |
with settings(warn_only=True): | |
result = local('./manage.py test appname', capture=True) | |
if result.failed and not confirm("Tests failed! D: Continue anyway?"): | |
abort("Aborting!") | |
# add and commit all local files | |
def commit(): | |
with settings(warn_only=True): | |
commit = local("git add -p && git commit") | |
if commit.failed: | |
print("Nothing to commit. Moving on.") | |
# push up to github | |
def push(): | |
local("git push origin master") | |
# run all the pre-flight tests | |
# this would also be a handy place to pack/minify, if so desired | |
def prepare_deploy(): | |
test() | |
commit() | |
push() | |
# --------------------- | |
# deploy to the remote server | |
def deploy(): | |
with settings(warn_only=True): | |
if run("test -d %s" % code_dir).failed: | |
run("git clone %s %s" % (code_repo, code_dir)) | |
with cd(code_dir): | |
run("git fetch") | |
run("git merge origin/master") | |
run("supervisorctl restart appname") |
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
# deploy/fabric needs | |
from fabric.api import * | |
# remote server configs | |
env.user = 'username' | |
env.hosts = ['domain.com'] | |
env.key_filename = '/path/to/.ssh/id_rsa' | |
# deploy directory and repo info | |
code_dir_root = '/path/to/code/directory' | |
code_dir_target = '/path/to/code/directory/target' | |
code_repo = '[email protected]:user/REPO.git' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This also assumes that you're using Git and SSH keys, to note.
If you'd rather deploy by tarring, the Flask docs have a great example.
And if you have questions about making SSH keys, there's a great sample on the Centos Wiki.