@@ -125,6 +125,12 @@ QUAL_NONE = "unqualified"
125125QUAL_DISTRO = "distro-qualified"
126126QUAL_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+
736757def 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
764779def 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