from __future__ import with_statement
from fabric import api
from fabric.contrib import django
import os
import logging
# Configuration
ENV_DIR = '.env'
REMOTE_ROOT = '/web/desconto'
api.env.user = 'web'
api.env.hosts = ['linode']
# This command will fail
# when virtualenv is not installed yet
try:
# Activating the virtualenv
activate_this = ENV_DIR + '/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
except IOError, ex:
pass
else:
django.settings_module('settings')
def database_name():
import settings
return settings.DATABASES['default']['NAME']
def reset(generate='all'):
import settings
if getattr(settings, 'DATABASE_RESET_SILENTLY', None):
response = 'yes'
else:
response = raw_input('Are you sure you want to reset site\'s database? Enter yes to continue: ')
if response == 'yes':
dbname = database_name()
with api.settings(warn_only=True):
api.local('dropdb %s' % dbname)
api.local('createdb %s' % dbname)
api.local('./manage.py syncdb --noinput --migrate')
api.local('./generate.py')
api.local('./load_city.py')
else:
print 'Cancelling'
def run():
api.local('./manage.py runserver 0.0.0.0:8000')
def run_plus():
api.local('./manage.py runserver_plus 0.0.0.0:8000')
def buildenv():
# Ignore "IOError: [Errno 26] Text file busy: 'var/.env/bin/python'"
# when debug server is live
with api.settings(warn_only=True):
api.local('virtualenv %s' % ENV_DIR, capture=False)
api.local('%s/bin/easy_install pip' % ENV_DIR, capture=False)
api.local('%s/bin/pip install --upgrade --no-deps -r pipreq.txt' % ENV_DIR, capture=False)
def dbsync():
dbname = database_name()
api.run('pg_dump -Fc -Z9 -x -f /tmp/%s.pack %s' % (dbname, dbname))
api.get('/tmp/%s.pack' % dbname, '/tmp/%s.pack' % dbname)
api.local('pg_restore -v -c -x -d %s /tmp/%s.pack' % (dbname, dbname))
def shell():
api.local('./manage.py shell_plus')
def test(*args):
with prefix('source var/.env/bin/activate'):
local('DJANGO_SETTINGS_MODULE="settings_test" PYTHONPATH="." nosetests %s' % ' '.join(args))
def deploy():
api.local('hg push')
with api.cd(REMOTE_ROOT):
api.run('hg fetch')
api.local('./manage.py syncdb --noinput --migrate')
api.local('./load_city.py')
api.run('touch app.py')
def buildenv_dev():
api.local('hg push')
with api.cd(REMOTE_ROOT):
api.run('hg fetch')
api.run('fab buildenv')
api.run('touch app.py')
def update_lib(name):
for line in open('pipreq.txt'):
if name in line:
api.local('%s/bin/pip install -U %s' % (ENV_DIR, line.strip()))