Created
December 6, 2024 13:06
-
-
Save YektaDev/0a35b6be2931b176768cf2b75a0fd486 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ---------------------------------------------------------------------------------------------------------------------- | |
# docker_unload.sh - Unloads images from a tarball. Returns 0 if any images are removed or skipped, 1 otherwise. | |
# ---------------------------------------------------------------------------------------------------------------------- | |
# MIT License - Ali Khaleqi Yekta (@YektaDev - https://yekta.dev) | |
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK. | |
# ---------------------------------------------------------------------------------------------------------------------- | |
set -euo pipefail | |
err() { echo -e "$(tput bold; tput setaf 1)$(basename "$0") - ERROR:$(tput sgr0) $1" >&2; exit 1; } | |
tarball_path="$1" | |
images_removed=0 | |
images_skipped=0 | |
images_failed=0 | |
overall_status=1 | |
command -v jq &> /dev/null || err "Error: jq command not found. Please install jq." | |
[[ -f "$tarball_path" ]] || err "Error: Tarball file not found at '$tarball_path'" | |
manifest_files=$(tar -tf "$tarball_path" | grep 'manifest.json$') | |
[[ -n "$manifest_files" ]] || err "Error: manifest.json not found in the tarball." | |
image_ids_and_names="" | |
# Loop through potential multiple manifest files and extract IDs and Names | |
while IFS= read -r manifest_file; do | |
image_ids_and_names+=$(tar -xf "$tarball_path" -O "$manifest_file" | jq -r '.[0] | (.RepoTags[]? // empty), .Id' 2>/dev/null) | |
done <<< "$manifest_files" | |
image_ids_and_names=$(echo "$image_ids_and_names" | grep -v '^null$' | grep -v '^$') | |
[[ -n "$image_ids_and_names" ]] || err "Error: No Image IDs or Names found in the tarball '$tarball_path'" | |
# Separate image names from IDs | |
image_names=$(echo "$image_ids_and_names" | grep ":" || true) | |
ids_only=$(echo "$image_ids_and_names" | grep -v ":" || true) | |
for id in $ids_only; do | |
if docker images -q --no-trunc | grep -q "^${id}$"; then | |
if docker rmi "$id" &> /dev/null; then | |
echo "Removed image with ID: $id" | |
images_removed=$((images_removed + 1)) | |
overall_status=0 | |
else | |
echo "Failed to remove image with ID: $id" >&2 | |
images_failed=$((images_failed + 1)) | |
fi | |
else | |
echo "Image with ID '$id' from '$tarball_path' not found among loaded images. Skipping." | |
images_skipped=$((images_skipped + 1)) | |
overall_status=0 | |
fi | |
done | |
for name in $image_names; do | |
if docker images -q "$name" | grep -q "[[:alnum:]]"; then | |
if docker rmi "$name" &> /dev/null; then | |
echo "Removed image with name: $name" | |
images_removed=$((images_removed + 1)) | |
overall_status=0 | |
else | |
echo "Failed to remove image with name: $name" >&2 | |
images_failed=$((images_failed + 1)) | |
fi | |
else | |
echo "Image with name '$name' from '$tarball_path' not found among loaded images. Skipping." | |
images_skipped=$((images_skipped + 1)) | |
overall_status=0 | |
fi | |
done | |
echo "Docker Unload: Removed: $images_removed, Skipped: $images_skipped, Failed: $images_failed" | |
exit "$overall_status" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment