Removed Install section from README.md and pointed to docs/source/os-install.rst
[oweals/karmaworld.git] / fabfile.py
old mode 100755 (executable)
new mode 100644 (file)
index da1e5e1..22fcf52
-"""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/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/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'
-########## END ENVIRONMENTS
+    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():
-    """Runs syncdb (along with any pending South migrations)"""
-    env.run('python manage.py syncdb --noinput --migrate')
-########## END DATABASE MANAGEMENT
+       """
+       Sync Database
+       """
+
+       env.run('%s/manage.py syncdb --migrate' % env.proj_root )
 
 
-########## FILE MANAGEMENT
+####### Collect Static Files
 @task
-def collectstatic():
-    """Collect all static files, and copy them to S3 for production usage."""
-    env.run('python manage.py collectstatic --noinput')
-########## END FILE MANAGEMENT
+def collect_static():
+       """
+       Collect static files (if AWS config. present, push to S3)
+       """
 
+       env.run('%s/manage.py collectstatic --noinput' % env.proj_root )        
 
-########## COMMANDS
+####### 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():
     """
-    Creates a virtualenv on the remote host
+    Starts supervisord
     """
-    env.run('mkvirtualenv %s' % env.virtualenv)
+    config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
+    env.run('supervisord -c %s' % config_file)
 
 
 @task
-def update_reqs():
+def stop_supervisord():
     """
-    Makes sure all packages listed in requirements are installed
+    Restarts 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('supervisorctl -c %s shutdown' % config_file)
 
 
 @task
-def clone():
+def restart_supervisord():
+    """
+    Restarts supervisord
+    """
+    stop_supervisord()
+    start_supervisord()
+
+
+def supervisorctl(action, process):
     """
-    Clones the project from the central repository
+    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.
     """
-    env.run('git clone %s %s' % (env.proj_repo, env.proj_dir))
+    supervisor_conf = '/var/www/karmaworld/confs/prod/supervisord.conf'
+    env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
 
 
+@task
+def start_celeryd():
+    """
+    Starts the celeryd process
+    """
+    supervisorctl('start', 'celeryd')
+
+
+@task
+def stop_celeryd():
+    """
+    Stops the celeryd process
+    """
+    supervisorctl('stop', 'celeryd')
+
+
+@task
+def restart_celery():
+    """
+    Restarts the celeryd process
+    """
+    supervisorctl('restart', 'celeryd')
+
+
+@task
+def start_gunicorn():
+    """
+    Starts the gunicorn process
+    """
+    supervisorctl('start', 'gunicorn')
+
+
+@task
+def stop_gunicorn():
+    """
+    Stops the gunicorn process
+    """
+    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():
     """
-    Pulls the latest changes from the central repository
+    Create backup using bup
+    """
+@task
+def first_deploy():
+    
     """
-    with cd(env.proj_dir):
-        env.run('git pull')
+    Sets up and deploys the project for the first time.
+    """
+    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 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()
-
     update_code()
     update_reqs()
     syncdb()
-    #TODO: run gunicorn
-    #restart_uwsgi()
+    collect_static()
+    restart_supervisord()
 ########## END COMMANDS