collecting static in case static hosting is not done by S3 (and won't hurt if it is)
[oweals/karmaworld.git] / fabfile.py
1
2 """ Karmaworld Fabric management script
3     Finals Club (c) 2013"""
4
5 import os
6 import ConfigParser
7
8 from fabric.api import cd, env, lcd, prefix, run, sudo, task, local, settings
9 from fabric.contrib import files
10
11 ######### GLOBAL
12 env.user = 'vagrant'
13 env.group = 'vagrant'
14 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
15 env.repo_root = '~/karmaworld'
16 env.proj_root = '/var/www/karmaworld'
17 env.branch = 'prod'
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
22 env.use_ssh_config = True
23
24 ######## Define host(s)
25 def here():
26     """
27     Connection information for the local machine
28     """
29     def _custom_local(command):
30         prefixed_command = '/bin/bash -l -i -c "%s"' % command
31         return local(prefixed_command)
32
33     # This is required for the same reason as above
34     env.proj_root = '/var/www/karmaworld'
35     env.cd = lcd
36     env.reqs = 'reqs/dev.txt'
37     env.confs = 'confs/beta'
38     env.branch = 'beta'
39
40
41
42 ####### Define production host
43 @task
44 def prod():
45     """
46     Connection Information for production machine
47     """
48
49     env.user = 'djkarma'
50     env.hosts = ['karmanotes.org']
51     env.proj_root = '/var/www/karmaworld'
52     env.reqs = 'reqs/prod.txt'
53     env.confs = 'confs/prod/'
54     env.branch = 'beta'
55     env.gunicorn_addr = '127.0.0.1:8000'
56
57 ####### Define beta host
58 @task
59 def beta():
60     """
61     Connection Information for beta machine
62     """
63
64     env.user = 'djkarma'
65     env.hosts = ['beta.karmanotes.org']
66     env.proj_root = '/var/www/karmaworld'
67     env.reqs = 'reqs/prod.txt'
68     env.confs = 'confs/prod/'
69     env.branch = 'beta'
70
71 ######## Run Commands in Virutal Environment
72 def virtenv_path():
73     """
74     Builds the virtualenv path.
75     """
76     # not much here now, but maybe useful later.
77     return env.env_root
78
79 def virtenv_exec(command):
80     """
81     Execute command in Virtualenv
82     """
83
84     path = virtenv_path()
85     with prefix('source {0}/bin/activate'.format(path)):
86         run(command)
87
88 ######## Sync database
89 @task
90 def syncdb():
91     """
92     Sync Database
93     """
94     virtenv_exec('{0}/manage.py syncdb --migrate'.format(env.code_root))
95
96
97 ####### Collect Static Files
98 @task
99 def collect_static():
100         """
101         Collect static files (if AWS config. present, push to S3)
102         """
103
104         virtenv_exec('%s/manage.py collectstatic --noinput' % env.code_root )   
105
106 ####### Run Dev Server
107 @task
108 def dev_server():
109         """
110         Runs the built-in django webserver
111         """
112
113         virtenv_exec('%s/manage.py runserver' % env.code_root)  
114
115 ####### Create Virtual Environment
116
117 @task
118 def link_code():
119     """
120     Link the karmaworld repo into the appropriate production location
121     """
122     if not files.exists(env.code_root):
123         run('ln -s {0} {1}'.format(env.repo_root, env.code_root))
124
125 @task
126 def make_virtualenv():
127     """
128     Create our Virtualenv
129     """
130     run('virtualenv {0}'.format(virtenv_path()))
131
132 @task
133 def start_supervisord():
134     """
135     Starts supervisord
136     """
137     virtenv_exec('supervisord -c {0}'.format(env.supervisor_conf))
138
139
140 @task
141 def stop_supervisord():
142     """
143     Restarts supervisord
144     """
145     virtenv_exec('supervisorctl -c {0} shutdown'.format(env.supervisor_conf))
146
147
148 @task
149 def restart_supervisord():
150     """
151     Restarts supervisord
152     """
153     stop_supervisord()
154     start_supervisord()
155
156
157 def supervisorctl(action, process):
158     """
159     Takes as arguments the name of the process as is
160     defined in supervisord.conf and the action that should
161     be performed on it: start|stop|restart.
162     """
163     virtenv_exec('supervisorctl -c {0} {1} {2}'.format(env.supervisor_conf, action, process))
164
165
166 @task
167 def start_celeryd():
168     """
169     Starts the celeryd process
170     """
171     supervisorctl('start', 'celeryd')
172
173
174 @task
175 def stop_celeryd():
176     """
177     Stops the celeryd process
178     """
179     supervisorctl('stop', 'celeryd')
180
181
182 @task
183 def restart_celery():
184     """
185     Restarts the celeryd process
186     """
187     supervisorctl('restart', 'celeryd')
188
189
190 @task
191 def start_gunicorn():
192     """
193     Starts the gunicorn process
194     """
195     supervisorctl('start', 'gunicorn')
196
197
198 @task
199 def stop_gunicorn():
200     """
201     Stops the gunicorn process
202     """
203     supervisorctl('stop', 'gunicorn')
204
205
206 @task
207 def restart_gunicorn():
208     """
209     Restarts the gunicorn process
210     """
211     supervisorctl('restart', 'gunicorn')
212
213
214 ####### Update Requirements
215 @task
216 def update_reqs():
217     virtenv_exec('pip install -r {0}/reqs/{1}.txt'.format(env.repo_root, env.branch))
218
219 ####### Pull new code
220 @task
221 def update_code():
222     virtenv_exec('cd %s; git pull' % env.proj_root )
223
224 def backup():
225     """
226     Create backup using bup
227     """
228     pass
229
230 @task
231 def file_setup():
232     """
233     Deploy expected files and directories from non-apt system services.
234     """
235     ini_parser = ConfigParser.SafeConfigParser()
236     if not ini_parser.read(env.supervisor_conf):
237       raise Exception("Could not parse INI file {0}".format(env.supervisor_conf))
238     for section, option in (('supervisord','logfile'),
239                             ('supervisord','pidfile'),
240                             ('unix_http_server','file'),
241                             ('program:celeryd','stdout_logfile')):
242       filepath = ini_parser.get(section, option)
243       # generate file's directory structure if needed
244       run('mkdir -p {0}'.format(os.path.split(filepath)[0]))
245       # touch a file and change ownership if needed
246       if 'log' in option and not files.exists(filepath):
247           sudo('touch {0}'.format(filepath))
248           sudo('chown {0}:{1} {2}'.format(env.user, env.group, filepath))
249
250 @task
251 def check_secrets():
252     """
253     Ensure secret files exist for syncdb to run.
254     """
255
256     secrets_path = env.code_root + '/karmaworld/secret'
257     secrets_files = ('filepicker.py', 'static_s3.py', 'db_settings.py', 'drive.py', 'client_secrets.json')
258
259     errors = []
260     for sfile in secrets_files:
261         ffile = os.path.sep.join((secrets_path,sfile))
262         if not files.exists(ffile):
263             errors.append('{0} missing. Please add and try again.'.format(ffile))
264     if errors:
265         raise Exception('\n'.join(errors))
266
267 @task
268 def first_deploy():
269     """
270     Sets up and deploys the project for the first time.
271     """
272     link_code()
273     make_virtualenv()
274     file_setup()
275     check_secrets()
276     update_reqs()
277     syncdb()
278     collect_static()
279     start_supervisord()
280
281
282 @task
283 def deploy():
284     """
285     Deploys the latest changes
286     """
287     update_code()
288     update_reqs()
289     syncdb()
290     collect_static()
291     restart_supervisord()
292 ########## END COMMANDS