-
Notifications
You must be signed in to change notification settings - Fork 10
/
fabfile.py
276 lines (222 loc) · 8.45 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from fabric.api import *
vars = {
'app_dir': '/usr/local/apps/land_owner_tools/lot',
'venv': '/usr/local/venv/lot',
'sitename': 'localhost:8080',
'branch': 'master'
}
env.forward_agent = True
env.key_filename = '~/.vagrant.d/insecure_private_key'
try:
from fab_vars import *
fab_vars_exists = True
except ImportError:
fab_vars_exists = False
def dev():
""" Use development server settings """
servers = ['[email protected]:2222']
env.hosts = servers
return servers
def prod_dev():
try:
if fab_vars_exists:
if DEV_BRANCH:
vars['branch'] = DEV_BRANCH
if DEV_SITENAME:
vars['sitename'] = DEV_SITENAME
if DEV_SERVER:
servers = DEV_SERVER
else:
servers = ['[email protected]:2222']
env.hosts = servers
return servers
else:
raise Exception("\nERROR: Cannot import file fab_vars.py. Have you created one from the template fab_vars.py.template?\n")
except Exception as inst:
print inst
def stage():
""" Use production server settings """
try:
if fab_vars_exists:
vars['branch'] = STAGE_BRANCH
if AWS_KEY_FILENAME_STAGE:
env.key_filename = AWS_KEY_FILENAME_STAGE
else:
env.key_filename = None
if STAGE_BRANCH:
vars['branch'] = STAGE_BRANCH
servers = AWS_PUBLIC_DNS_STAGE
env.hosts = servers
vars['sitename'] = AWS_SITENAME_STAGE
return servers
else:
raise Exception("\nERROR: Cannot import file fab_vars.py. Have you created one from the template fab_vars.py.template?\n")
except Exception as inst:
print inst
def prod():
""" Use production server settings """
try:
if fab_vars_exists:
vars['branch'] = PROD_BRANCH
env.key_filename = AWS_KEY_FILENAME_PROD
servers = AWS_PUBLIC_DNS_PROD
env.hosts = servers
vars['sitename'] = AWS_SITENAME_PROD
return servers
else:
raise Exception("\nERROR: Cannot import file fab_vars.py. Have you created one from the template fab_vars.py.template?\n")
except Exception as inst:
print inst
def test():
""" Use test server settings """
servers = ['ninkasi']
env.hosts = servers
return servers
def all():
""" Use all servers """
env.hosts = dev() + prod() + test()
def _install_requirements():
run('cd %(app_dir)s && %(venv)s/bin/pip install distribute' % vars)
run('cd %(app_dir)s && %(venv)s/bin/pip install -r ../requirements.txt' % vars)
def _install_django():
run('cd %(app_dir)s && %(venv)s/bin/python manage.py syncdb --noinput && \
%(venv)s/bin/python manage.py migrate --noinput && \
%(venv)s/bin/python manage.py install_media -a && \
%(venv)s/bin/python manage.py enable_sharing --all && \
%(venv)s/bin/python manage.py site %(sitename)s && \
%(venv)s/bin/python manage.py install_cleangeometry' % vars)
def _recache():
run('cd %(app_dir)s && %(venv)s/bin/python manage.py clear_cache' % vars)
# run('cd %(app_dir)s && %(venv)s/bin/python manage.py clear_cache && \
# %(venv)s/bin/python manage.py precache' % vars)
def manage(command):
""" Runs any manage.py command on the server """
vars['command'] = command
run('cd %(app_dir)s && %(venv)s/bin/python manage.py %(command)s' % vars)
del vars['command']
def create_superuser():
""" Create the django superuser (interactive!) """
run('cd %(app_dir)s && %(venv)s/bin/python manage.py createsuperuser' % vars)
def import_data():
""" Fetches and installs data fixtures (WARNING: 5+GB of data; hence not checking fixtures into the repo) """
run('cd %(app_dir)s && %(venv)s/bin/python manage.py import_data' % vars)
def init():
""" Initialize the forest planner application """
_install_requirements()
_install_django()
_install_starspan()
_recache()
#restart_services()
def restart_services():
run('sudo service uwsgi restart')
run('sudo service nginx restart')
run('sudo supervisorctl reload || sudo service supervisor start')
run('sleep 2 && sudo supervisorctl status')
def status():
init_services = ['postgresql', 'redis-server', 'supervisor']
for service in init_services:
run('sudo service %s status' % service)
run('sudo supervisorctl status')
run('sudo ps -eo pid,%cpu,%mem,comm,args --sort=-%cpu,-%mem | head -n 10')
ON = """\n !!!! maintenance_mode is on !!!!
Test and run \n fab <server> maintenance:off
when it's good to go
"""
OFF = """\n !!!! maintenance_mode is OFF; site is live !!!!"""
run('test -f /tmp/.maintenance_mode && echo "%s" || echo "%s"' % (ON, OFF))
def install_media():
""" Run the django install_media command """
run('cd %(app_dir)s && %(venv)s/bin/python manage.py install_media' % vars)
def copy_media():
""" Just copy the basic front end stuff. Speed! """
run('rsync -rtvu /usr/local/apps/land_owner_tools/media/common/ /usr/local/apps/land_owner_tools/mediaroot/common' % vars)
def runserver():
""" Run the django dev server on port 8000 """
run('cd %(app_dir)s && %(venv)s/bin/python manage.py runserver 0.0.0.0:8000' % vars)
def update():
""" Sync with correct branch in git repo """
with settings(
warn_only=True
):
if run('cd %(app_dir)s && \
git fetch && \
git show-ref --verify --quiet refs/heads/%(branch)s' % vars):
run('cd %(app_dir)s && \
git checkout %(branch)s && \
git merge origin/%(branch)s' % vars)
else:
run('cd %(app_dir)s && \
git checkout -b %(branch)s origin/%(branch)s && \
git merge origin/%(branch)s' % vars)
def branch():
run('cd %(app_dir)s && \
git fetch && \
git checkout -b %(branch)s && \
git merge origin/%(branch)s' % vars)
def _install_starspan():
run('mkdir -p ~/src && cd ~/src && \
if [ ! -d "starspan" ]; then git clone https://github.com/Ecotrust/starspan.git; fi && \
cd starspan && \
if [ ! `which starspan` ]; then ./configure && make && sudo make install; fi')
def deploy():
"""
Deploy to a staging/production environment
"""
for s in env.hosts:
if 'vagrant' in s:
raise Exception("You can't deploy() to local dev, just use `init restart_services`")
maintenance("on")
update()
init()
restart_services()
maintenance("off")
def maintenance(status):
"""
turn maintenance mode on or off
fab dev maintenance:on
fab dev maintenance:off
"""
if status == "on":
run("touch /tmp/.maintenance_mode")
else:
run("rm /tmp/.maintenance_mode")
def provision():
"""
Run puppet on a staging/production environment
"""
stage = False
for s in env.hosts:
if 'vagrant' in s:
raise Exception("You can't provision() on local dev, just vagrant up/provision")
if 'stage' in s:
stage = True
maintenance("on")
update()
# see lot.pp for defaults
if stage:
num_cpus = AWS_VARS_STAGE.get("num_cpus", 1)
postgres_shared_buffers = AWS_VARS_STAGE.get("postgres_shared_buffers", "48MB")
shmmax = AWS_VARS_STAGE.get("shmmax", 67108864)
else: # assume prod
num_cpus = AWS_VARS_PROD.get("num_cpus", 1)
postgres_shared_buffers = AWS_VARS_PROD.get("postgres_shared_buffers", "48MB")
shmmax = AWS_VARS_PROD.get("shmmax", 67108864)
run("""sudo \
facter_user=ubuntu \
facter_group=ubuntu \
facter_url_base=http://%s \
facter_num_cpus=%s \
facter_postgres_shared_buffers=%s \
facter_shmmax=%s \
facter_pgsql_base=/var/lib/postgresql/ puppet apply \
--templatedir=/usr/local/apps/land_owner_tools/scripts/puppet/manifests/files \
--modulepath=/usr/local/apps/land_owner_tools/scripts/puppet/modules \
/usr/local/apps/land_owner_tools/scripts/puppet/manifests/lot.pp
""" % (env.host,
num_cpus,
postgres_shared_buffers,
shmmax
)
)
restart_services()
status()