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