forked from nprapps/books13
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
591 lines (466 loc) · 17.2 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#!/usr/bin/env python
import copy
from glob import glob
import os
from fabric.api import *
from jinja2 import Template
import app
import app_config
import data
from etc import github
"""
Base configuration
"""
env.user = app_config.SERVER_USER
env.forward_agent = True
env.hosts = []
env.settings = None
"""
Environments
Changing environment requires a full-stack test.
An environment points to both a server and an S3
bucket.
"""
def production():
"""
Run as though on production.
"""
env.settings = 'production'
app_config.configure_targets(env.settings)
env.hosts = app_config.SERVERS
def staging():
"""
Run as though on staging.
"""
env.settings = 'staging'
app_config.configure_targets(env.settings)
env.hosts = app_config.SERVERS
"""
Fabcasting! Run commands on the remote server.
"""
def fabcast(command):
"""
Actually run specified commands on the server specified
by staging() or production().
"""
require('settings', provided_by=[production, staging])
if not app_config.DEPLOY_TO_SERVERS:
print 'You must set DEPLOY_TO_SERVERS = True in your app_config.py and setup a server before fabcasting..'
return
run('cd %s && bash run_on_server.sh fab %s $DEPLOYMENT_TARGET %s' % (app_config.SERVER_REPOSITORY_PATH, env.branch, command))
"""
Branches
Changing branches requires deploying that branch to a host.
"""
def stable():
"""
Work on stable branch.
"""
env.branch = 'stable'
def master():
"""
Work on development branch.
"""
env.branch = 'master'
def branch(branch_name):
"""
Work on any specified branch.
"""
env.branch = branch_name
"""
Template-specific functions
Changing the template functions should produce output
with fab render without any exceptions. Any file used
by the site templates should be rendered by fab render.
"""
def less():
"""
Render LESS files to CSS.
"""
for path in glob('less/*.less'):
filename = os.path.split(path)[-1]
name = os.path.splitext(filename)[0]
out_path = 'www/css/%s.less.css' % name
local('node_modules/bin/lessc %s %s' % (path, out_path))
def jst():
"""
Render Underscore templates to a JST package.
"""
local('node_modules/bin/jst --template underscore jst www/js/templates.js')
def download_copy():
"""
Downloads a Google Doc as an .xls file.
"""
base_url = 'https://docs.google.com/spreadsheet/pub?key=%s&output=xls'
doc_url = base_url % app_config.COPY_GOOGLE_DOC_KEY
local('curl -o data/copy.xls "%s"' % doc_url)
def update_copy():
"""
Fetches the latest Google Doc and updates local JSON.
"""
download_copy()
def app_config_js():
"""
Render app_config.js to file.
"""
from app import _app_config_js
response = _app_config_js()
js = response[0]
with open('www/js/app_config.js', 'w') as f:
f.write(js)
def copy_js():
"""
Render copy.js to file.
"""
from app import _copy_js
response = _copy_js()
js = response[0]
with open('www/js/copy.js', 'w') as f:
f.write(js)
def render():
"""
Render HTML templates and compile assets.
"""
from flask import g
update_copy()
less()
jst()
app_config_js()
copy_js()
compiled_includes = []
for rule in app.app.url_map.iter_rules():
rule_string = rule.rule
name = rule.endpoint
if name == 'static' or name.startswith('_'):
print 'Skipping %s' % name
continue
if rule_string.endswith('/'):
filename = 'www' + rule_string + 'index.html'
elif rule_string.endswith('.html'):
filename = 'www' + rule_string
else:
print 'Skipping %s' % name
continue
dirname = os.path.dirname(filename)
if not (os.path.exists(dirname)):
os.makedirs(dirname)
print 'Rendering %s' % (filename)
with app.app.test_request_context(path=rule_string):
g.compile_includes = True
g.compiled_includes = compiled_includes
view = app.__dict__[name]
content = view()
compiled_includes = g.compiled_includes
with open(filename, 'w') as f:
f.write(content.encode('utf-8'))
def tests():
"""
Run Python unit tests.
"""
local('nosetests')
"""
Setup
Changing setup commands requires a test deployment to a server.
Setup will create directories, install requirements, etc.
"""
def setup_server():
"""
Setup servers for deployment.
NB: This does not setup services or push to S3. Run deploy() next.
"""
require('settings', provided_by=[production, staging])
require('branch', provided_by=[stable, master, branch])
if not app_config.DEPLOY_TO_SERVERS:
print 'You must set DEPLOY_TO_SERVERS = True in your app_config.py before setting up the servers.'
return
setup_directories()
setup_virtualenv()
clone_repo()
checkout_latest()
install_requirements()
def setup_directories():
"""
Create server directories.
"""
require('settings', provided_by=[production, staging])
run('mkdir -p %(SERVER_PROJECT_PATH)s' % app_config.__dict__)
run('mkdir -p /var/www/uploads/%(PROJECT_FILENAME)s' % app_config.__dict__)
def setup_virtualenv():
"""
Setup a server virtualenv.
"""
require('settings', provided_by=[production, staging])
run('virtualenv -p %(SERVER_PYTHON)s --no-site-packages %(SERVER_VIRTUALENV_PATH)s' % app_config.__dict__)
run('source %(SERVER_VIRTUALENV_PATH)s/bin/activate' % app_config.__dict__)
def clone_repo():
"""
Clone the source repository.
"""
require('settings', provided_by=[production, staging])
run('git clone %(REPOSITORY_URL)s %(SERVER_REPOSITORY_PATH)s' % app_config.__dict__)
if app_config.REPOSITORY_ALT_URL:
run('git remote add bitbucket %(REPOSITORY_ALT_URL)s' % app_config.__dict__)
def checkout_latest(remote='origin'):
"""
Checkout the latest source.
"""
require('settings', provided_by=[production, staging])
require('branch', provided_by=[stable, master, branch])
run('cd %s; git fetch %s' % (app_config.SERVER_REPOSITORY_PATH, remote))
run('cd %s; git checkout %s; git pull %s %s' % (app_config.SERVER_REPOSITORY_PATH, env.branch, remote, env.branch))
def install_requirements():
"""
Install the latest requirements.
"""
require('settings', provided_by=[production, staging])
run('%(SERVER_VIRTUALENV_PATH)s/bin/pip install -U -r %(SERVER_REPOSITORY_PATH)s/requirements.txt' % app_config.__dict__)
run('cd %(SERVER_REPOSITORY_PATH)s; npm install less universal-jst -g --prefix node_modules' % app_config.__dict__)
def install_crontab():
"""
Install cron jobs script into cron.d.
"""
require('settings', provided_by=[production, staging])
sudo('cp %(SERVER_REPOSITORY_PATH)s/crontab /etc/cron.d/%(PROJECT_FILENAME)s' % app_config.__dict__)
def uninstall_crontab():
"""
Remove a previously install cron jobs script from cron.d
"""
require('settings', provided_by=[production, staging])
sudo('rm /etc/cron.d/%(PROJECT_FILENAME)s' % app_config.__dict__)
def bootstrap_issues():
"""
Bootstraps Github issues with default configuration.
"""
auth = github.get_auth()
github.delete_existing_labels(auth)
github.create_labels(auth)
github.create_tickets(auth)
github.create_milestones(auth)
github.create_hipchat_hook(auth)
"""
Deployment
Changes to deployment requires a full-stack test. Deployment
has two primary functions: Pushing flat files to S3 and deploying
code to a remote server if required.
"""
def _deploy_to_s3(path='.gzip'):
"""
Deploy the gzipped stuff to S3.
"""
# Clear files that should never be deployed
local('rm -rf %s/live-data' % path)
local('rm -rf %s/sitemap.xml' % path)
s3cmd = 's3cmd -P --add-header=Cache-Control:max-age=5 --guess-mime-type --recursive --exclude-from gzip_types.txt sync %s/ %s'
s3cmd_gzip = 's3cmd -P --add-header=Cache-Control:max-age=5 --add-header=Content-encoding:gzip --guess-mime-type --recursive --exclude "*" --include-from gzip_types.txt sync %s/ %s'
for bucket in app_config.S3_BUCKETS:
local(s3cmd % (path, 's3://%s/%s/' % (bucket, app_config.PROJECT_SLUG)))
local(s3cmd_gzip % (path, 's3://%s/%s/' % (bucket, app_config.PROJECT_SLUG)))
def _gzip(in_path='www', out_path='.gzip'):
"""
Gzips everything in www and puts it all in gzip
"""
local('python gzip_assets.py %s %s' % (in_path, out_path))
def _get_template_conf_path(service, extension):
"""
Derive the path for a conf template file.
"""
return 'confs/%s.%s' % (service, extension)
def _get_rendered_conf_path(service, extension):
"""
Derive the rendered path for a conf file.
"""
return 'confs/rendered/%s.%s.%s' % (app_config.PROJECT_FILENAME, service, extension)
def _get_installed_conf_path(service, remote_path, extension):
"""
Derive the installed path for a conf file.
"""
return '%s/%s.%s.%s' % (remote_path, app_config.PROJECT_FILENAME, service, extension)
def _get_installed_service_name(service):
"""
Derive the init service name for an installed service.
"""
return '%s.%s' % (app_config.PROJECT_FILENAME, service)
def render_confs():
"""
Renders server configurations.
"""
require('settings', provided_by=[production, staging])
with settings(warn_only=True):
local('mkdir confs/rendered')
# Copy the app_config so that when we load the secrets they don't
# get exposed to other management commands
context = copy.copy(app_config.__dict__)
context.update(app_config.get_secrets())
for service, remote_path, extension in app_config.SERVER_SERVICES:
template_path = _get_template_conf_path(service, extension)
rendered_path = _get_rendered_conf_path(service, extension)
with open(template_path, 'r') as read_template:
with open(rendered_path, 'wb') as write_template:
payload = Template(read_template.read())
write_template.write(payload.render(**context))
def deploy_confs():
"""
Deploys rendered server configurations to the specified server.
This will reload nginx and the appropriate uwsgi config.
"""
require('settings', provided_by=[production, staging])
render_confs()
with settings(warn_only=True):
for service, remote_path, extension in app_config.SERVER_SERVICES:
rendered_path = _get_rendered_conf_path(service, extension)
installed_path = _get_installed_conf_path(service, remote_path, extension)
a = local('md5 -q %s' % rendered_path, capture=True)
b = run('md5sum %s' % installed_path).split()[0]
if a != b:
print 'Updating %s' % installed_path
put(rendered_path, installed_path, use_sudo=True)
if service == 'nginx':
sudo('service nginx reload')
elif service == 'uwsgi':
service_name = _get_installed_service_name(service)
sudo('initctl reload-configuration')
sudo('service %s restart' % service_name)
elif service == 'app':
run('touch %s' % app_config.UWSGI_SOCKET_PATH)
sudo('chmod 644 %s' % app_config.UWSGI_SOCKET_PATH)
sudo('chown www-data:www-data %s' % app_config.UWSGI_SOCKET_PATH)
sudo('touch %s' % app_config.UWSGI_LOG_PATH)
sudo('chmod 644 %s' % app_config.UWSGI_LOG_PATH)
sudo('chown ubuntu:ubuntu %s' % app_config.UWSGI_LOG_PATH)
sudo('touch %s' % app_config.APP_LOG_PATH)
sudo('chmod 644 %s' % app_config.APP_LOG_PATH)
sudo('chown ubuntu:ubuntu %s' % app_config.APP_LOG_PATH)
else:
print '%s has not changed' % rendered_path
def deploy(remote='origin'):
"""
Deploy the latest app to S3 and, if configured, to our servers.
"""
require('settings', provided_by=[production, staging])
if app_config.DEPLOY_TO_SERVERS:
require('branch', provided_by=[stable, master, branch])
if (app_config.DEPLOYMENT_TARGET == 'production' and env.branch != 'stable'):
_confirm("You are trying to deploy the '%s' branch to production.\nYou should really only deploy a stable branch.\nDo you know what you're doing?" % env.branch)
if app_config.DEPLOY_TO_SERVERS:
checkout_latest(remote)
if app_config.DEPLOY_CRONTAB:
install_crontab()
if app_config.DEPLOY_SERVICES:
deploy_confs()
render()
_gzip('www', '.gzip')
_deploy_to_s3()
"""
Cron jobs
"""
def cron_test():
"""
Example cron task. Note we use "local" instead of "run"
because this will run on the server.
"""
require('settings', provided_by=[production, staging])
local('echo $DEPLOYMENT_TARGET > /tmp/cron_test.txt')
"""
App-Specific Commands
"""
def load_images():
"""
Scrapes images from Baker & Taylor API.
Does not save book data.
Requires existing books.json to work.
"""
with settings(warn_only=True):
local('mkdir -p www/img/cover')
data.load_images()
def load_books():
"""
Loads/reloads just the book data.
Does not save image files.
"""
data.get_books_csv()
data.parse_books_csv()
def bootstrap():
"""
Loads initial project data.
First, loads books data from Google docs.
Next, loads images from Baker & Taylor's API.
"""
# Reimport app_config in case this is part of the app_template bootstrap
import app_config
local('npm install less universal-jst -g --prefix node_modules')
local('pip install -r requirements.txt')
local('ln -s ~/Dropbox/nprapps/assets/%(REPOSITORY_NAME)s/ www/assets' % app_config.__dict__)
update_copy()
load_books()
"""
Destruction
Changes to destruction require setup/deploy to a test host in order to test.
Destruction should remove all files related to the project from both a remote
host and S3.
"""
def _confirm(message):
answer = prompt(message, default="Not at all")
if answer.lower() not in ('y', 'yes', 'buzz off', 'screw you'):
exit()
def nuke_confs():
"""
DESTROYS rendered server configurations from the specified server.
This will reload nginx and stop the uwsgi config.
"""
require('settings', provided_by=[production, staging])
for service, remote_path, extension in app_config.SERVER_SERVICES:
with settings(warn_only=True):
installed_path = _get_installed_conf_path(service, remote_path, extension)
sudo('rm -f %s' % installed_path)
if service == 'nginx':
sudo('service nginx reload')
elif service == 'uwsgi':
service_name = _get_installed_service_name(service)
sudo('service %s stop' % service_name)
sudo('initctl reload-configuration')
elif service == 'app':
sudo('rm %s' % app_config.UWSGI_SOCKET_PATH)
sudo('rm %s' % app_config.UWSGI_LOG_PATH)
sudo('rm %s' % app_config.APP_LOG_PATH)
def shiva_the_destroyer():
"""
Deletes the app from s3
"""
require('settings', provided_by=[production, staging])
_confirm("You are about to destroy everything deployed to %s for this project.\nDo you know what you're doing?" % app_config.DEPLOYMENT_TARGET)
with settings(warn_only=True):
s3cmd = 's3cmd del --recursive %s'
for bucket in app_config.S3_BUCKETS:
local(s3cmd % ('s3://%s/%s' % (bucket, app_config.PROJECT_SLUG)))
if app_config.DEPLOY_TO_SERVERS:
run('rm -rf %(SERVER_PROJECT_PATH)s' % app_config.__dict__)
if app_config.DEPLOY_CRONTAB:
uninstall_crontab()
if app_config.DEPLOY_SERVICES:
nuke_confs()
"""
App-template specific setup. Not relevant after the project is running.
"""
def app_template_bootstrap(project_name=None, repository_name=None):
"""
Execute the bootstrap tasks for a new project.
"""
config_files = ' '.join(['PROJECT_README.md', 'app_config.py'])
config = {}
config['$NEW_PROJECT_SLUG'] = os.getcwd().split('/')[-1]
config['$NEW_PROJECT_NAME'] = project_name or config['$NEW_PROJECT_SLUG']
config['$NEW_REPOSITORY_NAME'] = repository_name or config['$NEW_PROJECT_SLUG']
config['$NEW_PROJECT_FILENAME'] = config['$NEW_PROJECT_SLUG'].replace('-', '_')
_confirm("Have you created a Github repository named \"%s\"?" % config['$NEW_REPOSITORY_NAME'])
for k, v in config.items():
local('sed -i "" \'s|%s|%s|g\' %s' % (k, v, config_files))
local('rm -rf .git')
local('git init')
local('mv PROJECT_README.md README.md')
local('rm *.pyc')
local('git add * .gitignore')
local('git commit -am "Initial import from app-template."')
local('git remote add origin https://github.com/nprapps/%s.git' % config['$NEW_REPOSITORY_NAME'])
local('git push -u origin master')
local('npm install less universal-jst -g --prefix node_modules')
update_copy()