1 | #!/usr/bin/env bash
|
2 | #
|
3 | ## Delete old images except the latest one
|
4 |
|
5 | opskit_path="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)"
|
6 | source ${opskit_path}/lib/common.sh
|
7 |
|
8 | if [[ -z $1 ]]; then
|
9 | echo "Missing image family name"
|
10 | exit 1;
|
11 | fi
|
12 |
|
13 | family=$1
|
14 | ## Get the latest image from family
|
15 | latest_image=$(gcloud compute images describe-from-family "${family}" --format='value(name)')
|
16 | if [[ -z "${latest_image}" ]]; then
|
17 | echo "No ${family} image exists."
|
18 | exit 1;
|
19 | fi
|
20 |
|
21 | ## Delete old images and skip latest one
|
22 | echo "Attempting to delete all ${family} images except the latest one...."
|
23 | images=$(gcloud compute images list --regexp=^${family}.* --format='value(name)')
|
24 | if [ -z "${images}" ]; then
|
25 | echo "Old ${family} images not available"
|
26 | exit 1;
|
27 | fi
|
28 |
|
29 | images_to_delete=''
|
30 | for old_image in ${images}; do
|
31 | if [[ "${old_image}" -ne "${latest_image}" ]]; then
|
32 | images_to_delete="${images_to_delete} ${old_image}"
|
33 | fi
|
34 | images
|
35 | done
|
36 |
|
37 | gcloud compute images delete "${images_to_delete}" --quiet
|