fmartingr
/
shelfzilla
Archived
1
0
Fork 0
This repository has been archived on 2021-06-29. You can view files and clone it, but cannot push or open issues or pull requests.
shelfzilla/fabfile.py

164 lines
3.4 KiB
Python
Raw Normal View History

2014-03-18 12:48:12 +00:00
from __future__ import with_statement, print_function
from os.path import dirname, abspath, join
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 *
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
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 = {
'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
"""
2014-11-04 21:40:50 +00:00
print(yellow('Create virtualenv'))
2015-01-10 09:36:35 +00:00
local('virtualenv .virtualenv')
2014-03-18 12:48:12 +00:00
with virtualenv():
print(yellow('Installing requirements'))
2014-11-04 21:40:50 +00:00
local('pip install -r config/local/requirements.txt')
2014-03-18 12:48:12 +00:00
@task
def setup_tools():
# Setup frontend tools
print(yellow('Installing npm dependencies'))
2014-11-04 21:40:50 +00:00
local('npm install')
2014-03-18 12:48:12 +00:00
@task
def setup_database():
"""
Create or update the database
"""
with virtualenv():
print(yellow('SyncDB'))
2014-11-04 21:40:50 +00:00
local('python manage.py syncdb')
# print(yellow('Migrate'))
# local('python manage.py migrate')
2014-03-18 12:48:12 +00:00
@task
def doctor():
print(yellow('Checking for software:'))
for app in DOCTOR['apps']:
print(white('{}'.format(app)), end=': ')
2014-11-04 21:40:50 +00:00
check = local('which {}'.format(app), quiet=True)
2014-03-18 12:48:12 +00:00
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():
local('python manage.py runserver_plus 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')
@task
2014-08-25 16:02:14 +00:00
@hosts(['localhost'])
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-08-25 16:02:14 +00:00
#
# BACKUPS
#
@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..."))
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
@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()