Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / packer-validate.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 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 echo "---> packer-validate.sh"
12 # The script validates an packers files.
13
14 # $CLOUDENV            :  Provides the cloud credential file.
15
16 # Ensure we fail the job if any steps fail.
17 set -eu -o pipefail
18
19 # Functions to compare semantic versions x.y.z
20 version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
21
22 PACKER_LOGS_DIR="$WORKSPACE/archives/packer"
23 mkdir -p "$PACKER_LOGS_DIR"
24 export PATH="${WORKSPACE}/bin:$PATH"
25 set -x
26 cd packer
27
28 if version_ge "$PACKER_VERSION" "1.9.0"; then
29     varfiles=(vars/*.pkrvars.hcl common-packer/vars/*.pkrvars.hcl)
30     templates=(templates/*.pkr.hcl)
31 else
32     varfiles=(vars/*.json common-packer/vars/*.json)
33     templates=(templates/*.json)
34 fi
35
36 for varfile in "${varfiles[@]}"; do
37     # cloud-env.{json,pkrvars.hcl} is a file containing credentials which is
38     # pulled in via CLOUDENV variable so skip it here. Also handle case
39     # where a project does not vars/*.{json,pkrvars.hcl} file.
40     if [[ "$varfile" == *"cloud-env.json"* ]] || \
41        [[ "$varfile" == "vars/*.json" ]] || \
42        [[ "$varfile" == *"cloud-env.pkrvars.hcl"* ]] || \
43        [[ "$varfile" == *"cloud-env-aws.pkrvars.hcl"* ]] || \
44        [[ "$varfile" == "vars/*.pkrvars.hcl" ]]; then
45         continue
46     fi
47
48     echo "-----> Testing varfile: $varfile"
49     for template in "${templates[@]}"; do
50         if [[ "$template" == *"variables.pkr.hcl"* ]] || \
51            [[ "$template" == *"variables.auto.pkr.hcl"* ]]; then
52             continue
53         fi
54
55         if [[ "${template#*.}" == "pkr.hcl" ]]; then
56             echo "packer init $template ..."
57             packer.io init "$template"
58         fi
59
60         export PACKER_LOG="yes"
61         export PACKER_LOG_PATH="$PACKER_LOGS_DIR/packer-validate-${varfile##*/}-${template##*/}.log"
62         if output=$(packer.io validate -var-file="$CLOUDENV" -var-file="$varfile" "$template"); then
63             echo "$template: $output"
64         else
65             echo "$template: $output"
66             exit 1
67         fi
68     done
69 done