Same meaning for psycopg2, but removing explicit localhost allows unix domain user...
[oweals/karmaworld.git] / fabfile.py
1
2 """ Karmaworld Fabric management script
3     Finals Club (c) 2013"""
4
5 import os
6
7 from fabric.api import cd, env, lcd, prefix, run, task, local, settings
8 from fabric.contrib import files
9
10 ######### GLOBAL
11 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
12 env.repo_root = '~/karmaworld'
13 env.proj_root = '/var/www/karmaworld'
14 env.branch = 'prod'
15 env.code_root = '{0}/{1}-code'.format(env.proj_root, env.branch)
16
17 ######## Define host(s)
18 def here():
19     """
20     Connection information for the local machine
21     """
22     def _custom_local(command):
23         prefixed_command = '/bin/bash -l -i -c "%s"' % command
24         return local(prefixed_command)
25
26     env.run = _custom_local
27     # This is required for the same reason as above
28     env.proj_root = '/var/www/karmaworld'
29     env.cd = lcd
30     env.reqs = 'reqs/dev.txt'
31     env.confs = 'confs/beta'
32     env.branch = 'beta'
33
34
35
36 ####### Define production host
37 @task
38 def prod():
39     """
40     Connection Information for production machine
41     """
42
43     env.user = 'djkarma'
44     env.hosts = ['karmanotes.org']
45     env.proj_root = '/var/www/karmaworld'
46     env.reqs = 'reqs/prod.txt'
47     env.confs = 'confs/prod/'
48     env.branch = 'beta'
49     env.run = virtenv_exec
50     env.gunicorn_addr = '127.0.0.1:8000'
51
52 ####### Define beta host
53 @task
54 def beta():
55     """
56     Connection Information for beta machine
57     """
58
59     env.user = 'djkarma'
60     env.hosts = ['beta.karmanotes.org']
61     env.proj_root = '/var/www/karmaworld'
62     env.reqs = 'reqs/prod.txt'
63     env.confs = 'confs/prod/'
64     env.branch = 'beta'
65     env.run = virtenv_exec
66
67 ######## Run Commands in Virutal Environment
68 def virtenv_exec(command):
69     """
70     Execute command in Virtualenv
71     """
72
73     path = os.path.sep.join( (env.proj_root, env.branch) )
74     with prefix('source {0}/bin/activate'.format(path)):
75         run(command)
76
77 ######## Sync database
78 @task
79 def syncdb():
80     """
81     Sync Database
82     """
83     virtenv_exec('{0}/manage.py syncdb --migrate'.format(env.code_root))
84
85
86 ####### Collect Static Files
87 @task
88 def collect_static():
89         """
90         Collect static files (if AWS config. present, push to S3)
91         """
92
93         env.run('%s/manage.py collectstatic --noinput' % env.proj_root )        
94
95 ####### Run Dev Server
96 @task
97 def dev_server():
98         """
99         Runs the built-in django webserver
100         """
101
102         env.run('%s/manage.py runserver' % env.proj_root)       
103
104 ####### Create Virtual Environment
105 @task
106 def make_virtualenv():
107         """
108         Create our Virtualenv in env.proj_root
109         """
110
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))
116
117 @task
118 def start_supervisord():
119     """
120     Starts supervisord
121     """
122     config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
123     env.run('supervisord -c %s' % config_file)
124
125
126 @task
127 def stop_supervisord():
128     """
129     Restarts supervisord
130     """
131     config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
132     env.run('supervisorctl -c %s shutdown' % config_file)
133
134
135 @task
136 def restart_supervisord():
137     """
138     Restarts supervisord
139     """
140     stop_supervisord()
141     start_supervisord()
142
143
144 def supervisorctl(action, process):
145     """
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.
149     """
150     supervisor_conf = '/var/www/karmaworld/confs/prod/supervisord.conf'
151     env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
152
153
154 @task
155 def start_celeryd():
156     """
157     Starts the celeryd process
158     """
159     supervisorctl('start', 'celeryd')
160
161
162 @task
163 def stop_celeryd():
164     """
165     Stops the celeryd process
166     """
167     supervisorctl('stop', 'celeryd')
168
169
170 @task
171 def restart_celery():
172     """
173     Restarts the celeryd process
174     """
175     supervisorctl('restart', 'celeryd')
176
177
178 @task
179 def start_gunicorn():
180     """
181     Starts the gunicorn process
182     """
183     supervisorctl('start', 'gunicorn')
184
185
186 @task
187 def stop_gunicorn():
188     """
189     Stops the gunicorn process
190     """
191     supervisorctl('stop', 'gunicorn')
192
193
194 @task
195 def restart_gunicorn():
196     """
197     Restarts the gunicorn process
198     """
199     supervisorctl('restart', 'gunicorn')
200
201
202 ####### Update Requirements
203 @task
204 def update_reqs():
205     virtenv_exec('pip install -r {0}/reqs/{1}.txt'.format(env.repo_root, env.branch))
206
207 ####### Pull new code
208 @task
209 def update_code():
210     virtenv_exec('cd %s; git pull' % env.proj_root )
211
212 def backup():
213     """
214     Create backup using bup
215     """
216     pass
217
218 @task
219 def first_deploy():
220     """
221     Sets up and deploys the project for the first time.
222     """
223     make_virtualenv()
224     update_reqs()
225     syncdb()
226     manage_static()
227     start_supervisord()
228
229
230 @task
231 def deploy():
232     """
233     Deploys the latest changes
234     """
235     update_code()
236     update_reqs()
237     syncdb()
238     collect_static()
239     restart_supervisord()
240 ########## END COMMANDS