#!/usr/bin/env bash container_engine="docker" # Move to top level directory REAL_PATH=$(python3 -c "import os;print(os.path.realpath('$0'))") cd "$(dirname "$REAL_PATH")/.." function process_cmd { if [[ -z "$KOLLA_IMAGES" ]]; then echo "No images to cleanup, exit now." exit 0 fi $CMD if [[ $? -ne 0 ]]; then echo "Command failed $CMD" exit 1 fi } function usage { cat < Delete selected images --image-version Set Kolla image version --engine, -e Container engine to be used EOF } SHORT_OPTS="ahi:e:" LONG_OPTS="all,dangling,help,image:,image-version:,engine:" ARGS=$(getopt -o "${SHORT_OPTS}" -l "${LONG_OPTS}" --name "$0" -- "$@") || { usage >&2; exit 2; } for arg do shift if [ "$arg" = "-e" ] || [ "$arg" = "--engine" ]; then container_engine="$1" continue elif [ "$arg" = "$container_engine" ]; then continue fi eval set -- "$@" "$arg" done # catch empty arguments if [ "$ARGS" = " --" ]; then eval set -- "$ARGS" fi case "$1" in (--all|-a) KOLLA_IMAGES="$(sudo ${container_engine} images -a --filter "label=kolla_version" --format "{{.ID}}")" shift ;; (--dangling) KOLLA_IMAGES="$(sudo ${container_engine} images -a --filter dangling=true --format "{{.ID}}")" shift ;; (--image|-i) KOLLA_IMAGES="$(sudo ${container_engine} images -a --filter "label=kolla_version" --format "{{.Repository}}\t{{.ID}}" | grep -E "$2" | awk '{print $2}')" shift 2 ;; (--image-version) KOLLA_IMAGES="$(sudo ${container_engine} images -a --filter "label=kolla_version=${2}" --format "{{.ID}}")" shift 2 ;; (--help|-h) usage shift exit 0 ;; (--) echo -e "Error: no argument passed\n" usage exit 0 ;; esac CMD="sudo ${container_engine} rmi -f $@ -- $KOLLA_IMAGES" process_cmd