1 """Management utilities."""
4 from contextlib import contextmanager as _contextmanager
6 from fabric.api import cd, env, 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.
19 ########## END GLOBALS
26 Changes to the proj_dir and activates the virtualenv
28 with cd(env.proj_dir):
29 with prefix(env.activate):
32 ########## END HELPERS
34 ########## ENVIRONMENTS
38 Connection information for the local machine
40 env.proj_dir = os.getcwd()
41 env.proj_root = os.path.dirname(env.proj_dir)
43 env.reqs = 'reqs/dev.txt'
44 env.confs = 'confs/dev/'
51 Beta connection information
54 env.hosts = ['beta.karmanotes.org']
55 env.proj_root = '/var/www/karmaworld'
56 env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
57 env.reqs = 'reqs/prod.txt'
58 env.confs = 'confs/beta/'
65 Production connection information
68 env.hosts = ['karmanotes.org']
69 env.proj_root = '/var/www/karmaworld'
70 env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
71 env.reqs = 'reqs/prod.txt'
72 env.confs = 'confs/prod/'
74 ########## END ENVIRONMENTS
77 ########## DATABASE MANAGEMENT
80 """Runs syncdb (along with any pending South migrations)"""
81 env.run('python manage.py syncdb --noinput --migrate')
82 ########## END DATABASE MANAGEMENT
85 ########## FILE MANAGEMENT
89 Collects, compresses and uploads static files.
98 """Collect all static files, and copy them to S3 for production usage."""
99 env.run('python manage.py collectstatic --noinput')
103 def compress_static():
105 Compresses the static files.
113 Uploads the static files to the specified host.
116 ########## END FILE MANAGEMENT
121 def make_virtualenv():
123 Creates a virtualenv on the remote host
125 env.run('mkvirtualenv %s' % env.virtualenv)
131 Makes sure all packages listed in requirements are installed
134 with cd(env.proj_dir):
135 env.run('pip install -r %s' % env.reqs)
141 Clones the project from the central repository
143 env.run('git clone %s %s' % (env.proj_repo, env.proj_dir))
149 Pulls changes from the central repo and checks out the right branch
151 with cd(env.proj_dir):
152 env.run('git pull && git checkout %s' % env.branch)
156 def start_supervisord():
161 config_file = os.path.join(env.confs, 'supervisord.conf')
162 env.run('supervisord -c %s' % config_file)
166 def stop_supervisord():
171 config_file = os.path.join(env.confs, 'supervisord.conf')
172 env.run('supervisorctl -c %s shutdown' % config_file)
176 def restart_supervisord():
184 def supervisorctl(action, process):
186 Takes as arguments the name of the process as is
187 defined in supervisord.conf and the action that should
188 be performed on it: start|stop|restart.
190 supervisor_conf = os.path.join(env.confs, 'supervisord.conf')
191 env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
197 Starts the celeryd process
199 supervisorctl('start', 'celeryd')
205 Stops the celeryd process
207 supervisorctl('stop', 'celeryd')
211 def restart_celery():
213 Restarts the celeryd process
215 supervisorctl('restart', 'celeryd')
219 def start_gunicorn():
221 Starts the gunicorn process
223 supervisorctl('start', 'gunicorn')
229 Stops the gunicorn process
231 supervisorctl('stop', 'gunicorn')
235 def restart_gunicorn():
237 Restarts the gunicorn process
239 supervisorctl('restart', 'gunicorn')
245 Sets up and deploys the project for the first time.
247 # If we're on the local machine, there's no point in cloning
248 # the project, because it's already been cloned. Otherwise
249 # the user couldn't run this file
251 # We're doing this to filter out the hosts that have
252 # already been setup and deployed to
253 with settings(warn_only=True):
254 if env.run('test -d %s' % env.project).failed:
263 # We don't start supervisor on development machines
271 Deploys the latest changes
277 restart_supervisord()
278 ########## END COMMANDS