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 fabric.contrib import files
11 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
12 env.repo_root = '~/karmaworld'
13 env.proj_root = '/var/www/karmaworld'
15 env.code_root = '{0}/{1}-code'.format(env.proj_root, env.branch)
17 ######## Define host(s)
20 Connection information for the local machine
22 def _custom_local(command):
23 prefixed_command = '/bin/bash -l -i -c "%s"' % command
24 return local(prefixed_command)
26 # This is required for the same reason as above
27 env.proj_root = '/var/www/karmaworld'
29 env.reqs = 'reqs/dev.txt'
30 env.confs = 'confs/beta'
35 ####### Define production host
39 Connection Information for production machine
43 env.hosts = ['karmanotes.org']
44 env.proj_root = '/var/www/karmaworld'
45 env.reqs = 'reqs/prod.txt'
46 env.confs = 'confs/prod/'
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/'
64 ######## Run Commands in Virutal Environment
65 def virtenv_exec(command):
67 Execute command in Virtualenv
70 path = os.path.sep.join( (env.proj_root, env.branch) )
71 with prefix('source {0}/bin/activate'.format(path)):
74 ######## Sync database
80 virtenv_exec('{0}/manage.py syncdb --migrate'.format(env.code_root))
83 ####### Collect Static Files
87 Collect static files (if AWS config. present, push to S3)
90 virtenv_exec('%s/manage.py collectstatic --noinput' % env.proj_root )
92 ####### Run Dev Server
96 Runs the built-in django webserver
99 virtenv_exec('%s/manage.py runserver' % env.proj_root)
101 ####### Create Virtual Environment
103 def make_virtualenv():
105 Create our Virtualenv in env.proj_root
108 path = os.path.sep.join( (env.proj_root, env.branch) )
109 if not files.exists(path):
110 run('virtualenv {0}'.format(path))
111 if not files.exists(env.code_root):
112 run('ln -s {0} {1}'.format(env.repo_root, env.code_root))
115 def start_supervisord():
119 virtenv_exec('supervisord -c {0}/confs/{1}/supervisord.conf'.format(env.code_root, env.branch))
123 def stop_supervisord():
127 config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
128 virtenv_exec('supervisorctl -c %s shutdown' % config_file)
132 def restart_supervisord():
140 def supervisorctl(action, process):
142 Takes as arguments the name of the process as is
143 defined in supervisord.conf and the action that should
144 be performed on it: start|stop|restart.
146 supervisor_conf = '/var/www/karmaworld/confs/prod/supervisord.conf'
147 virtenv_exec('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
153 Starts the celeryd process
155 supervisorctl('start', 'celeryd')
161 Stops the celeryd process
163 supervisorctl('stop', 'celeryd')
167 def restart_celery():
169 Restarts the celeryd process
171 supervisorctl('restart', 'celeryd')
175 def start_gunicorn():
177 Starts the gunicorn process
179 supervisorctl('start', 'gunicorn')
185 Stops the gunicorn process
187 supervisorctl('stop', 'gunicorn')
191 def restart_gunicorn():
193 Restarts the gunicorn process
195 supervisorctl('restart', 'gunicorn')
198 ####### Update Requirements
201 virtenv_exec('pip install -r {0}/reqs/{1}.txt'.format(env.repo_root, env.branch))
203 ####### Pull new code
206 virtenv_exec('cd %s; git pull' % env.proj_root )
210 Create backup using bup
217 Sets up and deploys the project for the first time.
229 Deploys the latest changes
235 restart_supervisord()
236 ########## END COMMANDS