Update common.txt
[oweals/karmaworld.git] / fabfile.py
old mode 100755 (executable)
new mode 100644 (file)
index 56d57a8..22fcf52
-"""Management utilities."""
 
-import os
-from contextlib import contextmanager as _contextmanager
+""" Karmaworld Fabric management script
+    Finals Club (c) 2013"""
 
-from fabric.contrib.console import confirm
-from fabric.api import abort, 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
-########## 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)
+######## Define host(s)
+@task
+def here():
+    """
+    Connection information for the local machine
     """
-    with settings(warn_only=True):
-        result = local(cmd, capture=True)
+    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'
 
-    if message and result.failed and not confirm(message):
-        abort('Stopped execution per user request.')
 
 
-@_contextmanager
-def _virtualenv():
+####### Define production host
+@task
+def prod():
     """
-    Changes to the proj_dir and activates the virtualenv
+    Connection Information for production machine
     """
-    with cd(env.proj_dir):
-        with prefix(env.activate):
-            yield
 
-########## END HELPERS
+    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'
 
-########## ENVIRONMENTS
+####### Define beta host
+@task
 def beta():
+    """                                                                                                                                                                 
+    Connection Information for beta machine                                                                                                                       
     """
-    Beta connection information
-    """
+
     env.user = 'djkarma'
     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 = 'beta'
+    env.run = virtenv_exec
 
+######## Run Commands in Virutal Environment
+def virtenv_exec(command):
+        """
+       Execute command in Virtualenv
+       """
 
-def prod():
+        with virtualenv('%s/%s' % (env.proj_root, env.branch)):
+                run('%s' % (command))
+
+######## Sync database
+@task
+def syncdb():
+       """
+       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():
     """
-    Production connection information
+    Starts supervisord
     """
-    env.user = 'djkarma'
-    env.hosts = ['karmanotes.org']
-    env.proj_root = '/var/www/karmaworld'
-    env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
-########## END ENVIRONMENTS
+    config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+    env.run('supervisord -c %s' % config_file)
 
 
-########## DATABASE MANAGEMENT
 @task
-def syncdb():
-    """Run a syncdb."""
-    local('%(run)s syncdb --noinput' % env)
+def stop_supervisord():
+    """
+    Restarts supervisord
+    """
+    config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+    env.run('supervisorctl -c %s shutdown' % 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 restart_supervisord():
+    """
+    Restarts supervisord
+    """
+    stop_supervisord()
+    start_supervisord()
+
 
-    :param str app: Django app name to migrate.
+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.
     """
-    if app:
-        local('%s migrate %s --noinput' % (env.run, app))
-    else:
-        local('%(run)s migrate --noinput' % env)
-########## END DATABASE MANAGEMENT
+    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')
 
 
-########## COMMANDS
+@task
+def stop_celeryd():
+    """
+    Stops the celeryd process
+    """
+    supervisorctl('stop', 'celeryd')
 
-def make_virtualenv():
+
+@task
+def restart_celery():
     """
-    Creates a virtualenv on the remote host
+    Restarts the celeryd process
     """
-    run('mkvirtualenv %s' % env.virtualenv)
+    supervisorctl('restart', 'celeryd')
 
 
-def update_reqs():
+@task
+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')
 
 
-def clone():
+@task
+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