-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_management.py
383 lines (314 loc) · 20.7 KB
/
release_management.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
import shutil
import glob, os
import coloredlogs, logging
from dataclasses import dataclass
from release_dataclasses import DeployInfo, ReleaseInfo
import utils
coloredlogs.install()
# define some environment names - feel free to configure these strings for you but changing the varnames means some code has to change
# assuming we go in this order, from development->testing->qualitycontrol->production
production = "PROD"
qualitycontrol = "QC"
testing = "TEST"
development = "DEV"
def gather_deployments(release_info: ReleaseInfo) -> ReleaseInfo:
""" collect_releases: gather all releases for this deploy
In a real system this could look up the info for each deploy such as via an API in e.g. JIRA.
Here it is mocked. """
logging.debug("Collecting Release Info for %s ", release_info.release_key)
# throw an error here sometimes
if utils.isErrorRarely() :
raise RuntimeError(f"Deployment Exception: Gathering releases for {release_info.release_key} failed.!")
# mock gather things to deploy: for this purpose we'll just make some things up
if release_info.release_key == "RELEASE-DEC-2023-BUGFIXES-PROD":
deploy_frontend = DeployInfo(deploy_key= release_info.release_key+":frontend",
container_id= "frontend",
container_version="1.1",
git_url="https://github.com/mybiz/coolfrontend",
approved=True,
approved_by="Josh Smith",
envs=[production],
release_key=release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_frontend.deploy_key} collected.")
deploy_store = DeployInfo(deploy_key = release_info.release_key+":sbstore",
container_id = "sbstore",
container_version="1.7",
git_url = "https://github.com/mybiz/sbstore.git",
approved = True,
approved_by = "Bob Johnson",
envs=[production],
release_key = release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_store.deploy_key} collected.")
deploy_backend_for_frontend = DeployInfo(deploy_key = release_info.release_key+":BFF",
container_id = "backendforfrontend",
container_version="1.7",
git_url = "https://github.com/mybiz/backendff.git",
approved = True,
approved_by = "James Garner",
envs=[production],
release_key = release_info.release_key,
)
elif release_info.release_key == "RELEASE-JAN-24-ALPHA-DEV" :
deploy_frontend = DeployInfo(deploy_key= release_info.release_key+":frontend",
container_id= "frontend",
container_version="1.2",
git_url="https://github.com/mybiz/coolfrontend",
approved=True,
approved_by="Josh Smith",
envs=[development],
release_key=release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_frontend.deploy_key} collected.")
deploy_store = DeployInfo(deploy_key = release_info.release_key+":sbstore",
container_id = "sbstore",
container_version="1.8",
git_url = "https://github.com/mybiz/sbstore.git",
approved = True,
approved_by = "Bob Johnson",
envs=[development],
release_key = release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_store.deploy_key} collected.")
deploy_backend_for_frontend = DeployInfo(deploy_key = release_info.release_key+":BFF",
container_id = "backendforfrontend",
container_version="1.8",
git_url = "https://github.com/mybiz/backendff.git",
approved = True,
approved_by = "James Garner",
envs=[development],
release_key = release_info.release_key,
)
elif release_info.release_key == "RELEASE-JAN-24-ALPHA-QC" :
deploy_frontend = DeployInfo(deploy_key= release_info.release_key+":frontend",
container_id= "frontend",
container_version="1.2",
git_url="https://github.com/mybiz/coolfrontend",
approved=True,
approved_by="Josh Smith",
envs=[testing, qualitycontrol],
release_key=release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_frontend.deploy_key} collected.")
deploy_store = DeployInfo(deploy_key = release_info.release_key+":sbstore",
container_id = "sbstore",
container_version="1.8",
git_url = "https://github.com/mybiz/sbstore.git",
approved = True,
approved_by = "Bob Johnson",
envs=[testing, qualitycontrol],
release_key = release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_store.deploy_key} collected.")
deploy_backend_for_frontend = DeployInfo(deploy_key = release_info.release_key+":BFF",
container_id = "backendforfrontend",
container_version="1.8",
git_url = "https://github.com/mybiz/backendff.git",
approved = True,
approved_by = "James Garner",
envs=[testing, qualitycontrol],
release_key = release_info.release_key,
)
elif release_info.release_key == "RELEASE-JAN-24-ALPHA-PROD":
deploy_frontend = DeployInfo(deploy_key= release_info.release_key+":frontend",
container_id= "frontend",
container_version="1.2",
git_url="https://github.com/mybiz/coolfrontend",
approved=True,
approved_by="Josh Smith",
envs=[production],
release_key=release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_frontend.deploy_key} collected.")
deploy_store = DeployInfo(deploy_key = release_info.release_key+":sbstore",
container_id = "sbstore",
container_version="1.8",
git_url = "https://github.com/mybiz/sbstore.git",
approved = True,
approved_by = "Bob Johnson",
envs=[production],
release_key = release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_store.deploy_key} collected.")
deploy_backend_for_frontend = DeployInfo(deploy_key = release_info.release_key+":BFF",
container_id = "backendforfrontend",
container_version="1.8",
git_url = "https://github.com/mybiz/backendff.git",
approved = True,
approved_by = "James Garner",
envs=[production],
release_key = release_info.release_key,
)
elif release_info.release_key == "RELEASE-NOV-23-INITIAL":
deploy_frontend = DeployInfo(deploy_key= release_info.release_key+":frontend",
container_id= "frontend",
container_version="1.0",
git_url="https://github.com/mybiz/coolfrontend",
approved=True,
approved_by="Josh Smith",
envs=[development, testing, qualitycontrol, production],
release_key=release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_frontend.deploy_key} collected.")
deploy_store = DeployInfo(deploy_key = release_info.release_key+":sbstore",
container_id = "sbstore",
container_version="1.0",
git_url = "https://github.com/mybiz/sbstore.git",
approved = True,
approved_by = "Bob Johnson",
envs=[development, testing, qualitycontrol, production],
release_key = release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_store.deploy_key} collected.")
deploy_backend_for_frontend = DeployInfo(deploy_key = release_info.release_key+":BFF",
container_id = "backendforfrontend",
container_version="1.0",
git_url = "https://github.com/mybiz/backendff.git",
approved = True,
approved_by = "James Garner",
envs=[development, testing, qualitycontrol, production],
release_key = release_info.release_key,
)
elif release_info.release_key == "RELEASE-NOV-23-HOTFIX-STAGE":
deploy_frontend = DeployInfo(deploy_key= release_info.release_key+":frontend",
container_id= "frontend",
container_version="1.1",
git_url="https://github.com/mybiz/coolfrontend",
approved=True,
approved_by="Josh Smith",
envs=[development, testing, qualitycontrol],
release_key=release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_frontend.deploy_key} collected.")
deploy_store = DeployInfo(deploy_key = release_info.release_key+":sbstore",
container_id = "sbstore",
container_version="1.7",
git_url = "https://github.com/mybiz/sbstore.git",
approved = True,
approved_by = "Bob Johnson",
envs=[development, testing, qualitycontrol],
release_key = release_info.release_key,
)
logging.info(f"Deploy Info for { deploy_store.deploy_key} collected.")
deploy_backend_for_frontend = DeployInfo(deploy_key = release_info.release_key+":BFF",
container_id = "backendforfrontend",
container_version="1.7",
git_url = "https://github.com/mybiz/backendff.git",
approved = True,
approved_by = "James Garner",
envs=[development, testing, qualitycontrol],
release_key = release_info.release_key,
)
else:
raise RuntimeError(f"Release Exception: no release info found for {release_info.release_key}! Release failed!")
logging.info(f"Deploy Info for { deploy_backend_for_frontend.deploy_key} collected.")
deploys = [deploy_frontend, deploy_store, deploy_backend_for_frontend]
# throw an error here sometimes
if utils.isErrorRarely() :
raise RuntimeError(f"Release Exception: At end of gathering releases for {release_info.release_key} failed!")
release_info.deploys = deploys
logging.debug(f"Release Info for { release_info.release_key} collected.")
return release_info
def gather_release_approval(release_info: ReleaseInfo) -> ReleaseInfo:
""" gather_release_approval: look up if a release is approved.
This could be provided by an external system, here it is mocked. """
logging.debug("Collecting Release Approval for %s ", release_info.release_key)
# throw an error here sometimes
if utils.isErrorRarely() :
raise RuntimeError(f"Release Exception: Gathering release approval for {release_info.release_key} failed.")
release_info.approved = True
release_info.approved_by = "Joshua Smith"
logging.debug(f"Release Approval Info for { release_info.release_key} collected.")
return release_info
# todo: Build for Dev
# Trigger: ready to be built
# containers
# new version of container
# new version of main/checkin
# need old version and old dates?
# Build done after tests or mark it somewhere or after container is versioned or set somewhere if there's a release overlord
# need something to tie container versions to scans - need scan output and connect them somehow
# // pdfs or something you can link to
# set build done in database - why?
# notify success/fail in slack
def build(deployinfo: DeployInfo) -> str:
""" build: run a build
input: info on an app to build
output: status str
changes: creates a new artificat in the artifact repo place
this could be smart and do different kinds of builds, call an external build system, or whatever, but for demo it is mocked. """
logging.debug("Building for %s ", deployinfo.deploy_key)
# throw an error here sometimes
if utils.isErrorRarely() :
raise RuntimeError(f"Build Exception: build failed, artifact not created.")
#todo fail in a human-repairable way, to simulate a security or build problem that can be repaired
#todo make new artifacts in dev with new container_version
artifactname = deployinfo.container_id + ":" + deployinfo.container_version
f = open(f"./environments/artifactory/{artifactname}", "a")
f.write(f"\nbuilt {artifactname}")
f.close()
#todo create output artifacts to simulate test results and scans
#todo - update container version with new build info?
logging.debug(f"Build successful for {deployinfo.deploy_key}, new artifact in Dev.")
return "BUILD SUCCESSFUL"
def test(deployinfo: DeployInfo, evn: str) -> str:
""" test: validate a build
input: info on an app to test and env to test in
output: status str
this could be smart and do different kinds of tests, like performance or whatever, but for demo it is mocked. """
logging.debug("Testing for %s ", deployinfo.deploy_key)
# throw an error here sometimes
if utils.isErrorRarely() :
raise RuntimeError(f"Test Exception: test failed, could not complete test.")
#todo fail in a human-repairable way, to simulate a performance, security, or quality problem that can be repaired
#todo create output artifacts to simulate test results and scans
logging.debug(f"Test successful for {deployinfo.deploy_key}, new artifact is working in Dev.")
return "TESTS SUCCESSFUL"
# Deploy: for each env
# choose which environments to deploy to: [dev, IQ, CIT, Prod] - no skipping but could only go to lower envs
# assume it was already in the lower environment, so Dev->IQ, IQ->prod, or Dev->IQ->prod
# // pick up config and containers from lower, so like conf from dev and myapp-1.72:dev
# OK to deploy: secure, tested, approved, and scheduled has arrived
# // approved: for Dev: always yes or tech lead says yes
# // secure: passed tests
# // tested: passed automated tests (for dev, may want approvals for higher envs)
# // at the time of release XYZ
# Get container from build process or artifact repo
# install/update container and get from config in git:
# // go get config for env and replace from git by /env folder
# Validate health, if good, yay done, ping /health
# indicate deploy is done in database
def deploy(deployinfo: DeployInfo, destination: str) -> str:
""" deploy: deploy an artifact and its config to a destination
input: deploy info about an application and where to deploy it
output: status str
"""
logging.info(f"Deploying {deployinfo.deploy_key}")
artifactname = deployinfo.container_id + ":" + deployinfo.container_version
source = development
if destination == testing:
source = development
elif destination == qualitycontrol:
source = testing
elif destination == production:
source = qualitycontrol
elif destination == development:
source = "artifactory"
else :
logging.warn(f"Else-if Statement in deploy() hit default case, there's probably a bug.")
# delete existing artifacts
logging.debug(f"Removing old artifacts- ./environments/{destination}/{deployinfo.container_id}* for {deployinfo.deploy_key}")
for f in glob.glob(f"./environments/{destination}/{deployinfo.container_id}*"):
os.remove(f)
logging.debug(f"Moving {artifactname} from {source} to {destination} for {deployinfo.deploy_key}")
shutil.copyfile(f"./environments/{source}/{artifactname}", f"./environments/{destination}/{artifactname}")
logging.debug(f"Artifact Move complete for {deployinfo.deploy_key}...restarting {deployinfo.container_id}.")
#todo something with config?
logging.debug(f"Updating Config for {deployinfo.deploy_key} from {deployinfo.git_url}")
logging.debug(f"Config update complete for {deployinfo.deploy_key}")
# throw an error here sometimes
if utils.isErrorRarely() :
raise RuntimeError(f"Deploy Exception: deploy failed, app not deployed.")
logging.info(f"Deploy successful for {deployinfo.deploy_key}, new artifact is deployed to {destination}.")
return "DEPLOY SUCCESSFUL"