Skip to content

Commit

Permalink
Merge pull request #736 from NethServer/fixCleanImage
Browse files Browse the repository at this point in the history
Compare and remove outdated  image versions NethServer/dev#7073
  • Loading branch information
stephdl authored Oct 24, 2024
2 parents b15c353 + 1e31891 commit b43655c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
#!/bin/bash
#!/usr/bin/env python3

#
# Copyright (C) 2023 Nethesis S.r.l.
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

exec 1>&2
set -e
import os
import agent
import sys

# Clean up local images that are no longer required by the new module
# version
# Iterate over environment variables
for image_old in os.environ:

export LC_ALL=C # required by sort for portability
if not image_old.startswith('PREV_'):
continue # not a PREV image key, skip it.

# Find the images that are no longer required:
# retrieve from env all variable with the prefix PREV_
# remove not used images like PREV_*_IMAGE if !== *_IMAGE
# ignore errors and exit with successful exit status
for imageOld in ${!PREV_*}; do
if [[ $imageOld =~ _IMAGE$ ]]; then
# remove the prefix to compare PREV_*_IMAGE != *_IMAGE
imageNew=${imageOld##PREV_}
# if not the same we remove the image
if [[ "${!imageNew}" != "${!imageOld}" ]]; then
podman image rm "${!imageOld}" 2>/dev/null || :
fi
fi
done
if not image_old.endswith('_IMAGE'):
continue # we want to skip *DIGEST and ID keys

# Check if the new image is different
image_new = image_old.removeprefix('PREV_')
if os.environ.get(image_new) != os.environ.get(image_old):
print(f"Removing {os.environ.get(image_old)} (differs from {os.environ.get(image_new)})", file=sys.stderr)
agent.run_helper('podman', 'image', 'rm', os.environ.get(image_old))

# Handle removal of the old image URL
image_old = 'PREV_IMAGE_URL'
image_new = 'IMAGE_URL'
if os.environ.get(image_new) != os.environ.get(image_old):
print(f"Removing {os.environ.get(image_old)} (differs from {os.environ.get(image_new)})", file=sys.stderr)
agent.run_helper('podman', 'image', 'rm', os.environ[image_old])
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import subprocess
import agent
import sys

def get_podman_images():
result = subprocess.run(['podman', 'images', '--format', '{{.Repository}}:{{.Tag}}'],
stdout=subprocess.PIPE, text=True)
image_list = result.stdout.splitlines()
images = {}
for img in image_list:
image_name, tag = img.rsplit(':', 1)
images[image_name] = tag
return images

def parse_core_images(core_images):
parsed_images = {}
for key, value in core_images.items():
if not key.endswith('_IMAGE'):
continue # skip non-image variables
image_name, tag = value.rsplit(':', 1)
parsed_images[image_name] = tag
return parsed_images

def remove_versions(core_images, podman_images):
for image_name, core_version in core_images.items():
podman_version = podman_images.get(image_name)
if podman_version and podman_version != core_version:
print(f"Removing {image_name}:{podman_version} (differs from {core_version})", file=sys.stderr)
agent.run_helper('podman', 'rmi', '--ignore', f'{image_name}:{podman_version}')

# Read the environment file using the agent module
version_images = agent.read_envfile('/etc/nethserver/core.env')

# Parse the core images to extract image names and versions
core_images = parse_core_images(version_images)

# retrieve the images from podman
podman_images = get_podman_images()

# Remove the images that are no longer required
remove_versions(core_images, podman_images)

0 comments on commit b43655c

Please sign in to comment.