-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfablib.py
407 lines (303 loc) · 12.3 KB
/
fablib.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# -*- coding: utf-8 -*-
# Chicago Tribune News Applications fabfile
# Copying encouraged
import os
import subprocess
import urllib
from time import strftime, localtime
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.context_managers import cd
from getpass import getpass, getuser
from data.fabsettings import *
# This defaults the run and sudo functions to local, so we don't have to duplicate
# code for local development and deployed servers.
env.sudo = local
env.run = local
env.cache_servers = ["lb1"]
"""
Commands - setup
"""
def git_clone_repo():
"""
Do initial clone of the git repository.
"""
with settings(warn_only=True):
run('git clone %(gitrepo)s %(path)s' % env)
def git_checkout():
"""
Pull the latest code on the specified branch.
"""
with cd(env.path):
if env.gitbranch != 'master':
with settings(warn_only=True):
run('git checkout -b %(gitbranch)s origin/%(gitbranch)s' % env)
run('git checkout %(gitbranch)s' % env)
run('git pull origin %(gitbranch)s' % env)
run('git submodule update --init --recursive')
def git_tag_stable():
"""
Tag branch with datetime release number
"""
today = strftime('%y.%m.%d', localtime())
print "Checking for tags..."
with settings(hide('warnings'), warn_only=True):
response = local("git tag -l | grep '^%s'" % today)
if response.failed:
tag = '%s-0' % today
print "Found no tags for today. Tagging with %s." % tag
local('git tag %s' % tag)
local('git push --tags')
else:
numbers = [int(line.split('-')[1]) for line in response.splitlines()]
numbers.sort()
next = numbers[-1] + 1
tag = '%s-%s' % (today, next)
print "Found tags for today's date. Incrementing -- tagging with %s." % tag
local('git tag %s' % tag)
local('git push --tags')
def install_apache_conf():
"""
Setup the apache config file
"""
with cd(env.path):
sudo('cp http/%(settings)s-apache.conf ~/apache/%(project_name)s' % env)
sudo('service apache2 reload' % env)
run('run-for-cluster -t app "sudo service apache2 reload"')
def install_nginx_conf():
"""
Setup the nginx config file
"""
with cd(env.path):
sudo('cp http/%(settings)s-nginx.conf ~/nginx/%(project_name)s' % env)
run('run-for-cluster -t app "sudo service nginx reload"')
"""
Commands - deployment
"""
def setup():
"""
Setup the site
"""
_confirm_branch()
if env.strategy == 'git':
git_clone_repo()
git_checkout()
elif env.strategy == 'svn':
svn_checkout()
def deploy():
"""
Deploy new code to the site
"""
_confirm_branch()
require('settings', provided_by=[production, staging])
if env.strategy == 'git':
require('gitbranch', provided_by=[stable, master, branch])
git_checkout()
elif env.strategy == 'svn':
svn_checkout()
sync_app_servers()
"""
Commands - data
"""
def bootstrap():
with cd(env.path):
print("\nStep 1: Install required PHP extensions/apps")
if confirm('Continue installing requirements? Can skip if already installed.'):
env.run('sudo setup_env.sh')
print("\nStep 2: Database and basic Wordpress setup")
with settings(warn_only=True):
env.run('rm wp-config.php');
env.run(env.prefix + './manage.sh setup_wp-config')
create_db()
env.run(env.prefix + './manage.sh install')
env.run(env.prefix + './manage.sh install_network')
with settings(warn_only=True):
env.run('rm wp-config.php');
env.run(env.prefix + './manage.sh setup_wp-config --finish')
print("\nStep 3: Setup plugins")
env.run(env.prefix + './manage.sh setup_plugins')
print("\nStep 4: Cleanup, create blogs")
env.run(env.prefix + './manage.sh set_root_blog_defaults')
if confirm("Create child blogs?"): create_blogs()
with cd(env.path):
env.run(env.prefix + './manage.sh setup_upload_dirs')
def create_db():
if not env.db_root_pass:
env.db_root_pass = getpass("Database password: ")
if env.db_host == 'localhost':
env.run('mysqladmin --user=%(db_root_user)s --password=%(db_root_pass)s create %(db_name)s' % env)
env.run('echo "GRANT ALL ON * TO \'%(db_wpuser_name)s\'@\'localhost\' IDENTIFIED BY \'%(db_wpuser_pass)s\';" | mysql --user=%(db_root_user)s --password=%(db_root_pass)s %(db_name)s' % env)
else:
env.run('mysqladmin --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s create %(db_name)s' % env)
env.run('echo "GRANT ALL ON * TO \'%(db_wpuser_name)s\'@\'%%\' IDENTIFIED BY \'%(db_wpuser_pass)s\';" | mysql --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s %(db_name)s' % env)
def load_db(dump_slug='dump'):
env.dump_slug = dump_slug
if not env.db_root_pass:
env.db_root_pass = getpass("Database password: ")
if env.db_host == 'localhost':
env.run("bzcat data/%(dump_slug)s.sql.bz2 |sed s/WPDEPLOYDOMAN/%(wpdomain)s/g |mysql --user=%(db_root_user)s --password=%(db_root_pass)s %(db_name)s" % env)
else:
with cd(env.path):
env.run("bzcat data/%(dump_slug)s.sql.bz2 |sed s/WPDEPLOYDOMAN/%(wpdomain)s/g |mysql --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s %(db_name)s" % env)
def dump_db(dump_slug='dump'):
env.dump_slug = dump_slug
if not env.db_root_pass:
env.db_root_pass = getpass("Database password: ")
if env.db_host == 'localhost':
env.run("mysqldump --user=%(db_root_user)s --password=%(db_root_pass)s --quick %(project_name)s |sed s/%(wpdomain)s/WPDEPLOYDOMAN/g |bzip2 > data/%(dump_slug)s.sql.bz2" % env)
else:
with cd(env.path):
env.run("mysqldump --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s --quick --skip-lock-tables %(project_name)s |sed s/%(wpdomain)s/WPDEPLOYDOMAN/g |bzip2 > data/%(dump_slug)s.sql.bz2" % env)
def put_dump(dump_slug='dump'):
check_env()
env.dump_slug = dump_slug
put('data/%(dump_slug)s.sql.bz2' % env,'%(path)s/data/%(dump_slug)s.sql.bz2' % env)
print('Put %(dump_slug)s.sql.bz2 on server.\n' % env)
def get_dump(dump_slug='dump'):
check_env()
env.dump_slug = dump_slug
get('%(path)s/data/%(dump_slug)s.sql.bz2' % env, 'data/%(dump_slug)s.sql.bz2' % env)
print('Got %(dump_slug)s.sql.bz2 from the server.\n' % env)
def destroy_db():
if not env.db_root_pass:
env.db_root_pass = getpass("Database password: ")
with settings(warn_only=True):
if env.db_host == 'localhost':
env.run('mysqladmin -f --user=%(db_root_user)s --password=%(db_root_pass)s drop %(project_name)s' % env)
env.run('echo "DROP USER \'%(db_wpuser_name)s\'@\'localhost\';" | mysql --user=%(db_root_user)s --password=%(db_root_pass)s' % env)
elif confirm("Are you sure you want to drop the %s database?" % env.settings):
env.run('mysqladmin -f --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s drop %(project_name)s' % env)
env.run('echo "DROP USER \'%(db_wpuser_name)s\'@\'%%\';" | mysql --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s' % env)
def destroy_attachments():
with cd(env.path):
env.run('rm -rf wp-content/blogs.dir')
def reload_db(dump_slug='dump'):
destroy_db()
create_db()
load_db(dump_slug)
def create_blogs():
i = 0
response = ''
while "No more blogs" not in response:
with cd(env.path):
if 'settings' in env:
response = run(env.prefix + "./manage.sh setup_blog -n %s" % i)
else:
response = local("./manage.sh setup_blog -n %s" % i, capture=True)
print( response )
i += 1
def force_nfs_refresh():
check_env()
env.run("run-for-cluster -t app 'cd %(path)s; git status;'" % env)
def sync_app_servers():
check_env()
env.run("run-for-cluster -t app 'sudo rsync -a --delete /mnt/apps/sites/%(project_name)s/ %(path)s/'" % env)
def setup_media():
check_env()
with cd('/mnt/apps/media'):
env.sudo("mkdir %(project_name)s" % env)
env.sudo("mkdir %(project_name)s/uploads" % env)
env.sudo("mkdir %(project_name)s/blogs.dir" % env)
env.sudo("mkdir %(project_name)s/fragment-cache" % env)
link_media()
fix_perms()
def fix_perms():
check_env()
if env.fix_perms:
with cd('/mnt/apps/media'):
env.sudo("chgrp -Rf www-data %(project_name)s" % env)
env.sudo("chmod -Rf g+rw %(project_name)s" % env)
def link_media():
check_env()
with settings(warn_only=True):
env.run("ln -s /mnt/apps/media/%(project_name)s/* %(path)s/" % env)
print('Remember to sync!')
def wrap_media():
with cd(env.path):
env.run('tar zcf data/media.tgz blogs.dir/* uploads/*')
print('Wrapped up media.\n')
def unwrap_media():
with cd(env.path):
env.run('tar zxf data/media.tgz')
print('Unwrapped media.\n')
def put_media():
check_env()
put('data/media.tgz','%(path)s/data/media.tgz' % env)
print('Put media on server.\n')
def get_media():
check_env()
get('%(path)s/data/media.tgz' % env, 'data/media.tgz')
print('Got media from the server.\n')
"""
Deaths, destroyers of worlds
"""
def shiva_the_destroyer():
"""
Remove all directories, databases, etc. associated with the application.
"""
try:
env.settings
check_env()
env.run('rm -Rf %(path)s;' % env)
destroy_db()
except AttributeError, e:
with settings(warn_only=True):
env.run('rm .htaccess')
env.run('rm wp-config.php')
destroy_db()
"""
Varnish cache commands
"""
def clear_cache():
require('settings', provided_by=[production, staging])
if confirm("Are you sure? This can bring the servers to their knees..."):
for server in env.cache_servers:
env.run('curl -s -I -X PURGE -H "Host: %s" http://%s/' % (env.wpdomain, server))
def clear_media_cache():
require('settings', provided_by=[production, staging])
for server in env.cache_servers:
env.run('curl -s -I -X PURGE -H "Host: %s" http://%s/media/.*' % (env.wpdomain, server))
env.run('curl -s -I -X PURGE -H "Host: %s" http://%s/.*/media/.*' % (env.wpdomain, server))
env.run('curl -s -I -X PURGE -H "Host: %s" http://%s/.*/files/.*' % (env.wpdomain, server))
def clear_asset_cache():
require('settings', provided_by=[production, staging])
for server in env.cache_servers:
env.run('curl -s -I -X PURGE -H "Host: %s" http://%s/wp-content/.*' % (env.wpdomain, server))
env.run('curl -s -I -X PURGE -H "Host: %s" http://%s/.*/wp-content/.*' % (env.wpdomain, server))
def clear_admin_cache():
require('settings', provided_by=[production, staging])
for server in env.cache_servers:
env.run('curl -s -I -X PURGE -H "Host: %s" http://%s/.*/wp-admin/.*' % (env.wpdomain, server))
def clear_url( url ):
check_env()
if confirm("Are you sure? This can bring the servers to their knees..."):
for server in env.cache_servers:
env.run('curl -s -I -X PURGE -H "Host: %s" http://%s%s' % (env.wpdomain, server, url))
"""
Miscellaneous
"""
def run_script(script_name):
"""
Run a script in the /wp-scripts/ directory.
"""
check_env()
env.script_name = script_name
with cd(env.path):
env.run(env.prefix + './manage.sh %(script_name)s' % env)
def robots_setup():
check_env()
with cd(env.path):
env.run('ln -s robots_%(settings)s.txt robots.txt' % env)
print('Remember to sync!');
"""
Utilities
"""
def check_env():
require('settings', provided_by=[production, staging])
env.sudo = sudo
env.run = run
def _confirm_branch():
if (env.settings == 'production' and env.gitbranch != 'stable'):
answer = prompt("You are trying to deploy the '%(gitbranch)s' branch to production.\nYou should really only deploy a stable branch.\nDo you know what you're doing?" % env, default="Not at all")
if answer not in ('y','Y','yes','Yes','buzz off','screw you'):
exit()