af766c3cf05efc8a6c79350a79222eb7c14c3c01
[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 PACKER_LOGS_DIR="$WORKSPACE/archives/packer"
20 mkdir -p "$PACKER_LOGS_DIR"
21 export PATH="${WORKSPACE}/bin:$PATH"
22 export PACKER_BUILDER=${PACKER_BUILDER:-openstack}
23
24 cd packer
25 varfiles=(vars/*.json common-packer/vars/*.json)
26 templates=(templates/*.json)
27
28 for varfile in "${varfiles[@]}"; do
29     # cloud-env.json is a file containing credentials which is pulled in via
30     # CLOUDENV variable so skip it here. Also handle the case where a project
31     # has not vars/*.json file.
32     if [[ "$varfile" == *"cloud-env.json"* ]] || [[ "$varfile" == 'vars/*.json' ]]; then
33         continue
34     fi
35
36     echo "-----> Testing varfile: $varfile"
37     for template in "${templates[@]}"; do
38         export PACKER_LOG="yes"
39         export PACKER_LOG_PATH="$PACKER_LOGS_DIR/packer-validate-${varfile##*/}-${template##*/}.log"
40         if output=$(packer.io validate -var-file="$CLOUDENV" -var-file="$varfile" "$template"); then
41             echo "$template: $output"
42         else
43             echo "$template: $output"
44             exit 1
45         fi
46     done
47 done