-"""Management utilities."""
-import os
-from contextlib import contextmanager as _contextmanager
+""" Karmaworld Fabric management script
+ Finals Club (c) 2013"""
-from fabric.api import cd, env, prefix, run, settings, task, local
+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
-
-# Using this env var to be able to specify the function
-# used to run the commands. By default it's `run`, which
-# runs commands remotely, but in the `here` task we set
-# env.run to `local` to run commands locally.
-env.run = run
-########## END GLOBALS
-
-########## HELPERS
-@_contextmanager
-def _virtualenv():
- """
- Changes to the proj_dir and activates the virtualenv
- """
- with cd(env.proj_dir):
- with prefix(env.activate):
- yield
-########## END HELPERS
-
-########## ENVIRONMENTS
+######## Define host(s)
@task
def here():
"""
Connection information for the local machine
"""
- env.proj_dir = os.getcwd()
- env.proj_root = os.path.dirname(env.proj_dir)
- env.run = local
+ 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/dev/'
- env.branch = 'master'
+ env.confs = 'confs/beta'
+ env.branch = 'beta'
+
+####### 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/beta/'
+ 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')
env.reqs = 'reqs/prod.txt'
env.confs = 'confs/prod/'
- env.branch = 'master'
-########## END ENVIRONMENTS
+ 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():
- """Runs syncdb (along with any pending South migrations)"""
- env.run('python manage.py syncdb --noinput --migrate')
-########## END DATABASE MANAGEMENT
+ """
+ Sync Database
+ """
-
-########## FILE MANAGEMENT
-@task
-def manage_static():
- collect_static()
- compress_static()
- upload_static()
+ env.run('%s/manage.py syncdb --migrate' % env.proj_root )
+####### Collect Static Files
@task
def collect_static():
- """Collect all static files, and copy them to S3 for production usage."""
- env.run('python manage.py collectstatic --noinput')
+ """
+ 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 compress_static():
- """
- Compresses the static files.
- """
- pass
-
-
-@task
-def upload_static():
- """
- Uploads the static files to the specified host.
- """
- pass
-########## END FILE MANAGEMENT
+def dev_server():
+ """
+ Runs the built-in django webserver
+ """
+ env.run('%s/manage.py runserver' % env.proj_root)
-########## COMMANDS
+####### Create Virtual Environment
@task
def make_virtualenv():
- """
- Creates a virtualenv on the remote host
- """
- env.run('mkvirtualenv %s' % env.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 update_reqs():
+def start_supervisord():
"""
- Makes sure all packages listed in requirements are installed
+ Starts supervisord
"""
- with _virtualenv():
- with cd(env.proj_dir):
- env.run('pip install -r %s' % env.reqs)
+ config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+ env.run('supervisord -c %s' % config_file)
@task
-def clone():
+def stop_supervisord():
"""
- Clones the project from the central repository
+ Restarts supervisord
"""
- env.run('git clone %s %s' % (env.proj_repo, env.proj_dir))
+ config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+ env.run('supervisorctl -c %s shutdown' % config_file)
@task
-def update_code():
+def restart_supervisord():
"""
- Pulls changes from the central repo and checks out the right branch
+ Restarts supervisord
"""
- with cd(env.proj_dir):
- env.run('git pull && git checkout %s' % env.branch)
+ stop_supervisord()
+ start_supervisord()
-def manage_supervisor_proc(proc, action):
+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 = os.path.join(env.confs, 'supervisord.conf')
- env.run('supervisorctl -c %s %s %s' % (supervisor_conf, proc, action))
+ supervisor_conf = '/var/www/karmaworld/confs/prod/supervisord.conf'
+ env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
@task
"""
Starts the celeryd process
"""
- manage_supervisor_proc('celeryd', 'start')
+ supervisorctl('start', 'celeryd')
@task
"""
Stops the celeryd process
"""
- manage_supervisor_proc('celeryd', 'stop')
+ supervisorctl('stop', 'celeryd')
@task
"""
Restarts the celeryd process
"""
- manage_supervisor_proc('celeryd', 'restart')
+ supervisorctl('restart', 'celeryd')
@task
"""
Starts the gunicorn process
"""
- manage_supervisor_proc('gunicorn', 'start')
+ supervisorctl('start', 'gunicorn')
@task
"""
Stops the gunicorn process
"""
- manage_supervisor_proc('gunicorn', 'stop')
+ supervisorctl('stop', 'gunicorn')
@task
"""
Restarts the gunicorn process
"""
- manage_supervisor_proc('gunicorn', 'restart')
+ supervisorctl('restart', 'gunicorn')
+####### Update Requirements
@task
-def deploy():
+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():
"""
- Creates or updates the project, runs migrations, installs dependencies.
+ Create backup using bup
"""
- first_deploy = False
- with settings(warn_only=True):
- if env.run('test -d %s' % env.proj_dir).failed:
- # first_deploy var is for initial deploy information
- first_deploy = True
- clone()
- if env.run('test -d $WORKON_HOME/%s' % env.virtualenv).failed:
- make_virtualenv()
+@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()
- #TODO: run gunicorn
- #restart_uwsgi()
+ collect_static()
+ restart_supervisord()
########## END COMMANDS