2 """ Karmaworld Fabric management script
3 Finals Club (c) 2013"""
7 from fabric.api import cd, env, lcd, prefix, run, task, local, settings
8 from fabvenv import virtualenv
11 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
14 ######## Define host(s)
18 Connection information for the local machine
20 def _custom_local(command):
21 prefixed_command = '/bin/bash -l -i -c "%s"' % command
22 return local(prefixed_command)
24 env.run = _custom_local
25 # This is required for the same reason as above
26 env.proj_root = '/var/www/karmaworld'
28 env.reqs = 'reqs/dev.txt'
29 env.confs = 'confs/beta'
34 ####### Define production host
38 Connection Information for production machine
42 env.hosts = ['karmanotes.org']
43 env.proj_root = '/var/www/karmaworld'
44 env.reqs = 'reqs/prod.txt'
45 env.confs = 'confs/prod/'
47 env.run = virtenv_exec
48 env.gunicorn_addr = '127.0.0.1:8000'
50 ####### Define beta host
54 Connection Information for beta machine
58 env.hosts = ['beta.karmanotes.org']
59 env.proj_root = '/var/www/karmaworld'
60 env.reqs = 'reqs/prod.txt'
61 env.confs = 'confs/prod/'
63 env.run = virtenv_exec
65 ######## Run Commands in Virutal Environment
66 def virtenv_exec(command):
68 Execute command in Virtualenv
71 with virtualenv('%s/%s' % (env.proj_root, env.branch)):
74 ######## Sync database
81 env.run('%s/manage.py syncdb --migrate' % env.proj_root )
84 ####### Collect Static Files
88 Collect static files (if AWS config. present, push to S3)
91 env.run('%s/manage.py collectstatic --noinput' % env.proj_root )
93 ####### Run Dev Server
97 Runs the built-in django webserver
100 env.run('%s/manage.py runserver' % env.proj_root)
102 ####### Create Virtual Environment
104 def make_virtualenv():
106 Create our Virtualenv in $SRC_ROOT
109 run('virtualenv %s/%s' % (env.proj_root, env.branch))
110 env.run('pip install -r %s/reqs/%s.txt' % (env.proj_root, env.branch) )
113 def start_supervisord():
117 config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
118 env.run('supervisord -c %s' % config_file)
122 def stop_supervisord():
126 config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
127 env.run('supervisorctl -c %s shutdown' % config_file)
131 def restart_supervisord():
139 def supervisorctl(action, process):
141 Takes as arguments the name of the process as is
142 defined in supervisord.conf and the action that should
143 be performed on it: start|stop|restart.
145 supervisor_conf = '/var/www/karmaworld/confs/prod/supervisord.conf'
146 env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
152 Starts the celeryd process
154 supervisorctl('start', 'celeryd')
160 Stops the celeryd process
162 supervisorctl('stop', 'celeryd')
166 def restart_celery():
168 Restarts the celeryd process
170 supervisorctl('restart', 'celeryd')
174 def start_gunicorn():
176 Starts the gunicorn process
178 supervisorctl('start', 'gunicorn')
184 Stops the gunicorn process
186 supervisorctl('stop', 'gunicorn')
190 def restart_gunicorn():
192 Restarts the gunicorn process
194 supervisorctl('restart', 'gunicorn')
197 ####### Update Requirements
200 env.run('pip install -r reqs/prod.txt')
202 ####### Pull new code
205 env.run('cd %s; git pull' % env.proj_root )
210 Create backup using bup
216 Sets up and deploys the project for the first time.
228 Deploys the latest changes
234 restart_supervisord()
235 ########## END COMMANDS