From 5ee6856a07c3c6842a1393a142aaf4dfff5542ba Mon Sep 17 00:00:00 2001 From: William Wernert Date: Mon, 8 Mar 2021 15:43:54 -0500 Subject: [PATCH] Strip the last substring following a hyphen for automated branches Also don't show the user a stack trace on invalid version strings, just alert on the bad string and exit --- salt/common/tools/sbin/so-docker-prune | 31 +++++++++++++++++--------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/salt/common/tools/sbin/so-docker-prune b/salt/common/tools/sbin/so-docker-prune index 06e05d7dc..4a7bd4239 100755 --- a/salt/common/tools/sbin/so-docker-prune +++ b/salt/common/tools/sbin/so-docker-prune @@ -16,7 +16,7 @@ # along with this program. If not, see . import sys, argparse, re, docker -from packaging.version import Version +from packaging.version import Version, InvalidVersion from itertools import groupby, chain @@ -34,6 +34,11 @@ def get_image_version(string) -> str: # Version doesn't like "latest", so use a high semver return '999999.9.9' else: + try: + Version(ver) + except InvalidVersion: + # Strip the last substring following a hyphen for automated branches + ver = '-'.join(ver.split('-')[:-1]) return ver @@ -54,16 +59,20 @@ def main(quiet): no_prunable = True for t_list in grouped_tag_lists: - # Keep the 2 most current images - t_list.sort(key=lambda x: Version(get_image_version(x)), reverse=True) - if len(t_list) <= 2: - continue - else: - no_prunable = False - for tag in t_list[2:]: - if not quiet: print(f'Removing image {tag}') - client.images.remove(tag) - + try: + # Keep the 2 most current images + t_list.sort(key=lambda x: Version(get_image_version(x)), reverse=True) + if len(t_list) <= 2: + continue + else: + no_prunable = False + for tag in t_list[2:]: + if not quiet: print(f'Removing image {tag}') + client.images.remove(tag) + except InvalidVersion as e: + print(f'so-{get_so_image_basename(t_list[0])}: {e.args[0]}') + exit(1) + if no_prunable and not quiet: print('No Security Onion images to prune')