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 env.run = _custom_local
27 # This is required for the same reason as above
28 env.proj_root = '/var/www/karmaworld'
30 env.reqs = 'reqs/dev.txt'
31 env.confs = 'confs/beta'
36 ####### Define production host
40 Connection Information for production machine
44 env.hosts = ['karmanotes.org']
45 env.proj_root = '/var/www/karmaworld'
46 env.reqs = 'reqs/prod.txt'
47 env.confs = 'confs/prod/'
49 env.run = virtenv_exec
50 env.gunicorn_addr = '127.0.0.1:8000'
52 ####### Define beta host
56 Connection Information for beta machine
60 env.hosts = ['beta.karmanotes.org']
61 env.proj_root = '/var/www/karmaworld'
62 env.reqs = 'reqs/prod.txt'
63 env.confs = 'confs/prod/'
65 env.run = virtenv_exec
67 ######## Run Commands in Virutal Environment
68 def virtenv_exec(command):
70 Execute command in Virtualenv
73 path = os.path.sep.join( (env.proj_root, env.branch) )
74 with prefix('source {0}/bin/activate'.format(path)):
77 ######## Sync database
83 virtenv_exec('{0}/manage.py syncdb --migrate'.format(env.code_root))
86 ####### Collect Static Files
90 Collect static files (if AWS config. present, push to S3)
93 env.run('%s/manage.py collectstatic --noinput' % env.proj_root )
95 ####### Run Dev Server
99 Runs the built-in django webserver
102 env.run('%s/manage.py runserver' % env.proj_root)
104 ####### Create Virtual Environment
106 def make_virtualenv():
108 Create our Virtualenv in env.proj_root
111 path = os.path.sep.join( (env.proj_root, env.branch) )
112 if not files.exists(path):
113 run('virtualenv {0}'.format(path))
114 if not files.exists(env.code_root):
115 run('ln -s {0} {1}'.format(env.repo_root, env.code_root))
118 def start_supervisord():
122 config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
123 env.run('supervisord -c %s' % config_file)
127 def stop_supervisord():
131 config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
132 env.run('supervisorctl -c %s shutdown' % config_file)
136 def restart_supervisord():
144 def supervisorctl(action, process):
146 Takes as arguments the name of the process as is
147 defined in supervisord.conf and the action that should
148 be performed on it: start|stop|restart.
150 supervisor_conf = '/var/www/karmaworld/confs/prod/supervisord.conf'
151 env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
157 Starts the celeryd process
159 supervisorctl('start', 'celeryd')
165 Stops the celeryd process
167 supervisorctl('stop', 'celeryd')
171 def restart_celery():
173 Restarts the celeryd process
175 supervisorctl('restart', 'celeryd')
179 def start_gunicorn():
181 Starts the gunicorn process
183 supervisorctl('start', 'gunicorn')
189 Stops the gunicorn process
191 supervisorctl('stop', 'gunicorn')
195 def restart_gunicorn():
197 Restarts the gunicorn process
199 supervisorctl('restart', 'gunicorn')
202 ####### Update Requirements
205 virtenv_exec('pip install -r {0}/reqs/{1}.txt'.format(env.repo_root, env.branch))
207 ####### Pull new code
210 virtenv_exec('cd %s; git pull' % env.proj_root )
214 Create backup using bup
221 Sets up and deploys the project for the first time.
233 Deploys the latest changes
239 restart_supervisord()
240 ########## END COMMANDS