2014-03-18 12:48:12 +00:00
|
|
|
from __future__ import with_statement, print_function
|
|
|
|
from os.path import dirname, abspath, join
|
2014-06-01 19:57:27 +00:00
|
|
|
from os.path import exists as os_exists
|
2014-08-25 16:02:14 +00:00
|
|
|
from os import getcwd
|
2014-03-18 12:48:12 +00:00
|
|
|
|
|
|
|
from fabric.api import *
|
2014-04-02 15:07:57 +00:00
|
|
|
from fabric.context_managers import settings, cd
|
2014-03-18 12:48:12 +00:00
|
|
|
from fabric.contrib.files import exists
|
|
|
|
from fabric.colors import yellow, red, white, green
|
2014-06-01 19:57:27 +00:00
|
|
|
from fabric.operations import local
|
2014-03-18 12:48:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# GLOBALS
|
|
|
|
#
|
|
|
|
env.LOCAL_PATH = dirname(abspath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
if not env.hosts:
|
2014-08-25 16:02:14 +00:00
|
|
|
env.hosts = ['localhost']
|
2014-03-18 12:48:12 +00:00
|
|
|
|
|
|
|
# Doctor checkups
|
|
|
|
DOCTOR = {
|
2014-04-02 15:23:06 +00:00
|
|
|
'apps': ['virtualenv', 'python', 'npm', 'grunt', 'bower']
|
2014-03-18 12:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# CONTEXT MANAGERS
|
|
|
|
#
|
|
|
|
def virtualenv():
|
|
|
|
"""
|
|
|
|
Activates virtualenv first
|
|
|
|
"""
|
2014-08-25 16:02:14 +00:00
|
|
|
return prefix('source .virtualenv/bin/activate')
|
2014-03-18 12:48:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# TASKS
|
|
|
|
#
|
|
|
|
@task
|
|
|
|
def setup_environment():
|
|
|
|
"""
|
|
|
|
Prepares environment for the application
|
|
|
|
"""
|
|
|
|
execute(setup_virtualenv)
|
|
|
|
execute(setup_tools)
|
|
|
|
execute(setup_database)
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def setup_virtualenv():
|
|
|
|
"""
|
|
|
|
Creates or updates a virtualenv
|
|
|
|
"""
|
|
|
|
if not exists('.virtualenv'):
|
|
|
|
print(yellow('Create virtualenv'))
|
|
|
|
run('virtualenv-2.7 .virtualenv')
|
|
|
|
|
|
|
|
with virtualenv():
|
|
|
|
print(yellow('Installing requirements'))
|
|
|
|
run('pip install -r config/{}/requirements.txt --use-mirrors'.format(
|
|
|
|
env.appenv))
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def setup_tools():
|
|
|
|
# Setup frontend tools
|
|
|
|
print(yellow('Installing npm dependencies'))
|
|
|
|
run('npm install')
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def setup_database():
|
|
|
|
"""
|
|
|
|
Create or update the database
|
|
|
|
"""
|
|
|
|
with virtualenv():
|
|
|
|
print(yellow('SyncDB'))
|
|
|
|
run('python manage.py syncdb')
|
|
|
|
print(yellow('Migrate'))
|
|
|
|
run('python manage.py migrate')
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def doctor():
|
|
|
|
print(yellow('Checking for software:'))
|
|
|
|
for app in DOCTOR['apps']:
|
|
|
|
print(white('{}'.format(app)), end=': ')
|
|
|
|
check = run('which {}'.format(app), quiet=True)
|
|
|
|
if check.succeeded:
|
|
|
|
print(green('present'))
|
|
|
|
else:
|
|
|
|
print(red('not present'))
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# LOCAL ONLY
|
|
|
|
#
|
|
|
|
@task
|
2014-08-25 16:02:14 +00:00
|
|
|
@hosts(['localhost'])
|
2014-03-18 12:48:12 +00:00
|
|
|
def runserver():
|
|
|
|
"""
|
|
|
|
Executes local development server
|
|
|
|
"""
|
|
|
|
with virtualenv():
|
2014-08-25 16:02:14 +00:00
|
|
|
local('python manage.py runserver 0.0.0.0:8000')
|
2014-03-18 12:48:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def clean_pyc():
|
2014-08-25 16:02:14 +00:00
|
|
|
local('find . -name "*.pyc" -exec rm -rf {} \;')
|
2014-03-18 12:48:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2014-08-25 16:02:14 +00:00
|
|
|
@hosts(['localhost'])
|
2014-03-18 12:48:12 +00:00
|
|
|
def rungrunt():
|
|
|
|
"""
|
|
|
|
Executes grunt
|
|
|
|
"""
|
2014-08-25 16:02:14 +00:00
|
|
|
local('grunt --force')
|
2014-04-02 15:07:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
2014-08-25 16:02:14 +00:00
|
|
|
@hosts(['localhost'])
|
2014-04-02 15:07:57 +00:00
|
|
|
def makemessages():
|
|
|
|
"""
|
|
|
|
Executes django-admin makemessages where needed
|
|
|
|
"""
|
2014-08-25 16:02:14 +00:00
|
|
|
with virtualenv():
|
|
|
|
local('cd shelfzilla && django-admin.py makemessages -l es')
|
2014-04-02 15:07:57 +00:00
|
|
|
|
2014-06-01 19:57:27 +00:00
|
|
|
|
2014-08-25 16:02:14 +00:00
|
|
|
#
|
|
|
|
# BACKUPS
|
|
|
|
#
|
2014-06-01 19:57:27 +00:00
|
|
|
@task
|
|
|
|
def clean_backups(BCK_BASE_PATH='/backups/sql', DAYS='30'):
|
|
|
|
"""
|
|
|
|
This function clean old backups from backup base path
|
|
|
|
"""
|
2014-06-01 20:04:05 +00:00
|
|
|
print(white("\tCleaning oldest backups..."))
|
2014-06-01 19:57:27 +00:00
|
|
|
with settings(hide('warnings', 'running', 'stdout', 'stderr')):
|
|
|
|
local('find %s -mtime +%s -exec rm -rf {} \;' % (BCK_BASE_PATH, DAYS))
|
|
|
|
|
2014-08-25 16:02:14 +00:00
|
|
|
|
2014-06-01 19:57:27 +00:00
|
|
|
@task
|
|
|
|
def backup():
|
|
|
|
"""
|
|
|
|
This function makes a PostgreSQL Backup and put it in backup base path
|
|
|
|
"""
|
|
|
|
import time
|
|
|
|
|
|
|
|
BCK_BASE_PATH = '/backups/sql'
|
|
|
|
DATABASE = "shelfzilla"
|
|
|
|
print(white("\n\tMaking backup of [%s] database" % DATABASE))
|
|
|
|
with settings(hide('running')):
|
|
|
|
if not os_exists(BCK_BASE_PATH + '/' + time.strftime("%d_%m_%Y")):
|
|
|
|
local('mkdir -p %s' % BCK_BASE_PATH + '/' + time.strftime("%d_%m_%Y"))
|
|
|
|
|
|
|
|
with lcd(BCK_BASE_PATH + '/' + time.strftime("%d_%m_%Y")):
|
|
|
|
local('pg_dump %s | gzip > %s.gz' %
|
|
|
|
(DATABASE, "shelfzilla_" + time.strftime("%H:%M_%d_%m_%Y")) )
|
|
|
|
|
|
|
|
clean_backups()
|