-
Notifications
You must be signed in to change notification settings - Fork 5
/
fabfile.py
301 lines (226 loc) · 6.99 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
import os
import sys
import datetime
from fabric.colors import red, green
from fabric.operations import local, prompt
PROJECT_NAME = 'ssheepdog'
APPS = ['ssheepdog']
TESTS = ' '.join(APPS)
COVERAGE_SOURCES = ','.join(APPS)
COVERAGE_PARAMS = "--omit='*migrations*,*tests*"
ENVS = {
'dev': {
'repo_url': '[email protected]:sheltered-bastion-8737.git',
'site_url': 'http://sheltered-bastion-8737.herokuapp.com'
},
'prod': {
'repo_url': '[email protected]:setsailforfail.git',
'site_url': 'http://setsailforfail.herokuapp.com'
},
}
def check_dest(fn):
"""
Decorator that verifies whether kwarg 'dest' is present and is a valid
environment name.
"""
def validate_dest(input):
if input not in ENVS:
raise Exception('Invalid environment specified.')
return input
def wrapper(*args, **kwargs):
dest = kwargs.get('dest', None)
if dest:
validate_dest(dest)
else:
kwargs['dest'] = prompt(
"Enter one of the following destinations [%s]:"
% ', '.join(ENVS),
validate=validate_dest)
return fn(*args, **kwargs)
return wrapper
##### Git remote repo management #####
def get_repos():
return local("git remote", capture=True).split("\n")
def _add_repo(repo, url, repos):
if repo not in repos:
local("git remote add %s %s" % (repo, url))
def _rm_repo(repo, url, repos):
if repo in repos:
local("git remote rm %s" % repo)
def _reset_repo(repo, url, repos):
_rm_repo(repo, url, repos)
_add_repo(repo, url, [])
def env_repos(action=None):
"""
Perform an action on each environment repository, specified by action.
"""
actions = {
'add': _add_repo,
'reset': _reset_repo,
'rm': _rm_repo
}
def validate_action(input):
if input not in actions:
raise Exception('Invalid action specified.')
return input
if action:
validate_action(action)
else:
action = prompt(
"Enter one of the following actions: <%s>" % ", ".join(actions),
validate=validate_action)
repos = get_repos()
for env, details in ENVS.iteritems():
actions[action](env, details['repo_url'], repos)
def _get_local_branches():
return [b.strip() for b in local("git branch", capture=True).split('\n')]
def _get_current_branch():
selected = [b for b in _get_local_branches() if b.startswith('* ')]
return selected[0].replace('* ', "") if len(selected) else None
@check_dest
def deploy(dest=''):
"""
Deploy from current repo to respective environment
"""
# Make sure our env repos are added as remotes
env_repos('add')
# Get the current branch
current_branch = _get_current_branch()
if current_branch == "(no branch)":
current_branch = local("git rev-parse --short HEAD", capture=True)
# Push to the destination
local('git push %s %s:master --force' % (dest, current_branch))
remote('syncdb --noinput', dest=dest)
remote('migrate', dest=dest)
deploy_static(dest=dest)
check(dest=dest)
def run():
local('./manage.py runserver 0.0.0.0:8000')
##### Static file management #####
@check_dest
def deploy_static(dest=''):
"""
Compress and upload static files to S3.
"""
local('./manage.py collectstatic --noinput'
' --settings=%s.settings.heroku.%s' % (PROJECT_NAME, dest))
# local('./manage.py compress'
# ' --force --settings=%s.settings.heroku.%s' % (PROJECT_NAME, dest))
def _now():
return datetime.now().strftime('%Y%m%s-%H%M')
##### Database management #####
def reset_local_db():
local('dropdb %s' % PROJECT_NAME)
local('createdb %s' % PROJECT_NAME)
def try_migrations():
reset_local_db()
local('./manage.py syncdb')
local('./manage.py migrate')
def try_clean():
reset_local_db()
local('./manage.py syncdb --noinput')
local('./manage.py migrate')
def reset_heroku_db():
local('heroku pg:reset')
def load_db():
"""
Populate empty Heroku database via json fixture
"""
print red('This will drop all tables from the database.')
print 'Please make sure you understand what this command does.'
print 'Do you want to continue? [y/n]'
answer = raw_input()
if answer != 'y':
print 'Aborting...'
return
commands = [
'./manage.py syncdb --noinput',
'./manage.py migrate',
'./manage.py droptables -y',
'./manage.py loaddata dump.json',
]
local('heroku run "%s"' % '; '.join(commands))
def make_dump():
local('./manage.py dumpdata | python -mjson.tool > dump.json')
##### Heroku specific helpers #####
@check_dest
def remote(cmd='', dest=''):
"""
Run a manage.py command on Heroku using ``settings_heroku``
Usage:
$ fab remote:'sendtestemail [email protected]'
$ fab remote:syncdb
Or
$ fab remote
Command to run: syncdb
"""
if not cmd:
cmd = prompt('Command to run:')
local("heroku run python manage.py %s \
--settings=%s.settings.heroku.%s" % (cmd, PROJECT_NAME, dest))
##### Testing, coverage & site validation #####
def test():
"""
Run unit tests for this Django Application
"""
if len(APPS) == 0:
return
local('./manage.py test %s' % TESTS)
def coverage():
"""
Generate Coverage report for this Django Application
"""
if len(APPS) == 0:
return
local('coverage run --source=%s ./manage.py test %s' % COVERAGE_SOURCES, TESTS)
print '============================================'
print 'Coverage Results:'
local('coverage report %s' % COVERAGE_PARAMS)
local('rm .coverage')
@check_dest
def check(dest=''):
"""
Check that the home page of the site returns an HTTP 200.
"""
print 'Checking site status...'
response = local('curl --silent -I "%s"' % ENVS[dest]['site_url'],
capture=True)
if not '200 OK' in response:
print(red('\nSomething seems to have gone wrong!\n'))
else:
print(green('\nLooks good from here!\n'))
##### Local utility tasks #####
def clean():
"""
Remove all .pyc files
"""
local('find . -name "*.pyc" -exec rm {} \;')
def debug():
"""
Find files with debug symbols
"""
clean()
local('grep -ir "print" *')
local('grep -ir "console.log" *')
def todo():
"""
Find all TODO and XXX
"""
clean()
local('grep -ir "TODO" *')
local('grep -ir "XXX" *')
def stats():
"""
Show number of additions and deletions between 1.0 and now
"""
local('git diff 1.0..HEAD --shortstat')
def freeze():
"""
Generate a stable requirements.txt based on requirements.spec.txt.
"""
local('pip freeze -r requirements.spec.txt > requirements.txt')
try:
assert os.getcwd() == os.path.dirname(os.path.abspath(__file__))
except AssertionError:
print red('You must run this from the root of the project.')
sys.exit(1)