1 """Management utilities."""
4 from contextlib import contextmanager as _contextmanager
6 from fabric.api import cd, env, lcd, prefix, run, task, local, settings
10 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
11 env.virtualenv = 'venv-kw'
12 env.activate = 'workon %s' % env.virtualenv
14 # Using this env var to be able to specify the function
15 # used to run the commands. By default it's `run`, which
16 # runs commands remotely, but in the `here` task we set
17 # env.run to `local` to run commands locally.
20 ########## END GLOBALS
27 Changes to the proj_dir and activates the virtualenv
29 with env.cd(env.proj_dir):
30 with prefix(env.activate):
33 ########## END HELPERS
35 ########## ENVIRONMENTS
39 Connection information for the local machine
41 # This is required, because local doesn't read the user's
42 # .bashrc file, because it doesn't use an interactive shell
43 def _custom_local(command):
44 prefixed_command = '/bin/bash -l -i -c "%s"' % command
45 return local(prefixed_command)
47 # This is required for the same reason as above
48 env.activate = '/bin/bash -l -i -c "workon %s"' % env.virtualenv
49 env.proj_dir = os.getcwd()
50 env.proj_root = os.path.dirname(env.proj_dir)
51 env.run = _custom_local
53 env.reqs = 'reqs/dev.txt'
54 env.confs = 'confs/dev/'
61 Beta connection information
64 env.hosts = ['beta.karmanotes.org']
65 env.proj_root = '/var/www/karmaworld'
66 env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
67 env.reqs = 'reqs/prod.txt'
68 env.confs = 'confs/beta/'
75 Production connection information
78 env.hosts = ['karmanotes.org']
79 env.proj_root = '/var/www/karmaworld'
80 env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
81 env.reqs = 'reqs/prod.txt'
82 env.confs = 'confs/prod/'
84 ########## END ENVIRONMENTS
87 ########## DATABASE MANAGEMENT
90 """Runs syncdb (along with any pending South migrations)"""
91 env.run('python manage.py syncdb --noinput --migrate')
92 ########## END DATABASE MANAGEMENT
95 ########## FILE MANAGEMENT
99 Collects, compresses and uploads static files.
107 def collect_static():
108 """Collect all static files, and copy them to S3 for production usage."""
109 env.run('python manage.py collectstatic --noinput')
113 def compress_static():
115 Compresses the static files.
123 Uploads the static files to the specified host.
126 ########## END FILE MANAGEMENT
131 def make_virtualenv():
133 Creates a virtualenv on the remote host
135 env.run('mkvirtualenv --no-site-packages %s' % env.virtualenv)
141 Makes sure all packages listed in requirements are installed
144 env.run('pip install -r %s' % env.reqs)
150 Clones the project from the central repository
152 env.run('git clone %s %s' % (env.proj_repo, env.proj_dir))
158 Pulls changes from the central repo and checks out the right branch
160 with env.cd(env.proj_dir):
161 env.run('git pull && git checkout %s' % env.branch)
165 def start_supervisord():
170 config_file = os.path.join(env.confs, 'supervisord.conf')
171 env.run('supervisord -c %s' % config_file)
175 def stop_supervisord():
180 config_file = os.path.join(env.confs, 'supervisord.conf')
181 env.run('supervisorctl -c %s shutdown' % config_file)
185 def restart_supervisord():
193 def supervisorctl(action, process):
195 Takes as arguments the name of the process as is
196 defined in supervisord.conf and the action that should
197 be performed on it: start|stop|restart.
199 supervisor_conf = os.path.join(env.confs, 'supervisord.conf')
200 env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
206 Starts the celeryd process
208 supervisorctl('start', 'celeryd')
214 Stops the celeryd process
216 supervisorctl('stop', 'celeryd')
220 def restart_celery():
222 Restarts the celeryd process
224 supervisorctl('restart', 'celeryd')
228 def start_gunicorn():
230 Starts the gunicorn process
232 supervisorctl('start', 'gunicorn')
238 Stops the gunicorn process
240 supervisorctl('stop', 'gunicorn')
244 def restart_gunicorn():
246 Restarts the gunicorn process
248 supervisorctl('restart', 'gunicorn')
254 Sets up and deploys the project for the first time.
256 # If we're on the local machine, there's no point in cloning
257 # the project, because it's already been cloned. Otherwise
258 # the user couldn't run this file
260 # We're doing this to filter out the hosts that have
261 # already been setup and deployed to
262 with settings(warn_only=True):
263 if env.run('test -d %s' % env.project).failed:
271 # We don't collect the static files and start supervisor on
272 # development machines
281 Deploys the latest changes
287 restart_supervisord()
288 ########## END COMMANDS