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