Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / openstack-protect-in-use-images.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017, 2018 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # Checks the image "protected" value and set "True" marker
12 #
13 # The script searches the ciman repo for the images presently used and ensures
14 # the protection setting is set for those images to prevent the image from
15 # getting purged by the image cleanup script.
16 # This script assumes images prefixed with the string "ZZCI - " are ci-managed
17 # images.
18 set -eu -o pipefail
19 echo "---> Protect in-use images"
20 os_cloud="${OS_CLOUD:-vex}"
21
22 # shellcheck disable=SC1090
23 source ~/lf-env.sh
24
25 lf-activate-venv --python python3 "lftools[openstack]" \
26     kubernetes \
27     python-heatclient \
28     python-openstackclient
29
30 images=()
31 while read -r -d $'\n' ; do
32     images+=("$REPLY")
33 done < <(grep -r IMAGE_NAME --include \*.cfg jenkins-config \
34             | awk -F'=' '{print $2}' \
35             | sort -u)
36
37 jjbimages=()
38 while read -r -d $'\n' ; do
39     jjbimages+=("$REPLY")
40 done < <(grep -r 'ZZCI - ' --include \*.yaml jjb \
41             | awk -F": " '{print $3}' \
42             | sed -e "s:'::;s:'$::;/^$/d" -e 's/^"//' -e 's/"$//' \
43             | sort -u)
44
45 if ! [[ ${#images[@]} -eq 0 ]]; then
46     echo "INFO: There are images to protect defined in jenkins-config."
47 else
48     echo "ERROR: No images detected in the jenkins-config dir."
49     exit 1
50 fi
51
52 if ! [[ ${#jjbimages[@]} -eq 0 ]]; then
53     echo "INFO: There are additional images to protect in the jjb dir."
54     images=("${images[@]}" "${jjbimages[@]}")
55     #dedupe
56     readarray -t images < <(printf '%s\n' "${images[@]}" | sort -u)
57 fi
58
59
60 echo "INFO: Protecting the following images:"
61 for image in "${images[@]}"; do
62     echo "$image"
63 done
64
65 for image in "${images[@]}"; do
66     os_image_protected=$(openstack --os-cloud "$os_cloud" \
67         image show "$image" -f value -c protected)
68     echo "Protected setting for $image: $os_image_protected"
69
70     if [[ $os_image_protected != True ]]; then
71         echo "    Image NOT set as protected, changing the protected value."
72         openstack --os-cloud "$os_cloud" image set --protected "$image"
73     fi
74 done