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
This commit is contained in:
William Wernert
2021-03-08 15:43:54 -05:00
parent ed4f8025be
commit 5ee6856a07

View File

@@ -16,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, argparse, re, docker import sys, argparse, re, docker
from packaging.version import Version from packaging.version import Version, InvalidVersion
from itertools import groupby, chain 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 # Version doesn't like "latest", so use a high semver
return '999999.9.9' return '999999.9.9'
else: else:
try:
Version(ver)
except InvalidVersion:
# Strip the last substring following a hyphen for automated branches
ver = '-'.join(ver.split('-')[:-1])
return ver return ver
@@ -54,6 +59,7 @@ def main(quiet):
no_prunable = True no_prunable = True
for t_list in grouped_tag_lists: for t_list in grouped_tag_lists:
try:
# Keep the 2 most current images # Keep the 2 most current images
t_list.sort(key=lambda x: Version(get_image_version(x)), reverse=True) t_list.sort(key=lambda x: Version(get_image_version(x)), reverse=True)
if len(t_list) <= 2: if len(t_list) <= 2:
@@ -63,6 +69,9 @@ def main(quiet):
for tag in t_list[2:]: for tag in t_list[2:]:
if not quiet: print(f'Removing image {tag}') if not quiet: print(f'Removing image {tag}')
client.images.remove(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: if no_prunable and not quiet:
print('No Security Onion images to prune') print('No Security Onion images to prune')