1111SPDX-License-Identifier: CC-BY-SA-4.0
1212"""
1313
14+ import calendar
15+ from collections import Counter
16+ import getopt
17+ import logging
1418import os
1519import sys
1620import time
17- import calendar
18- import getopt
21+
1922import openstack
20- from collections import Counter
23+
24+
25+ logger = logging .getLogger (__name__ )
2126
2227
2328def usage (ret ):
@@ -31,8 +36,10 @@ def usage(ret):
3136 print (" -v/--verbose : Be more verbose" )
3237 print (" -s/--skip-completeness: Don't check whether we have all mandatory images" )
3338 print (" -h/--help : Print this usage information" )
34- print ("If you pass images, only these will be validated, otherwise all (public unless" )
35- print (" -p is specified) images from the catalog will be processed." )
39+ print (" [-V/--image-visibility VIS_LIST] : filters images by visibility" )
40+ print (" (default: 'public,community'; use '*' to disable)" )
41+ print ("If you pass images, only these will be validated, otherwise all images" )
42+ print ("(filtered according to -p, -V) from the catalog will be processed." )
3643 sys .exit (ret )
3744
3845
@@ -335,43 +342,59 @@ def miss_replacement_images(by_name, outd_list):
335342
336343def main (argv ):
337344 "Main entry point"
345+ # configure logging, disable verbose library logging
346+ logging .basicConfig (format = '%(levelname)s: %(message)s' , level = logging .INFO )
347+ openstack .enable_logging (debug = False )
338348 # Option parsing
339349 global verbose
350+ image_visibility = set ()
340351 private = False
341352 skip = False
342353 cloud = os .environ .get ("OS_CLOUD" )
343354 err = 0
344355 try :
345- opts , args = getopt .gnu_getopt (argv [1 :], "phvc:s " ,
346- ("private" , "help" , "os-cloud=" , "verbose" , "skip-completeness" ))
356+ opts , args = getopt .gnu_getopt (argv [1 :], "phvc:sV: " ,
357+ ("private" , "help" , "os-cloud=" , "verbose" , "skip-completeness" , "image-visibility=" ))
347358 except getopt .GetoptError : # as exc:
348359 print ("CRITICAL: Command-line syntax error" , file = sys .stderr )
349360 usage (1 )
350361 for opt in opts :
351362 if opt [0 ] == "-h" or opt [0 ] == "--help" :
352363 usage (0 )
353364 elif opt [0 ] == "-p" or opt [0 ] == "--private" :
354- private = True
365+ private = True # only keep this for backwards compatibility (we have -V now)
355366 elif opt [0 ] == "-v" or opt [0 ] == "--verbose" :
356367 verbose = True
368+ logging .getLogger ().setLevel (logging .DEBUG )
357369 elif opt [0 ] == "-s" or opt [0 ] == "--skip-completeness" :
358370 skip = True
359371 elif opt [0 ] == "-c" or opt [0 ] == "--os-cloud" :
360372 cloud = opt [1 ]
373+ if opt [0 ] == "-V" or opt [0 ] == "--image-visibility" :
374+ image_visibility .update ([v .strip () for v in opt [1 ].split (',' )])
361375 images = args
362376 if not cloud :
363377 print ("CRITICAL: Need to specify --os-cloud or set OS_CLOUD environment." , file = sys .stderr )
364378 usage (1 )
379+ if not image_visibility :
380+ image_visibility .update (("public" , "community" ))
381+ if private :
382+ image_visibility .add ("private" )
365383 try :
366384 conn = openstack .connect (cloud = cloud , timeout = 24 )
367385 all_images = list (conn .image .images ())
386+ if '*' not in image_visibility :
387+ logger .debug (f"Images: filter for visibility { ', ' .join (sorted (image_visibility ))} " )
388+ all_images = [img for img in all_images if img .visibility in image_visibility ]
389+ all_image_names = [f"{ img .name } ({ img .visibility } )" for img in all_images ]
390+ logger .debug (f"Images: { ', ' .join (all_image_names ) or '(NONE)' } " )
368391 by_name = {img .name : img for img in all_images }
369392 if len (by_name ) != len (all_images ):
370393 counter = Counter ([img .name for img in all_images ])
371394 duplicates = [name for name , count in counter .items () if count > 1 ]
372395 print (f'WARNING: duplicate names detected: { ", " .join (duplicates )} ' , file = sys .stderr )
373396 if not images :
374- images = [img .name for img in all_images if private or img . visibility == 'public' ]
397+ images = [img .name for img in all_images ]
375398 # Analyse image metadata
376399 outdated_images = []
377400 for imgnm in images :
0 commit comments