@@ -125,6 +125,12 @@ QUAL_NONE = "unqualified"
125
125
QUAL_DISTRO = "distro-qualified"
126
126
QUAL_FQIN = "fqin"
127
127
128
+ # conditions
129
+ EXISTS = "exists"
130
+ AUTO = "auto"
131
+ AUTO_INDEX = "auto-index"
132
+ REBUILD = "rebuild"
133
+
128
134
129
135
_DISCOVERED_CONTAINER_ENGINES = []
130
136
@@ -733,15 +739,25 @@ class QMatcher:
733
739
return False
734
740
735
741
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
+
736
757
def push (cli , target ):
737
758
"""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 )
745
761
746
762
to_push = []
747
763
push_name = target .image_name ()
@@ -752,22 +768,17 @@ def push(cli, target):
752
768
to_push .append ((target .image_name (tag = tag ), qual ))
753
769
to_push .append ((push_name , QUAL_FQIN ))
754
770
qmatcher = cli .push_selected_tags or QMatcher ("" )
755
- manifest = isinstance (target , TargetIndex ) # is it a manifest push?
756
771
for push_name , tag_qual in to_push :
757
772
if qmatcher (tag_qual ):
758
773
logger .debug (
759
- "pushing named object: %s (manifest=%s)" , push_name , manifest
774
+ "pushing named object: %s (manifest=%s)" , push_name , is_index
760
775
)
761
- container_push (cli , push_name , manifest = manifest )
776
+ container_push (cli , push_name , manifest = is_index )
762
777
763
778
764
779
def archive (cli , target , location ):
765
780
"""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 )
771
782
772
783
eng = container_engine (cli )
773
784
fname = pathlib .Path (location ) / f"{ target .flat_name ()} .tar"
@@ -920,11 +931,16 @@ def main():
920
931
)
921
932
parser .add_argument (
922
933
"--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 ,
925
938
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."
928
944
),
929
945
)
930
946
parser .add_argument (
0 commit comments