Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
131 lines (99 sloc) 3.53 KB
"""Management utilities."""
from fabric.contrib.console import confirm
from fabric.api import abort, env, local, settings, task
########## GLOBALS
env.run = 'heroku run python manage.py'
env.settings = 'replayswithfriends.settings.prod'
HEROKU_STACK = 'cedar'
HEROKU_ADDONS = (
'shared-database:5mb',
'pgbackups:auto-month',
'scheduler:standard',
'memcache:5mb',
'rabbitmq', # Currently in BETA.
)
########## END GLOBALS
########## HELPERS
def cont(cmd, message):
"""Given a command, ``cmd``, and a message, ``message``, allow a user to
either continue or break execution if errors occur while executing ``cmd``.
:param str cmd: The command to execute on the local system.
:param str message: The message to display to the user on failure.
.. note::
``message`` should be phrased in the form of a question, as if ``cmd``'s
execution fails, we'll ask the user to press 'y' or 'n' to continue or
cancel exeuction, respectively.
Usage::
cont('heroku run ...', "Couldn't complete %s. Continue anyway?" % cmd)
"""
with settings(warn_only=True):
result = local(cmd, capture=True)
if message and result.failed and not confirm(message):
abort('Stopped execution per user request.')
########## END HELPERS
########## DATABASE MANAGEMENT
@task
def syncdb():
"""Run a syncdb."""
local('%(run)s syncdb --noinput --settings=%(settings)s' % env)
@task
def migrate(app=None):
"""Apply one (or more) migrations. If no app is specified, fabric will
attempt to run a site-wide migration.
:param str app: Django app name to migrate.
"""
if app:
local('%s migrate %s --noinput --settings=%s' % (env.run, app,
env.settings))
else:
local('%(run)s migrate --noinput --settings=%(settings)s' % env)
########## END DATABASE MANAGEMENT
########## FILE MANAGEMENT
@task
def collectstatic():
"""Collect all static files, and copy them to S3 for production usage."""
local('%(run)s collectstatic --noinput --settings=%(settings)s' % env)
########## END FILE MANAGEMENT
########## HEROKU MANAGEMENT
@task
def bootstrap():
"""Bootstrap your new application with Heroku, preparing it for a production
deployment. This will:
- Create a new Heroku application.
- Install all ``HEROKU_ADDONS``.
- Sync the database.
- Apply all database migrations.
"""
cont('heroku create --stack %s' % HEROKU_STACK,
"Couldn't create the Heroku app, continue anyway?")
for addon in HEROKU_ADDONS:
cont('heroku addons:add %s' % addon,
"Couldn't add %s to your Heroku app, continue anyway?" % addon)
cont('git push heroku master',
"Couldn't push your application to Heroku, continue anyway?")
syncdb()
migrate()
@task
def compress():
local("python manage.py collectstatic --noinput --settings=%s" % env.settings)
local("python manage.py compress --settings=%s" % env.settings)
@task
def deploy():
cont('git push heroku master',
"Couldn't push your application to Heroku, continue anyway?")
syncdb()
migrate()
compress()
@task
def light_deploy():
cont('git push heroku master',
"Couldn't push your application to Heroku, continue anyway?")
ld = light_deploy
@task
def destroy():
"""Destroy this Heroku application. Wipe it from existance.
.. note::
This really will completely destroy your application. Think twice.
"""
local('heroku apps:destroy')
########## END HEROKU MANAGEMENT