We need dj-static
[oweals/karmaworld.git] / fabfile.py
index e5999760cd4c79399e3065f85e2f2fe818d2d06c..1f0320efaab00053f870e79fae8c30d0ea0e3ff6 100644 (file)
@@ -1,24 +1,34 @@
-
 """ Karmaworld Fabric management script
     Finals Club (c) 2013"""
 
 import os
 import ConfigParser
+from cStringIO import StringIO
 
-from fabric.api import cd, env, lcd, prefix, run, sudo, task, local, settings
+from fabric.api import cd, lcd, prefix, run, sudo, task, local, settings
+from fabric.state import env as fabenv
 from fabric.contrib import files
 
-######### GLOBAL
+from dicthelpers import fallbackdict
+
+# Use local SSH config for connections if available.
+fabenv['use_ssh_config'] = True
+
+######## env wrapper
+# global environment variables fallback to fabric env variables
+# (also getting vars will do format mapping on strings with env vars)
+env = fallbackdict(fabenv)
+
+######### GLOBALS
+env.django_user = '{user}' # this will be different when sudo/django users are
 env.group = 'www-data'
 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
-env.repo_root = '~/karmaworld' # transient setting for VMs only
+env.repo_root = '/home/{django_user}/karmaworld'
 env.proj_root = '/var/www/karmaworld'
 env.branch = 'prod' # only used for supervisor conf two lines below. cleanup?
 env.code_root = env.proj_root
-env.supervisor_conf = '{0}/confs/{1}/supervisord.conf'.format(env.code_root, env.branch)
-env.usde_csv = '{0}/confs/acceditation.csv'.format(env.code_root)
-
-env.use_ssh_config = True
+env.supervisor_conf = '{code_root}/confs/{branch}/supervisord.conf'
+env.usde_csv = '{code_root}/confs/accreditation.csv'
 
 ######## Run Commands in Virtual Environment
 def virtenv_path():
@@ -63,7 +73,7 @@ def syncdb():
     """
     Sync Database
     """
-    virtenv_exec('{0}/manage.py syncdb --migrate'.format(env.code_root))
+    virtenv_exec('{0}/manage.py syncdb --migrate --noinput'.format(env.code_root))
 
 
 ####### Collect Static Files
@@ -75,6 +85,16 @@ def collect_static():
 
        virtenv_exec('{0}/manage.py collectstatic --noinput'.format(env.code_root))
 
+####### Compress Static Files
+@task
+def compress_static():
+       """
+       Compress static files
+       """
+
+       virtenv_exec('{0}/manage.py compress'.format(env.code_root))
+
+
 ####### Run Dev Server
 @task
 def dev_server():
@@ -181,12 +201,26 @@ def restart_gunicorn():
     """
     supervisorctl('restart', 'gunicorn')
 
+@task
+def flush_memcache():
+    """
+    Clear everything cached in memcached
+    """
+    virtenv_exec('echo "flush_all" | nc localhost 11211')
 
 ####### Update Requirements
 @task
-def update_reqs():
+def install_reqs():
+    # first install must be done without --upgrade for a few packages that break
+    # due to a pip problem.
     virtenv_exec('pip install -r {0}/reqs/prod.txt'.format(env.code_root))
 
+@task
+def update_reqs():
+    # this should generally work to install reqs too, save for a pip problem
+    # with a few packages.
+    virtenv_exec('pip install --upgrade -r {0}/reqs/prod.txt'.format(env.code_root))
+
 ####### Pull new code
 @task
 def update_code():
@@ -204,19 +238,22 @@ def file_setup():
     Deploy expected files and directories from non-apt system services.
     """
     ini_parser = ConfigParser.SafeConfigParser()
-    if not ini_parser.read(env.supervisor_conf):
-      raise Exception("Could not parse INI file {0}".format(env.supervisor_conf))
+    # read remote data into a file like object
+    data_flo = StringIO(run('cat {supervisor_conf}'.format(**env)))
+    ini_parser.readfp(data_flo)
     for section, option in (('supervisord','logfile'),
                             ('supervisord','pidfile'),
                             ('unix_http_server','file'),
                             ('program:celeryd','stdout_logfile')):
+      if not ini_parser.has_section(section):
+          raise Exception("Could not parse INI file {supervisor_conf}".format(**env))
       filepath = ini_parser.get(section, option)
       # generate file's directory structure if needed
       run('mkdir -p {0}'.format(os.path.split(filepath)[0]))
       # touch a file and change ownership if needed
       if 'log' in option and not files.exists(filepath):
           sudo('touch {0}'.format(filepath))
-          sudo('chown {0}:{1} {2}'.format(env.local_user, env.group, filepath))
+          sudo('chown {0}:{1} {2}'.format(env.django_user, env.group, filepath))
 
 @task
 def check_secrets():
@@ -225,7 +262,7 @@ def check_secrets():
     """
 
     secrets_path = env.code_root + '/karmaworld/secret'
-    secrets_files = ('filepicker.py', 'static_s3.py', 'db_settings.py', 'drive.py', 'client_secrets.json', 'drive.p12')
+    secrets_files = ('filepicker.py', 'static_s3.py', 'drive.py', 'client_secrets.json', 'drive.p12')
 
     errors = []
     for sfile in secrets_files:
@@ -259,12 +296,14 @@ def first_deploy():
     make_virtualenv()
     file_setup()
     check_secrets()
-    update_reqs()
+    install_reqs()
     syncdb()
+    compress_static()
     collect_static()
     fetch_usde()
     import_usde()
     start_supervisord()
+    print "You should run `manage.py createsuperuser` in the virtual environment"
 
 
 @task
@@ -275,6 +314,7 @@ def deploy():
     update_code()
     update_reqs()
     syncdb()
+    compress_static()
     collect_static()
     restart_supervisord()
 ########## END COMMANDS