kolla-ansible/tools/cleanup-images
Mark Goddard b123bf6621 Use become for all docker tasks
Many tasks that use Docker have become specified already, but
not all. This change ensures all tasks that use the following
modules have become:

* kolla_docker
* kolla_ceph_keyring
* kolla_toolbox
* kolla_container_facts

It also adds become for 'command' tasks that use docker CLI.

Change-Id: I4a5ebcedaccb9261dbc958ec67e8077d7980e496
2019-06-06 19:04:58 +01:00

78 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# Move to top level directory
REAL_PATH=$(python -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 <<EOF
Usage: $0 COMMAND [options]
Options:
--all, -a Remove all kolla images
--dangling Remove orphaned images
--help, -h Show this usage information
--image, -i <image> Delete selected images
--image-version <image_version> Set Kolla image version
EOF
}
SHORT_OPTS="ahi:"
LONG_OPTS="all,dangling,help,image:,image-version:"
ARGS=$(getopt -o "${SHORT_OPTS}" -l "${LONG_OPTS}" --name "$0" -- "$@") || { usage >&2; exit 2; }
eval set -- "$ARGS"
case "$1" in
(--all|-a)
KOLLA_IMAGES="$(sudo docker images -a --filter "label=kolla_version" --format "{{.ID}}")"
shift
;;
(--dangling)
KOLLA_IMAGES="$(sudo docker images -a --filter dangling=true --format "{{.ID}}")"
shift
;;
(--image|-i)
KOLLA_IMAGES="$(sudo docker images -a --filter "label=kolla_version" --format "{{.Repository}}\t{{.ID}}" | grep -E "$2" | awk '{print $2}')"
shift 2
;;
(--image-version)
KOLLA_IMAGES="$(sudo docker 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 docker rmi -f $@ $KOLLA_IMAGES"
process_cmd