X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=blobdiff_plain;f=shell%2Fpacker-validate.sh;h=86cf8de84f1286ad58fcadd47b1cfe0a44b93723;hb=532e97b5b965cab5ace406d8ecc845afc98920ac;hp=e9f1a8f0af4901656f9b48f5cd1b52fd32cbed6d;hpb=73c0c7ed8afa17596a835fe8e51367dff3708ce0;p=releng%2Fglobal-jjb.git diff --git a/shell/packer-validate.sh b/shell/packer-validate.sh index e9f1a8f0..86cf8de8 100644 --- a/shell/packer-validate.sh +++ b/shell/packer-validate.sh @@ -16,22 +16,54 @@ echo "---> packer-validate.sh" # Ensure we fail the job if any steps fail. set -eu -o pipefail +# Functions to compare semantic versions x.y.z +version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; } + PACKER_LOGS_DIR="$WORKSPACE/archives/packer" mkdir -p "$PACKER_LOGS_DIR" export PATH="${WORKSPACE}/bin:$PATH" - +set -x cd packer -varfiles=(../packer/vars/*.json) -templates=(../packer/templates/*.json) + +if version_ge "$PACKER_VERSION" "1.9.0"; then + varfiles=(vars/*.pkrvars.hcl common-packer/vars/*.pkrvars.hcl) + templates=(templates/*.pkr.hcl) +else + varfiles=(vars/*.json common-packer/vars/*.json) + templates=(templates/*.json) +fi for varfile in "${varfiles[@]}"; do + # cloud-env.{json,pkrvars.hcl} is a file containing credentials which is + # pulled in via CLOUDENV variable so skip it here. Also handle case + # where a project does not vars/*.{json,pkrvars.hcl} file. + if [[ "$varfile" == *"cloud-env.json"* ]] || \ + [[ "$varfile" == "vars/*.json" ]] || \ + [[ "$varfile" == *"cloud-env.pkrvars.hcl"* ]] || \ + [[ "$varfile" == *"cloud-env-aws.pkrvars.hcl"* ]] || \ + [[ "$varfile" == "vars/*.pkrvars.hcl" ]]; then + continue + fi + + echo "-----> Testing varfile: $varfile" for template in "${templates[@]}"; do - export PACKER_LOG="yes" && \ - export PACKER_LOG_PATH="$PACKER_LOGS_DIR/packer-validate-${varfile##*/}-${template##*/}.log" && \ - packer.io validate -var-file="$CLOUDENV" \ - -var-file="$varfile" "$template" - if [ $? -ne 0 ]; then - break + if [[ "$template" == *"variables.pkr.hcl"* ]] || \ + [[ "$template" == *"variables.auto.pkr.hcl"* ]]; then + continue + fi + + if [[ "${template#*.}" == "pkr.hcl" ]]; then + echo "packer init $template ..." + packer.io init "$template" + fi + + export PACKER_LOG="yes" + export PACKER_LOG_PATH="$PACKER_LOGS_DIR/packer-validate-${varfile##*/}-${template##*/}.log" + if output=$(packer.io validate -var-file="$CLOUDENV" -var-file="$varfile" "$template"); then + echo "$template: $output" + else + echo "$template: $output" + exit 1 fi done done