pulling in nginx config
[oweals/karmaworld.git] / fabfile.py
index e9de037fe4eb583c9af606ca3922f0cd0d75b725..41ac5a9750b45e550474cceb2d5e91cbb9fbecda 100644 (file)
@@ -3,16 +3,21 @@
     Finals Club (c) 2013"""
 
 import os
+import ConfigParser
 
-from fabric.api import cd, env, lcd, prefix, run, task, local, settings
+from fabric.api import cd, env, lcd, prefix, run, sudo, task, local, settings
 from fabric.contrib import files
 
 ######### GLOBAL
+env.user = 'vagrant'
+env.group = 'vagrant'
 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
 env.repo_root = '~/karmaworld'
 env.proj_root = '/var/www/karmaworld'
 env.branch = 'prod'
-env.code_root = '{0}/{1}-code'.format(env.proj_root, env.branch)
+env.code_root = env.proj_root
+env.env_root = env.proj_root
+env.supervisor_conf = '{0}/confs/{1}/supervisord.conf'.format(env.code_root, env.branch)
 
 ######## Define host(s)
 def here():
@@ -62,12 +67,19 @@ def beta():
     env.branch = 'beta'
 
 ######## Run Commands in Virutal Environment
+def virtenv_path():
+    """
+    Builds the virtualenv path.
+    """
+    # not much here now, but maybe useful later.
+    return env.env_root
+
 def virtenv_exec(command):
     """
     Execute command in Virtualenv
     """
 
-    path = os.path.sep.join( (env.proj_root, env.branch) )
+    path = virtenv_path()
     with prefix('source {0}/bin/activate'.format(path)):
         run(command)
 
@@ -87,7 +99,7 @@ def collect_static():
        Collect static files (if AWS config. present, push to S3)
        """
 
-       virtenv_exec('%s/manage.py collectstatic --noinput' % env.proj_root )   
+       virtenv_exec('%s/manage.py collectstatic --noinput' % env.code_root )   
 
 ####### Run Dev Server
 @task
@@ -96,27 +108,31 @@ def dev_server():
        Runs the built-in django webserver
        """
 
-       virtenv_exec('%s/manage.py runserver' % env.proj_root)  
+       virtenv_exec('%s/manage.py runserver' % env.code_root)  
 
 ####### Create Virtual Environment
+
 @task
-def make_virtualenv():
-       """
-       Create our Virtualenv in env.proj_root
-       """
+def link_code():
+    """
+    Link the karmaworld repo into the appropriate production location
+    """
+    if not files.exists(env.code_root):
+        run('ln -s {0} {1}'.format(env.repo_root, env.code_root))
 
-        path = os.path.sep.join( (env.proj_root, env.branch) )
-        if not files.exists(path):
-            run('virtualenv {0}'.format(path))
-        if not files.exists(env.code_root):
-            run('ln -s {0} {1}'.format(env.repo_root, env.code_root))
+@task
+def make_virtualenv():
+    """
+    Create our Virtualenv
+    """
+    run('virtualenv {0}'.format(virtenv_path()))
 
 @task
 def start_supervisord():
     """
     Starts supervisord
     """
-    virtenv_exec('supervisord -c {0}/confs/{1}/supervisord.conf'.format(env.code_root, env.branch))
+    virtenv_exec('supervisord -c {0}'.format(env.supervisor_conf))
 
 
 @task
@@ -124,8 +140,7 @@ def stop_supervisord():
     """
     Restarts supervisord
     """
-    config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
-    virtenv_exec('supervisorctl -c %s shutdown' % config_file)
+    virtenv_exec('supervisorctl -c {0} shutdown'.format(env.supervisor_conf))
 
 
 @task
@@ -143,8 +158,7 @@ def supervisorctl(action, process):
     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'
-    virtenv_exec('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
+    virtenv_exec('supervisorctl -c {0} {1} {2}'.format(env.supervisor_conf, action, process))
 
 
 @task
@@ -211,15 +225,54 @@ def backup():
     """
     pass
 
+@task
+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))
+    for section, option in (('supervisord','logfile'),
+                            ('supervisord','pidfile'),
+                            ('unix_http_server','file'),
+                            ('program:celeryd','stdout_logfile')):
+      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.user, env.group, filepath))
+
+@task
+def check_secrets():
+    """
+    Ensure secret files exist for syncdb to run.
+    """
+
+    secrets_path = env.code_root + '/karmaworld/secret'
+    secrets_files = ('filepicker.py', 'static_s3.py', 'db_settings.py')
+
+    errors = []
+    for sfile in secrets_files:
+        ffile = os.path.sep.join((secrets_path,sfile))
+        if not files.exists(ffile):
+            errors.append('{0} missing. Please add and try again.'.format(ffile))
+    if errors:
+        raise Exception('\n'.join(errors))
+
 @task
 def first_deploy():
     """
     Sets up and deploys the project for the first time.
     """
+    link_code()
     make_virtualenv()
+    file_setup()
+    check_secrets()
     update_reqs()
     syncdb()
-    manage_static()
     start_supervisord()