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)
21 env.usde_csv = '{0}/confs/acceditation.csv'.format(env.code_root)
23 env.use_ssh_config = True
25 ######## Define host(s)
28 Connection information for the local machine
30 def _custom_local(command):
31 prefixed_command = '/bin/bash -l -i -c "%s"' % command
32 return local(prefixed_command)
34 # This is required for the same reason as above
35 env.proj_root = '/var/www/karmaworld'
37 env.reqs = 'reqs/dev.txt'
38 env.confs = 'confs/beta'
43 ####### Define production host
47 Connection Information for production machine
51 env.hosts = ['karmanotes.org']
52 env.proj_root = '/var/www/karmaworld'
53 env.reqs = 'reqs/prod.txt'
54 env.confs = 'confs/prod/'
56 env.gunicorn_addr = '127.0.0.1:8000'
58 ####### Define beta host
62 Connection Information for beta machine
66 env.hosts = ['beta.karmanotes.org']
67 env.proj_root = '/var/www/karmaworld'
68 env.reqs = 'reqs/prod.txt'
69 env.confs = 'confs/prod/'
72 ######## Run Commands in Virutal Environment
75 Builds the virtualenv path.
77 # not much here now, but maybe useful later.
80 def virtenv_exec(command):
82 Execute command in Virtualenv
86 with prefix('source {0}/bin/activate'.format(path)):
89 ######## Sync database
95 virtenv_exec('{0}/manage.py syncdb --migrate'.format(env.code_root))
98 ####### Collect Static Files
100 def collect_static():
102 Collect static files (if AWS config. present, push to S3)
105 virtenv_exec('%s/manage.py collectstatic --noinput' % env.code_root )
107 ####### Run Dev Server
111 Runs the built-in django webserver
114 virtenv_exec('%s/manage.py runserver' % env.code_root)
116 ####### Create Virtual Environment
121 Link the karmaworld repo into the appropriate production location
123 if not files.exists(env.code_root):
124 run('ln -s {0} {1}'.format(env.repo_root, env.code_root))
127 def make_virtualenv():
129 Create our Virtualenv
131 run('virtualenv {0}'.format(virtenv_path()))
134 def start_supervisord():
138 virtenv_exec('supervisord -c {0}'.format(env.supervisor_conf))
142 def stop_supervisord():
146 virtenv_exec('supervisorctl -c {0} shutdown'.format(env.supervisor_conf))
150 def restart_supervisord():
158 def supervisorctl(action, process):
160 Takes as arguments the name of the process as is
161 defined in supervisord.conf and the action that should
162 be performed on it: start|stop|restart.
164 virtenv_exec('supervisorctl -c {0} {1} {2}'.format(env.supervisor_conf, action, process))
170 Starts the celeryd process
172 supervisorctl('start', 'celeryd')
178 Stops the celeryd process
180 supervisorctl('stop', 'celeryd')
184 def restart_celery():
186 Restarts the celeryd process
188 supervisorctl('restart', 'celeryd')
192 def start_gunicorn():
194 Starts the gunicorn process
196 supervisorctl('start', 'gunicorn')
202 Stops the gunicorn process
204 supervisorctl('stop', 'gunicorn')
208 def restart_gunicorn():
210 Restarts the gunicorn process
212 supervisorctl('restart', 'gunicorn')
215 ####### Update Requirements
218 virtenv_exec('pip install -r {0}/reqs/{1}.txt'.format(env.repo_root, env.branch))
220 ####### Pull new code
223 virtenv_exec('cd %s; git pull' % env.proj_root )
227 Create backup using bup
234 Deploy expected files and directories from non-apt system services.
236 ini_parser = ConfigParser.SafeConfigParser()
237 if not ini_parser.read(env.supervisor_conf):
238 raise Exception("Could not parse INI file {0}".format(env.supervisor_conf))
239 for section, option in (('supervisord','logfile'),
240 ('supervisord','pidfile'),
241 ('unix_http_server','file'),
242 ('program:celeryd','stdout_logfile')):
243 filepath = ini_parser.get(section, option)
244 # generate file's directory structure if needed
245 run('mkdir -p {0}'.format(os.path.split(filepath)[0]))
246 # touch a file and change ownership if needed
247 if 'log' in option and not files.exists(filepath):
248 sudo('touch {0}'.format(filepath))
249 sudo('chown {0}:{1} {2}'.format(env.user, env.group, filepath))
254 Ensure secret files exist for syncdb to run.
257 secrets_path = env.code_root + '/karmaworld/secret'
258 secrets_files = ('filepicker.py', 'static_s3.py', 'db_settings.py', 'drive.py', 'client_secrets.json', 'drive.p12')
261 for sfile in secrets_files:
262 ffile = os.path.sep.join((secrets_path,sfile))
263 if not files.exists(ffile):
264 errors.append('{0} missing. Please add and try again.'.format(ffile))
266 raise Exception('\n'.join(errors))
271 Download USDE accreditation school CSV.
273 virtenv_exec('{0}/manage.py fetch_usde_csv {1}'.format(env.code_root, env.usde_csv))
278 Import accreditation school CSV into the database and scrub it.
280 virtenv_exec('{0}/manage.py import_usde_csv {1}'.format(env.code_root, env.usde_csv))
281 virtenv_exec('{0}/manage.py sanitize_usde_schools'.format(env.code_root))
286 Sets up and deploys the project for the first time.
303 Deploys the latest changes
309 restart_supervisord()
310 ########## END COMMANDS