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