-"""Management utilities."""
+""" Karmaworld Fabric management script
+ Finals Club (c) 2013"""
-from fabric.contrib.console import confirm
-from fabric.api import abort, env, local, settings, task
+import os
+from fabric.api import cd, env, lcd, prefix, run, task, local, settings
+from fabvenv import virtualenv
-########## GLOBALS
-env.run = 'heroku run python manage.py'
-HEROKU_ADDONS = (
- 'cloudamqp:lemur',
- 'heroku-postgresql:dev',
- 'scheduler:standard',
- 'memcache:5mb',
- 'newrelic:standard',
- 'pgbackups:auto-month',
- 'sentry:developer',
-)
-HEROKU_CONFIGS = (
- 'DJANGO_SETTINGS_MODULE=karmaworld.settings.prod',
- 'SECRET_KEY=(s1k!&^7l28k&nrm2ek(qqo&19%y(zn#=^zq_*ur2@irjun0x4'
- 'AWS_ACCESS_KEY_ID=xxx',
- 'AWS_SECRET_ACCESS_KEY=xxx',
- 'AWS_STORAGE_BUCKET_NAME=xxx',
-)
-########## END GLOBALS
+######### GLOBAL
+env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
-########## 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``.
+######## Define host(s)
+@task
+def here():
+ """
+ Connection information for the local machine
+ """
+ def _custom_local(command):
+ prefixed_command = '/bin/bash -l -i -c "%s"' % command
+ return local(prefixed_command)
- :param str cmd: The command to execute on the local system.
- :param str message: The message to display to the user on failure.
+ env.run = _custom_local
+ # This is required for the same reason as above
+ env.proj_root = '/var/www/karmaworld'
+ env.cd = lcd
+ env.reqs = 'reqs/dev.txt'
+ env.confs = 'confs/beta'
+ env.branch = 'beta'
- .. 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)
+####### Define production host
+@task
+def prod():
+ """
+ Connection Information for production machine
+ """
+
+ env.user = 'djkarma'
+ env.hosts = ['karmanotes.org']
+ env.proj_root = '/var/www/karmaworld'
+ env.reqs = 'reqs/prod.txt'
+ env.confs = 'confs/prod/'
+ env.branch = 'beta'
+ env.run = virtenv_exec
+ env.gunicorn_addr = '127.0.0.1:8000'
+
+####### Define beta host
+@task
+def beta():
+ """
+ Connection Information for beta machine
"""
- 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
+ env.user = 'djkarma'
+ env.hosts = ['beta.karmanotes.org']
+ env.proj_root = '/var/www/karmaworld'
+ env.reqs = 'reqs/prod.txt'
+ env.confs = 'confs/prod/'
+ env.branch = 'beta'
+ env.run = virtenv_exec
+######## Run Commands in Virutal Environment
+def virtenv_exec(command):
+ """
+ Execute command in Virtualenv
+ """
-########## DATABASE MANAGEMENT
+ with virtualenv('%s/%s' % (env.proj_root, env.branch)):
+ run('%s' % (command))
+
+######## Sync database
@task
def syncdb():
- """Run a syncdb."""
- local('%(run)s syncdb --noinput' % env)
+ """
+ Sync Database
+ """
+
+ env.run('%s/manage.py syncdb --migrate' % env.proj_root )
+
+
+####### Collect Static Files
+@task
+def collect_static():
+ """
+ Collect static files (if AWS config. present, push to S3)
+ """
+
+ env.run('%s/manage.py collectstatic --noinput' % env.proj_root )
+
+####### Run Dev Server
+@task
+def dev_server():
+ """
+ Runs the built-in django webserver
+ """
+ env.run('%s/manage.py runserver' % env.proj_root)
+####### Create Virtual Environment
@task
-def migrate(app=None):
- """Apply one (or more) migrations. If no app is specified, fabric will
- attempt to run a site-wide migration.
+def make_virtualenv():
+ """
+ Create our Virtualenv in $SRC_ROOT
+ """
- :param str app: Django app name to migrate.
+ run('virtualenv %s/%s' % (env.proj_root, env.branch))
+ env.run('pip install -r %s/reqs/%s.txt' % (env.proj_root, env.branch) )
+
+@task
+def start_supervisord():
"""
- if app:
- local('%s migrate %s --noinput' % (env.run, app))
- else:
- local('%(run)s migrate --noinput' % env)
-########## END DATABASE MANAGEMENT
+ Starts supervisord
+ """
+ config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+ env.run('supervisord -c %s' % config_file)
-########## FILE MANAGEMENT
@task
-def collectstatic():
- """Collect all static files, and copy them to S3 for production usage."""
- local('%(run)s collectstatic --noinput' % env)
-########## END FILE MANAGEMENT
+def stop_supervisord():
+ """
+ Restarts supervisord
+ """
+ config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+ env.run('supervisorctl -c %s shutdown' % config_file)
-########## HEROKU MANAGEMENT
@task
-def bootstrap():
- """Bootstrap your new application with Heroku, preparing it for a production
- deployment. This will:
+def restart_supervisord():
+ """
+ Restarts supervisord
+ """
+ stop_supervisord()
+ start_supervisord()
+
- - Create a new Heroku application.
- - Install all ``HEROKU_ADDONS``.
- - Sync the database.
- - Apply all database migrations.
- - Initialize New Relic's monitoring add-on.
+def supervisorctl(action, process):
"""
- cont('heroku create', "Couldn't create the Heroku app, continue anyway?")
+ Takes as arguments the name of the process as is
+ defined in supervisord.conf and the action that should
+ be performed on it: start|stop|restart.
+ """
+ supervisor_conf = '/var/www/karmaworld/confs/prod/supervisord.conf'
+ env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
- for addon in HEROKU_ADDONS:
- cont('heroku addons:add %s' % addon,
- "Couldn't add %s to your Heroku app, continue anyway?" % addon)
- for config in HEROKU_CONFIGS:
- cont('heroku config:add %s' % config,
- "Couldn't add %s to your Heroku app, continue anyway?" % config)
+@task
+def start_celeryd():
+ """
+ Starts the celeryd process
+ """
+ supervisorctl('start', 'celeryd')
- cont('git push heroku master',
- "Couldn't push your application to Heroku, continue anyway?")
- syncdb()
- migrate()
+@task
+def stop_celeryd():
+ """
+ Stops the celeryd process
+ """
+ supervisorctl('stop', 'celeryd')
- cont('%(run)s newrelic-admin validate-config - stdout' % env,
- "Couldn't initialize New Relic, continue anyway?")
+
+@task
+def restart_celery():
+ """
+ Restarts the celeryd process
+ """
+ supervisorctl('restart', 'celeryd')
@task
-def destroy():
- """Destroy this Heroku application. Wipe it from existance.
+def start_gunicorn():
+ """
+ Starts the gunicorn process
+ """
+ supervisorctl('start', 'gunicorn')
+
- .. note::
- This really will completely destroy your application. Think twice.
+@task
+def stop_gunicorn():
+ """
+ Stops the gunicorn process
"""
- local('heroku apps:destroy')
-########## END HEROKU MANAGEMENT
+ supervisorctl('stop', 'gunicorn')
+
+
+@task
+def restart_gunicorn():
+ """
+ Restarts the gunicorn process
+ """
+ supervisorctl('restart', 'gunicorn')
+
+
+####### Update Requirements
+@task
+def update_reqs():
+ env.run('pip install -r reqs/prod.txt')
+
+####### Pull new code
+@task
+def update_code():
+ env.run('cd %s; git pull' % env.proj_root )
+
+@task
+def backup():
+ """
+ Create backup using bup
+ """
+@task
+def first_deploy():
+
+ """
+ Sets up and deploys the project for the first time.
+ """
+ make_virtualenv()
+ update_reqs()
+ syncdb()
+ manage_static()
+ start_supervisord()
+
+
+@task
+def deploy():
+ """
+ Deploys the latest changes
+ """
+ update_code()
+ update_reqs()
+ syncdb()
+ collect_static()
+ restart_supervisord()
+########## END COMMANDS