Changed the deploy task to only deploy new changes and not handle initial setup.
[oweals/karmaworld.git] / fabfile.py
1 """Management utilities."""
2
3 import os
4 from contextlib import contextmanager as _contextmanager
5
6 from fabric.api import cd, env, prefix, run, task, local
7
8
9 ########## GLOBALS
10 env.proj_repo = 'git@github.com:FinalsClub/karmaworld.git'
11 env.virtualenv = 'venv-kw'
12 env.activate = 'workon %s' % env.virtualenv
13
14 # Using this env var to be able to specify the function
15 # used to run the commands. By default it's `run`, which
16 # runs commands remotely, but in the `here` task we set
17 # env.run to `local` to run commands locally.
18 env.run = run
19 ########## END GLOBALS
20
21
22 ########## HELPERS
23 @_contextmanager
24 def _virtualenv():
25     """
26     Changes to the proj_dir and activates the virtualenv
27     """
28     with cd(env.proj_dir):
29         with prefix(env.activate):
30             yield
31
32 ########## END HELPERS
33
34 ########## ENVIRONMENTS
35 @task
36 def here():
37     """
38     Connection information for the local machine
39     """
40     env.proj_dir = os.getcwd()
41     env.proj_root = os.path.dirname(env.proj_dir)
42     env.run = local
43     env.reqs = 'reqs/dev.txt'
44     env.confs = 'confs/dev/'
45     env.branch = 'master'
46
47
48 @task
49 def beta():
50     """
51     Beta connection information
52     """
53     env.user = 'djkarma'
54     env.hosts = ['beta.karmanotes.org']
55     env.proj_root = '/var/www/karmaworld'
56     env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
57     env.reqs = 'reqs/prod.txt'
58     env.confs = 'confs/beta/'
59     env.branch = 'beta'
60
61
62 @task
63 def prod():
64     """
65     Production connection information
66     """
67     env.user = 'djkarma'
68     env.hosts = ['karmanotes.org']
69     env.proj_root = '/var/www/karmaworld'
70     env.proj_dir = os.path.join(env.proj_root, 'karmaworld')
71     env.reqs = 'reqs/prod.txt'
72     env.confs = 'confs/prod/'
73     env.branch = 'master'
74 ########## END ENVIRONMENTS
75
76
77 ########## DATABASE MANAGEMENT
78 @task
79 def syncdb():
80     """Runs syncdb (along with any pending South migrations)"""
81     env.run('python manage.py syncdb --noinput --migrate')
82 ########## END DATABASE MANAGEMENT
83
84
85 ########## FILE MANAGEMENT
86 @task
87 def manage_static():
88     """
89     Collects, compresses and uploads static files.
90     """
91     collect_static()
92     compress_static()
93     upload_static()
94
95
96 @task
97 def collect_static():
98     """Collect all static files, and copy them to S3 for production usage."""
99     env.run('python manage.py collectstatic --noinput')
100
101
102 @task
103 def compress_static():
104     """
105     Compresses the static files.
106     """
107     pass
108
109
110 @task
111 def upload_static():
112     """
113     Uploads the static files to the specified host.
114     """
115     pass
116 ########## END FILE MANAGEMENT
117
118
119 ########## COMMANDS
120 @task
121 def make_virtualenv():
122     """
123     Creates a virtualenv on the remote host
124     """
125     env.run('mkvirtualenv %s' % env.virtualenv)
126
127
128 @task
129 def update_reqs():
130     """
131     Makes sure all packages listed in requirements are installed
132     """
133     with _virtualenv():
134         with cd(env.proj_dir):
135             env.run('pip install -r %s' % env.reqs)
136
137
138 @task
139 def clone():
140     """
141     Clones the project from the central repository
142     """
143     env.run('git clone %s %s' % (env.proj_repo, env.proj_dir))
144
145
146 @task
147 def update_code():
148     """
149     Pulls changes from the central repo and checks out the right branch
150     """
151     with cd(env.proj_dir):
152         env.run('git pull && git checkout %s' % env.branch)
153
154
155 @task
156 def start_supervisord():
157     """
158     Starts supervisord
159     """
160     with _virtualenv():
161         config_file = os.path.join(env.confs, 'supervisord.conf')
162         env.run('supervisord -c %s' % config_file)
163
164
165 @task
166 def stop_supervisord():
167     """
168     Restarts supervisord
169     """
170     with _virtualenv():
171         config_file = os.path.join(env.confs, 'supervisord.conf')
172         env.run('supervisorctl -c %s shutdown' % config_file)
173
174
175 @task
176 def restart_supervisord():
177     """
178     Restarts supervisord
179     """
180     stop_supervisord()
181     start_supervisord()
182
183
184 def supervisorctl(action, process):
185     """
186     Takes as arguments the name of the process as is
187     defined in supervisord.conf and the action that should
188     be performed on it: start|stop|restart.
189     """
190     supervisor_conf = os.path.join(env.confs, 'supervisord.conf')
191     env.run('supervisorctl -c %s %s %s' % (supervisor_conf, action, process))
192
193
194 @task
195 def start_celeryd():
196     """
197     Starts the celeryd process
198     """
199     supervisorctl('start', 'celeryd')
200
201
202 @task
203 def stop_celeryd():
204     """
205     Stops the celeryd process
206     """
207     supervisorctl('stop', 'celeryd')
208
209
210 @task
211 def restart_celery():
212     """
213     Restarts the celeryd process
214     """
215     supervisorctl('restart', 'celeryd')
216
217
218 @task
219 def start_gunicorn():
220     """
221     Starts the gunicorn process
222     """
223     supervisorctl('start', 'gunicorn')
224
225
226 @task
227 def stop_gunicorn():
228     """
229     Stops the gunicorn process
230     """
231     supervisorctl('stop', 'gunicorn')
232
233
234 @task
235 def restart_gunicorn():
236     """
237     Restarts the gunicorn process
238     """
239     supervisorctl('restart', 'gunicorn')
240
241
242 @task
243 def deploy():
244     """
245     Deploys the latest changes
246     """
247     update_code()
248     update_reqs()
249     syncdb()
250     manage_static()
251     restart_supervisord()
252 ########## END COMMANDS