UNPKG

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