huogerac
9/25/2013 - 4:35 AM

fabfile.py

""" Deployment of your django project.
"""

from fabric.api import *

env.hosts = ['yourdomain.com']
env.user = "your-user"

def update_django_project():
    """ Updates the remote django project.
    """
    with cd('/path/to/your/django/project'):
        run('git pull')
        with prefix('source /path/to/your/virtualenv/bin/activate'):
            run('pip install -r your_pip_requirement_file.txt')
            run('python manage.py syncdb')
            run('python manage.py migrate') # if you use south
            run('python manage.py collectstatic --noinput')

def restart_webserver():
    """ Restarts remote nginx and uwsgi.
    """
    sudo("service uwsgi restart")
    sudo("/etc/init.d/nginx restart")

def deploy():
    """ Deploy Django Project.
    """
    update_django_project()
    restart_webserver()
from fabric.contrib.files import exists
from fabric.api import * 
from fabric.colors import green, red
from os import path
from fabric.operations import local as lrun
from fabric.api import task

REPO_URL = 'https://github.com/huogerac/cardapiodogia.git'
SITES_FOLDER = '/home/roger'

@task
def localhost():
	""" uses localhost machine """
	env.run = lrun
	env.hosts = ['localhost']

@task
def stage():
	""" uses a remote server """
	env.hosts = ['stage']	
	env.user = 'huogerac'


@task
def bootstrap():
	""" steps to bootstrap the app """
	print(red("Starting bootstrap:"))
	run("cd ~")
	run("pwd")
	message = prompt("enter the version: ")
	local('git st')
	
	print(green("Syncing the database..."))

	path = '/media/rac-data/rAc/desenv/2013/python/'
	with cd("%s/superlists" % path):
		run("pwd") 
		print(green("Pulling master from GitHub...")) 
		run("ls") 
		print(green("Installing requirements...")) 
		run("source /media/rac-data/novos/django15.env/bin/activate && pip freeze") 
		print(green("Collecting static files..."))


##this commands are from superlist - Obey
def deploy():
	""" steps to make a new deployment """
    _create_directory_structure_if_necessary(env.host)
    source_folder = path.join(SITES_FOLDER, env.host, 'source')
    _get_latest_source(source_folder)
    _update_virtualenv(source_folder)
    _update_static_files(source_folder)
    _update_database(source_folder)


def _create_directory_structure_if_necessary(site_name):
    base_folder = path.join(SITES_FOLDER, site_name)
    run('mkdir -p %s' % (base_folder))
    for subfolder in ('database', 'static', 'virtualenv', 'source'):
        run('mkdir -p %s/%s' % (base_folder, subfolder))


def _get_latest_source(source_folder):
    if exists(path.join(source_folder, '.git')):
        run('cd %s && git pull' % (source_folder,))
        run('cd %s && git reset --hard' % (source_folder,))
    else:
        run('git clone %s %s' % (REPO_URL, source_folder))


def _update_virtualenv(source_folder):
    virtualenv_folder = path.join(source_folder, '../virtualenv')
    if not exists(path.join(virtualenv_folder, 'bin', 'pip')):
        run('virtualenv %s' % (virtualenv_folder,))
    run('%s/bin/pip install -r %s/requirements.txt' % (
            virtualenv_folder, source_folder
    ))


def _update_static_files(source_folder):
    run('cd %s && python manage.py collectstatic --noinput' % (
        source_folder,
    ))


def _update_database(source_folder):
    run('cd %s && python manage.py syncdb --noinput' % (
        source_folder,
    ))