-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtasks.py
255 lines (202 loc) · 8.21 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
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
import git
import json
import os
import sys
from shutil import copyfile
from invoke import task
APP_NAME = "fecfile-web-app"
ORG_NAME = "fec-fecfileonline-prototyping"
def _detect_space(repo, branch=None):
"""Detect space from active git branch.
:param str branch: Optional branch name override
:returns: Space name if space is detected and confirmed, else `None`
"""
space = _resolve_rule(repo, branch)
if space is None:
print(
"The current configuration does not require a deployment to cloud.gov. "
)
return None
print("Detected space {space}".format(**locals()))
return space
def _resolve_rule(repo, branch):
"""Get space associated with first matching rule."""
for space, rule in DEPLOY_RULES:
if rule(repo, branch):
print(f"Deploying to {space} due to matching branch name {branch}")
return space
print(f"Current branch {branch} does not match any deployment specifications.")
print(f"Skipping deployment.")
return None
def _detect_branch(repo):
try:
return repo.active_branch.name
except TypeError:
return None
DEPLOY_RULES = (
("test", lambda _, branch: branch == "main"),
("stage", lambda _, branch: branch.startswith("release")),
("dev", lambda _, branch: branch == "develop"),
)
def _build_angular_app(ctx, space):
orig_directory = os.getcwd()
os.chdir(os.path.join(orig_directory, "front-end"))
ctx.run("npm install", warn=True, echo=True)
print(f"Starting build: npm run build-{space}")
result = ctx.run(f"npm run build-{space}", warn=True, echo=True)
if result.return_code != 0:
print(f"error building Angular app. Exiting with code {result.return_code}")
exit(result.return_code)
os.chdir(orig_directory)
# copies a few nginx config files into the Angualr app distribution directory
def _prep_distribution_directory(ctx):
dist_directory = os.path.join(os.getcwd(), "front-end", "dist")
nginx_config_dir = os.path.join(
os.getcwd(), "deploy-config", "front-end-nginx-config"
)
copyfile(
os.path.join(nginx_config_dir, "nginx.conf"),
os.path.join(dist_directory, "nginx.conf"),
)
copyfile(
os.path.join(nginx_config_dir, "mime.types"),
os.path.join(dist_directory, "mime.types"),
)
def _login_to_cf(ctx, space):
# Set api
api = "https://api.fr.cloud.gov"
ctx.run(f"cf api {api}", echo=True)
# Authenticate
user_var_name = f"FEC_CF_USERNAME_{space.upper()}"
pass_var_name = f"FEC_CF_PASSWORD_{space.upper()}"
login_command = f'cf auth "${user_var_name}" "${pass_var_name}"'
result = ctx.run(login_command, echo=True, warn=True)
if result.return_code != 0:
print("\n\nError logging into cloud.gov.")
if os.getenv(user_var_name) and os.getenv(pass_var_name):
print("Please check your authentication environment variables:")
print(f" - {user_var_name}")
print(f" - {pass_var_name}")
else:
print(f"You must set the {user_var_name} and {pass_var_name} environment ")
print(f"variables with space-deployer service account credentials")
print(f"")
print(
f"If you don't have a service account, you can create one with the following commands:"
)
print(
f" cf login -u [email-address] -o {ORG_NAME} -a api.fr.cloud.gov --sso"
)
print(f" cf target -o {ORG_NAME} -s {space}")
print(
f" cf create-service cloud-gov-service-account space-deployer [my-service-account-name]"
)
print(
f" cf create-service-key [my-server-account-name] [my-service-key-name]"
)
print(f" cf service-key [my-server-account-name] [my-service-key-name]")
exit(1)
def _do_deploy(ctx, space):
orig_directory = os.getcwd()
os.chdir(os.path.join(orig_directory, "front-end", "dist"))
print(f"new dir {os.getcwd()}")
manifest_filename = os.path.join(
orig_directory, "deploy-config", f"{APP_NAME}-{space}-manifest.yml"
)
existing_deploy = ctx.run("cf app {0}".format(APP_NAME), echo=True, warn=True)
print("\n")
cmd = "push --strategy rolling" if existing_deploy.ok else "push"
new_deploy = ctx.run(
f"cf {cmd} {APP_NAME} -f {manifest_filename}",
echo=True,
warn=True,
)
os.chdir(orig_directory)
return new_deploy
def _print_help_text():
help_text = """
Usage:
invoke deploy [--space SPACE] [--branch BRANCH] [--login LOGIN] [--help] [--nobuild]
--space SPACE If provided, the SPACE space in cloud.gov will be targeted for deployment.
Either --space or --branch must be provided
Allowed values are dev, stage, test, and prod.
--branch BRANCH Name of the branch to use for deployment. Will auto-detect
the git branch in the current directory by default
Either --space or --branch must be provided
--login If this flag is set, deploy with attempt to login to a
service account specified in the environemnt variables
$FEC_CF_USERNAME_[SPACE] and $FEC_CF_PASSWORD_[SPACE]
--help If set, display help/usage text and exit
--nobuild If set, skip the Angular applicaiton build process. Useful
for debugging, but should not be used in most cases.
"""
print(help_text)
def _rollback(ctx):
print("Build failed!")
# Check if there are active deployments
app_guid = ctx.run("cf app {} --guid".format(APP_NAME), hide=True, warn=True)
app_guid_formatted = app_guid.stdout.strip()
status = ctx.run(
'cf curl "/v3/deployments?app_guids={}&status_values=ACTIVE"'.format(
app_guid_formatted
),
hide=True,
warn=True,
)
active_deployments = (
json.loads(status.stdout).get("pagination").get("total_results")
)
# Try to roll back
if active_deployments > 0:
print("Attempting to roll back any deployment in progress...")
# Show the in-between state
ctx.run("cf app {}".format(APP_NAME), echo=True, warn=True)
cancel_deploy = ctx.run(
"cf cancel-deployment {}".format(APP_NAME), echo=True, warn=True
)
if cancel_deploy.ok:
print("Successfully cancelled deploy. Check logs.")
else:
print("Unable to cancel deploy. Check logs.")
@task
def deploy(ctx, space=None, branch=None, login=False, help=False, nobuild=False):
"""Deploy app to Cloud Foundry.
Log in using credentials stored per environment
like `FEC_CF_USERNAME_DEV` and `FEC_CF_PASSWORD_DEV`;
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 Circle.
Example usage: invoke deploy --space dev
"""
if help:
_print_help_text()
exit(0)
# Detect space
repo = git.Repo(".")
branch = branch or _detect_branch(repo)
space = space or _detect_space(repo, branch)
if space is None:
# this is not an error condition, it just means the current space/branch is not
# a candidate for deployment. Return successful exit code
return sys.exit(0)
if login:
_login_to_cf(ctx, space)
if not nobuild:
_build_angular_app(ctx, space)
_prep_distribution_directory(ctx)
# Target space
ctx.run("cf target -o {0} -s {1}".format(ORG_NAME, space), echo=True)
# Set deploy variables
with open(".cfmeta", "w") as fp:
json.dump({"user": os.getenv("USER"), "branch": branch}, fp)
new_deploy = _do_deploy(ctx, space)
if not new_deploy.ok:
_rollback(ctx)
return sys.exit(1)
ctx.run("cf apps", echo=True, warn=True)
print(
f"A new version of your application '{APP_NAME}' has been successfully pushed!"
)
# Needed for CircleCI
return sys.exit(0)