forked from fecgov/openFEC
/
tasks.py
212 lines (174 loc) · 5.85 KB
/
tasks.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
import git
from invoke import run
from invoke import task
DEFAULT_FRACTION = 0.5
FULL_TABLES = [
'dimdates',
'dimparty',
'dimyears',
'dimoffice',
'dimreporttype',
]
EXCLUDE_TABLES = [
'*_mv',
'*_tmp',
'*_old',
'sched_e',
'sched_b2',
'sched_e2',
'pacronyms',
'ofec_*',
]
# Include records used in integration tests
FORCE_INCLUDE = [
('dimcand', 10025229), # Nancy Pelosi
('dimcand', 10012694), # John Boehner
('dimcmte', 10031117), # Raul Grijalva (committee)
]
@task
def fetch_schemas(source, dest):
cmd = 'pg_dump {0} --format c --schema-only --no-acl --no-owner'.format(source)
for table in (FULL_TABLES + EXCLUDE_TABLES):
cmd += ' --exclude-table {0}'.format(table)
cmd += ' | pg_restore --dbname {0} --no-acl --no-owner'.format(dest)
run(cmd, echo=True)
@task
def fetch_full(source, dest):
cmd = 'pg_dump {0} --format c --no-acl --no-owner'.format(source)
for table in FULL_TABLES:
cmd += ' --table {0}'.format(table)
cmd += ' | pg_restore --dbname {0} --no-acl --no-owner'.format(dest)
run(cmd, echo=True)
@task
def fetch_subset(source, dest, fraction=DEFAULT_FRACTION, log=True):
cmd = 'rdbms-subsetter {source} {dest} {fraction}'.format(**locals())
if log:
cmd += ' --logarithmic'
for table in (FULL_TABLES + EXCLUDE_TABLES):
cmd += ' --exclude-table {0}'.format(table)
for table, key in FORCE_INCLUDE:
cmd += ' --force {0}:{1}'.format(table, key)
cmd += ' --config data/subset-config.json'
cmd += ' --yes'
run(cmd, echo=True)
@task
def clear_triggers(dest):
"""Clear all triggers in database `dest`.
"""
cmd = 'psql -f data/functions/strip_triggers.sql {dest}'.format(**locals())
run(cmd, echo=True)
@task
def build_test(source, dest, fraction=DEFAULT_FRACTION, log=True):
fetch_full(source, dest)
fetch_schemas(source, dest)
clear_triggers(dest)
fetch_subset(source, dest, fraction=fraction, log=log)
@task
def dump(source, dest):
cmd = 'pg_dump {source} --format c --no-acl --no-owner -f {dest}'.format(**locals())
for table in EXCLUDE_TABLES:
cmd += ' --exclude-table {0}'.format(table)
run(cmd, echo=True)
@task
def add_hooks():
run('ln -s ../../bin/post-merge .git/hooks/post-merge')
run('ln -s ../../bin/post-checkout .git/hooks/post-checkout')
@task
def remove_hooks():
run('rm .git/hooks/post-merge')
run('rm .git/hooks/post-checkout')
def _detect_prod(repo, branch):
"""Deploy to production if master is checked out and tagged."""
if branch != 'master':
return False
try:
# Equivalent to `git describe --tags --exact-match`
repo.git().describe('--tags', '--exact-match')
return True
except git.exc.GitCommandError:
return False
def _resolve_rule(repo, branch):
"""Get space associated with first matching rule."""
for space, rule in DEPLOY_RULES:
if rule(repo, branch):
return space
return None
def _detect_space(branch=None, yes=False):
"""Detect space from active git branch.
:param str branch: Optional branch name override
:param bool yes: Skip confirmation
:returns: Space name if space is detected and confirmed, else `None`
"""
repo = git.Repo('.')
# Fail gracefully if `branch` is not provided and repo is in detached
# `HEAD` mode
try:
branch = branch or repo.active_branch.name
except TypeError:
return None
space = _resolve_rule(repo, branch)
if space is None:
print(
'No space detected from repo {repo}; '
'skipping deploy'.format(**locals())
)
return None
print('Detected space {space} from repo {repo}'.format(**locals()))
if not yes:
run = input(
'Deploy to space {space} (enter "yes" to deploy)? > '.format(**locals())
)
if run.lower() not in ['y', 'yes']:
return None
return space
DEPLOY_RULES = (
('prod', _detect_prod),
('stage', lambda _, branch: branch.startswith('release')),
('dev', lambda _, branch: branch == 'develop'),
)
def _detect_apps(blue, green):
"""Detect old and new apps for blue-green deploy."""
status = run('cf app {0}'.format(blue), echo=True, warn=True)
if status.ok and 'started' in status.stdout:
return (blue, green)
return (green, blue)
SPACE_URLS = {
'dev': [('cf.18f.us', 'fec-dev-api')],
'stage': [('cf.18f.us', 'fec-stage-api')],
'prod': [('cf.18f.us', 'fec-prod-api')],
}
@task
def deploy(space=None, branch=None, yes=False):
"""Deploy app to Cloud Foundry. Log in using credentials stored in
`FEC_CF_USERNAME` and `FEC_CF_PASSWORD`; push to either `space` or the space
detected from the name and tags of the current branch. Note: Must pass `space`
or `branch` if repo is in detached HEAD mode, e.g. when running on Travis.
"""
# Detect space
space = space or _detect_space(branch, yes)
if space is None:
return
# Log in
args = (
('--a', 'https://api.18f.gov'),
('--u', '$FEC_CF_USERNAME'),
('--p', '$FEC_CF_PASSWORD'),
('--o', 'fec'),
('--s', space),
)
run('cf login {0}'.format(' '.join(' '.join(arg) for arg in args)), echo=True)
old, new = _detect_apps('api-a', 'api-b')
# Push
push = run('cf push {0} -f manifest_{1}.yml'.format(new, space), echo=True, warn=True)
if push.failed:
print('Error pushing app {0}'.format(new))
run('cf stop {0}'.format(new), echo=True)
return
# Remap
for route, host in SPACE_URLS[space]:
opts = route
if host:
opts += ' -n {0}'.format(host)
run('cf map-route {0} {1}'.format(new, opts), echo=True)
run('cf unmap-route {0} {1}'.format(old, opts), echo=True, warn=True)
run('cf stop {0}'.format(old), echo=True, warn=True)