Skip to content

Commit a7e608f

Browse files
hack/build-image: clean up logic around auto-building on push & archive
The description of `exists` didn't match the code and now we can extend and clean things up in the presence of indexes. Signed-off-by: John Mulligan <[email protected]>
1 parent 3d9cf9e commit a7e608f

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

hack/build-image

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ QUAL_NONE = "unqualified"
125125
QUAL_DISTRO = "distro-qualified"
126126
QUAL_FQIN = "fqin"
127127

128+
# conditions
129+
EXISTS = "exists"
130+
AUTO = "auto"
131+
AUTO_INDEX = "auto-index"
132+
REBUILD = "rebuild"
133+
128134

129135
_DISCOVERED_CONTAINER_ENGINES = []
130136

@@ -733,15 +739,25 @@ class QMatcher:
733739
return False
734740

735741

742+
def _autobuild(cli, target, is_index=None):
743+
if is_index is None:
744+
is_index = isinstance(target, TargetIndex)
745+
if cli.condition == REBUILD:
746+
rebuild(cli, target)
747+
elif maybe_container_id(cli, target):
748+
logger.debug("target item exists: %s", target)
749+
elif cli.condition == AUTO or (cli.condition == AUTO_INDEX and is_index):
750+
logger.debug("target item auto build: %s", target)
751+
build(cli, target)
752+
else:
753+
log.error("no existing image or index: %s", target)
754+
raise ValueError("not present", target)
755+
756+
736757
def push(cli, target):
737758
"""Command to push images."""
738-
if cli.push_state == "rebuild":
739-
build(cli, target)
740-
if cli.push_state == "exists":
741-
try:
742-
container_id(cli, target)
743-
except subprocess.CalledProcessError:
744-
build(cli, target)
759+
is_index = isinstance(target, TargetIndex) # is it a manifest push?
760+
_autobuild(cli, target, is_index=is_index)
745761

746762
to_push = []
747763
push_name = target.image_name()
@@ -752,22 +768,17 @@ def push(cli, target):
752768
to_push.append((target.image_name(tag=tag), qual))
753769
to_push.append((push_name, QUAL_FQIN))
754770
qmatcher = cli.push_selected_tags or QMatcher("")
755-
manifest = isinstance(target, TargetIndex) # is it a manifest push?
756771
for push_name, tag_qual in to_push:
757772
if qmatcher(tag_qual):
758773
logger.debug(
759-
"pushing named object: %s (manifest=%s)", push_name, manifest
774+
"pushing named object: %s (manifest=%s)", push_name, is_index
760775
)
761-
container_push(cli, push_name, manifest=manifest)
776+
container_push(cli, push_name, manifest=is_index)
762777

763778

764779
def archive(cli, target, location):
765780
"""Write tarballs to archive location."""
766-
# reuse push_stage flag. TODO rename flag
767-
if cli.push_state == "rebuild" or (
768-
cli.push_state == "exists" and not maybe_container_id(cli, target)
769-
):
770-
build(cli, target)
781+
_autobuild(cli, target)
771782

772783
eng = container_engine(cli)
773784
fname = pathlib.Path(location) / f"{target.flat_name()}.tar"
@@ -920,11 +931,16 @@ def main():
920931
)
921932
parser.add_argument(
922933
"--push-state",
923-
choices=("exists", "rebuild"),
924-
default="exists",
934+
"--image-condition",
935+
dest="condition",
936+
choices=(EXISTS, AUTO, AUTO_INDEX, REBUILD),
937+
default=EXISTS,
925938
help=(
926-
"Only push if a state is met:"
927-
"exists - image exists; rebuild - image must be rebuilt."
939+
"Image state must be met before continuing:"
940+
" exists - item already exists;"
941+
" auto - automatically build missing image;"
942+
" auto-index - automatically build missing indexes only;"
943+
" rebuild - image must be rebuilt."
928944
),
929945
)
930946
parser.add_argument(

0 commit comments

Comments
 (0)