Add a CLI options to order tags by date
This commit is contained in:
45
registry.py
45
registry.py
@@ -546,6 +546,12 @@ for more detail on garbage collection read here:
|
||||
default='POST',
|
||||
metavar="POST|GET"
|
||||
)
|
||||
parser.add_argument(
|
||||
'--order-by-date',
|
||||
help=('Orders images by date instead of by tag name.'
|
||||
'Useful if your tag names are not in a fixed order.'),
|
||||
action='store_true'
|
||||
)
|
||||
return parser.parse_args(args)
|
||||
|
||||
|
||||
@@ -672,6 +678,29 @@ def get_newer_tags(registry, image_name, hours, tags_list):
|
||||
return result
|
||||
|
||||
|
||||
def get_datetime_tags(registry, image_name, tags_list):
|
||||
def newer(tag):
|
||||
image_config = registry.get_tag_config(image_name, tag)
|
||||
if image_config == []:
|
||||
print("tag not found")
|
||||
return None
|
||||
image_age = registry.get_image_age(image_name, image_config)
|
||||
if image_age == []:
|
||||
print("timestamp not found")
|
||||
return None
|
||||
return {
|
||||
"tag": tag,
|
||||
"datetime": dt.strptime(image_age[:-4], "%Y-%m-%dT%H:%M:%S.%f")
|
||||
}
|
||||
|
||||
print('---------------------------------')
|
||||
p = ThreadPool(4)
|
||||
result = list(x for x in p.map(newer, tags_list) if x)
|
||||
p.close()
|
||||
p.join()
|
||||
return result
|
||||
|
||||
|
||||
def keep_images_like(image_list, regexp_list):
|
||||
if image_list is None or regexp_list is None:
|
||||
return []
|
||||
@@ -685,6 +714,18 @@ def keep_images_like(image_list, regexp_list):
|
||||
return result
|
||||
|
||||
|
||||
def get_ordered_tags(registry, image_name, tags_list, order_by_date=False):
|
||||
if order_by_date:
|
||||
tags_date = get_datetime_tags(registry, image_name, tags_list)
|
||||
sorted_tags_by_date = sorted(
|
||||
tags_date,
|
||||
key=lambda x: x["datetime"]
|
||||
)
|
||||
return [x["tag"] for x in sorted_tags_by_date]
|
||||
|
||||
return sorted(tags_list, key=natural_keys)
|
||||
|
||||
|
||||
def main_loop(args):
|
||||
global DEBUG
|
||||
|
||||
@@ -778,8 +819,8 @@ def main_loop(args):
|
||||
if args.delete_all:
|
||||
tags_list_to_delete = list(tags_list)
|
||||
else:
|
||||
tags_list_to_delete = sorted(tags_list, key=natural_keys)[
|
||||
:-keep_last_versions]
|
||||
ordered_tags_list = get_ordered_tags(registry, image_name, tags_list, args.order_by_date)
|
||||
tags_list_to_delete = ordered_tags_list[:-keep_last_versions]
|
||||
|
||||
# A manifest might be shared between different tags. Explicitly add those
|
||||
# tags that we want to preserve to the keep_tags list, to prevent
|
||||
|
||||
Reference in New Issue
Block a user