-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_cleanup.sh
executable file
·58 lines (55 loc) · 1.2 KB
/
image_cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
source ./common.sh
set -euf -o pipefail
# shellcheck source=./common.sh
YES=false
function send_help(){
cat <<"EOF"
Usage: ./update_cleanup <flags>
Flags:
-s Show image list and exit
-y Don't ask for confirmation at all.
-h For when you're confused.
Function:
The script will get all deactivated images and allow you to clean them up.
EOF
exit 0
}
OPTSTRING=":ythf:"
while getopts ${OPTSTRING} opt; do
case ${opt} in
y)
YES=true
;;
h)
send_help
;;
*)
warn "Unknown flag"
send_help
;;
esac
done
sourcerc
function deleteImages(){
COUNT=0
for id in $(openstack image list --public -f json | jq -r '.[] | select(.Status=="deactivated") | .ID');do
openstack image delete "$id"
COUNT=$((COUNT+1))
done
success "$COUNT images cleaned up."
}
IMAGES="$(openstack image list --public --status deactivated)"
if [ -z "$IMAGES" ];then
warn "0 deactivated public images found"
exit 0
fi
echo "$IMAGES"
if [ $YES == false ];then
getConfirmation "Delete all deactivated images?" "Deleting all deactivated images!"
else
REPLY="y"
fi
if [[ $REPLY =~ ^[Yy]$ ]]; then
deleteImages
fi