-
Notifications
You must be signed in to change notification settings - Fork 169
/
cmd-build-validate
executable file
·72 lines (58 loc) · 2.3 KB
/
cmd-build-validate
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
#!/usr/bin/env python3
import os
import sys
import subprocess
import json
import argparse
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from cosalib.builds import Builds
from cosalib.cmdlib import sha256sum_file
parser = argparse.ArgumentParser()
parser.add_argument("--build", help="Build ID")
parser.add_argument("--image", help="Just verify given image type")
parser.add_argument("--no-checksum", help="Skip validating checksum", action='store_true')
args = parser.parse_args()
def validate_checksum(builddir, imgtype, data):
path = os.path.join(builddir, data['path'])
if not args.no_checksum:
actual = sha256sum_file(path)
expected = data['sha256']
if actual != expected:
raise SystemExit(f"Corrupted image '{imgtype}'; found sha256={actual} expected={expected}")
def validate_build(builddir):
buildmeta_path = os.path.join(builddir, 'meta.json')
with open(buildmeta_path) as f:
buildmeta = json.load(f)
found = args.image is None
for img_format, data in buildmeta['images'].items():
if args.image is not None and img_format != args.image:
continue
found = True
print(f"Validating artifact: {img_format}")
validate_checksum(builddir, img_format, data)
if img_format == 'ostree':
# In the future maybe we'll unpack and ostree fsck or so
continue
if img_format.endswith(('iso', 'kernel', 'initramfs')):
# And we could validate these too
continue
# Otherwise, assume it's a mountable disk image we can fsck
validate_image(builddir, img_format, data)
if not found:
raise SystemExit("Failed to find image type: {args.image}")
def validate_image(builddir, imgtype, data):
path = os.path.join(builddir, data['path'])
rc = subprocess.call(['/usr/lib/coreos-assembler/gf-fsck', path])
if rc != 0:
raise SystemExit(f"Detected corrupted image: {imgtype}")
print(f"Validated image: {imgtype}")
builds = Builds()
# default to latest build if not specified
if args.build:
build = args.build
else:
build = builds.get_latest()
for arch in builds.get_build_arches(build):
print(f"Validating build: {build}/{arch}")
builddir = builds.get_build_dir(build, arch)
validate_build(builddir)