Skip to content

Commit 3ff6d17

Browse files
committed
cosalib: add Builds().get_build_image_json(); drop use of flatten_image_yaml()
As part of c61da89 we started to allow templating in the image.yaml file. This means we now need to pass the dict of things to template into flatten-image-yaml and that is now causing us issues if there are things to template but not template dict passed in. One case we were doing this was in our AWS upload where we pick up some values from image.yaml. Except, we shouldn't have been picking them up from image.yaml in the src/config as this stage but rather the image.json that had been baked into the build. Let's switch over cosalib/aws to pick the info up from image.json instead. As part of this add a helper the the cosalib Builds library to make this easier for us in various places to do it. This addition was conveniently stolen from cmd-compress that already had code to do this.
1 parent e93f9c2 commit 3ff6d17

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

src/cmd-compress

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import argparse
1111

1212
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
1313
from cosalib.builds import Builds
14-
from cosalib.meta import GenericBuildMeta
1514
from cosalib.cmdlib import (
16-
import_ostree_commit,
1715
ncpu,
1816
rm_allow_noent,
1917
runcmd,
@@ -42,7 +40,7 @@ parser.add_argument("--fast", action='store_true',
4240
help="Override compression to `gzip -1`")
4341
args = parser.parse_args()
4442

45-
builds = Builds()
43+
builds = Builds(workdir=os.getcwd())
4644

4745
# default to latest build if not specified
4846
if args.build:
@@ -250,30 +248,11 @@ def uncompress_one_builddir(builddir):
250248
return at_least_one
251249

252250

253-
def get_image_json():
254-
# All arches might not be local. Find one that has the info.
255-
workdir = os.getcwd()
256-
for arch in builds.get_build_arches(build):
257-
builddir = builds.get_build_dir(build, arch)
258-
if not os.path.exists(os.path.join(builddir, 'meta.json')):
259-
continue
260-
buildmeta = GenericBuildMeta(workdir=workdir, build=build, basearch=arch)
261-
if not os.path.exists(os.path.join(builddir, buildmeta['images']['ostree']['path'])):
262-
continue
263-
import_ostree_commit(workdir, builddir, buildmeta) # runs extract_image_json()
264-
with open(os.path.join(workdir, 'tmp/image.json')) as f:
265-
return json.load(f)
266-
# If we get here we were unsuccessful
267-
print("Could not find/extract image.json. Please pass --compressor\n" +
268-
"or make sure local ociarchive exists in the build directory.")
269-
raise Exception("Could not find/extract image.json")
270-
271-
272251
changed = []
273252
if args.mode == "compress":
274253
# Find what compressor we should use, either picking it up from
275254
# CLI args or from image.json
276-
image_json = get_image_json()
255+
image_json = builds.get_build_image_json(build)
277256
gzip_level = 9
278257
if args.fast:
279258
args.compressor = 'gzip'

src/cosalib/aws.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import subprocess
44
import sys
55

6-
from cosalib.cmdlib import (
7-
flatten_image_yaml,
8-
runcmd
9-
)
6+
from cosalib.builds import Builds
7+
from cosalib.cmdlib import runcmd
108
from tenacity import (
119
retry,
1210
stop_after_attempt
@@ -124,17 +122,15 @@ def aws_run_ore(build, args):
124122
region = "us-east-1"
125123
if args.region is not None and len(args.region) > 0:
126124
region = args.region[0]
127-
# Capture any input from image.yaml
128-
image_yaml = flatten_image_yaml(
129-
'/usr/lib/coreos-assembler/image-default.yaml',
130-
flatten_image_yaml('src/config/image.yaml')
131-
)
132-
if 'aws-imdsv2-only' in image_yaml and image_yaml['aws-imdsv2-only']:
125+
126+
# Capture any settings from image json.
127+
image_json = Builds(workdir=os.getcwd()).get_build_image_json(build.build_id)
128+
if 'aws-imdsv2-only' in image_json and image_json['aws-imdsv2-only']:
133129
ore_args.extend(['--imdsv2-only'])
134-
if 'aws-volume-type' in image_yaml:
135-
ore_args.extend(['--volume-type', image_yaml['aws-volume-type']])
136-
if 'aws-x86-boot-mode' in image_yaml:
137-
ore_args.extend(['--x86-boot-mode', image_yaml['aws-x86-boot-mode']])
130+
if 'aws-volume-type' in image_json:
131+
ore_args.extend(['--volume-type', image_json['aws-volume-type']])
132+
if 'aws-x86-boot-mode' in image_json:
133+
ore_args.extend(['--x86-boot-mode', image_json['aws-x86-boot-mode']])
138134

139135
ore_args.extend([
140136
'--region', f"{region}",

src/cosalib/builds.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
from cosalib.cmdlib import (
1414
get_basearch,
15-
rfc3339_time,
1615
get_timestamp,
16+
import_ostree_commit,
1717
load_json,
18+
rfc3339_time,
1819
write_json)
1920

2021
Build = collections.namedtuple('Build', ['id', 'timestamp', 'basearches'])
@@ -161,6 +162,22 @@ def raw(self):
161162
def flush(self):
162163
write_json(self._fn, self._data)
163164

165+
def get_build_image_json(self, build_id):
166+
# All arches might not be local. Find one that has the info.
167+
workdir = self._workdir
168+
for arch in self.get_build_arches(build_id):
169+
builddir = self.get_build_dir(build_id, arch)
170+
if not os.path.exists(os.path.join(builddir, 'meta.json')):
171+
continue
172+
buildmeta = self.get_build_meta(build_id, basearch=arch)
173+
if not os.path.exists(os.path.join(builddir, buildmeta['images']['ostree']['path'])):
174+
continue
175+
import_ostree_commit(workdir, builddir, buildmeta) # runs extract_image_json()
176+
with open(os.path.join(workdir, 'tmp/image.json')) as f:
177+
return json.load(f)
178+
# If we get here we were unsuccessful
179+
raise Exception("Could not find/extract image.json")
180+
164181

165182
def get_local_builds(builds_dir):
166183
scanned_builds = []

0 commit comments

Comments
 (0)