Skip to content

Commit d0a0874

Browse files
committed
src/cmd-compress: don't require image.json for decompression
b60dc1f caused the `cosa buildfetch --artifact=ostree && cosa decompress` path to fail since there was no local ostree repo and no ociarchive that got downloaded. Let's drop the image.json search into the compress path only. While we are here let's call import_ostree_commit (which also calls extract_image_json) instead of extract_image_json and add a function called get_image_json() that helps us iterate over the possible architectures to find a build with all the pieces necessary to get the image.json.
1 parent 0083086 commit d0a0874

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

src/cmd-compress

+33-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
1313
from cosalib.builds import Builds
1414
from cosalib.meta import GenericBuildMeta
1515
from cosalib.cmdlib import (
16-
extract_image_json,
16+
import_ostree_commit,
1717
ncpu,
1818
rm_allow_noent,
1919
runcmd,
@@ -52,24 +52,8 @@ else:
5252

5353
print(f"Targeting build: {build}")
5454

55-
# that's the tool default
56-
gzip_level = 6
57-
if args.fast:
58-
args.compressor = 'gzip'
59-
gzip_level = 1
60-
elif not args.compressor:
61-
workdir = os.getcwd()
62-
arches = builds.get_build_arches(build)
63-
# image.json isn't arch-dependent, so just extract it from the first arch
64-
buildmeta = GenericBuildMeta(workdir=workdir, build=build, basearch=arches[0])
65-
extract_image_json(workdir, buildmeta['ostree-commit'])
66-
with open(os.path.join(workdir, 'tmp/image.json')) as f:
67-
image_json = json.load(f)
68-
args.compressor = image_json.get('compressor', DEFAULT_COMPRESSOR)
69-
70-
# find extension for compressor
55+
# common extensions for known compressors
7156
ext_dict = {'xz': '.xz', 'gzip': '.gz'}
72-
ext = ext_dict[args.compressor]
7357

7458

7559
def get_cpu_param(param):
@@ -93,6 +77,9 @@ def compress_one_builddir(builddir):
9377
with open(buildmeta_path) as f:
9478
buildmeta = json.load(f)
9579

80+
# Find what extension to use based on the selected compressor
81+
ext = ext_dict[args.compressor]
82+
9683
tmpdir = 'tmp/compress'
9784
if os.path.isdir(tmpdir):
9885
shutil.rmtree(tmpdir)
@@ -252,8 +239,36 @@ def uncompress_one_builddir(builddir):
252239
return at_least_one
253240

254241

242+
def get_image_json():
243+
# All arches might not be local. Find one that has the info.
244+
workdir = os.getcwd()
245+
for arch in builds.get_build_arches(build):
246+
builddir = builds.get_build_dir(build, arch)
247+
if not os.path.exists(os.path.join(builddir, 'meta.json')):
248+
continue
249+
buildmeta = GenericBuildMeta(workdir=workdir, build=build, basearch=arch)
250+
if not os.path.exists(os.path.join(builddir, buildmeta['images']['ostree']['path'])):
251+
continue
252+
import_ostree_commit(workdir, builddir, buildmeta) # runs extract_image_json()
253+
with open(os.path.join(workdir, 'tmp/image.json')) as f:
254+
return json.load(f)
255+
# If we get here we were unsuccessful
256+
print("Could not find/extract image.json. Please pass --compressor\n" +
257+
"or make sure local ociarchive exists in the build directory.")
258+
raise Exception("Could not find/extract image.json")
259+
260+
255261
changed = []
256262
if args.mode == "compress":
263+
# Find what compressor we should use, either picking it up from
264+
# CLI args or from image.json
265+
gzip_level = 6
266+
if args.fast:
267+
args.compressor = 'gzip'
268+
gzip_level = 1
269+
elif not args.compressor:
270+
image_json = get_image_json()
271+
args.compressor = image_json.get('compressor', DEFAULT_COMPRESSOR)
257272
for arch in builds.get_build_arches(build):
258273
builddir = builds.get_build_dir(build, arch)
259274
changed.append(compress_one_builddir(builddir))

0 commit comments

Comments
 (0)