-
Notifications
You must be signed in to change notification settings - Fork 20
/
rpm-ostree-bisect
executable file
·434 lines (368 loc) · 13.6 KB
/
rpm-ostree-bisect
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
#!/usr/bin/python3
#
# Copyright 2018 Dusty Mabe <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
#
# This program will bisect your RPM-OSTree system. Some high level
# representation of what it does is:
#
# grab info on every commit in history
# A -> B -> C -> D -> E -> F -> G
#
# user provided good/bad commits
# - good (default to first in history: A)
# - bad (default to current commit: G)
#
# run test script
# returns 0 for pass and 1 for failure
#
# known good is A, known bad is G
#
# start bisect:
# deploy D, test --> bad
# mark D, E, F bad
#
# deploy B, test --> good
# mark B good
#
# deploy C, test --> bad
#
# Failure introduced in B -> C
#
# At a minimum place this script in /usr/local/bin/rpm-ostree-bisect
# and create a test script at /usr/local/bin/test.sh. Then:
#
# $ rpm-ostree-bisect --testscript /usr/local/bin/test.sh && reboot
# ORR
# $ python2 /usr/local/bin/rpm-ostree-bisect --testscript /usr/local/bin/test.sh && reboot
#
# Later check systemctl status rpm-ostree-bisect.service for result
#
# python2 compatibility
from __future__ import print_function
import argparse
import json
import os
import os.path
import subprocess
import sys
import tempfile
from collections import OrderedDict
import gi
gi.require_version('OSTree', '1.0')
from gi.repository import GLib, Gio, OSTree
DATA_FILE = '/var/lib/rpm-ostree-bisect.json'
SYSTEMD_UNIT_FILE = '/etc/systemd/system/rpm-ostree-bisect.service'
SYSTEMD_UNIT_NAME = 'rpm-ostree-bisect.service'
SYSTEMD_UNIT_CONTENTS = """
[Unit]
Description=RPM-OSTree Bisect Testing
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
#ExecStart=/usr/bin/sleep 20
ExecStart=%s %s --resume
StandardOutput=journal+console
StandardError=journal+console
[Install]
WantedBy=multi-user.target
""" % (sys.executable, sys.argv[0])
def fatal(msg):
print(msg, file=sys.stderr)
sys.exit(1)
def log(msg):
print(msg)
sys.stdout.flush()
"""
Find out of the given commitid is a layered commit (i.e. layered
packages). We'll cue off of the `rpmostree.clientlayer` metadata for this.
"""
def is_layered_commit(repo, commitid):
# Grab commit object. If None then history has been
# trimmed from the remote and we can break
_, commit = repo.load_variant_if_exists(
OSTree.ObjectType.COMMIT, commitid)
# Grab version info from commit
meta = commit.get_child_value(0)
return meta.lookup_value('rpmostree.clientlayer', GLib.VariantType.new('b'))
"""
Find the base commit of the deployment for the system. This
only differs from what you would expect if the system has
layered packages.
"""
def get_deployed_base_commit(deployment, repo):
commitid = deployment.get_csum()
if is_layered_commit(repo, commitid):
_, commit = repo.load_variant_if_exists(
OSTree.ObjectType.COMMIT, commitid)
commitid = OSTree.commit_get_parent(commit)
return commitid
"""
Initialize commit info ordered dict. The array will be a list of
commits in descending order. Each entry will be a dict with
key of commitid and value = a dict of version, heuristic
(TESTED, GIVEN, ASSUMED), and status (GOOD/BAD/UNKNOWN)
commits = {
'abcdef' => { 'version': '28.20180302.0' ,
'heuristic', 'GIVEN',
'status': 'BAD',
},
'bbcdef' => { 'version': '28.20180301.0' ,
'heuristic', 'ASSUMED',
'status': 'UNKNOWN',
},
'cbcdef' => { 'version': '28.20180228.0' ,
'heuristic', 'TESTED',
'status': 'GOOD',
},
}
"""
def initialize_commits_info(repo, bad, good):
# An ordered dictionary of commit info
info = OrderedDict()
# The first commit in our list will be the "BAD" commit
commitid = bad
# Iterate over all commits and add them to the dict
while commitid is not None:
# Grab commit object. If None then history has been
# trimmed from the remote and we can break
_, commit = repo.load_variant_if_exists(
OSTree.ObjectType.COMMIT, commitid)
if commit is None:
break
# Grab version info from commit
meta = commit.get_child_value(0)
version = meta.lookup_value('version', GLib.VariantType.new('s'))
if version is None:
version = ''
else:
version = version.get_string()
# Grab timestamp info from commit
commit_ts = OSTree.commit_get_timestamp(commit)
commit_datetime = GLib.DateTime.new_from_unix_utc(commit_ts)
commit_datetime_iso8601 = commit_datetime.format("%FT%H:%M:%SZ")
# Update info dict
info.update({ commitid: { 'version': version,
'timestamp': commit_datetime_iso8601,
'heuristic': 'ASSUMED',
'status': 'UNKNOWN' }})
# Next iteration
commitid = OSTree.commit_get_parent(commit)
# Mark the bad commit bad
info[bad]['status'] = 'BAD'
info[bad]['heuristic'] = 'GIVEN'
# Mark the good commit good
if good:
info[good]['status'] = 'GOOD'
info[good]['heuristic'] = 'GIVEN'
return info
"""
Grab all commit history from the remote (just
the metadata).
"""
def pull_commit_history(deployment, repo):
# Get repo, remote and refspec from the booted deployment
# The refspec in the metadata is either `refspec` if there
# are no layered packages (i.e. the deployed commit is a base
# commit) or `baserefspec` if there are or ever have been.
# Try first for `refspec` and fall back to `baserefspec.
origin = deployment.get_origin()
refspec = ""
try:
refspec = origin.get_string('origin', 'refspec')
except GLib.Error as e:
# If not a "key not found" error then raise the exception
if not e.matches(GLib.KeyFile.error_quark(), GLib.KeyFileError.KEY_NOT_FOUND):
raise(e)
# Fallback to `baserefspec`
refspec = origin.get_string('origin', 'baserefspec')
_, remote, ref = OSTree.parse_refspec(refspec)
# Run the ostree pull operation. Use the binary here rather than
# the Python API because binary will properly remount /sysroot
# read/write on systems that have it mounted read-only.
cmd = ['/usr/bin/ostree', 'pull',
'--commit-metadata-only', '--depth=-1', f'{remote}:{ref}']
subprocess.call(cmd)
def load_data(datafile):
with open(datafile, 'r') as f:
data = json.load(f, object_pairs_hook=OrderedDict)
commits_info = data['commits_info']
testscript = data['test_script']
return commits_info, testscript
def write_data(datafile, commits_info, testscript):
data = { 'commits_info': commits_info,
'test_script': testscript }
dirname = os.path.dirname(datafile)
(_, tmpfile) = tempfile.mkstemp(
dir=dirname,
prefix="rpm-ostree-bisect")
with open(tmpfile, 'w') as f:
json.dump(data, f, indent=4)
os.rename(tmpfile, datafile)
def verify_script(testscript):
# Verify test script exists and is executable
if not testscript:
fatal("Must provide a --testscript to run")
if not (os.path.isfile(testscript)
and os.access(testscript, os.X_OK)):
fatal("provided test script: %s is not an executable file"
% testscript)
def bisect_start(args, deployment, repo):
badcommit = args.badcommit
goodcommit = args.goodcommit
datafile = args.datafile
testscript = args.testscript
# verify test script
verify_script(testscript)
# Assume currently booted commit is bad if no
# bad commit was given
if badcommit is None:
badcommit = get_deployed_base_commit(deployment, repo)
# pull commit history
pull_commit_history(deployment, repo)
# initialize data
commits_info = initialize_commits_info(repo,
badcommit,
goodcommit)
# Write data to file
write_data(datafile, commits_info, testscript)
# write/enable systemd unit
with open(SYSTEMD_UNIT_FILE, 'w') as f:
f.write(SYSTEMD_UNIT_CONTENTS)
cmd = ['/usr/bin/systemctl', 'daemon-reload']
subprocess.call(cmd)
cmd = ['/usr/bin/systemctl', 'enable', SYSTEMD_UNIT_NAME]
subprocess.call(cmd)
return 0
def bisect_resume(args, deployment, repo):
badcommit = args.badcommit
goodcommit = args.goodcommit
datafile = args.datafile
# load data
commits_info, testscript = load_data(datafile)
# verify test script
verify_script(testscript)
# run test
ec = subprocess.call(testscript, shell=True)
if ec == 0:
success = True
else:
success = False
# update and write data
commit = get_deployed_base_commit(deployment, repo)
if success:
for c in reversed(commits_info.keys()):
commits_info[c]['status'] = 'GOOD'
if c == commit:
commits_info[c]['heuristic'] = 'TESTED'
break
else:
for c in commits_info.keys():
commits_info[c]['status'] = 'BAD'
if c == commit:
commits_info[c]['heuristic'] = 'TESTED'
break
write_data(datafile, commits_info, testscript)
# Find list of unknown status commits
unknowns = []
lastbad = None
firstgood = None
for commitid in commits_info.keys():
status = commits_info[commitid]['status']
if status == 'BAD':
lastbad = commitid
elif status == 'UNKNOWN':
unknowns.append(commitid)
elif status == 'GOOD':
firstgood = commitid
break
if len(unknowns) == 0:
# We're done!
cmd = ['/usr/bin/systemctl', 'disable', SYSTEMD_UNIT_NAME]
subprocess.call(cmd)
log("BISECT TEST RESULTS:")
if firstgood is None:
log("No good commits were found in the history!")
log("Is it possible the remote has trimmed history?")
return 0
# Do a sanity check to see if the good commit was actually tested.
if commits_info[firstgood]['heuristic'] == 'GIVEN':
log("WARNING: The good commit detected was the one given by the user.")
log("WARNING: Are you sure this commit is good?")
log("Last known good commit:\n %s : %s : %s" %
(firstgood[0:7],
commits_info[firstgood]['version'],
commits_info[firstgood]['timestamp']))
log("First known bad commit:\n %s : %s : %s" %
(lastbad[0:7],
commits_info[lastbad]['version'],
commits_info[lastbad]['timestamp']))
# print out some db info
pull_commit_history(deployment, repo)
cmd = ['/usr/bin/rpm-ostree', 'db', 'diff',
firstgood, lastbad]
subprocess.call(cmd)
return 0
# Bisect for new test commit id
newcommitid = unknowns[len(unknowns)//2]
# Deploy new commit for testing. Retry download if fails
log("Deploying new commit for testing\n\t%s : %s : %s" %
(newcommitid[0:7],
commits_info[newcommitid]['version'],
commits_info[newcommitid]['timestamp']))
cmd = ['/usr/bin/rpm-ostree', 'deploy', newcommitid]
log("Trying to run '%s'" % cmd)
tries = 30 # retry if download timesout
while tries > 0:
ec = subprocess.call(cmd)
if ec == 0:
break
tries = tries - 1
# If we deployed the new commit then we can reboot!
if tries > 0:
# Success, reboot now
cmd = ['/usr/sbin/shutdown', 'now', '-r']
subprocess.call(cmd)
else:
fatal("Failed to deploy new commit")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--bad", dest='badcommit',
help="Known Bad Commit", action='store')
parser.add_argument("--good", dest='goodcommit',
help="Known Good Commit", action='store')
parser.add_argument("--testscript", help="A test script to run",
action='store')
parser.add_argument("--resume", help="Resume a running bisection",
action='store_true')
parser.add_argument("--datafile", help="data file to use for state",
action='store', default=DATA_FILE)
args = parser.parse_args()
# Get sysroot, deployment, repo
sysroot = OSTree.Sysroot.new_default()
sysroot.load(None)
deployment = sysroot.get_booted_deployment()
if deployment is None:
fatal("Not in a booted OSTree system!")
_, repo = sysroot.get_repo(None)
log("Using data file at: %s" % args.datafile)
if args.resume:
bisect_resume(args, deployment, repo)
else:
bisect_start(args, deployment, repo)
if __name__ == '__main__':
main()