Fix: Use lf-activate-venv to install openstack dep
[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 # Check if openstack venv was previously created
26 if [ -f "/tmp/.os_lf_venv" ]; then
27     os_lf_venv=$(cat "/tmp/.os_lf_venv")
28 fi
29
30 if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then
31     echo "Re-use existing venv: ${os_lf_venv}"
32     PATH=$os_lf_venv/bin:$PATH
33 else
34     lf-activate-venv --python python3 \
35         python-heatclient \
36         python-openstackclient
37 fi
38
39 images=()
40 while read -r -d $'\n' ; do
41     images+=("$REPLY")
42 done < <(grep -r IMAGE_NAME --include \*.cfg jenkins-config \
43             | awk -F'=' '{print $2}' \
44             | sort -u)
45
46 jjbimages=()
47 while read -r -d $'\n' ; do
48     jjbimages+=("$REPLY")
49 done < <(grep -r 'ZZCI - ' --include \*.yaml jjb \
50             | awk -F": " '{print $3}' \
51             | sed -e "s:'::;s:'$::;/^$/d" -e 's/^"//' -e 's/"$//' \
52             | sort -u)
53
54 if ! [[ ${#images[@]} -eq 0 ]]; then
55     echo "INFO: There are images to protect defined in jenkins-config."
56 else
57     echo "ERROR: No images detected in the jenkins-config dir."
58     exit 1
59 fi
60
61 if ! [[ ${#jjbimages[@]} -eq 0 ]]; then
62     echo "INFO: There are additional images to protect in the jjb dir."
63     images=("${images[@]}" "${jjbimages[@]}")
64     #dedupe
65     readarray -t images < <(printf '%s\n' "${images[@]}" | sort -u)
66 fi
67
68
69 echo "INFO: Protecting the following images:"
70 for image in "${images[@]}"; do
71     echo "$image"
72 done
73
74 for image in "${images[@]}"; do
75     os_image_protected=$(openstack --os-cloud "$os_cloud" \
76         image show "$image" -f value -c protected)
77     echo "Protected setting for $image: $os_image_protected"
78
79     if [[ $os_image_protected != True ]]; then
80         echo "    Image NOT set as protected, changing the protected value."
81         openstack --os-cloud "$os_cloud" image set --protected "$image"
82     fi
83 done