Updated fabfile
This commit is contained in:
parent
713e0c26fd
commit
1d8cd454b2
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import with_statement, print_function
|
from __future__ import with_statement, print_function
|
||||||
from os.path import dirname, abspath, join
|
from os.path import dirname, abspath, join
|
||||||
from os.path import exists as os_exists
|
from os.path import exists as os_exists
|
||||||
|
from os import getcwd
|
||||||
|
|
||||||
from fabric.api import *
|
from fabric.api import *
|
||||||
from fabric.context_managers import settings, cd
|
from fabric.context_managers import settings, cd
|
||||||
|
@ -14,23 +15,9 @@ from fabric.operations import local
|
||||||
#
|
#
|
||||||
env.LOCAL_PATH = dirname(abspath(__file__))
|
env.LOCAL_PATH = dirname(abspath(__file__))
|
||||||
|
|
||||||
#
|
|
||||||
# HOSTS
|
|
||||||
#
|
|
||||||
HOSTS = {
|
|
||||||
'local': {
|
|
||||||
'host': '127.0.0.1',
|
|
||||||
'path': env.LOCAL_PATH,
|
|
||||||
},
|
|
||||||
'dev': {
|
|
||||||
'host': 'dev',
|
|
||||||
'path': '~',
|
|
||||||
'user': 'app',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if not env.hosts:
|
if not env.hosts:
|
||||||
env.hosts = ['local']
|
env.hosts = ['localhost']
|
||||||
|
|
||||||
# Doctor checkups
|
# Doctor checkups
|
||||||
DOCTOR = {
|
DOCTOR = {
|
||||||
|
@ -38,39 +25,6 @@ DOCTOR = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# HELPERS
|
|
||||||
#
|
|
||||||
def get_host_string(host_config):
|
|
||||||
host_string = ''
|
|
||||||
if 'user' in host_config:
|
|
||||||
host_string += '{}@'.format(host_config['user'])
|
|
||||||
|
|
||||||
host_string += host_config['host']
|
|
||||||
|
|
||||||
if 'port' in host_config:
|
|
||||||
host_string += ':{}'.format(host_config['port'])
|
|
||||||
|
|
||||||
return host_string
|
|
||||||
|
|
||||||
|
|
||||||
def get_host_app_environment():
|
|
||||||
"""
|
|
||||||
Get remote $ENVIRONMENT variable value.
|
|
||||||
Default: local
|
|
||||||
"""
|
|
||||||
# In case we're setting up a new host we need this defined
|
|
||||||
app_environment = 'local'
|
|
||||||
try:
|
|
||||||
ssh_io = run('echo -e "\n$ENVIRONMENT"', quiet=True).split()[1]
|
|
||||||
except IndexError:
|
|
||||||
ssh_io = 'local'
|
|
||||||
if ssh_io and exists(join(env.LOCAL_PATH, 'config', ssh_io)):
|
|
||||||
app_environment = ssh_io
|
|
||||||
|
|
||||||
return app_environment
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# CONTEXT MANAGERS
|
# CONTEXT MANAGERS
|
||||||
#
|
#
|
||||||
|
@ -78,35 +32,12 @@ def virtualenv():
|
||||||
"""
|
"""
|
||||||
Activates virtualenv first
|
Activates virtualenv first
|
||||||
"""
|
"""
|
||||||
return prefix(
|
return prefix('source .virtualenv/bin/activate')
|
||||||
'source {}/.virtualenv/bin/activate'.format(
|
|
||||||
env.host_config['path']
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# DECORATORS
|
|
||||||
#
|
|
||||||
def task_environment(method):
|
|
||||||
"""
|
|
||||||
Retrieves host based configuration and app_environment from the host
|
|
||||||
and automatically CDs into the specified path
|
|
||||||
"""
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
env.host_config = HOSTS[env.host]
|
|
||||||
with settings(host_string=get_host_string(env.host_config)):
|
|
||||||
env.appenv = get_host_app_environment()
|
|
||||||
|
|
||||||
with cd(env.host_config['path']):
|
|
||||||
return method(*args, **kwargs)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# TASKS
|
# TASKS
|
||||||
#
|
#
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
def setup_environment():
|
def setup_environment():
|
||||||
"""
|
"""
|
||||||
|
@ -117,7 +48,6 @@ def setup_environment():
|
||||||
execute(setup_database)
|
execute(setup_database)
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
def setup_virtualenv():
|
def setup_virtualenv():
|
||||||
"""
|
"""
|
||||||
|
@ -133,7 +63,6 @@ def setup_virtualenv():
|
||||||
env.appenv))
|
env.appenv))
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
def setup_tools():
|
def setup_tools():
|
||||||
# Setup frontend tools
|
# Setup frontend tools
|
||||||
|
@ -141,7 +70,6 @@ def setup_tools():
|
||||||
run('npm install')
|
run('npm install')
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
def setup_database():
|
def setup_database():
|
||||||
"""
|
"""
|
||||||
|
@ -154,28 +82,6 @@ def setup_database():
|
||||||
run('python manage.py migrate')
|
run('python manage.py migrate')
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
|
||||||
def set_environment():
|
|
||||||
env_type = prompt('Environment type?')
|
|
||||||
bashrc = run('cat $HOME/.bashrc', quiet=True)
|
|
||||||
if 'export ENVIRONMENT=`cat $HOME/.environment`' not in bashrc:
|
|
||||||
print(red('Error: .bashrc is not properly configured!'))
|
|
||||||
run("echo export ENVIRONMENT=\$\(cat $HOME/.environment\) "
|
|
||||||
">> .bash_profile")
|
|
||||||
run('echo {} > .environment'.format(env_type))
|
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
|
||||||
def shell():
|
|
||||||
"""
|
|
||||||
Opens a shell to the given host
|
|
||||||
"""
|
|
||||||
open_shell('cd {}'.format(env.host_config['path']))
|
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
def doctor():
|
def doctor():
|
||||||
print(yellow('Checking for software:'))
|
print(yellow('Checking for software:'))
|
||||||
|
@ -191,57 +97,43 @@ def doctor():
|
||||||
#
|
#
|
||||||
# LOCAL ONLY
|
# LOCAL ONLY
|
||||||
#
|
#
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
@hosts(['local'])
|
@hosts(['localhost'])
|
||||||
def runserver():
|
def runserver():
|
||||||
"""
|
"""
|
||||||
Executes local development server
|
Executes local development server
|
||||||
"""
|
"""
|
||||||
with virtualenv():
|
with virtualenv():
|
||||||
run('python manage.py runserver 0.0.0.0:8000')
|
local('python manage.py runserver 0.0.0.0:8000')
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
def clean_pyc():
|
def clean_pyc():
|
||||||
run('find . -name "*.pyc" -exec rm -rf {} \;')
|
local('find . -name "*.pyc" -exec rm -rf {} \;')
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
@hosts(['local'])
|
@hosts(['localhost'])
|
||||||
def rungrunt():
|
def rungrunt():
|
||||||
"""
|
"""
|
||||||
Executes grunt
|
Executes grunt
|
||||||
"""
|
"""
|
||||||
run('grunt --force')
|
local('grunt --force')
|
||||||
|
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
@hosts(['local'])
|
@hosts(['localhost'])
|
||||||
def makemessages():
|
def makemessages():
|
||||||
"""
|
"""
|
||||||
Executes django-admin makemessages where needed
|
Executes django-admin makemessages where needed
|
||||||
"""
|
"""
|
||||||
with cd('shelfzilla'):
|
with virtualenv():
|
||||||
if not exists('locale'):
|
local('cd shelfzilla && django-admin.py makemessages -l es')
|
||||||
run('mkdir locale')
|
|
||||||
with virtualenv():
|
|
||||||
run('django-admin.py makemessages -l es', quiet=True)
|
|
||||||
|
|
||||||
"""
|
|
||||||
apps = ['homepage', 'landing', 'manga', 'users']
|
|
||||||
for app in apps:
|
|
||||||
with cd('shelfzilla/apps/{}'.format(app)):
|
|
||||||
if not exists('locale'):
|
|
||||||
run('mkdir locale')
|
|
||||||
with virtualenv():
|
|
||||||
run('django-admin.py makemessages -l es', quiet=True)
|
|
||||||
"""
|
|
||||||
|
|
||||||
@task_environment
|
#
|
||||||
|
# BACKUPS
|
||||||
|
#
|
||||||
@task
|
@task
|
||||||
def clean_backups(BCK_BASE_PATH='/backups/sql', DAYS='30'):
|
def clean_backups(BCK_BASE_PATH='/backups/sql', DAYS='30'):
|
||||||
"""
|
"""
|
||||||
|
@ -251,7 +143,7 @@ def clean_backups(BCK_BASE_PATH='/backups/sql', DAYS='30'):
|
||||||
with settings(hide('warnings', 'running', 'stdout', 'stderr')):
|
with settings(hide('warnings', 'running', 'stdout', 'stderr')):
|
||||||
local('find %s -mtime +%s -exec rm -rf {} \;' % (BCK_BASE_PATH, DAYS))
|
local('find %s -mtime +%s -exec rm -rf {} \;' % (BCK_BASE_PATH, DAYS))
|
||||||
|
|
||||||
@task_environment
|
|
||||||
@task
|
@task
|
||||||
def backup():
|
def backup():
|
||||||
"""
|
"""
|
||||||
|
|
Reference in New Issue