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