From: Aric Gardner Date: Wed, 23 Sep 2020 18:52:41 +0000 (-0400) Subject: Fix protect images X-Git-Tag: v0.57.4~1 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=d56275b88a022a810b9a39f4c7e142c8c8d626bd;hp=--cc;p=releng%2Fglobal-jjb.git Fix protect images Projects that do not have images in jjb are failing this step of the build fix code so we don't need to mess with set -u or shellcheck disable also only merge arrays if needed Signed-off-by: Aric Gardner Change-Id: I119b68e344a34bb5bbe8baea2cc310497b7ee629 --- d56275b88a022a810b9a39f4c7e142c8c8d626bd diff --git a/releasenotes/notes/openstack-protect-in-use-images-cf8e4f722a1512ec.yaml b/releasenotes/notes/openstack-protect-in-use-images-cf8e4f722a1512ec.yaml new file mode 100644 index 00000000..72f16e6b --- /dev/null +++ b/releasenotes/notes/openstack-protect-in-use-images-cf8e4f722a1512ec.yaml @@ -0,0 +1,9 @@ +--- +fixes: + - | + openstack-protect-in-use-images.sh + Projects that do not have images in jjb were failing this step of the build. + code now runs with set -eu -o pipefail for the duration of the script. + shellcheck disable comments no longer needed and removed. + Code now only merges arrays if non-empty. + Simplify dedupe array code. diff --git a/shell/openstack-protect-in-use-images.sh b/shell/openstack-protect-in-use-images.sh index a422cd87..e2299060 100644 --- a/shell/openstack-protect-in-use-images.sh +++ b/shell/openstack-protect-in-use-images.sh @@ -15,22 +15,44 @@ # 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 +images=() +while read -r -d $'\n' ; do + images+=("$REPLY") +done < <(grep -r IMAGE_NAME --include \*.cfg jenkins-config \ + | awk -F'=' '{print $2}' \ + | 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 -declare -a images -images+=("$(grep -r IMAGE_NAME --include \*.cfg jenkins-config \ - | awk -F'=' '{print $2}' | sort -u)") -set +o pipefail # Not all projects have images in YAML files and grep returns non-zero on 0 results -# Ignore SC2179 since we do not want () to result in an empty array item. -#shellcheck disable=SC2179 -images+=("$(grep -r 'ZZCI - ' --include \*.yaml jjb \ - | awk -F": " '{print $3}' | sed -e "s:'::;s:'$::;/^$/d" -e 's/^"//' -e 's/"$//' | sort -u)") -set -o pipefail -readarray -t images <<< "$(for i in "${images[@]}"; do echo "$i"; done | sort -u)" +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" \