Latest changes to fabfile.py and confs/prod/supervisord.conf
[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
21     # This is required for the same reason as above
22     env.proj_dir = os.getcwd()
23     env.proj_root = os.path.dirname(env.proj_dir)
24     env.cd = lcd
25     env.reqs = 'reqs/dev.txt'
26     env.confs = 'confs/stag/'
27     env.branch = 'beta'
28     env.run = virtenv_exec
29
30
31
32 ####### Define production host
33 @task
34 def prod():
35     """
36     Connection Information for production machine
37     """
38
39     env.user = 'djkarma'
40     env.hosts = ['karmanotes.org']
41     env.proj_root = '/var/www/karmaworld'
42     env.proj_dir = '/var/www/karmaworld'
43     env.reqs = 'reqs/prod.txt'
44     env.confs = 'confs/beta/'
45     env.branch = 'beta'
46     env.run = virtenv_exec
47     env.gunicorn_addr = '127.0.0.1:8000'
48
49 ####### Define beta host
50 @task
51 def beta():
52     """                                                                                                                                                                 
53     Connection Information for beta machine                                                                                                                       
54     """
55
56     env.user = 'djkarma'
57     env.hosts = ['beta.karmanotes.org']
58     env.proj_root = '/var/www/karmaworld'
59     env.proj_dir = '/var/www/karmaworld'
60     env.reqs = 'reqs/beta.txt'
61     env.confs = 'confs/beta/'
62     env.branch = 'beta'
63     env.run = virtenv_exec
64     env.gunicorn_addr = '127.0.0.1:8000'
65
66 ######## Run Commands in Virutal Environment
67 def virtenv_exec(command):
68         """
69         Execute command in Virtualenv
70         """
71
72         with virtualenv('%s/%s' % (env.proj_dir, env.branch)):
73                 run('%s' % (command))
74
75 ######## Sync database
76 @task
77 def syncdb():
78         """
79         Sync Database
80         """
81
82         env.run('%s/manage.py syncdb --noinput --migrate' % env.proj_dir )
83
84
85 ####### Collect Static Files
86 @task
87 def collect_static():
88         """
89         Collect static files (if AWS config. present, push to S3
90         """
91
92         env.run('%s/manage.py collectstatic --noinput' % env.proj_dir ) 
93
94 ####### Run Dev Server
95 @task
96 def dev_server():
97         """
98         Runs the built-in django webserver
99         """
100
101         env.run('%s/manage.py runserver' % env.proj_dir )       
102
103 ####### Create Virtual Environment
104 @task
105 def make_virtualenv():
106         """
107         Create our Virtualenv in $SRC_ROOT
108         """
109
110         run('virtualenv %s/%s' % (env.proj_root, env.branch))
111         env.run('pip install -r %s/reqs/%s.txt' % (env.proj_dir, env.branch) )
112
113 @task
114 def start_supervisord():
115     """
116     Starts supervisord
117     """
118     config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
119     env.run('supervisord -c %s' % config_file)
120
121
122 @task
123 def stop_supervisord():
124     """
125     Restarts supervisord
126     """
127     config_file = '/var/www/karmaworld/confs/prod/supervisord.conf'
128     env.run('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     env.run('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         env.run('pip install -r reqs/prod.txt')
202
203 ####### Pull new code
204 @task
205 def update_code():
206         env.run('cd %s; git pull' % env.proj_dir)
207