Fix: Use lf-activate-venv to install openstack dep
[releng/global-jjb.git] / shell / openstack-protect-in-use-images.sh
index a23a4ed..0729440 100644 (file)
 # getting purged by the image cleanup script.
 # This script assumes images prefixed with the string "ZZCI - " are ci-managed
 # images.
+set -eu -o pipefail
 echo "---> Protect in-use images"
-
 os_cloud="${OS_CLOUD:-vex}"
 
-set -eu -o pipefail
+# shellcheck disable=SC1090
+source ~/lf-env.sh
+
+# Check if openstack venv was previously created
+if [ -f "/tmp/.os_lf_venv" ]; then
+    os_lf_venv=$(cat "/tmp/.os_lf_venv")
+fi
+
+if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then
+    echo "Re-use existing venv: ${os_lf_venv}"
+    PATH=$os_lf_venv/bin:$PATH
+else
+    lf-activate-venv --python python3 \
+        python-heatclient \
+        python-openstackclient
+fi
+
+images=()
+while read -r -d $'\n' ; do
+    images+=("$REPLY")
+done < <(grep -r IMAGE_NAME --include \*.cfg jenkins-config \
+            | awk -F'=' '{print $2}' \
+            | sort -u)
 
-declare -a images
-declare -a cfg_images
-declare -a yaml_images
-readarray -t cfg_images <<< "$(grep -r IMAGE_NAME --include \*.cfg \
-    | awk -F'=' '{print $2}' | sort -u)"
-readarray -t yaml_images <<< "$(grep -r 'ZZCI - ' --include \*.yaml \
-    | awk -F": " '{print $3}' | sed "s:'::;s:'$::;/^$/d" | sort -u)"
-mapfile -t images < <(for R in "${cfg_images[@]}" "${yaml_images[@]}" ; do echo "$R" ; done | sort -u)
+jjbimages=()
+while read -r -d $'\n' ; do
+    jjbimages+=("$REPLY")
+done < <(grep -r 'ZZCI - ' --include \*.yaml jjb \
+            | awk -F": " '{print $3}' \
+            | sed -e "s:'::;s:'$::;/^$/d" -e 's/^"//' -e 's/"$//' \
+            | sort -u)
 
+if ! [[ ${#images[@]} -eq 0 ]]; then
+    echo "INFO: There are images to protect defined in jenkins-config."
+else
+    echo "ERROR: No images detected in the jenkins-config dir."
+    exit 1
+fi
+
+if ! [[ ${#jjbimages[@]} -eq 0 ]]; then
+    echo "INFO: There are additional images to protect in the jjb dir."
+    images=("${images[@]}" "${jjbimages[@]}")
+    #dedupe
+    readarray -t images < <(printf '%s\n' "${images[@]}" | sort -u)
+fi
+
+
+echo "INFO: Protecting the following images:"
+for image in "${images[@]}"; do
+    echo "$image"
+done
 
 for image in "${images[@]}"; do
     os_image_protected=$(openstack --os-cloud "$os_cloud" \