-"""Management utilities."""
-import os
-from contextlib import contextmanager as _contextmanager
+""" Karmaworld Fabric management script
+ Finals Club (c) 2013"""
-from fabric.api import cd, env, local, prefix, run, settings, task
+import os
+from fabric.api import cd, env, lcd, prefix, run, task, local, settings
+from fabvenv import virtualenv
-########## GLOBALS
+######### GLOBAL
env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
-env.virtualenv = 'venv-kw'
-env.activate = 'workon %s' % env.virtualenv
-env.run = './manage.py'
-########## END GLOBALS
-########## HELPERS
-@_contextmanager
-def _virtualenv():
+######## Define host(s)
+@task
+def here():
"""
- Changes to the proj_dir and activates the virtualenv
+ Connection information for the local machine
"""
- with cd(env.proj_dir):
- with prefix(env.activate):
- yield
+ def _custom_local(command):
+ prefixed_command = '/bin/bash -l -i -c "%s"' % command
+ return local(prefixed_command)
+
+ 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'
-########## END HELPERS
-########## ENVIRONMENTS
+
+####### Define production host
@task
-def beta():
+def prod():
"""
- Beta connection information
+ Connection Information for production machine
"""
+
env.user = 'djkarma'
- env.hosts = ['beta.karmanotes.org']
+ env.hosts = ['karmanotes.org']
env.proj_root = '/var/www/karmaworld'
- env.proj_dir = os.path.join(env.proj_root, '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 prod():
- """
- Production connection information
+def beta():
+ """
+ Connection Information for beta machine
"""
+
env.user = 'djkarma'
- env.hosts = ['karmanotes.org']
+ env.hosts = ['beta.karmanotes.org']
env.proj_root = '/var/www/karmaworld'
- env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
-########## END ENVIRONMENTS
+ 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
+ """
+ with virtualenv('%s/%s' % (env.proj_root, env.branch)):
+ run('%s' % (command))
-########## DATABASE MANAGEMENT
+######## 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 make_virtualenv():
+ """
+ Create our Virtualenv in $SRC_ROOT
+ """
+
+ 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():
+ """
+ Starts supervisord
+ """
+ config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+ env.run('supervisord -c %s' % config_file)
@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 stop_supervisord():
+ """
+ Restarts supervisord
+ """
+ config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+ env.run('supervisorctl -c %s shutdown' % config_file)
- :param str app: Django app name to migrate.
+
+@task
+def restart_supervisord():
"""
- if app:
- local('%s migrate %s --noinput' % (env.run, app))
- else:
- local('%(run)s migrate --noinput' % env)
-########## END DATABASE MANAGEMENT
+ Restarts supervisord
+ """
+ stop_supervisord()
+ start_supervisord()
+
+
+def supervisorctl(action, process):
+ """
+ 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))
-########## 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 start_celeryd():
+ """
+ Starts the celeryd process
+ """
+ supervisorctl('start', 'celeryd')
+
+@task
+def stop_celeryd():
+ """
+ Stops the celeryd process
+ """
+ supervisorctl('stop', 'celeryd')
-########## COMMANDS
@task
-def make_virtualenv():
+def restart_celery():
"""
- Creates a virtualenv on the remote host
+ Restarts the celeryd process
"""
- run('mkvirtualenv %s' % env.virtualenv)
+ supervisorctl('restart', 'celeryd')
@task
-def update_reqs():
+def start_gunicorn():
"""
- Makes sure all packages listed in requirements are installed
+ Starts the gunicorn process
"""
- with _virtualenv():
- with cd(env.proj_dir):
- run('pip install -r requirements/production.pip')
+ supervisorctl('start', 'gunicorn')
@task
-def clone():
+def stop_gunicorn():
"""
- Clones the project from the central repository
+ Stops the gunicorn process
"""
- run('git clone %s %s' % (env.proj_repo, env.proj_dir))
+ 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():
+
"""
- Pulls the latest changes from the central repository
+ Sets up and deploys the project for the first time.
"""
- with cd(env.proj_dir):
- run('git pull')
+ make_virtualenv()
+ update_reqs()
+ syncdb()
+ manage_static()
+ start_supervisord()
@task
def deploy():
"""
- Creates or updates the project, runs migrations, installs dependencies.
+ Deploys the latest changes
"""
- first_deploy = False
- with settings(warn_only=True):
- if run('test -d %s' % env.proj_dir).failed:
- # first_deploy var is for initial deploy information
- first_deploy = True
- clone()
- if run('test -d $WORKON_HOME/%s' % env.virtualenv).failed:
- make_virtualenv()
-
update_code()
update_reqs()
syncdb()
- #TODO: run gunicorn
- #restart_uwsgi()
+ collect_static()
+ restart_supervisord()
########## END COMMANDS