Skip to content

Instantly share code, notes, and snippets.

@Raisins
Created December 4, 2013 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raisins/7781097 to your computer and use it in GitHub Desktop.
Save Raisins/7781097 to your computer and use it in GitHub Desktop.
Simple Fabric file with some django
from fabric.api import local, run, cd, env, sudo, get, put, settings as fabric_settings
PROJECT_NAME = "myamazingproject"
# lets first set up some environments which we might want to go into
# allows us to use fab environment command
# we'll see later how this is used
def staging():
"""Sets up the staging environment for fab remote commands"""
env.PROJECT_PATH = "~/path/to/your/%s/" % (PROJECT_NAME)
env.VENDOR_PATH = "~/vendor/" # place where compiled code lives before install. Ex: Node, redis
env.user = 'your_user_name_on_server'
env.hosts = ['ip.of.server', '124.124.123.123']
##############################################################################
##############################################################################
#here is a simple command that allows us to ssh in to a server
def ssh(id=0):
"""Launch console for given ssh host"""
try:
host = env.hosts[int(id) - 1]
except IndexError:
raise Exception("Wrong index provided")
except ValueError:
raise Exception("Argument must be integer")
local('ssh %s@%s' % (env.user, host))
#example usage 'fab staging ssh'
#if we have more then one host 'fab staging ssh:3'
# no more remembering usernames or trying to remember all the servers ips
##############################################################################
##############################################################################
# a lot of times during django development you run things off of your own machine
# typing all that out can be a pain. and what if you are using an alternative server
# Lets make it easy
def r():
from django.conf import settings
"""
Shortcut to do quick runserver
"""
if 'gunicorn' in settings.INSTALLED_APPS:
try:
local('kill -TERM `cat gunicorn.pid`')
except:
print 'No existing gunicorn process'
local('python manage.py run_gunicorn -w 4 --timeout=240 --pid=gunicorn.pid')
else:
local('python manage.py runserver')
# fab r will now run your dev server if are using gunicorn it will auto detect
# it and run it for you
##############################################################################
##############################################################################
# lets say we are doing server tests and need to setup elastic search, over and over.
# on several servers.
def setup_es_ubuntu(self):
sudo('sudo apt-get update')
sudo('sudo apt-get install openjdk-7-jre-headless -y')
sudo('cd %s' % env.VENDOR_PATH)
sudo('wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.5.deb')
sudo('sudo dpkg -i elasticsearch-0.90.5.deb')
sudo('sudo service elasticsearch start')
# fab staging setup_es_ubuntu
# fab dev1 setup_es_ubuntu
# etc...
##############################################################################
##############################################################################
#I was recently working on a node app and was using forever to keep it running
# wanted a 1 line deploy and reset
def launch(full=False):
"""Launch new code. Does a git pull and bounce"""
with cd(env.PROJECT_PATH):
run('git pull')
bounce()
def bounce():
"""Bounce node/forever"""
sudo('forever restartall') # restart node
#this can now be used either as
# fab staging launch
# fab staging bounce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment