forked from coreos/fedora-coreos-streams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollout.py
executable file
·458 lines (412 loc) · 15.2 KB
/
rollout.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
#!/usr/bin/python3
import argparse
from copy import deepcopy
from datetime import datetime, timezone
import dateutil.tz
from itertools import islice
import json
import os
import re
import requests
import string
import time
RELEASES = string.Template("https://builds.coreos.fedoraproject.org/prod/streams/${stream}/releases.json")
# https://github.com/coreos/fedora-coreos-tracker/blob/main/Design.md#version-numbers
VERSION_STREAM_CODES = {
'next': 1,
'testing': 2,
'stable': 3,
}
# Copied from https://github.com/coreos/fedora-coreos-releng-automation/blob/ff24355d21472a281c181d0c9e952871ce2659d8/scripts/versionary.py#L152-L161
def parse_version(version):
m = re.match(r'^([0-9]{2})\.([0-9]{8})\.([0-9]+)\.([0-9]+)$', version)
if m is None:
raise Exception(f'Invalid version {version}')
# sanity-check date
try:
time.strptime(m.group(2), '%Y%m%d')
except ValueError:
raise Exception(f'Invalid date in version {version}')
return tuple(map(int, m.groups()))
def load(path):
'''Load an update JSON file.'''
with open(path) as fh:
return json.load(fh)
def save(path, data):
'''Save an update JSON file, updating the last-modified timestamp.'''
if not os.path.exists(path):
raise Exception(f'Refusing to create {path}')
now = datetime.now(timezone.utc).isoformat(timespec='seconds'). \
replace('+00:00', 'Z')
data['metadata']['last-modified'] = now
with open(path, 'w') as fh:
json.dump(data, fh, indent=' ')
fh.write('\n')
def add(info, version, start, duration, barrier=None, deadend=None,
replace_existing=False):
'''Append a new rollout. Start is an arbitrary human-readable string;
duration is in hours.'''
# Parse and validate date
import dateparser # dnf install python3-dateparser
start_time = dateparser.parse(start, settings={
'PREFER_DATES_FROM': 'future'
})
if start_time is None:
raise Exception(f"Couldn't parse '{start}'")
# Validate duration
if duration <= 0:
raise Exception(f'Duration must be positive; found {duration}')
# Validate version
if info['stream'] not in VERSION_STREAM_CODES:
raise Exception(f"Unknown stream '{info['stream']}'")
version_parts = parse_version(version)
stream_code = version_parts[2]
if VERSION_STREAM_CODES[info['stream']] != stream_code:
raise Exception(f"Incorrect stream code '{stream_code}' in version for {info['stream']} stream")
# Handle duplicate rollouts
if replace_existing:
# Remove existing rollout for this version
info['releases'] = [
rel for rel in info['releases'] if rel['version'] != version
]
else:
# Fail if there's an existing rollout
if [rel for rel in info['releases'] if rel['version'] == version]:
raise Exception(f"Version {version} already exists in update metadata; use --replace to replace it")
# Build rollout
release = {
'version': version,
'metadata': {
'rollout': {
'duration_minutes': duration * 60,
'start_epoch': int(start_time.timestamp()),
'start_percentage': 0.0,
}
}
}
if barrier:
release['metadata']['barrier'] = {
'reason': barrier,
}
if deadend:
release['metadata']['deadend'] = {
'reason': deadend,
}
info['releases'].append(release)
def clean(info):
'''Normalize the release list.'''
completed_rollout = {'start_percentage': 1.0}
# Drop timing parameters for completed rollouts
for rel in info['releases']:
roll = rel['metadata'].get('rollout')
if roll and roll.get('duration_minutes'):
# Have in-progress rollout
end = roll.get('start_epoch', 0) + 60 * roll['duration_minutes']
if end < time.time():
# It's stale; terminate it
rel['metadata']['rollout'] = completed_rollout
# Drop completed rollouts except for the last one
predicate = lambda rel: rel['metadata'].get('rollout') == completed_rollout
for rel in islice(filter(predicate, reversed(info['releases'])), 1, None):
del rel['metadata']['rollout']
# Drop releases that have no rollout, barrier, or deadend
def predicate(rel):
for section in 'barrier', 'deadend', 'rollout':
if section in rel['metadata']:
return True
return False
info['releases'] = [rel for rel in info['releases'] if predicate(rel)]
def report(info, skip_version_check=False):
'''Summarize the latest rollout.'''
stream = info["stream"]
if not info["releases"]:
print(f"{stream} has no rollouts")
return
release = info["releases"][-1]
version = release["version"]
rollout = release["metadata"].get("rollout", None)
if not rollout:
print(f"latest entry {version} on {stream} is not a rollout")
return
latest_info = "unvalidated"
if not skip_version_check:
releases_url = RELEASES.substitute(stream=stream)
releases = requests.get(releases_url).json()["releases"]
versions = [r["version"] for r in releases]
if versions[-1] == version:
latest_info = "latest"
elif version in versions:
latest_info = "*** NOT LATEST ***"
else:
latest_info = "*** UNRELEASED (TYPO?) ***"
start_percentage = rollout["start_percentage"]
# totally just going to ignore floating-point concerns here
if int(start_percentage * 100) == 100:
print(f"{stream} rollout of {version} at 100%")
return
ts = datetime.fromtimestamp(rollout["start_epoch"], timezone.utc)
berlin_ts = ts.astimezone(dateutil.tz.gettz("Europe/Berlin"))
brazil_ts = ts.astimezone(dateutil.tz.gettz("America/Sao_Paulo"))
colorado_ts = ts.astimezone(dateutil.tz.gettz("America/Denver"))
ireland_ts = ts.astimezone(dateutil.tz.gettz("Europe/Dublin"))
raleigh_ts = ts.astimezone(dateutil.tz.gettz("America/Toronto"))
vancouver_ts = ts.astimezone(dateutil.tz.gettz("America/Vancouver"))
mins = rollout["duration_minutes"]
hrs = mins / 60.0
ts_now = datetime.now(timezone.utc)
if ts_now > ts:
delta_str = str(ts_now - ts).split(".")[0]
delta_str = f"{delta_str} ago"
else:
delta_str = str(ts - ts_now).split(".")[0]
delta_str = f"in {delta_str}"
print(f"{stream}")
print(f" version: {version} ({latest_info})")
print(f" start: {ts} UTC ({delta_str})")
print(f" {berlin_ts} Germany/France/Poland")
print(f" {brazil_ts} Brazil")
print(f" {colorado_ts} Colorado")
print(f" {ireland_ts} Ireland")
print(f" {raleigh_ts} Raleigh/New York/Toronto")
print(f" {vancouver_ts} Vancouver")
print(f" duration: {mins}m ({hrs}h)")
def path(stream):
'''Get the relative path of an update JSON file for a stream.'''
return f'updates/{stream}.json'
def _do_add(args):
selftest()
info = load(path(args.stream))
clean(info)
add(info, args.version, args.start, args.duration, barrier=args.barrier,
deadend=args.deadend, replace_existing=args.replace)
report(info, args.skip_version_check)
save(path(args.stream), info)
def _do_clean(args):
selftest()
for stream in args.stream:
info = load(path(stream))
clean(info)
save(path(stream), info)
def _do_print(args):
for stream in args.stream:
info = load(path(stream))
report(info, args.skip_version_check)
def _main():
parser = argparse.ArgumentParser(description='Manage rollouts.')
# "dest" to work around https://bugs.python.org/issue29298
subcommands = parser.add_subparsers(title='subcommands', required=True,
dest='command')
add = subcommands.add_parser('add',
description='Add a rollout and clean up old ones.')
add.set_defaults(func=_do_add)
add.add_argument('stream',
help='stream name (e.g. "testing")')
add.add_argument('version',
help='new release version (e.g. "34.20210501.2.0")')
add.add_argument('start',
help='rollout start (e.g. "10 am")')
add.add_argument('duration', metavar='duration-hours', type=int,
help='rollout duration (e.g. "48")')
add.add_argument('--replace', action='store_true',
help='replace any existing rollout for this version')
add.add_argument('--skip-version-check', action='store_true',
help='skip validating versions')
group = add.add_mutually_exclusive_group()
group.add_argument('--barrier', metavar='reason',
help='make this version a barrier with the specified reason URL')
group.add_argument('--deadend', metavar='reason',
help='make this version a deadend with the specified reason URL')
clean = subcommands.add_parser('clean',
description='Clean up old rollouts.')
clean.set_defaults(func=_do_clean)
clean.add_argument('stream', nargs='+')
print_ = subcommands.add_parser('print',
description='Print latest rollout.')
print_.set_defaults(func=_do_print)
print_.add_argument('--skip-version-check', action='store_true',
help='skip validating versions')
print_.add_argument('stream', nargs='+')
args = parser.parse_args()
args.func(args)
def selftest():
def try_add(input, output, *args, **kwargs):
info = deepcopy(input)
clean(info)
add(info, *args, **kwargs)
# Start time will vary
info['releases'][-1]['metadata']['rollout']['start_epoch'] = \
output['releases'][-1]['metadata']['rollout']['start_epoch']
if info != output:
print(f"Expected: {json.dumps(output, indent=' ')}")
print(f"Found: {json.dumps(info, indent=' ')}")
raise Exception(f"Self-test failed when adding {args} {kwargs}")
input = {
"stream": "stable",
"metadata": {
"last-modified": "2021-07-21T20:10:18Z"
},
"releases": [
{
"version": "31.20200517.3.0",
"metadata": {
"deadend": {
"reason": "https://github.com/coreos/fedora-coreos-tracker/issues/480#issuecomment-631724629"
}
}
},
{
"version": "32.20200615.3.0",
"metadata": {
"barrier": {
"reason": "https://github.com/coreos/fedora-coreos-tracker/issues/484"
},
"rollout": {
"start_percentage": 1.0
}
}
},
{
"version": "34.20210511.3.0",
"metadata": {
"barrier": {
"reason": "https://github.com/coreos/fedora-coreos-tracker/issues/829"
},
"rollout": {
"duration_minutes": 2880,
"start_epoch": 1616742400,
"start_percentage": 5.0
}
}
},
{
"version": "34.20210611.3.0",
"metadata": {
"rollout": {
"duration_minutes": 2880,
"start_epoch": 1616752400,
"start_percentage": 0.0
}
}
},
{
"version": "34.20210626.3.1",
"metadata": {
"barrier": {
"reason": "https://github.com/coreos/fedora-coreos-tracker/issues/829"
},
"rollout": {
"duration_minutes": 1440,
"start_epoch": 1616762400,
"start_percentage": 0.0
}
}
}
]
}
output = {
"stream": "stable",
"metadata": {
"last-modified": "2021-07-21T20:10:18Z"
},
"releases": [
{
"version": "31.20200517.3.0",
"metadata": {
"deadend": {
"reason": "https://github.com/coreos/fedora-coreos-tracker/issues/480#issuecomment-631724629"
}
}
},
{
"version": "32.20200615.3.0",
"metadata": {
"barrier": {
"reason": "https://github.com/coreos/fedora-coreos-tracker/issues/484"
}
}
},
{
"version": "34.20210511.3.0",
"metadata": {
"barrier": {
"reason": "https://github.com/coreos/fedora-coreos-tracker/issues/829"
}
}
},
{
"version": "34.20210626.3.1",
"metadata": {
"barrier": {
"reason": "https://github.com/coreos/fedora-coreos-tracker/issues/829"
},
"rollout": {
"start_percentage": 1.0
}
}
},
{
"version": "35.20210801.3.0",
"metadata": {
"rollout": {
"duration_minutes": 1440,
"start_epoch": 0,
"start_percentage": 0.0
}
}
}
]
}
try_add(input, output, '35.20210801.3.0', '10 AM', 24)
input = {
"stream": "stable",
"metadata": {
"last-modified": "2021-07-21T20:10:18Z"
},
"releases": []
}
output = {
"stream": "stable",
"metadata": {
"last-modified": "2021-07-21T20:10:18Z"
},
"releases": [
{
"version": "31.20200517.3.0",
"metadata": {
"rollout": {
"duration_minutes": 1440,
"start_epoch": 0,
"start_percentage": 0.0
},
"deadend": {
"reason": "a reason"
}
}
}
]
}
try_add(input, output, '31.20200517.3.0', '10 AM', 24, deadend="a reason")
output = {
"stream": "stable",
"metadata": {
"last-modified": "2021-07-21T20:10:18Z"
},
"releases": [
{
"version": "31.20200517.3.0",
"metadata": {
"rollout": {
"duration_minutes": 1440,
"start_epoch": 0,
"start_percentage": 0.0
},
"barrier": {
"reason": "a reason"
}
}
}
]
}
try_add(input, output, '31.20200517.3.0', '10 AM', 24, barrier="a reason")
if __name__ == '__main__':
_main()