2 """ Karmaworld Fabric management script
3 Finals Club (c) 2013"""
8 from fabric.api import cd, env, lcd, prefix, run, sudo, task, local, settings
9 from fabric.contrib import files
14 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
15 env.repo_root = '~/karmaworld'
16 env.proj_root = '/var/www/karmaworld'
18 env.code_root = env.proj_root
19 env.env_root = env.proj_root
20 env.supervisor_conf = '{0}/confs/{1}/supervisord.conf'.format(env.code_root, env.branch)
22 ######## Define host(s)
25 Connection information for the local machine
27 def _custom_local(command):
28 prefixed_command = '/bin/bash -l -i -c "%s"' % command
29 return local(prefixed_command)
31 # This is required for the same reason as above
32 env.proj_root = '/var/www/karmaworld'
34 env.reqs = 'reqs/dev.txt'
35 env.confs = 'confs/beta'
40 ####### Define production host
44 Connection Information for production machine
48 env.hosts = ['karmanotes.org']
49 env.proj_root = '/var/www/karmaworld'
50 env.reqs = 'reqs/prod.txt'
51 env.confs = 'confs/prod/'
53 env.gunicorn_addr = '127.0.0.1:8000'
55 ####### Define beta host
59 Connection Information for beta machine
63 env.hosts = ['beta.karmanotes.org']
64 env.proj_root = '/var/www/karmaworld'
65 env.reqs = 'reqs/prod.txt'
66 env.confs = 'confs/prod/'
69 ######## Run Commands in Virutal Environment
72 Builds the virtualenv path.
74 # not much here now, but maybe useful later.
77 def virtenv_exec(command):
79 Execute command in Virtualenv
83 with prefix('source {0}/bin/activate'.format(path)):
86 ######## Sync database
92 virtenv_exec('{0}/manage.py syncdb --migrate'.format(env.code_root))
95 ####### Collect Static Files
99 Collect static files (if AWS config. present, push to S3)
102 virtenv_exec('%s/manage.py collectstatic --noinput' % env.code_root )
104 ####### Run Dev Server
108 Runs the built-in django webserver
111 virtenv_exec('%s/manage.py runserver' % env.code_root)
113 ####### Create Virtual Environment
118 Link the karmaworld repo into the appropriate production location
120 if not files.exists(env.code_root):
121 run('ln -s {0} {1}'.format(env.repo_root, env.code_root))
124 def make_virtualenv():
126 Create our Virtualenv
128 run('virtualenv {0}'.format(virtenv_path()))
131 def start_supervisord():
135 virtenv_exec('supervisord -c {0}'.format(env.supervisor_conf))
139 def stop_supervisord():
143 virtenv_exec('supervisorctl -c {0} shutdown'.format(env.supervisor_conf))
147 def restart_supervisord():
155 def supervisorctl(action, process):
157 Takes as arguments the name of the process as is
158 defined in supervisord.conf and the action that should
159 be performed on it: start|stop|restart.
161 virtenv_exec('supervisorctl -c {0} {1} {2}'.format(env.supervisor_conf, action, process))
167 Starts the celeryd process
169 supervisorctl('start', 'celeryd')
175 Stops the celeryd process
177 supervisorctl('stop', 'celeryd')
181 def restart_celery():
183 Restarts the celeryd process
185 supervisorctl('restart', 'celeryd')
189 def start_gunicorn():
191 Starts the gunicorn process
193 supervisorctl('start', 'gunicorn')
199 Stops the gunicorn process
201 supervisorctl('stop', 'gunicorn')
205 def restart_gunicorn():
207 Restarts the gunicorn process
209 supervisorctl('restart', 'gunicorn')
212 ####### Update Requirements
215 virtenv_exec('pip install -r {0}/reqs/{1}.txt'.format(env.repo_root, env.branch))
217 ####### Pull new code
220 virtenv_exec('cd %s; git pull' % env.proj_root )
224 Create backup using bup
231 Deploy expected files and directories from non-apt system services.
233 ini_parser = ConfigParser.SafeConfigParser()
234 if not ini_parser.read(env.supervisor_conf):
235 raise Exception("Could not parse INI file {0}".format(env.supervisor_conf))
236 for section, option in (('supervisord','logfile'),
237 ('supervisord','pidfile'),
238 ('unix_http_server','file'),
239 ('program:celeryd','stdout_logfile')):
240 filepath = ini_parser.get(section, option)
241 # generate file's directory structure if needed
242 run('mkdir -p {0}'.format(os.path.split(filepath)[0]))
243 # touch a file and change ownership if needed
244 if 'log' in option and not files.exists(filepath):
245 sudo('touch {0}'.format(filepath))
246 sudo('chown {0}:{1} {2}'.format(env.user, env.group, filepath))
251 Ensure secret files exist for syncdb to run.
254 secrets_path = env.code_root + '/karmaworld/secret'
255 secrets_files = ('filepicker.py', 'static_s3.py', 'db_settings.py')
258 for sfile in secrets_files:
259 ffile = os.path.sep.join((secrets_path,sfile))
260 if not files.exists(ffile):
261 errors.append('{0} missing. Please add and try again.'.format(ffile))
263 raise Exception('\n'.join(errors))
268 Sets up and deploys the project for the first time.
282 Deploys the latest changes
288 restart_supervisord()
289 ########## END COMMANDS