forked from dimagi/mobile-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins_utils.py
460 lines (345 loc) · 16 KB
/
jenkins_utils.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
#!/bin/python
import jenkins
import re
import subprocess
import os
import xml.etree.ElementTree as ET
import utils as util
from version import Version
from user_interaction import verify_value_with_user
from deploy_config import JENKINS_USER, JENKINS_PASSWORD, BRANCH_BASE
MOBILE_VIEW_NAME = "CommCare Mobile"
ARCHIVED_MOBILE_VIEW_NAME = "CommCare Mobile Archive"
j = jenkins.Jenkins('https://jenkins.dimagi.com',
JENKINS_USER,
JENKINS_PASSWORD)
job_roots = ['commcare-core', 'commcare-android']
repo_to_jobs = {'commcare-core': 'commcare-core',
'commcare-android': 'commcare-android'}
# None -> String
def create_new_release_jobs():
"""
Copy last releases jenkins jobs and update values to mirror the release
being staged.
"""
version = get_next_release_version()
last_release = version.get_last_version_short()
verify_message = ("Are these values correct?: " +
"last release: {}, this release {}").format(last_release,
version)
exit_message = "Release versions from jenkins are incorrect, exiting."
verify_value_with_user(verify_message, exit_message)
assert_jobs_dont_exist(version)
for job_root in job_roots:
create_new_release_job(job_root, last_release, version)
archive_old_release_job(job_root, version)
set_build_numbers(version)
inc_minor_version('commcare-android')
inc_minor_version('commcare-android-{}'.format(version.short_string()))
return version.short_string()
# None -> Version
def get_next_release_version():
"""
Reads the version number off of the 'commcare-core' job, which should be
set to the next release.
"""
xml = j.get_job_config('commcare-android')
versionPattern = re.compile(r'VERSION=(\d+).(\d+).(\d+)')
current_version_raw = versionPattern.search(xml).groups()
if len(current_version_raw) != 3:
raise Exception("Couldn't find next version to deploy")
return Version(*map(int, current_version_raw))
# Version -> None
def assert_jobs_dont_exist(version):
for job_root in job_roots:
job = '{}-{}'.format(job_root, version.short_string())
if j.job_exists(job):
raise Exception("'{}' jenkins job already exists".format(job))
# String String Version -> None
def create_new_release_job(base_job_name, last_release, new_release_version):
last_release_job_name = '{}-{}'.format(base_job_name, last_release)
xml = j.get_job_config(last_release_job_name)
new_version = new_release_version.short_string()
new_release_job_name = '{}-{}'.format(base_job_name, new_version)
print("Creating job '{}' from old job '{}'".format(new_release_job_name,
last_release_job_name))
xml = replace_references_to_old_jobs(xml, last_release, new_version)
old_version = get_old_tag_version(xml)
old_tag_name = 'refs/tags/commcare_{}'.format(old_version)
new_ref_name = 'refs/heads/commcare_{}'.format(new_version)
print("Replacing git tag from {} to {}".format(old_tag_name, new_ref_name))
xml = xml.replace(old_tag_name, new_ref_name)
xml = xml.replace("CCCORE_BRANCH=commcare_{}".format(old_version),
"CCCORE_BRANCH=commcare_{}".format(new_version))
j.create_job(new_release_job_name, xml)
print("Adding {} job to {} view".format(new_release_job_name,
MOBILE_VIEW_NAME))
add_job_to_view(new_release_job_name, MOBILE_VIEW_NAME)
def replace_references_to_old_jobs(xml, last_release, new_version):
for job_base in job_roots:
xml = xml.replace('{}-{}'.format(job_base, last_release),
'{}-{}'.format(job_base, new_version))
return xml
def get_old_tag_version(xml):
versionPattern = re.compile(r'refs/tags/commcare_(\d+).(\d+).(\d+)')
branch_version_numbers = versionPattern.search(xml).groups()
if len(branch_version_numbers) != 3:
raise Exception("couldn't find git branch reference of format " +
"refs/tags/commcare_X.X.X")
return '{}.{}.{}'.format(*branch_version_numbers)
# String Version -> None
def archive_old_release_job(base_job_name, version):
two_release_ago = Version(version.major, version.minor - 2, 0)
last_release_job_name = '{}-{}'.format(base_job_name,
two_release_ago.short_string())
print("moving {} to {} view".format(last_release_job_name,
ARCHIVED_MOBILE_VIEW_NAME))
remove_job_from_view(last_release_job_name, MOBILE_VIEW_NAME)
add_job_to_view(last_release_job_name, ARCHIVED_MOBILE_VIEW_NAME)
# String -> None
def add_job_to_view(job_name, view_name):
xml = j.get_view_config(view_name)
tree = ET.fromstring(xml)
jobs = tree.find('jobNames')
e = ET.SubElement(jobs, 'string')
e.text = job_name
new_xml = ET.tostring(tree).decode('utf-8')
j.reconfig_view(view_name, new_xml)
# String -> None
def remove_job_from_view(job_name, view_name):
xml = j.get_view_config(view_name)
tree = ET.fromstring(xml)
jobs = tree.find('jobNames')
for job in jobs.getchildren():
if job.text == job_name:
jobs.remove(job)
break
new_xml = ET.tostring(tree).decode('utf-8')
j.reconfig_view(view_name, new_xml)
# Version -> None
def set_build_numbers(new_version):
"""
Update next build number for new release jobs by 1 and master jobs by 2000.
"""
print("updating build numbers on jenkins jobs")
update_release_build_number('commcare-core', new_version, 1)
update_release_build_number('commcare-android', new_version, 1)
update_master_build_number('commcare-core', 2000)
update_master_build_number('commcare-android', 2000)
# String Version Integer -> None
def update_release_build_number(job_base, current_version, increment_by):
current_build_number = j.get_job_info(job_base)['nextBuildNumber']
new_job = '{}-{}'.format(job_base, current_version.short_string())
next_build_number = int(current_build_number) + increment_by
print('INFO:\t{} build #: {}'.format(job_base, current_build_number))
print('\t\tsetting {} build # to {}'.format(new_job, next_build_number))
create_next_build_number_file(next_build_number)
upload_next_build_number(new_job, next_build_number)
os.remove('nextBuildNumber')
# String Integer -> None
def update_master_build_number(job_name, increment_by):
current_build_number = j.get_job_info(job_name)['nextBuildNumber']
next_build_number = int(current_build_number) + increment_by
print("INFO\tupdating {} build # " +
"from {} to {}".format(job_name,
current_build_number,
next_build_number))
create_next_build_number_file(next_build_number)
upload_next_build_number(job_name, next_build_number)
os.remove('nextBuildNumber')
def create_next_build_number_file(next_build_number):
f = open('nextBuildNumber', 'w', encoding='utf-8', newline='\n')
f.write('{}\n'.format(next_build_number))
f.close()
# String Integer -> None
def upload_next_build_number(job, next_build_number):
try:
j.set_next_build_number(job, next_build_number)
except Exception:
show_manual_next_build_message(job, next_build_number)
return
# String Integer -> None
def show_manual_next_build_message(job_name, next_build_number):
print('Failed setting nextBuildNumber for {}'.format(job_name))
next_build_url = ("https://jenkins.dimagi.com/" +
"job/{}/nextbuildnumber/").format(job_name)
print(("Please manually set {}'s nextBuildNumber " +
"to {} at \n {}").format(job_name, next_build_number,
next_build_url))
# String -> None
def reload_job_into_jenkins_memory(job_name):
"""
Force jenkins to reload a job config from memory. Necessary if config
files, such as nextBuildNumber, have changed.
"""
xml = j.get_job_config(job_name)
j.reconfig_job(job_name, xml)
# String -> None
def inc_minor_version(job_name):
"""
Bump the VERSION build parameter by a minor version.
"""
print(("Incrementing the minor version # on " +
"{} jenkins job").format(job_name))
xml = j.get_job_config(job_name)
versionPattern = re.compile(r'VERSION=(\d+).(\d+).(\d+)')
current_version_raw = versionPattern.search(xml).groups()
if len(current_version_raw) != 3:
raise Exception("Couldn't parse version")
current_version = Version(*map(int, current_version_raw))
next_minor_version = current_version.get_next_minor_release()
print('changing {} version reference {} to {}'.format(job_name,
current_version,
next_minor_version))
xml = xml.replace("VERSION={}".format(current_version),
"VERSION={}".format(next_minor_version))
j.reconfig_job(job_name, xml)
# String -> None
def inc_hotfix_version(version):
inc_hotfix_version_on_job("commcare-android", version)
# String String -> None
def inc_hotfix_version_on_job(base_job_name, version):
"""
Bump the commcare-android VERSION build parameter of a release job by a
hotfix version.
"""
job_name = "{}-{}".format(base_job_name, version.short_string())
xml = j.get_job_config(job_name)
versionPattern = re.compile(r'VERSION=(\d+).(\d+).(\d+)')
current_version_raw = versionPattern.search(xml).groups()
if len(current_version_raw) != 3:
raise Exception("Couldn't parse version")
current_version = Version(*map(int, current_version_raw))
next_hotfix_version = current_version.get_next_hotfix()
print('changing {} version reference {} to {}'.format(job_name,
current_version,
next_hotfix_version))
xml = xml.replace("VERSION={}".format(current_version),
"VERSION={}".format(next_hotfix_version))
j.reconfig_job(job_name, xml)
# String String Version -> None
def make_release_jobs_use_tags(branch, tag, version):
for job_root in job_roots:
make_release_job_use_tag(job_root, version.short_string(), branch, tag,
job_root == "commcare-android")
# String String String String Boolean -> None
def make_release_job_use_tag(base_job_name, version_str, branch, tag,
update_core_ref):
"""
Update release job to build off a tag instead of a dev branch.
"""
full_branch = "refs/heads/{}".format(branch)
full_tag = "refs/tags/{}".format(tag)
replacement_map = [(full_branch, full_tag)]
if update_core_ref:
# commcare-android-X.XX uses CCCORE_BRANCH to checkout the proper
# commcare-core branch
full_core_branch = "CCCORE_BRANCH={}".format(branch)
full_core_tag = "CCCORE_BRANCH={}".format(tag)
replacement_map.append((full_core_branch, full_core_tag))
replace_references_in_job(base_job_name, version_str,
replacement_map)
# String String String String Boolean -> None
def make_release_job_use_branch(base_job_name, version_str, tag, branch,
update_core_ref):
"""
Update release job to build off a dev branch instead of a release tag.
"""
full_tag = "refs/tags/{}".format(tag)
full_branch = "refs/heads/{}".format(branch)
replacement_map = [(full_tag, full_branch)]
if update_core_ref:
# commcare-android-X.XX uses CCCORE_BRANCH to checkout the proper
# commcare-core branch
full_core_tag = "CCCORE_BRANCH={}".format(tag)
full_core_branch = "CCCORE_BRANCH={}".format(branch)
replacement_map.append((full_core_tag, full_core_branch))
print(("changing jenkins job {} to build with following update" +
"{}").format(base_job_name, replacement_map))
replace_references_in_job(base_job_name, version_str,
replacement_map)
# String String [List-of (String, String)] -> None
def replace_references_in_job(base_job_name, version_str, replacement_map):
"""
Updates job such that each occurence of the provided mapping key is
replaced with the mapping value.
"""
job_name = '{}-{}'.format(base_job_name, version_str)
print("updating {} to build with: {}".format(job_name, replacement_map))
xml = j.get_job_config(job_name)
for current_ref, new_ref in replacement_map:
xml = xml.replace(current_ref, new_ref)
j.reconfig_job(job_name, xml)
# Version String -> None
def build_release(version):
j.build_job("commcare-core-{}".format(version.short_string()))
print(("Release builds have been triggered. " +
"When they finish (~10 minutes) name them {} " +
"and mark 'keep this build forever'").format(version))
# None -> Version
def get_latest_release_job_version():
"""
Reads the version number off of the commcare-android job, and use it to
find the hotfix version in the latest commcare-android-X.XX job.
"""
master_xml = j.get_job_config('commcare-android')
versionPattern = re.compile(r'VERSION=(\d+).(\d+).(\d+)')
next_version_raw = versionPattern.search(master_xml).groups()
if len(next_version_raw) != 3:
raise Exception("Couldn't find next version to deploy")
next_version = Version(*map(int, next_version_raw))
last_version = next_version.get_last_version_short()
staged_release_job = 'commcare-android-{}'.format(last_version)
release_xml = j.get_job_config(staged_release_job)
current_version_raw = versionPattern.search(release_xml).groups()
if len(current_version_raw) != 3:
raise Exception("Couldn't find next version to deploy")
return Version(*map(int, current_version_raw))
# [List-of String] -> None
def build_jobs_against_hotfix_branches(hotfix_repos):
"""
Make release jobs for hotfix repos build off of the newly opened hotfix
branches.
"""
# NOTE PLM: not sure if the hotfix version is correct, but it is okay
# because it isn't used here
version = get_latest_release_job_version()
version_short = version.short_string()
for repo in hotfix_repos:
last_hotfix = util.get_last_hotfix_number_in_repo(repo, version_short)
hotfix_version = Version(version.major,
version.minor,
last_hotfix)
tag = get_tag_name(hotfix_version)
job_name = repo_to_jobs[repo]
branch = get_branch_name(version)
update_core_ref = (repo == "commcare-android" and
"commcare-core" in hotfix_repos)
make_release_job_use_branch(job_name, version.short_string(),
tag, branch, update_core_ref)
# [List-of String] -> None
def build_jobs_against_hotfix_tags(hotfix_repos):
"""
Make release jobs for hotfix repos build off of the newly created hotfix
tags.
"""
# NOTE PLM: not sure if the hotfix version is correct, but it is okay
# because it isn't used here
version = get_latest_release_job_version()
version_short = version.short_string()
for repo in hotfix_repos:
last_hotfix = util.get_last_hotfix_number_in_repo(repo, version_short)
hotfix_version = Version(version.major,
version.minor,
last_hotfix)
tag = get_tag_name(hotfix_version)
job_name = repo_to_jobs[repo]
branch = get_branch_name(version)
update_core_ref = (repo == "commcare-android" and
"commcare-core" in hotfix_repos)
make_release_job_use_tag(job_name, version.short_string(), branch, tag,
update_core_ref)
def get_branch_name(v):
return "{}{}".format(BRANCH_BASE, v.short_string())
def get_tag_name(v):
return "{}{}".format(BRANCH_BASE, v)